var isFull=false;
$(function(){
   $(".fg-button:not(.ui-state-disabled)")
   .hover(
   function(){ $(this).addClass("ui-state-hover");},
   function(){ $(this).removeClass("ui-state-hover");}
)
.mousedown(function(){
   $(this).parents(".fg-buttonset-single:first").find(".fg-button.ui-state-active").removeClass("ui-state-active");
   if( $(this).is(".ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active") ){ $(this).removeClass("ui-state-active"); }
   else { $(this).addClass("ui-state-active"); }})
.mouseup(function(){
   if(! $(this).is(".fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button") ){
   $(this).removeClass("ui-state-active");}});
$(".ui-button:not(.ui-state-disabled)")
.hover(
function(){ $(this).addClass("ui-state-hover");},
function(){ $(this).removeClass("ui-state-hover"); }
)
.mousedown(function(){
   $(this).parents(".ui-buttonset-single:first").find(".ui-button.ui-state-active").removeClass("ui-state-active");
   if( $(this).is(".ui-state-active.fg-button-toggleable, .ui-buttonset-multi .ui-state-active") ){ $(this).removeClass("ui-state-active"); }
   else { $(this).addClass("ui-state-active"); }})
.mouseup(function(){
   if(! $(this).is(".ui-button-toggleable, .ui-buttonset-single .ui-button,  .ui-buttonset-multi .ui-button") ){
   $(this).removeClass("ui-state-active");}});
});
var elementoDEL;
var idDEL;
var userDEL;
$(document).ready(function(){
 $("#ButFull").click(function (){if (isFull) {isFull=false;fullScreenCancel();}else{isFull=true;fullScreen()}})
   lastVal = totHistory;
   $("#slider").slider({
      value:totHistory,
      min: 1,
      max: totHistory,
      animate: true,
      slide: function(event, ui) {
         if(lastVal>ui.value)
            $(buildQ(lastVal,ui.value)).hide("fast").find(".addComment").remove();
  	  else if(lastVal<ui.value)
            $(buildQ(lastVal,ui.value)).show("fast");
            lastVal = ui.value;
      }
   });
$("#borrar").dialog({
   autoOpen: false,
   modal: true,
   height: 200,
   width: 300,
   buttons:{
      "Si" : function(){
         $.ajax({
            type: "POST",
            url: "deleComment.php",
            data: "&id="+idDEL+"&user="+userDEL,
            success: function(msg){
               location.reload();
            }
         });
         $(this).dialog("close");
       },
      "Cancelar": function(){ $(this).dialog("close")}
   }
});
$("#avatar").dialog({
   autoOpen: false,
   modal: true,
   height: 175,
   width: 400,
   buttons:{
      "Si" : function(){
         document.form1.submit();
         $(this).dialog("close");
      },
      "Cancelar": function(){ $(this).dialog("close")}
   }
});
});
var totHistory=0;
var positions = new Array();
var lastVal;
function addHistory(obj){
   totHistory++;
   positions.push(obj.id);
}
function buildQ(from,to){
   if(from>to){
      var tmp=to;
      to=from;
      from=tmp;
   }
   from++;
   to++;
   var query="";
for(var i=from;i<to;i++){
   if(i!=from) query+=",";
   query+=".com-"+positions[i-1];
}
return query;
}
function addNew(user){
   var $el;
   if($(".waveButton").length) return false;
   $el = $("#commentArea");
   parent=0;
   $(".waveComment").show("slow");
   lastVal = totHistory;
   $("#slider").slider("option","value",totHistory);
   imageAdd=document.getElementById("imageUser").src;
   var comment ='<div class="waveComment addComment">\
      \
      <div class="comment">\
      <div class="commentAvatar">\
      <img src="'+imageAdd+'" width="30" height="30" />\
      </div>\
      \
      <div class="commentText">\
      \
      <textarea class="textArea" rows="2" cols="70" name="" />\
      <div><button class="ui-button ui-widget ui-state-default ui-corner-all" onclick="addSubmit(this,'+parent+',\''+user+'\')" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-plusthick"></span></button><button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="cancelAdd(this);return false" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-minusthick"></span></button></div>\
	\
      </div>\
      </div>\
      </div>';
      $el.append(comment);
}
function addComment(where,parent,user){
   var $el;
   if($(".waveButton").length) return false;
   if(!where)
      $el = $("#commentArea");
   else
      $el = $(where).closest(".waveComment");
   if(!parent) parent=0;
   $(".waveComment").show("slow");
   lastVal = totHistory;
   $("#slider").slider("option","value",totHistory);
   imageAdd=document.getElementById("imageUser").src;
   var comment ='<div class="waveComment addComment">\
   \
   <div class="comment">\
   <div class="commentAvatar">\
   <img src="'+imageAdd+'" width="30" height="30" />\
   </div>\
   \
   <div class="commentText">\
   \
   <textarea class="textArea" rows="2" cols="70" name="" />\
   <div><button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="addSubmit(this,'+parent+',\''+user+'\')" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-plusthick"></span></button><button class="ui-button ui-widget ui-state-default ui-corner-all"  onclick="cancelAdd(this);return false" style="margin-bottom:5px; margin-left:5px;"><span class="ui-icon ui-icon-minusthick"></span></button></div>\
   \
   </div>\
   </div>\
   </div>';
   $el.append(comment);
}
function cancelAdd(el){ $(el).closest(".waveComment").remove();}
function addSubmit(el,parent,user){
   var cText = $(el).closest(".commentText");
   var text = cText.find("textarea").val();
   var wC = $(el).closest(".waveComment");
   if(text.length<4){
      alert("Your comment is too short!");
      return false;
}
$(el).parent().html('<img src="img/ajax_load.gif" width="16" height="16" />');
$.ajax({
   type: "POST",
   url: "saveComment.php",
   data: "comment="+encodeURIComponent(text)+"&parent="+parent+"&user="+user,
   success: function(msg){
   var ins_id = parseInt(msg);
   if(ins_id){
      wC.addClass("com-"+ins_id);
      addHistory({id:ins_id});
      $("#slider").slider("option", "max", totHistory).slider("option","value",totHistory);
      lastVal=totHistory;
   }
   transForm(text,cText,user);
   }
});
}
function transForm(text,cText,user){
   var tmpStr ='<span class="name">'+user+':</span> '+text;
   cText.html(tmpStr);
}
function delComment(el,id,user){
   elementoDEL=el;
   idDEL=id;
   userDEL=user;
   $("#borrar").dialog("open");
   return false;
}
function showDialogProfe(){
  $("#profe").dialog("open");
  return false;
}
$(document).ready(function() {
  $("#profe").dialog({
  autoOpen: false,
  modal: true,
  height: 575,
  width: 925,
  buttons:{
      "Pechar": function(){ $(this).dialog("close")}
  }
 });
});

function addAvatar(user){
   document.getElementById("avatarCambio").src=document.getElementById("imageUser").src;
   userDEL=user;
   $("#avatar").dialog("open");
   return false;
}
function fullScreen() {
var docElement, request;docElement = document.getElementById("main");
request = docElement.requestFullScreen || docElement.webkitRequestFullScreen || docElement.mozRequestFullScreen || docElement.msRequestFullScreen;
if (typeof request != "undefined" && request) {request.call(docElement);}}
function fullScreenCancel() {
var docElement, request;docElement = document;
request = docElement.cancelFullScreen || docElement.webkitCancelFullScreen || docElement.mozCancelFullScreen || docElement.msCancelFullScreen || docElement.exitFullscreen;
if (typeof request != "undefined" && request) {request.call(docElement);}}
