﻿function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function showcomments() {
  if (window.countsmiles) {
  }
  else {
     countsmiles = 0;
  }
  if ( document.getElementById('cm-opener').getAttribute('rel')=='closed' ) {  
  
      document.getElementById('cm_form').style.display = "block";
	  document.getElementById('shadow').style.display = "block";
  
	  var form = document.getElementById("cm_form");
	  var shadow = document.getElementById("shadow");
	  var DivHeight = form.offsetHeight;
	  var DivWidth = form.offsetWidth;
	  
	  h = (window.innerHeight) ? window.innerHeight
	: (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight
	: document.body.clientHeight;	
	  
	  xy = getScrollXY();
	  
      form.style.top = (xy[1] + h/2 - DivHeight/2)+'px';
      form.style.left = (xy[0] + document.body.clientWidth/2 - DivWidth/2)+'px';
	  
	  shadow.style.top = (xy[1] + h/2 - DivHeight/2 + 4)+'px';
      shadow.style.left = (xy[0] + document.body.clientWidth/2 - DivWidth/2 + 4)+'px';	
	  
      shadow.style.height=(DivHeight-3)+'px';
	  shadow.style.width=(DivWidth-3)+'px';
	  
	  document.getElementById('cm-opener').setAttribute("rel", "open");
      return '';
  }
  if ( document.getElementById('cm-opener').getAttribute('rel')=='open' ) {
      cmform = document.getElementById('commentform').comment;
	  cmform.value = "";
	  countsmiles = 0;
	  document.getElementById('cm_form').style.display = "none";
	  document.getElementById('shadow').style.display = "none";
      document.getElementById('cm-opener').setAttribute("rel", "closed");	  
      return '';
  }
}
function addsmile(smile) {
     cmform = document.getElementById('commentform').comment;
     countsmiles = countsmiles + 1;
     if (countsmiles<=10){
         code = " *" + smile + "*";
         cmform.value += code;
         ismaxlength(cmform);
     }
     else {
         alert('Вы можете ввести максимум 10 смайликов!')
     }
     document.getElementById('commentform').comment.focus();
}


function ismaxlength(obj){
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("tabindex")) : ""
    if (obj.getAttribute && obj.value.length>mlength) obj.value=obj.value.substring(0,mlength)
}