var widthHtmlSplash = 0;
var widthEMCII = new Array();
var isHome = false;
var isImgGal = false;
var splashShowed = 1;
var idShowSplashTimeOut;
var imgEMShowed = 1;
var tSliceEMCII = new Array();
var imgSlided = 5;
var agregarComentario;
var verComentarios;
var slideImagenes;
function sliceHtmlSplash(idItemSplash){
	widthHtmlSplash += 8;
	var htmlSplashContainer = document.getElementById( 'htmlSplashContainer' + idItemSplash );
   htmlSplashContainer.style.width = "" + widthHtmlSplash + "px";
	if( widthHtmlSplash < 319 ){
		setTimeout("sliceHtmlSplash(" + idItemSplash + ")",1);
	}
}
function openHtmlSplash(idItemSplash){
   //var htmlSplashContainer = document.getElementById( 'htmlSplashContainer' + idItemSplash );
   //var htmlSplash = document.getElementById( 'htmlSplash' + idItemSplash );
   //htmlSplashContainer.style.width = '287px';
	sliceHtmlSplash(idItemSplash);
}
function closeHtmlSplash(idItemSplash){
   var htmlSplashContainer = document.getElementById( 'htmlSplashContainer' + idItemSplash );
   htmlSplashContainer.style.width = '0px';
}
function closeSplash(){
   var cItemsSplash = xGetElementsByClassName('itemSplashHidden');
   //var oItemsSplash = document.getElementsByName('oItemSplash');
   var menusSplash = document.getElementsByName('menuSplash');
   for(var i=0; i < cItemsSplash.length; i++){
      xDisplay(cItemsSplash[i],'none');
      xVisibility(cItemsSplash[i],false);
      //itemsSplash[i].style.display = 'none';
      menusSplash[i].style.color = '#FFF';
      closeHtmlSplash(i+1);
   }
	widthHtmlSplash = 0;
}
function showSplash( idItemSplash ){
   closeSplash();
   //var itemSplash = document.getElementById( 'itemSplash' + idItemSplash );
   var menuSplash = document.getElementById( 'menuSplash' + idItemSplash );
   xDisplay('itemSplash'+idItemSplash,'block');
   xVisibility('itemSplash'+idItemSplash,true);
   //itemSplash.style.display = 'block';
   menuSplash.style.color = '#900';
	widthHtmlSplash = 7;
   openHtmlSplash(idItemSplash);
   splashShowed = idItemSplash;
   splashShowed++;
   //alert("splashShowed: " + splashShowed + " maxSplashShowed: " + document.getElementsByName('menuSplash').length);
   if( splashShowed > document.getElementsByName('menuSplash').length ){
      splashShowed = 1;
   }
   clearTimeout(idShowSplashTimeOut);
   idShowSplashTimeOut = setTimeout("showSplash(" + splashShowed + ")",10000);
}


function abreFormComentarios(){
	xDisplay('agregarComentario','block');
	xVisibility('agregarComentario',true);
	var f = document.getElementById('formComentario');
	f.comentNombre.focus();
}
function cierraFormComentarios(){
	xDisplay('agregarComentario','none');
	xVisibility('agregarComentario',false);
}
function enviaComentarios(){
	var f = document.getElementById('formComentario');
	var ret = Spry.Widget.Form.validate(f);
	if (ret) {
		var postDatos = 'comentNombre=' + f.comentNombre.value + '&comentCorreo=' + f.comentCorreo.value +
						'&comentCiudad=' + f.comentCiudad.value + '&comentPais=' + f.comentPais.value +
						'&comentComentarios=' + f.comentComentarios.value;
		var header = { "Content-Type": "application/x-www-form-urlencoded; charset=iso-8859-1" };
		agregarComentario.loadContent(f.action,{id: "agregarComentario", headers: header, method: "POST", postData: postDatos });
		return false;
	}
	return false;
}

function closeEM(){
   var item = document.getElementById('cVideos');
   item.style.display = 'none';
   item = document.getElementById('cImagenes');
   item.style.display = 'none';
   item = document.getElementById('cAnuncio');
   item.style.display = 'none';
   
   var menu = document.getElementById('mnucVideos');
   var enlace = menu.getElementsByTagName('a');
   enlace[0].style.color = '#ccc';
   
   menu = document.getElementById('mnucImagenes');
   enlace = menu.getElementsByTagName('a');
   enlace[0].style.color = '#ccc';
   
   menu = document.getElementById('mnucAnuncio');
   enlace = menu.getElementsByTagName('a');
   enlace[0].style.color = '#ccc';
}
function showEM( idItem ){
   closeEM();
   var item = document.getElementById(idItem);
   item.style.display = 'block';
   var menu = document.getElementById('mnu'+idItem);
   var enlace = menu.getElementsByTagName('a');
   enlace[0].style.color = '#ffcc00';
}

function closeVideos(){
   var cVideos = document.getElementById('cVideos');
   cVideosArray = xGetElementsByClassName('centralVEM');
   for(var i=0; i<cVideosArray.length; i++){
      cVideosArray[i].style.display = 'none';
   }
}
function showVideo(idItem){
   closeVideos();
   var video = document.getElementById('videoEM' + idItem);
   video.style.display = 'block';
}

function closeImgEM(){
   var cImagenes = document.getElementById('cImagenes');
   var cImagenesArray = xGetElementsByClassName('centralIEM');
   for(var i=0; i<cImagenesArray.length; i++){
      cImagenesArray[i].style.display = 'none';
      closeEMCII(i+1);
   }
}
function showImgEM(idItemImgEM){
   closeImgEM();
   var imgEM = document.getElementById('imgEM'+idItemImgEM);
   imgEM.style.display = 'block';
   sliceEMCII(idItemImgEM);
}
function sliceEMCII(idItem){
   clearTimeout(tSliceEMCII[idItem]);
   var maxSlice = 95;
   if( arguments.length > 1 ){
      maxSlice = arguments[1];
   }
	if( widthEMCII[idItem] < maxSlice ){
      widthEMCII[idItem] += 5;
      var emCIIContainer = document.getElementById( 'emCII' + idItem );
      emCIIContainer.style.height = "" + widthEMCII[idItem] + "px";
      if( arguments.length > 1 ){
         tSliceEMCII[idItem] = setTimeout("sliceEMCII(" + idItem + "," + arguments[1] + ")",50);
      }else{
         tSliceEMCII[idItem] = setTimeout("sliceEMCII(" + idItem + ")",50);
      }
	}
}
function sliceCloseEMCII(idItem){
   clearTimeout(tSliceEMCII[idItem]);
   if( widthEMCII[idItem] > 0 ){
      widthEMCII[idItem] -= 5;
      var emCIIContainer = document.getElementById( 'emCII' + idItem );
      emCIIContainer.style.height = "" + widthEMCII[idItem] + "px";
		tSliceEMCII[idItem] = setTimeout("sliceCloseEMCII(" + idItem + ")",50);
	}
}
function openAllEMCII(){
   var cImgGals = xGetElementsByClassName('centralIEM');
   for(var i=0; i<cImgGals.length; i++){
      widthEMCII[i+1] = 0;
      if( arguments.length > 0 ){
         sliceEMCII(i+1,arguments[0]);
      }else{
         sliceEMCII(i+1);
      }
   }
}
function closeEMCII(idItem){
   var emCIIContainer = document.getElementById( 'emCII' + idItem );
   emCIIContainer.style.height = "0px";
   widthEMCII[idItem] = 0;
}

function closeAnuncioEM(){
   var cAnuncios = document.getElementById('cAnuncio');
   var cAnunciosArray = xGetElementsByClassName('centralAEM');
   for(var i=0; i<cAnunciosArray.length; i++){
      cAnunciosArray[i].style.display = 'none';
   }
}
function showAnuncioEM(idItemAnuncioEM){
   closeAnuncioEM();
   var anuncioEM = document.getElementById('anuncioEM'+idItemAnuncioEM);
   anuncioEM.style.display = 'block';
}

function imagenesNextPanel(){
   var cImgGals = xGetElementsByClassName('centralIEM');
   if( imgSlided < cImgGals.length ){
      imgSlided++;
      slideImagenes.showNextPanel();
   }else{
      imgSlided = 5;
      slideImagenes.showFirstPanel();
   }
}

function imagenesPreviousPanel(){
   var cImgGals = xGetElementsByClassName('centralIEM');
   if( imgSlided > 5 ){
      imgSlided--;
      slideImagenes.showPreviousPanel();
   }else{
      imgSlided = cImgGals.length;
      slideImagenes.showPanel(imgSlided - 5);
   }
}

function ajustaAlturaContenidoImagen(){
   var img = document.getElementById('imagenG');
   var contenido = document.getElementById('contenidoG');
   contenido.style.height = img.height+"px";
}

function hideVideoFlash(){
   var contVideo = document.getElementById('videoYT');
   if( contVideo != null ){
      contVideo.style.display = 'none';
   }
}

function showVideoFlash(){
   var contVideo = document.getElementById('videoYT');
   if( contVideo != null ){
      contVideo.style.display = 'block';
   }
}

function shImg(href){
   var effectObs = {
      onPostEffect:function(){
         closeItImagenGaleria.removeObserver(effectObs);
         if( href == "" ){
            imagenGaleria.loadContent(window.location.href, {id:'imagenGaleria'});
         }else{
            imagenGaleria.loadContent(href, {id:'imagenGaleria'});
         }
      }
   };
   var effectObs2 = {
      onPostEffect:function(){
         openItImagenGaleria.removeObserver(effectObs2);
         showVideoFlash();
         makeHistory(href);
         niceScrollTo(210);  // Ajustar en caso de cambio de altura deste la parte superior de la página hasta la sección de galerias
      }
   };
   var obs = {
      onPostUpdate:function(){
         imagenGaleria.removeObserver(obs);
         openItImagenGaleria.start();
      }
   };
   closeItImagenGaleria.addObserver(effectObs);
   openItImagenGaleria.addObserver(effectObs2);
   imagenGaleria.addObserver(obs);
   hideVideoFlash();
   closeItImagenGaleria.start();
   return false;
}

/*Código para el manejo del hash con AJAX*/
var expectedHash = "";

function makeHistory(newHash){
   if( newHash != "" ){
      //alert( "newHash: " + newHash );
      //alert( "expectedHash: " + expectedHash );
      urlHost = window.location.protocol + "//" + window.location.hostname;
      //alert("urlHost: " + newHash.substring(0, urlHost.length) );
      //alert( "newHash2: " + newHash.substring( urlHost.length ) );
      if( urlHost == newHash.substring(0, urlHost.length) ){
         window.location.hash = newHash.substring( urlHost.length );
      }else{
         window.location.hash = newHash;
      }
      //window.location.hash = newHash;
   }
   expectedHash = window.location.hash;
   return true;
}

function handleHistory(){
   if ( window.location.hash != expectedHash ){
      expectedHash = window.location.hash;
      if( window.location.hash == "" ){
         shImg('');
      }else{
         var url = expectedHash.substring( 1 );
         shImg(url);
      }
      
   }
   return true;
}

function pollHash(){
   handleHistory();
   window.setInterval("handleHistory()", 500);
   return true;
}

function setCookie(c_name,value,expiredays){
   var exdate=new Date();
   exdate.setDate( exdate.getDate() + expiredays);
   document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function getCookie(c_name){
   if (document.cookie.length>0){
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1){
         c_start=c_start + c_name.length+1;
         c_end=document.cookie.indexOf(";",c_start);
         if (c_end==-1) c_end=document.cookie.length;
         return unescape(document.cookie.substring(c_start,c_end));
      }
   }
   return "";
}

function niceScrollTo(yPos){
   var sizeScrolling = 10;
   if( pageYOffset < yPos ){
      if( (yPos - pageYOffset) < sizeScrolling ){
         scrollTo(0,yPos);
      }else{
         scrollTo(0,pageYOffset+sizeScrolling);
         setTimeout("niceScrollTo(" + yPos + ")",30);
      }
   }
}

function shSlide(href){
   var effectObs = {
      onPostEffect:function(){
         closeItSlideImagenGaleria.removeObserver(effectObs);
         if( href == "" ){
            contenedorSlide.loadContent(window.location.href, {id:'contenedorSlide'});
         }else{
            contenedorSlide.loadContent(href, {id:'contenedorSlide'});
         }
      }
   };
   var effectObs2 = {
      onPostEffect:function(){
         openItSlideImagenGaleria.removeObserver(effectObs2);
      }
   };
   var obs = {
      onPostUpdate:function(){
         contenedorSlide.removeObserver(obs);
         openItSlideImagenGaleria.start();
      }
   };
   closeItSlideImagenGaleria.addObserver(effectObs);
   openItSlideImagenGaleria.addObserver(effectObs2);
   contenedorSlide.addObserver(obs);
   closeItSlideImagenGaleria.start();
   return false;
}

function init(){
	if( isHome == true ){
		showSplash(splashShowed);
      openAllEMCII();
      showImgEM(imgEMShowed);
      showVideo(1);
      showEM('cVideos');
      showAnuncioEM(1);
	}
   if( isImgGal == true ){
      pollHash();
   }
}
