var subPicado = new Array();
var winName = "ventana";
var win = null;

$( document ).ready
(
	function()
	{
		CmpNavegador();
	}
);

function AbreN( theURL, ancho, alto )
{
    var h = ( screen.width - ancho ) / 2;
    var v = ( screen.height - alto ) / 2;
    var windowprops = "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=" + ancho + ",height=" + alto + ",left=" + h + ",top=" + v;
    
    if( win )
    {
        win.close();
    }
    
    win = window.open( theURL, winName, windowprops );
}

function AbreComercial()
{
	$('#comercialImagen').stop().animate( { opacity: 1 }, 2000,
		function() {
		$( '#comercialContenido').stop().animate( { height: "500px" }, 'slow', 'swing');
		}
	);
}

function AbreHome()
{
	$( '#homePromoEtiqueta').stop().animate( { marginRight: "0px" }, 'slow', 'swing');
}

function CambiaMini(modo, cual)
{
	switch( modo )
	{
		case 'over':
			$( '#info' + cual ).stop().animate( { marginTop: '0px' }, 300 );
			$( '#tit' + cual ).stop().animate( { color: '#000' }, 300 );
		break;
		case 'out':
			$( '#info' + cual ).stop().animate( { marginTop: '-20px' }, 300 );
			$( '#tit' + cual ).stop().animate( { color: '#666' }, 300 );
		break;
	}
}

function CambiaCurso(modo, cual)
{
	switch( modo )
	{
		case 'over':
			$( '#tit' + cual ).stop().animate( { backgroundColor: '#4f4c40' }, 300 );
			$( '#fecha' + cual ).stop().animate( { marginTop: '0px' }, 300 );
		break;
		case 'out':
			$( '#tit' + cual ).stop().animate( { backgroundColor: '#aaa895'}, 300 );
			$( '#fecha' + cual ).stop().animate( { marginTop: '-25px' }, 300 );
		break;
	}
}

function CambiaFormaPago( tipo )
{
	switch( tipo )
	{
		case 'tarjeta':
			aviso = 'AVISO:<br />En la página de confirmación de la compra se abrirá la pasarela de pago del banco en una ventana nueva o pop-up.<br />Deberá tener en cuenta las siguientes consideraciones:<ul style="margin-top: 2px;margin-bottom: 0;" ><li>Su navegador debe permitir la apertura de ventanas emergentes o pop-ups.</li><li>No cierre en ningún caso dicha ventana, limítese a seguir las instrucciones de la página que contiene. Al finalizar la transacción, pulse el botón "cerrar".</li></ul>';
			
			$( '#resumenAviso' ).html( aviso );
			$( '#resumenAviso' ).show();
		break;
		case 'paypal':
			$( '#resumenAviso' ).html( "" );
			$( '#resumenAviso' ).hide();
			$( '#btConfirmar' ).css( "marginTop", "20px" );
		break;
	}
}

//PROMOCIONES...
var promoPicada = "";

function CambiaPromo(modo, cual)
{
	if(cual!= promoPicada)
	{

		switch( modo )
		{
			case 'over':
				$( '#info' + cual ).stop().animate( { marginTop: '-60px' }, 300 );
			break;
			case 'out':
				$( '#info' + cual ).stop().animate( { marginTop: '-40px' }, 300 );
			break;
		}
	}
}

function CmpNavegador()
{
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera",
				versionSearch: "Version"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]

	};
	BrowserDetect.init();
	
	if( BrowserDetect.browser == "Explorer" && BrowserDetect.version < 8 )
	{
		$( '#avisoIE' ).html( "<img src=\"img/comunes/icoError.png\" > Est&aacute; utilizando una versi&oacute;n obsoleta de Internet Explorer, de manera que algunos elementos de esta p&aacute;gina no se ver&aacute;n correctamente. Actual&iacute;cese gratu&iacute;tamente desde <a href=\"http://www.microsoft.com/spain/windows/internet-explorer/\" target=\"_blank\" style=\"color: #333\" >aqu&iacute;</a>." );
		$( '#avisoIE' ).addClass( 'avisoJS' );
	}
	
	if( BrowserDetect.browser == "Firefox" && BrowserDetect.version < 4 )
	{
		$( '#avisoIE' ).html( "<img src=\"img/comunes/icoError.png\" > Est&aacute; utilizando una versi&oacute;n obsoleta de Firefox, de manera que algunos elementos de esta p&aacute;gina no se ver&aacute;n correctamente. Actual&iacute;cese gratu&iacute;tamente desde <a href=\"http://www.mozilla-europe.org/\" target=\"_blank\" style=\"color: #333\" >aqu&iacute;</a>." );
		$( '#avisoIE' ).addClass( 'avisoJS' );
	}
}

function SacaPromo(cual)
{
	if(promoPicada != "")
	{
		$( '#info' + promoPicada ).css( 'marginTop', '-40px' );
		$( '#info' + promoPicada ).stop().animate( { marginTop: '-40px', backgroundColor: '#c7c5bb' }, 300 );
		$( '#promoFoto' + promoPicada ).css( 'display', 'block' );
		$( '#cont' + promoPicada ).css( 'display', 'none' );
	}
	
	promoPicada = cual;
	$( '#info' + promoPicada ).stop().animate( {backgroundColor: '#fff' }, 300 );
	$( '#info' + promoPicada ).css( 'marginTop', '0px' );
	$( '#promoFoto' + promoPicada ).css( 'display', 'none' );
	$( '#cont' + promoPicada ).css( 'display', 'block' );
}

//DESTACADOS HOME
var destacadoActual;
function CambiaIcodestacadoes(idElemento, modo)
{
	if(destacadoActual != idElemento)
	{
		switch( modo )
		{
			case 'over':
				$( '#ico' + idElemento ).stop().animate( {marginTop: '-58px' }, 200);
			break;
			case 'out':
				$( '#ico' + idElemento ).stop().animate( {marginTop: '0px' }, 200 );
			break;
		}
	}
}

function DestacadosAnimacion( cual, estado )
{
	if( destacadoActual != cual )
	{
		switch( estado )
		{
			case 'anima':
				if( destacadoActual != null ) //ANIMACIONES PARA DESPICAR EL PICADO...
				{
					$( "#destacado" + destacadoActual ).stop().delay( 0 ).animate( { left: "-600px" }, 'slow', 'easeInExpo' );
				}
				
				destacadoActual = cual;
				
				$( "#destacado" + destacadoActual ).css( 'left', 480 );
				$( "#destacado" + destacadoActual ).stop().animate( { left: "0px" }, 'slow', 'linear' );
				

				$( "#destacadoTitulo" + destacadoActual ).css( 'bottom', -200 );
				$( "#destacadoTitulo" + destacadoActual ).stop().delay( 100 ).animate( { bottom: "0px" }, 'slow', 'linear' );

			break;
			case 'click':
				window.clearInterval( destacadosBucle );
				DestacadosAnimacion( cual, 'anima' );
				destacadosBucle = window.setInterval( "DestacadosBucle();", 15000 ); //LO VOLVEMOS A ACTIVAR DESPUES DE UN BUEN RATO
			break;
			case 'inicia':
				destacadoActual = cual;
				destacadosNum = $( '#homeAnimacion' ).children().length -1;
				
				$( "#destacado" + destacadoActual ).css( 'left', 0 );

				$( "#destacadoTitulo" + destacadoActual ).css( 'bottom', -200 );
				$( "#destacadoTitulo" + destacadoActual ).stop().delay( 100 ).animate( { bottom: "0px" }, 'slow', 'linear' );
				
				if( destacadosNum > 1 ) destacadosBucle = window.setInterval( "DestacadosBucle();", 8000 );
			break;
		}
	}
	else
	{
		switch( estado )
		{		
			case 'over':
				window.clearInterval( destacadosBucle );
				$( '#destacado' + cual ).children( 'a' ).children( 'img' ).stop().animate( { opacity: 0.4}, 'slow');
			break;
			case 'out':
				$( '#destacado' + cual ).children( 'a' ).children( 'img' ).stop().animate( { opacity: 1}, 'slow',	
				function() {
						destacadosBucle = window.setInterval( "DestacadosBucle();", 8000 );
					}
				  ); 
			break;
		}
	}
}

function DestacadosBucle()
{
	var tmp = parseInt( destacadoActual ) + 1;
	
	if( tmp >= destacadosNum ) tmp = 0;
	
	DestacadosAnimacion( tmp, 'anima' );
}


function CambiaGrupoList(modo, cual)
{
	switch( modo )
	{
		case 'over':
			$( '#grupo' + cual ).stop().animate( { color: '#91885f' }, 300 );
		break;
		case 'out':
			$( '#grupo' + cual ).stop().animate( { color: '#999' }, 300 );
		break;
	}
}

function CambiaImg( idElemento, imagen )
{
    document.getElementById( idElemento ).src = imagen;
}

function DeshabilitaEnlace( cual )
{
	cual.preventDefault(); // -> cancela el evento
	return false;
}

//FORMULARIOS...
function FormuRecomendar( error1, error2)
{
	var campo1 = document.getElementById( 'tuNombre' ).value;
	var campo2 = document.getElementById( 'tuEmail' ).value;
	var campo3 = document.getElementById( 'amigoNombre' ).value;
	var campo4 = document.getElementById( 'amigoEmail' ).value;


	
	if( campo1 == "" || campo2 == "" || campo3 == "" || campo4 == "")
	{
		alert( error1 );
		return false;
	}
	else if( campo2.length <  8 || campo2.indexOf('@',0)==-1 || campo2.indexOf('@',0)==0 || campo2.indexOf('.',0)==-1 || campo2.indexOf('.',0)==0 )
	{
		alert( error2 );
		return false;
	}
	else if( campo4.length <  8 || campo4.indexOf('@',0)==-1 || campo4.indexOf('@',0)==0 || campo4.indexOf('.',0)==-1 || campo4.indexOf('.',0)==0 )
	{
		alert( error2 );
		return false;
	}
	else
	{
		return true;
	}
}

function FormuPedido( error1, error2)
{
	var campo1 = document.getElementById( 'nombre' ).value;
	var campo2 = document.getElementById( 'apellidos' ).value;
	var campo3 = document.getElementById( 'direccion' ).value;
	var campo4 = document.getElementById( 'cp' ).value;
	var campo5 = document.getElementById( 'poblacion' ).value;
	var campo6 = document.getElementById( 'provincia' ).value;
	var campo7 = document.getElementById( 'cp' ).value;
	var campo8 = document.getElementById( 'poblacion' ).value;
	var campo9 = document.getElementById( 'provincia' ).value;
	var campo10 = document.getElementById( 'pais' ).value;
	var campo11 = document.getElementById( 'email' ).value;


	
	if( campo1 == "" || campo2 == "" || campo3 == "" || campo4 == "" || campo5 == "" || campo6 == "" || campo7 == "" || campo8 == "" || campo9 == "" || campo10 == "" || campo11 == "" )
	{
		alert( error1 );
		return false;
	}

	else if( campo11.length <  8 || campo11.indexOf('@',0)==-1 || campo11.indexOf('@',0)==0 || campo11.indexOf('.',0)==-1 || campo11.indexOf('.',0)==0 )
	{
		alert( error2 );
		return false;
	}
	else
	{
		return true;
	}
}

function Actualiza()
{
    w = document.getElementById ('flash');
    ancho = alto = 0;
    if (typeof (window.innerWidth) == 'number') {
        ancho = window.innerWidth;
        alto = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        ancho = document.documentElement.clientWidth;
        alto = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        ancho = document.body.clientWidth;
        alto = document.body.clientHeight;
    }
    w.style.width = ancho < minAncho ? minAncho + "px" : "100%";
    w.style.height = alto < minAlto ? minAlto + "px" : "100%";
}

//FONDOS DE INDEX Y WEB...
function PonPatron(ruta)
{
	$('#contenedor').css(
	{
	"background-image": "url("+ ruta +")",
	"background-repeat": "no-repeat",
	"background-position": "center top",
	"background-size": "100%"
	});  
 
}
function PonFlash(ruta)
{
	var alto = 0;
	var ancho = 0;

	if (window.innerWidth != undefined)
	{
		ancho = window.innerWidth;
		alto = window.innerHeight;
	}
	else if (document.documentElement != undefined &&  document.documentElement.clientWidth != undefined && document.documentElement.clientWidth != 0)
	{
		
		ancho = document.documentElement.clientWidth;
		alto = document.documentElement.clientHeight;
	}
	else   
	{
		ancho = document.getElementById('body').clientWidth;
		alto = document.getElementById('body').clientHeight;
	}
	
	var flashvars = {},
	params = {wmode:"transparent"},
	attributes = {};
	swfobject.embedSWF(ruta, "flash", ancho, alto, "9.0.0", ruta, flashvars, params, attributes);	
}
function PonFondo()
{
	var alto = 0;
	var ancho = 0;

	if (window.innerWidth != undefined)
	{
		ancho = window.innerWidth;
		alto = window.innerHeight;
	}
	else if (document.documentElement != undefined &&  document.documentElement.clientWidth != undefined && document.documentElement.clientWidth != 0)
	{
		
		ancho = document.documentElement.clientWidth;
		alto = document.documentElement.clientHeight;
	}
	else   
	{
		ancho = document.getElementById('body').clientWidth;
		alto = document.getElementById('body').clientHeight;
	}	
	
	
	if( ancho > alto)
	{
		document.getElementById( 'imgFondo' ).width = ancho;
	}
	else
	{
		document.getElementById( 'imgFondo' ).height = alto;
	}
	
	$('#fondoWeb').stop().animate( { opacity: 1 }, 'slow' );
}

//MENU...
function PonSubMnu( cual )
{
	var nivel = cual.substring( 10,11 );
	
	
	if( subPicado[ nivel ] != cual )
	{
		$( '#subMnu' + subPicado[ nivel ] ).stop().slideUp( 400 );
		if( subPicado.length > 0 )
		{
			var mismaRama = false;
			
			for( x=subPicado.length - 1;x>=0; x-- )
			{
				if( subPicado[ nivel - 1 ] == subPicado[ x ] ) mismaRama = true;
				
				if( !mismaRama )
				{
					$( '#bt' + subPicado[ x ] ).removeClass( 'mnuProdOpcResaltado' );
					$( '#subMnu' + subPicado[ x ] ).removeClass( 'mnuProdOpcResaltado' );
				}
				
				if( x >= nivel )
				{
					$( '#subMnu' + subPicado[ x ] ).stop().slideUp( 400 );
					subPicado.pop();
				}
			}
		}
		
		subPicado[ nivel ] = cual;
		
		$( '#bt' + subPicado[ nivel ] ).addClass( 'mnuProdOpcResaltado' );
		$( '#subMnu' + subPicado[ nivel ] ).addClass( 'mnuProdOpcResaltado' );
		$( '#subMnu' + subPicado[ nivel ] ).stop().slideDown( 400 );
	}
}

function Volver( modo )
{
	switch( modo )
	{
		case 'over':
			$( '#btVolver' ).stop().animate( { marginLeft: 0 }, 300 );
		break;
		case 'out':
			$( '#btVolver' ).stop().animate( { marginLeft: 10 }, 300 );
		break;
	}
}
