//TODO: revisar todo el js porqeu lo hice a medida qeu avance.

//TODO: funcion candidata a desaparecer
function toggleDisplayBlock(id){

element = document.getElementById(id);
if(element){
  if(element.style.display == 'none'){
    element.style.display = 'block';
  }
  else{
    element.style.display = 'none';
  }

}
}

function isExplorer(){
    return (navigator.appName == 'Microsoft Internet Explorer')?true:false;
}
//TODO: rehacer para que se puede usar con popups de cualquier tamanio
function screenCenter(id){
//TODO: ver como solucionar el tema de los indices
  if (id){
    var element = document.getElementById(id);
// FIXMEEEEEE

    var W = (element.style.width)?element.style.width:"0px";
    var H = (element.style.height)?element.style.height:"0px";
    if ((id == "share" || id == "previewWindow" ) && !isExplorer())
    {
      W = '200px';
      H = '455px';
    }
    if(!isExplorer()){
        var screenWidth = window.innerWidth/2 - parseInt(W)/2;
        var screenHeight = window.innerHeight/2 - parseInt(H)/2;
        var position = 'fixed';
    }
    else
    {
        var screenWidth = 640/2 - parseInt(W)/2 - 200;
        var screenHeight = 480/2 - parseInt(H)/2;
        var position = 'absolute';
    }

    screenWidth += 'px';
    screenHeight += 'px'; 
    
    positionBlock(id,position,screenWidth,screenHeight);
  }
}

function positionBlock(id, position, xValue, yValue){

    var element = document.getElementById(id);
    if(element){
        element.style.top = yValue;
        element.style.left = xValue;
	element.style.position = position;
    }
    
    var frame_msg = document.getElementById('frame_messages');
    var float = document.getElementById('floatingdiv');
    if (frame_msg && element && float)
    {
      var w = parseInt((element.style.width)?element.style.width:383);
      var h = parseInt((element.style.height)?element.style.height:100);
      
      frame_msg.style.width   = "1px";
      frame_msg.style.height  = "1px";
      frame_msg.style.top     = yValue;
      frame_msg.style.left    = xValue;
      frame_msg.style.position = position;
      
      float.style.width   = w + "px";
      float.style.height  = h + "px";
      
      float.style.position = position;
      float.style.left = xValue;
      float.style.top  = yValue;
      if(isExplorer()){
        AssignPosition(float,w,h,id);
      }

      if(float.lastChild.tagName != 'IFRAME'){
        div = document.createElement("div");
        div.appendChild(float.lastChild);
        div.style.display = 'none';
        float.appendChild(div);
      }
      float.appendChild(element);
    }
}

function showBlock(id){
  var element = document.getElementById(id);
  var float = document.getElementById('floatingdiv');
  var frame_msg = document.getElementById('frame_messages');
  if (frame_msg && float && element){
    frame_msg.style.display = 'block'; 
    float.style.display = 'block'; 
    element.style.display = 'block';
 }
}

function hideBlock(id){

var element = document.getElementById(id);
if(element){
    element.style.display = 'none';

}
var float = document.getElementById('floatingdiv');
var frame_msg = document.getElementById('frame_messages');
  if (frame_msg && float){
    frame_msg.style.display = 'none';
    float.style.display = 'none';  
  }
}

function hideAllByName(name){
var previewDivs = document.getElementsByName(name);
if(previewDivs){
    for(i=0; i<previewDivs.length; i++){
	previewDivs[i].style.display = 'none';  
    }
}
}

function showAllByName(name){
var previewDivs = document.getElementsByName(name);
if(previewDivs){
    for(i=0; i<previewDivs.length; i++){
	previewDivs[i].style.display = 'block';  
    }
}

}

function retrieveValueByCookieKey(key){
 var cookie = document.cookie;
 var pairs = document.cookie.split(';');
 if(!pairs){
    return 'false';
 }
 for(var i=0; i<pairs.length;i++ ){
    var keyValue = pairs[i].split('=');
    if(keyValue[0] == key){
	return keyValue[1];
    }
 }
 return 'false';
}

function addToCart(id){
    var element = document.getElementById('addForm_'+id);
    //TODO: para poner estas dos lineas en una nueva funcion
    new Ajax.Updater('addResult_'+id, '/index.php/cart/add/'+element.product_id.value+'/'+element.variant_id.value+'/1');
    new Ajax.Updater('cart','/index.php/cart/minicart');
    updatePromoWindow(updatePromoLink);
}

function isAddSubmitForm(id){
  var element = document.getElementById('addForm_'+id);
  if(element.add.value == 1){
    addToCart(id);
//    updatePromoWindow(updatePromoLink);
  }
  else{
    showBlock('addSection_'+id);
  }
}

function submitForm(id){
  var element = document.getElementById('addForm_'+id);
  if(element){
    //TODO: sacar el 1 de la variante flac de forma dinamica
    var showFlacAlert = (retrieveValueByCookieKey('flacAlertDontShow')=='false')?true:false;
    if(element.variant_id.value == 1 && showFlacAlert){
    //TODO: implementar una funcion para que solamente oculte los divs de previews qeu estan detras de la ventana flotante.
    //TODO: reemplazar lo qeu corresponda por la funcin show window
        showBlock('flacAlert');
	var fa = document.getElementById('flacAlert');
	fa.setAttribute('idProduct', id);
	hideBlock('addSection_'+id);
	new Draggable('flacAlert');
    }
    else
    {
	addToCart(id);
	hideAllByName('addSection');
	updatePromoWindow(updatePromoLink);
    }
  }
}

function showWindow(id, nameElementsToHide){
    if(nameElementsToHide instanceof Array){
        for(j=0; j<nameElementsToHide.length;j++){
		hideAllByName(nameElementsToHide[j]);
        }
    }
    showBlock(id);
//    new Draggable(id);
}

function getPreview(id){
  new Ajax.Updater('previewWindowImage', '/index.php/preview/index?id='+id, {parameters: {evalScript: true}});
}

function share(id){
  var productForm = document.getElementById(id);
  var shareForm = document.getElementById('shareForm'); 

  shareForm.idItem.value = productForm.id.value;
  shareForm.typeItem.value = productForm.type.value;
  shareForm.shareLink.value = productForm.shareLink.value;
  screenCenter('share');
  showWindow('shareContent');
}

function updatePromoWindow(action)
{
    new Ajax.Updater('promoDiv',action);
}

function addProductToCart(element){
    var showFlacAlert = (retrieveValueByCookieKey('flacAlertDontShow')=='false')?true:false;
    if(element.variant_id.value == 1 && showFlacAlert){
    //TODO: implementar una funcion para que solamente oculte los divs de previews qeu estan detras de la ventana flotante.
    //TODO: reemplazar lo qeu corresponda por la funcin show window
	screenCenter('flacAlert');
	showWindow('flacAlert');
	var fa = document.getElementById('flacAlert');
	fa.setAttribute('idProduct', element.product_id.value);
    }
    else{
	//TODO: pasar a una funcion para no repetir
	new Ajax.Updater('addResult_'+element.product_id.value, '/index.php/cart/add/'+element.product_id.value+'/'+element.variant_id.value+'/'+element.quantity.value);
	new Ajax.Updater('cart','/index.php/cart/minicart');
	updatePromoWindow(updatePromoLink);
    }
}




/*********Para saber posicion del mouse*************/
var cX = 0;
var cY = 0; 
var rX = 0; 
var rY = 0;

function UpdateCursorPosition(e){ 
  cX = e.pageX;
  cY = e.pageY;
}

function UpdateCursorPositionDocAll(e){ 
  cX = event.clientX;
  cY = event.clientY;
}
if(document.all) { 
  document.onmousemove = UpdateCursorPositionDocAll; 
}
else { 
  document.onmousemove = UpdateCursorPosition; 
}


function AssignPosition(d,w,h,id) {
  if(self.pageYOffset) {
    rX = self.pageXOffset;
    rY = self.pageYOffset;
  }
  else if(document.documentElement && document.documentElement.scrollTop) {
    rX = document.documentElement.scrollLeft;
    rY = document.documentElement.scrollTop;
  }
  else if(document.body) {
    rX = document.body.scrollLeft;
    rY = document.body.scrollTop;
  }
  if(document.all) {
    cX += rX; 
    cY += rY;
  }
  if (id == 'share')
  {
    w = w + w/2;
    h = h*4;
  }
  if (id == 'previewWindow')
  {
    h = h*3;
  }
  if(id == 'confirmPopup')
  {
    h = h*3;
  }
  d.style.left = (cX+10-w) + "px";
  d.style.top = (cY+10-h) + "px";
}
