// JavaScript Document

window.onload = function () {
	var DOM = new GluedTools.dom();
	var eCommentArea = DOM.getElementByID('comment_area');
	var eFormComment = DOM.getElementByID('form_comments');
	var currentPageY = DOM.pageY(0);
	
	if (DOM.getBrowserType() != 'IE') { setInterval(handleMovement, 50); }
	
	function handleMovement () {
		if (DOM.pageY(0) != currentPageY) {
			currentPageY = DOM.pageY(0);
			var posCommentArea = DOM.getElementPosition(eCommentArea);
			var posFormComment = DOM.getElementPosition(eFormComment);
			if (currentPageY > posCommentArea.top) {
				if (DOM.getElementStyle(eFormComment, 'position') != 'fixed' && parseInt(posCommentArea.height) > parseInt(posFormComment.height) + 100) {
					DOM.setElementStyle(eCommentArea, 'height', parseInt(posCommentArea.height) + 'px');
					DOM.setElementStyle(eFormComment, 'position', 'fixed');
					DOM.setElementStyle(eFormComment, 'top', '20px');
					DOM.setElementStyle(eFormComment, 'marginLeft', '460px');
				}
			} else {
				DOM.setElementStyle(eFormComment, 'position', 'relative');
				DOM.setElementStyle(eFormComment, 'top', '0px');
				DOM.setElementStyle(eFormComment, 'marginLeft', '20px');
			}
		}
	}
	//initLightbox();
}


/*
AJAX COMENTARIOS
*/
$("input_comment").onfocus= function() {
	if (this.value != "") this.value="";		
}


$('form_comments').onsubmit = function () {
		autor = this.elements["author"].value;
		email = this.elements["email"].value;
		url = this.elements["url"].value;
		id = this.elements["comment_post_ID"].value;
		comment = this.elements["comment"].value;
		this.elements["comment"].value = "";
		
		$('ajax_comments').innerHTML += '<p style="text-align:center;">Insertando el comentario</p>';
		$('ajax_comments').innerHTML += Loading();
		var myConn = CreaAjax();
    var inserta = function (oXML) {commentNew(id);};
    myConn.connect(newComent, "POST", "comment_post_ID="+ id +"&author=" + autor + "&email=" + email + "&url=" + url + "&comment=" + comment, inserta);
    return false;
}

function commentNew(id){
        target= $('ajax_comments');
        target.style.display= "";
		var myConn = CreaAjax();
	    var comments = function (oXML) {
			target.style.display = "none";
			target.innerHTML = oXML.responseText;
			Muestra("ajax_comments");
		};
		myConn.connect(urlComments, "POST", "comment_post_ID="+id, comments);
}

function Oculta(id){$(id).style.display = "none";}
function Muestra(id){$(id).style.display = "";}
function CreaAjax(){var myConn = new XHConn(); if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");return myConn;}
function Loading() { return '<p style="text-align:center;"><img src="' + imgLoading + '" alt="Cargando..." /></p>';}

/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET"){xmlhttp.open(sMethod, sURL+"?"+sVars, true); sVars = "";}
      else {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete){ bComplete = true;fnDone(xmlhttp);}};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}
