﻿var idioma = null;
var picado = null;
var arrFavoritos = null;




var navegador = '';
var navegadorVersion = '';

var winName = "ventana";
var win = null;

$( document ).ready
(
	function()
	{
		NavegadorCmp();
		
		if( navegador == "Explorer" && navegadorVersion < 8 )
		{
			$( '#avisoIE' ).html( "<img src=\"img/comunes/error.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><br/>Si su sistema operativo es más antiguo que Explorer Vista no podrá actualizar a Internet Explorer 9, en este caso le recomendamos utilizar otro navegador:<br><a href=\"http://www.mozilla.org/es-ES/firefox/new/\" target=\"_blank\" style=\"color: #333\" >Firefox</a><br><a href=\"http://www.google.com/chrome/eula.html?hl=es\" target=\"_blank\" style=\"color: #333\" >Google Chrome</a><br><a href=\"http://www.opera.com/download/\" target=\"_blank\" style=\"color: #333\" >Opera</a>." );
			$( '#avisoIE' ).addClass( 'avisoTop' );
		}
		
		if( navegador == "Firefox" && navegadorVersion < 4 )
		{
			$( '#avisoIE' ).html( "<img src=\"img/comunes/error.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( 'avisoTop' );
		}
		
		if(localStorage)
		{
			if(sessionStorage.posMenu == null)
			{
				sessionStorage.posMenu = "abierto"; 
			}
		}
		else
		{
			alert("No tienes LocalStorage");
		}
		AnimaIni();
	}
);

/*$( document ).ready
(
	function()
	{
		if( navigator.appName == "Microsoft Internet Explorer" && parseFloat( navigator.appVersion.split( "MSIE" )[ 1 ] ) < 8 )
		{
			$( '#avisoIE' ).html( "<img src=\"img/error.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(localStorage)
		{
			if(sessionStorage.posMenu == null)
			{
				sessionStorage.posMenu = "abierto"; 
			}
		}
		else
		{
			alert("No tienes LocalStorage");
		}
		AnimaIni();
	}
);*/

function CambiaMenu (idioma)
{
	if(sessionStorage.posMenu == "cerrado")
	{
		$('#elboton').css({opacity: 0});
		document.getElementById('elboton').style.textAlign  = 'left';
		document.getElementById('elboton').innerHTML = "<img src='img/imgCerrar_" + idioma + ".jpg' />"
		$( '#banda' ).animate( { width: [ 940, 'swing' ] }, 1000, 'slow' , 
			function(){
			$('#losMenus').fadeTo('slow', 1); 
			$( '#elboton' ).animate( { opacity: [ 100, 'swing' ] }, 2000, 'slow' );
			}
			
		);
		sessionStorage.posMenu = "abierto"; 
	}
	else
	{
		$('#elboton').css({opacity: 0});
		document.getElementById('elboton').style.textAlign = 'right';
		document.getElementById('elboton').innerHTML = "<img src='img/imgAbrir_" + idioma + ".jpg' />"
		$('#losMenus').fadeTo('slow', 0, function() {
				$( '#banda' ).animate( { width: [ 230, 'swing' ] }, 1000, 'slow' );
				$( '#elboton' ).animate( { opacity: [ 100, 'swing' ] }, 5000, 'slow' );
		});
		sessionStorage.posMenu = "cerrado"; 
		
	}
	
}

function EvaluaMenu(idioma)
{
	if(sessionStorage.posMenu == "abierto")
	{
		document.getElementById('elboton').style.textAlign = 'left';
		document.getElementById('elboton').innerHTML = "<img src='img/imgCerrar_" + idioma + ".jpg' />"
	}
	else
	{
		document.getElementById('elboton').style.textAlign = 'right';
		document.getElementById('elboton').innerHTML = "<img src='img/imgAbrir_" + idioma + ".jpg' />"
	}
}

function AbreListadoColeccion( cuantos )
{
	for(x=0; x<cuantos; x++)
	{
		$('#mod' + x).fadeOut(0).delay(100*x).fadeIn(400);
		$('#tit' + x).css({opacity: 0});
	}
}

function AnimaIni()
{
	if( sessionStorage.posMenu == "abierto" )
	{
		$( '#banda' ).animate( { width: [ 940, 'swing' ] }, 1000, 'slow' );
		$('#losMenus').fadeTo('slow', 1);
	}
	else
	{
		$( '#banda' ).animate( { width: [ 230, 'swing' ] }, 1000, 'slow' );
		$('#losMenus').fadeTo('fast', 0);
	}
	
	$( '#menu' ).animate( { left: [ 0, 'swing' ] }, 1000, 'easeOutBack' );
	if( picado != null && picado != 'home' )
	{
		$( 'body' ).animate( { backgroundColor: '#ffffff' }, 5000 );
	}
}

function AnimaMini( tipo, cual )
{
	if( tipo == "over" )
	{
		
		$('#mod' + cual).stop().animate({marginTop: '-30px'}, 'slow', function(){$('#mod' + cual).fadeTo('slow', 0.3); });
		$('#tit' + cual).stop().delay(400).animate({opacity: 1}, 'slow');
	}
	
	if( tipo == "out" )
	{
		
		$('#tit' + cual).stop().animate({opacity: 0}, 1000);
		$('#mod' + cual).stop().animate({opacity: 1, marginTop: '0' }, 1000);
	}
}

//FUNCIONES PARA FAVORITOS...
var totalFavoritos = 0;
var historia;
function Favoritos( coleccion, modelo)
{
	var vestido = {"col":coleccion, "mod":modelo};

	 if(!localStorage.arrFavoritos) //first time	    
	 {
	        localStorage.arrFavoritos = "[" + JSON.stringify( vestido ) + "]";
			historia = JSON.parse(localStorage.arrFavoritos);
			totalFavoritos = historia.length;
			alert(modelo + " añadido a favoritos");
	 }
     else  //there was some data before
	 {
        historia = JSON.parse(localStorage.arrFavoritos);
		totalFavoritos = historia.length;
		var modExiste = false;
		
		for(x=0; x<totalFavoritos; x++)
		{
			if(JSON.stringify(historia[x].mod) == ('"'+modelo+'"'))
			{
				modExiste = true;
				alert("Este modelo ya está en tus favoritos");
			}
			
			if(x==totalFavoritos-1 && modExiste == false)
			{
				historia.push(vestido);	        
				localStorage.arrFavoritos = JSON.stringify(historia);
				alert(modelo + " añadido a favoritos");
			}
		}
	}
}

function QuitaFavorito(coleccion, modelo)
{
	var vestido = {"col":coleccion, "mod":modelo};
	
	historia = JSON.parse(localStorage.arrFavoritos);
	totalFavoritos = historia.length;
	var modExiste = false;
	
	for(x=0; x<totalFavoritos; x++)
	{
		if(JSON.stringify(historia[x].mod) == ('"'+modelo+'"'))
		{
			modExiste = true;
			historia.splice(x, 1);	
			if (historia.length>0) 
			{
				localStorage.arrFavoritos = JSON.stringify(historia);	
			}
			else
			{
				 localStorage.removeItem("arrFavoritos");
			}
			alert(modelo + " eliminado de tus favoritos");
			location.replace("favoritos.php");
		}
		
		if(x==totalFavoritos-1 && modExiste == false)
		{
			alert("El modelo no existe en favoritos");
		}
	}
}

function SacaFavoritos(idioma)
{
	if(localStorage.arrFavoritos)
	{
		var modelos = localStorage.arrFavoritos.toString();
		var ajax = ajaxFunction();
		ajax.open( "POST", "favoritosList.php?arrFavoritos=" + modelos + "&idioma=" + idioma, true );
	}
	else
	{
		var ajax = ajaxFunction();
		ajax.open( "POST", "favoritosList.php?idioma=" + idioma, true );
	}
	

	ajax.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded");
	ajax.onreadystatechange = function()
	{

	if (ajax.readyState == 4)
	{
	document.getElementById("contenido").innerHTML = ajax.responseText;

	}}
	ajax.send(null);
}

//FUNCIONES DE PAISES DISTRIBUIDORES / TIENDAS//

var paisPicado;

function SacaPaises(cuantos)
{
	for(x=0; x<cuantos; x++)
	{
		$('#pais' + x).delay(100*x).animate({marginLeft: '5px'}, 'slow');
	}
	paisPicado = null;
}

function BtPais( tipo, cual)
{
	if(paisPicado != cual)
	{
		if( tipo == "over" )
		{
			$('#pais' + cual).stop(true, true).animate({'font-size': '20pt'}, 'fast');
		}
		
		if( tipo == "out" )
		{
			
			$('#pais' + cual).stop(true, true).animate({'font-size': '13pt'});
		}
	}
}

function PicaPais( cual , zona, tipo, idioma )
{
	if( paisPicado != null)
	{
		divElemAnt = document.getElementById("pais"+paisPicado);
		divElemAnt.className= 'tiendasPaisesBt';
		$('#pais' + paisPicado).stop().animate({'font-size': '13pt'});
	}
	
	paisPicado = cual;
	divElemPic = document.getElementById("pais"+paisPicado);
	divElemPic.className= 'tiendasPaisesBtPicado';
	
	if(tipo == "tiendas")
	{
		var pagina = 'puntosVenta.php?zona=' + zona + '&idioma=' + idioma;
		
	} else if(tipo == "agentes")
	{
		var pagina = 'agentes.php?zona=' + zona + '&idioma=' + idioma;
	}
	CargarContenido(pagina, 'contenidoInfo')
}

function ajaxFunction() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		return xmlHttp;
	} catch (e) {
		// Internet Explorer
	try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		return xmlHttp;
	} catch (e) {
	try {
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		return xmlHttp;
	} catch (e) {
		alert("Tu navegador no soporta AJAX!");
		return false;
	}}}
}

function CargarContenido(_pagina,capa) {
	var ajax = ajaxFunction();

	ajax.open("POST", _pagina, true);

	ajax.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded");
	ajax.onreadystatechange = function()
	{

	if (ajax.readyState == 4)
	{
	document.getElementById(capa).innerHTML =
	ajax.responseText;

	}}
	ajax.send(null);
}




function scrolea()
{
	var divHeight;
	var obj = document.getElementById('foto');
	if(obj.offsetHeight)          {divHeight=obj.offsetHeight;}
	else if(obj.style.pixelHeight){divHeight=obj.style.pixelHeight;}
	
	var pos = divHeight/4;
	
	$('html,body').stop().scrollTo(0, 10);
	//$('html,body').stop().animate({scrollTop:'200px'}, 'slow');
	
	$('html,body').delay(800).animate({
    scrollTop: [pos]}, 'slow', 'easeOutExpo', function() {
  });
}


function NavegadorCmp()
{
	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();
	
	navegador = BrowserDetect.browser;
	navegadorVersion = BrowserDetect.version;
}

//-------------


