/*---------------------------------------Galeria-----------------------------*/
/*---------------------------------------------------------------------------*/
var actual=-2;
function ant_img(){
  if(actual==-2){actual=document.getElementById("num_selec").value;}
  dire_ant=document.getElementById("mostra").src;
  dire_act=document.getElementById("mostra_seg").src;
  num_img=document.getElementById("num_fotos").value;
  if(actual==num_img){
    actual=0;
  }
  else{
    actual=actual+1;
  } 
  if(actual==num_img){   
    dire_seg=document.getElementById(0).src;
  }
  else{
    ant=actual+1;
    dire_seg=document.getElementById(ant).src;
  } 
  refer_src(dire_ant,dire_act,dire_seg);
  adecuar_nav();
}

function seg_img(){
  if(actual==-2){actual=document.getElementById("num_selec").value;}
  dire_seg=document.getElementById("mostra").src;
  dire_act=document.getElementById("mostra_ant").src;
  num_img=document.getElementById("num_fotos").value;
  if(actual==0){
    actual=num_img;
  }
  else{
    actual=actual-1;
  } 
  if(actual==0){   
    dire_ant=document.getElementById(num_img).src;
  }
  else{
    ant=actual-1;
    dire_ant=document.getElementById(ant).src;
  }
  refer_src(dire_ant,dire_act,dire_seg);
  adecuar_nav();
}

function refer_src(dire_ant,dire_act,dire_seg){
  ant=document.getElementById("mostra_ant");
  dire_ant=dire_ant.replace("Mitjanes","Minis");
  dire_ant=dire_ant.replace("_mitjana","_mini");
  ant.src=dire_ant;
  
  act=document.getElementById("mostra");
  dire_act=dire_act.replace("Minis","Mitjanes");
  dire_act=dire_act.replace("_mini","_mitjana");
  act.src=dire_act;
  
  seg=document.getElementById("mostra_seg");
  dire_seg=dire_seg.replace("Mitjanes","Minis");
  dire_seg=dire_seg.replace("_mitjana","_mini");
  seg.src=dire_seg;
}

function seleccionar(num){
  actual=num;
  dire_act=document.getElementById(num).src;
  num_img=document.getElementById("num_fotos").value;
  if(num==0){
    dire_ant=document.getElementById(num_img).src;
  }
  else{
    ant=num-1;
    dire_ant=document.getElementById(ant).src;
  }
  if(num==num_img){
    dire_seg=document.getElementById(0).src;
  }
  else{
    seg=num+1;
    dire_seg=document.getElementById(seg).src;
  }
  refer_src(dire_ant,dire_act,dire_seg);
  adecuar_nav();
}

actual_pup=0;
function ant_img_pup(){
  if(actual_pup==num_img){
    actual_pup=0;
  }
  else{
    actual_pup=actual_pup+1;
  }
  refer_src_pop(document.getElementById(actual_pup).src);
}

function refer_src_pop(dire){
  dire=dire.replace("Mitjanes","Originals");
  dire=dire.replace("_mitjana","_original");
  dire=dire.replace("Minis","Originals");
  dire=dire.replace("_mini","_original");
  document.getElementById("mostra2").src=dire;
}

function seg_img_pup(){    
  num_img=document.getElementById("num_fotos").value;
  if(actual_pup==0){
    actual_pup=num_img;
  }
  else{
    actual_pup=actual_pup-1;
  }
  refer_src_pop(document.getElementById(actual_pup).src);
}

function pause(){
  parar=true;
  document.getElementById("stop").style.display='none';
  document.getElementById("play").style.display='block';
  clearTimeout(temp);
}

function seleccionar_pup(num){
  actual_pup=num;
  refer_src_pop(document.getElementById(num).src);
}

function tancar(){
  document.getElementById("popup").style.display='none';
  pause();
}

function presentacio(num){  
  document.getElementById("popup").style.display='block';
  refer_src_pop(document.getElementById("mostra").src);
  document.getElementById("stop").style.display='none';  
  document.getElementById("play").style.display='block';
  if(num==0){
    document.getElementById("play").style.display='none';
    document.getElementById("stop").style.display='block';
    setTimeout("continuar()",2000);
    setTimeout("reproduir()",2000);
  }
}

parar=false;temp=0;
function reproduir(){  
  if(parar==false){seg_img_pup();temp=setTimeout("reproduir()", 2000);} 
}

function continuar(){
  parar=false;
  document.getElementById("stop").style.display='block';
  document.getElementById("play").style.display='none';
}


function apareixer(){
  pause();
  document.getElementById("comentaris").style.margin='-250px';
}

function desapareixer(){
  document.getElementById("comentaris").style.margin='0px';  
}

function adecuar_nav(){
  w=document.getElementById("mostra").width;
  h=document.getElementById("mostra").height;
  imat=document.getElementById("capa_centre_img");
  imat.style.width=w;
  imat.style.height=h;

  document.getElementById("capa_dreta_img").style.height=h;
  document.getElementById("capa_esq_img").style.height=h;
  h_treure_d=document.getElementById("mostra_seg").height/2;
  h_treure_e=document.getElementById("mostra_ant").height/2;
  document.getElementById("mostra_ant").style.marginTop=(h/2)-(h_treure_e);
  document.getElementById("mostra_seg").style.marginTop=(h/2)-(h_treure_d);
  
  document.getElementById("fl_esq_r_capa").style.marginTop=(h/2)-23;
  document.getElementById("fl_dreta_r").style.marginTop=(h/2)-23;
}

/*---------------------------------------UTILS--------------------------------*/
/*---------------------------------------------------------------------------*/
ie=false;
function resize_img(img){
    nova_img = new Image();
    nova_img.src = img.src;
    w=nova_img.width;
    h=nova_img.height; 
    w_i=nova_img.width;
    h_i=nova_img.height;
    if(w==0 || w==null || h==0 || h==null){/*És IE*/
      ie=true;
      w=img.style.width;
      h=img.style.height;  
    }      	
    if(w!=0 && h!=0){
    maxh=h;maxw=w;
    if(h>window.innerHeight || w>window.innerWidth){maxh=window.innerHeight-90;maxw=window.innerWidth-250;}
    if((h>window.document.body.clientHeight || w>document.body.clientWidth) && ie==true){maxh=document.body.clientHeight-90;maxw=document.body.clientWidth-250;}

    proph = maxh/h;
  	propw = maxw/w;		
  	
  	if(h>w){
  		if(h != maxh){
        h =h*proph; 
        w = w*proph;
      }  				
  		if(w>maxw){
        propw = maxw/w;
        w = w*propw; 
        h = h*propw;        
      }	
    }
    else{		
      if(w != maxw){
      w = w*propw; 
      h = h*propw;
      }
    	if(h > maxh){
        proph = maxh/h;
        h = h*proph; 
        w = w*proph;
      }			
    }	
  }
  if(ie==true){
    img.style.width=w;
    img.style.height=h;
  }
  else{
   img.width=w;
   img.height=h;  
  }
  imatge_centre=document.getElementById("imatge_pop_centre");
  imatge_centre.style.width=w;
  imatge_centre.style.height=h;
  
  document.getElementById("f_dreta_popUp").style.height=h;
  document.getElementById("f_esq_popUp").style.height=h;

  ant2=document.getElementById("mostra_ant2");
  seg2=document.getElementById("mostra_seg2");
  ant2.style.marginTop=(h/2)-45;
  seg2.style.marginTop=(h/2)-45;
}

function getDataServer(url, vars){
   var xml = null;
   try{
       xml = new ActiveXObject("Microsoft.XMLHTTP");
   }catch(expeption){
       xml = new XMLHttpRequest();
   }
   xml.open("Get",url + vars, false);
   xml.send(null);
   if(xml.status == 404) alert("Url no valida");
   return xml.responseText;
}

/*---------------------------------------Gestió Galeria-----------------------*/
/*---------------------------------------------------------------------------*/
function validar(){
  seleccionat=document.getElementById("categoria").selectedIndex;
  if(seleccionat==0){
    alert("Seleccione una categoría, una subcategoría o una subsubcategoría");
    return false;
  }
  return true;
}

function assignar_id(){
  hid=document.getElementById("id_selecc");
  select=document.getElementById("categoria");
  hid.value=select[select.selectedIndex].value;
}

function nova_categoria(){
  que=document.getElementById("crear_editar").value;
  if(que==0 || que==1){
    nom=document.getElementById("nom").value;
    logo=document.getElementById("logo").value;
    actuar=false;
    if(logo!="" && que==0 && nom!=""){actuar=true;}
    else if(logo=="" && que==1 && nom!=""){actuar=true;}
    else if(logo!="" && que==1 && nom==""){actuar=true;}
    else if(logo!="" && que==1 && nom!=""){actuar=true;}
    else{
      if(que==0){alert("Indique el nombre i el logotipo del elemento para crearlo.");}
      else if(que==1){alert("Para editar un elemento es preciso indicar el nuevo nombre y/o logotipo.");}
    }
  }
  else{return true;}
  return actuar;
}
function fer(num){/*que=0=>Crear  que=1=>Editar*/
  document.getElementById("crear_editar").value=num;
}