// JavaScript Document

/*------------EFECTOS/ FUNCIONES DEL BUSCADOR DE PRODUCTOS-----------------------*/
function FixIniBuscador()
{
	document.location.href = "../index.php";
	
	var Cargando =  "<div align='center'><img src='images/spinner.gif' /></div>";	
	$('hldr_MainLayout').innerHTML = Cargando;
	
	IniBuscadorProductos(1);
}

function IniBuscadorProductos(idDepartamento)
{
	var iniBuscador = new Ajax.Request("templates/BuscadorProductos.php",
                        {
                                method: 'get',
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$("hldr_MainLayout").innerHTML = resp.responseText;
									//setContentIniBuscador(idDepartamento);
									setViewBannerBuscadorProductos(0);
								}
                        });						
}

function setContentIniBuscador(idDepartamento)
{				
	setViewBannerBuscadorProductos(idDepartamento);
	
	var setBuscador = new Ajax.Request("comp/buscadorProductosResult.php",
                        {
                                method: 'get',
                                parameters: 'idDepartamento=' + idDepartamento + '&idFamilia=-1',
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('hldr_contenidoBuscador').innerHTML =resp.responseText;
									setFiltrosBuscador('idDepartamento=' + idDepartamento + '&idFamilia=-1');
									var a=setTimeout('animarBuscadorProductos()',500);
									setPaginacionBuscadorProductos();
								}
                        });
}

function setFiltrosBuscador(urlParams)
{
	var setFiltrosBuscador = new Ajax.Request("comp/setFiltrosBuscadorProductos.php",
                        {
                                method: 'get',
                                parameters: urlParams,
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('hldr_BuscadorFiltros').innerHTML =resp.responseText;
								}
                        });		
}

function departamento_Change(idDepartamento)
{
	if(document.getElementById('BuscadorCurrentState'))
	{
		if(document.getElementById('BuscadorCurrentState').value == 1)
		{
			//animarSalidaBuscadorProductos();
			setCargandoBuscadorProductos();
			setContentIniBuscador(idDepartamento);
		}
		else
		{
			document.getElementById('BuscadorCurrentState').value = 1;
			setCargandoBuscadorProductos();
			
			setContentIniBuscador(idDepartamento);
		}
	}
}

function animarBuscadorProductos()
{
	//Effect.SlideDown('productos',{duration: 1.0, queue:{scope:'myscope', position:'end', limit: 1}});
	//Effect.DropOut('productos',{duration: 1.0, queue:{scope:'myscope', position:'end', limit: 1}});
	//Effect.Fade('productos', { duration: 2.0, from: 0.0, to: 1.0 });
	/*Effect.Appear('productos');
	di=document.images;
	ni=di.length;	
	
	appear = 0;*/
	
	/*for(var q=0;q<ni;++q) 
	{
		if(di[q].id.substring(0,7) == "imgProd")
		{
			appear++;
			<?php //echo "var t=setTimeout(\"Effect.Appear('\" + di["?> q <?php //echo "].id + \"');\","?> (appear*500) <?php //echo ");" ?>					
		}		
	}*/
}

function animarSalidaBuscadorProductos()
{
	Effect.Fade('productos',{duration: 1.0, queue:{scope:'myscope', position:'end', limit: 1}});	
}

function setCargandoBuscadorProductos()
{
	//Tabla Buscador Cargando... para el cambio de Departamento o Primera carga del Buscador.
	var tblBuscadorCargando =  "<div align='center'><img src='images/spinner.gif' /></div>";
	
	$('hldr_contenidoBuscador').innerHTML = tblBuscadorCargando;
}

function setPaginacionBuscadorProductos()
{
	new Control.Tabs('tabs_paginacion');
}

function setViewBannerBuscadorProductos(idDepartamento)
{		
	switch(idDepartamento)
	{
		case 0:
			idVista = 1;
			break;
		case 1:
			idVista = 2;
			break;
		case 2:
			idVista = 3;
			break;
		case 3:	
			idVista = 4;
			break;
	}		
	
	var sObjectFlash;
	var oDiv = document.getElementById("hldr_headerBuscador");
	sObjectFlash = '<object id="bannerBuscadorProductos" type="application/x-shockwave-flash" data="swf/baseBuscadorProductos.swf"  height="272" width="826" > \
					<param name="Movie" value="swf/baseBuscadorProductos.swf"> \
					<param name="Src" value="swf/baseBuscadorProductos.swf"> \
					<param name="Menu" value="0"> \
					<param name="FlashVars" value="clip_cargar=' + idVista  +'"> \
					<param name="WMode" value="Transparent"> \
					</object>'    
	oDiv.innerHTML = sObjectFlash;
}

/*****************EVENTOS POSTBACK DEL BUSCADOR*******************/
function makePostBackBuscador(urlParams)
{
	var familiaChange = new Ajax.Request(urlParams,
                        {
                                method: 'get',
                                parameters: 'NDVisbile=1',
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('hldr_contenidoBuscador').innerHTML =resp.responseText;
									setFiltrosBuscador(urlParams);
									var a=setTimeout('animarBuscadorProductos()',500);
									setPaginacionBuscadorProductos();
								}
                        });
}

function SetProductoDetalle(idDepartamento, idProducto)
{			
	setCargandoBuscadorProductos();	
	
	var productSelected = new Ajax.Request('comp/detalleProducto.php',
                        {
                                method: 'get',
                                parameters: 'idDepartamento=' + idDepartamento + '&idProducto=' + idProducto,
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('hldr_contenidoBuscador').innerHTML =resp.responseText;
									var a=setTimeout('animarDetalleProducto()',0);
									
									document.getElementById('BuscadorCurrentState').value = 2;														
								}
                        });
	
	setViewBannerBuscadorProductos(idDepartamento);
}

function SetBuildPageContent(idPagina)
{
	$('hldr_contenidoBuscador').innerHTML = "<div align='center'><br><br><br><br><br><img src='images/spinner.gif' style='height:32px; width:32px;' /></div>";

	var BuildPageContent = new Ajax.Request('comp/buildPage.php',
                        {
                                method: 'get',
                                parameters: 'idPagina=' + idPagina,
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('hldr_contenidoBuscador').innerHTML =resp.responseText;									
									document.getElementById('BuscadorCurrentState').value = 2;														
								}
                        });	
}

function ddlFamilia_Change(urlParams)
{	
	if(document.getElementById('BuscadorCurrentState'))
	{
		if(document.getElementById('BuscadorCurrentState').value == 1)
		{						
			animarSalidaBuscadorProductos();	
		
			var c=setTimeout("makePostBackBuscador('" + urlParams + "')",1000);
		}
		else
		{					
			makePostBackBuscador(urlParams);
			
			document.getElementById('BuscadorCurrentState').value = 1;
		}
	}
}

function ddlPropiedad_Change(urlParams)
{
	animarSalidaBuscadorProductos();
	
    selects = document.getElementsByName ('ddlPropiedad');
	num = selects.length;
	propiedadesParam = "&idPropiedad=";	
	
	for(var i=0;i<num;++i)
	{
		if(selects[i].value != -1)
		{
			propiedadesParam += selects[i].value + ",";
		}
	}	
	
	var c=setTimeout("makePostBackBuscador('" + urlParams + propiedadesParam + "')",1000);
}

function productSelected(idDepartamento, idProducto)
{
	if(document.getElementById('BuscadorCurrentState'))
	{
		if(document.getElementById('BuscadorCurrentState').value == 1)
		{							
			ScrollTop();
			animarSalidaBuscadorProductos();			
			var c=setTimeout("Effect.SwitchOff('buscadorTbl')",750);		
			setFiltrosBuscador("idDepartamento=" + idDepartamento);
			var p=setTimeout("SetProductoDetalle(" + idDepartamento + "," + idProducto + ")",1500);
		}
		else
		{
			ScrollTop();						
			setFiltrosBuscador("idDepartamento=" + idDepartamento);
			SetProductoDetalle(idDepartamento, idProducto);
			
			document.getElementById('BuscadorCurrentState').value = 2;
		}
	}
}

function printBuscadorProductos(parametrosBuscador)
{
	window.open('templates/ImprimirBuscadorProductos.php' + parametrosBuscador, '_blank', 'width=750,height=550,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=yes');
}


/*------------EFECTOS/ FUNCIONES DEL DETALLE DE PRODUCTO-----------------------*/
function animarDetalleProducto()
{
	Effect.Appear('producto',{duration: 1.0, queue:{scope:'myscope', position:'end', limit: 1}});	
}

function zoomImagen()
{
	//FUNCION QUE ABRE LA VENTANA MODAL PARA VER LA IMAGEN ZOOM DEL PRODUCTO ACTUALMENTE SELECCIONADO.
	//MANDA LLAMAR LA FUNCION modalAjaxWindow CON LOS PARAMETROS SOLICITADOS.
	document.getElementById("imgZoom").href = document.getElementById('hdnZoomImg').value;		 
	document.getElementById("imgZoom").rel = "lightbox[" + document.getElementById("hdnIdColor").value + "]";
}

function cambiarImagenProducto(imgThumb, imgZoom)
{		
	//ESTA FUNCION HACE LOS EFECTOS DE TRANSICION DE SALIDA DE IMAGEN, LLAMA A LA FUNCION QUE CAMBIA LA IMAGEN EN EL CONTROL
	//Y DESPUES HACE APARECER LA IMAGEN CON UN NUEVO EFECTO ESTABLECIDO.
	Effect.Fade('imgProducto', { duration: 0.5 });
	
	var i=setTimeout("cambiaImagen('" + imgThumb + "','" + imgZoom + "')", 500);
	
	var t=setTimeout("Effect.Appear('imgProducto');",1000);
}

function cambiaImagen(imgThumb, imgZoom)
{
	//ESTA FUNCION CAMBIA LA IMAGEN EN EL CONTROL IMG DEL PRODUCTO.
	var img = document.getElementById("imgProducto");
	img.setAttribute("src", imgThumb);	
}

function colorRollover(idImg, img, nombreColor)
{
	//FUNCION QUE CAMBIA LA IMAGEN DEL COLOR, DEL ID QUE RECIBE Y QUE CAMBIA EL NOMBRE DEL COLOR EN EL DIV NOMBRE COLOR.
	//SEGUN EL PARAMETRO nombreColor.
	//ESTO LO HACE SIEMPRE Y CUANDO EL ID RECIBIDO NO SE EL ACTUALMENTE SELECCIONADO.
	if(document.getElementById('hdnIdColor').value == idImg)
	{

	}
	else
	{
		document.getElementById(idImg).src = img;
			
		document.getElementById("divImgColor").innerHTML = nombreColor;	 
	}
}

function colorRollout(idImg, img)
{
	//FUNCION QUE CAMBIA LA IMAGEN DEL COLOR, DEL ID QUE RECIBE Y QUE CAMBIA EL NOMBRE DEL COLOR EN EL DIV NOMBRE COLOR.
	//SEGUN EL ESTABLECIDO EN EL HIDDEN CONTROL NOMBRE COLOR.
	//ESTO LO HACE SIEMPRE Y CUANDO EL ID RECIBIDO NO SE EL ACTUALMENTE SELECCIONADO.
	if(document.getElementById('hdnIdColor').value == idImg)
	{
		
	}
	else
	{
		document.getElementById(idImg).src = img;
				
		document.getElementById("divImgColor").innerHTML = document.getElementById('hdnNombreColor').value;
	} 
}

function colorSelected(idImg, imgThumb, imgZoom, imgColor, NombreColor)
{
	//FUNCION QUE SE DISPARA CUANDO SE SELECCIONA UN NUEVO COLOR EN LA GALERIA DE LOS PRODUCTOS.
	//HACE EL SWITCH DE IMAGEN Y COLOR SELECCIONADO.
	//ESTO LO HACE SIEMPRE Y CUANDO EL ID RECIBIDO NO SE EL ACTUALMENTE SELECCIONADO.
	if(document.getElementById('hdnIdColor').value == idImg)
	{

	}
	else
	{
		//Cambiar la Imagen del color actualmente seleccionado por la de estado "normal".
		var arrayImagenNormal = document.getElementById(document.getElementById('hdnIdColor').value).src.split("_",2);	
		document.getElementById(document.getElementById('hdnIdColor').value).src = arrayImagenNormal[0] + ".jpg";
	
		//seteamos en los Hidden los nuevos valores del color seleccionado.
		document.getElementById('hdnIdColor').value = idImg;
		document.getElementById('hdnNombreColor').value = NombreColor;
		document.getElementById('hdnZoomImg').value = imgZoom;		
			
		
		//Seteamos la nueva imagen seleccioanda, en el id de img recibido.
		document.getElementById(idImg).src = imgColor;
		
		//pintamos el nombre del Nuevo color seleccionado en el Div del nombre de Color.
		document.getElementById("divImgColor").innerHTML = NombreColor;	
		
		//llamamos a la funcion que cambia la imagen del producto con los efectos establecidos en esa funcion.
		cambiarImagenProducto(imgThumb, imgZoom);
	}
}


/*------------------FUNCIONES EFECTOS DEL LAYOUT CONTENIDO---------------------------------*/

function LoadTemplate_PageContent(urlTemplate, idPagina)
{
	$('content').innerHTML = "<div align='center'><br><br><br><br><br><img src='images/spinner.gif' style='height:32px; width:32px;' /></div>";

	var BuildPageContent = new Ajax.Request(urlTemplate,
                        {
                                method: 'get',
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									showResponse(resp);																		
									SetBuildPage_Contenido(idPagina);
								}
                        });	
}

/* Función para cargar una familia desde cualquier parte del sitio (Que no sea el Buscador de Productos) */
function LoadCategoryProduct(idDepartamento, idFamilia)
{
	$('hldr_MainLayout').innerHTML = "<div align='center'><br><br><br><br><br><img src='images/spinner.gif' style='height:32px; width:32px;' /></div>";
	
	var BuildPageContent = new Ajax.Request('templates/BuscadorProductos.php',
                        {
                                method: 'get',
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$("hldr_MainLayout").innerHTML = resp.responseText;
									$('hldr_contenidoBuscador').innerHTML = "";
									setViewBannerBuscadorProductos(idDepartamento);
									ddlFamilia_Change('comp/buscadorProductosResult.php?idDepartamento='+ idDepartamento + '&idFamilia=' + idFamilia);
								}
                        });
}

function SetBuildPage_Contenido(idPagina)
{
	$('contenido').innerHTML = "<div align='center'><br><br><br><br><br><img src='images/spinner.gif' style='height:32px; width:32px;' /></div>";

	var BuildPageContent = new Ajax.Request('comp/buildPage.php',
                        {
                                method: 'get',
                                parameters: 'idPagina=' + idPagina,
                                asynchronous: true,
								evalScripts: true,
                                onComplete: function (resp)
								{
									$('contenido').innerHTML =resp.responseText;
								}
                        });	
}

function animarContenido()
{
	Effect.Appear('contenido');
}


/*-----------------EFECTOS, FUNCIONES, VALIDACIONES DE ENVIAR MAIL CONTACTO------------------------*/

function ContactoValidarCampos()
{
	var mensaje = "";
	var isValid = true;
	
	if(document.getElementById('txtNombre').value == "")
	{
		mensaje += " Nombre es requerido\n";
		isValid =false;
	}
	
	if(document.getElementById('txtTelefono').value == "")
	{
		mensaje += "Telefono es requerido\n";	
		isValid =false;
	}
	
	if(document.getElementById('txtEmail').value == "")
	{
		mensaje += "Email es requerido\n";
		isValid =false;
	}
	else
	{
		var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		
		if (filter.test(document.getElementById('txtEmail').value))
		{
		
		}	
		else
		{
			mensaje += "Ingrese una direcci\u00f3n de correo v\u00e1lida\n";
			isValid =false;
		}
	}	
	
	if(document.getElementById('txtEdad').value == "")
	{
		mensaje += "Edad es requerido\n";
		isValid =false;
	}
	
	if(document.getElementById('txtCiudad').value == "")
	{
		mensaje += "Ciudad es requerido\n";
		isValid =false;	
	}
	
	if(document.getElementById('txtMensaje').value == "")
	{
		mensaje += "Mensaje es requerido\n";
		isValid =false;
	}
	
	if(!isValid)
	{
		alert(mensaje);
		return false;
	}
	else
	{
		return true;
	}
}

function ContactoEnviarMail()
{			
	if(ContactoValidarCampos())
	{
		document.getElementById('btnEnviarMailContacto').enabled = false;
	
		var parametros = "nombre=" + document.getElementById('txtNombre').value;
		parametros += "&telefono=" + document.getElementById('txtTelefono').value;
		parametros += "&email=" + document.getElementById('txtEmail').value;
		parametros += "&edad=" + document.getElementById('txtEdad').value;
		parametros += "&ciudad=" + document.getElementById('txtCiudad').value;
		parametros += "&mensaje=" + document.getElementById('txtMensaje').value;
		parametros += "&depMail=" + document.getElementById('ddlDepMail').value;
	
		getHTML('contacto', 'comp/contactoEnviarMail.php', parametros);
	}					
}


/*-----------------EFECTOS, FUNCIONES, VALIDACIONES DE ENVIAR A UN AMIGO------------------------*/

function openEnviarLinkAmigo()
{
	//Modal Window  
	var modal = new Control.Modal($('EnviarAmigo'),{  
    overlayOpacity: 0.75,  
    className: 'EnviarAmigoModal',  
    fade: true  
	});
}

function ScrollTop()
{
	new Effect.ScrollTo('divContenedorGlobal');
}

/*--------------- FUNCIONES DE CONTROLES EN EL SITIO---------------------------------------------*/
function writeMoviePlayer(sTitulo,sFlv)
{
	var sObjectFlash;
	var oDiv = document.getElementById("divMoviePlayer");
	sObjectFlash = '<object id="fla_MoviePlayer" type="application/x-shockwave-flash" data="swf/VideoPlayer.swf"  height="380" width="391" > \
					<param name="Movie" value="swf/VideoPlayer.swf"> \
					<param name="Src" value="swf/VideoPlayer.swf"> \
					<param name="Menu" value="0"> \
					<param name="FlashVars" value="vFLV=' + sFlv + '&vTitulo=' + sTitulo + '"> \
					<param name="WMode" value="Transparent"> \
					</object>'    
	oDiv.innerHTML = sObjectFlash;
}
