$(document).ready(function() {
	
	function getUrlVars()
	{
		var vars = [], hash;
		var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++)
		{
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	};
	
	//Página activa
	var pageName = $('meta[name=web-secction]').attr("content");
	var subPageName = $('meta[name=web-sub-secction]').attr("content");

	
	//Idioma página
	var pageLang = $('html[lang]').attr('lang');
	$.pageLang = $('html[lang]').attr('lang');
	
	//Duración de las animaciones
	var durationAni = 500;
	var aDurationAni = 300;
	
	var mainPos;
	var headerPos
	var menuPos;
	var subMenuArr = new Array();
	
	//Load Header
	$.get('header.html', function(data){
		//Insertamos en contenido al principio del main
		$('#main').prepend(data);
		
		mainPos = $('#main').position();
		headerPos = $('#header').position();
		menuPos = $('#menu').position();
		
		//Programación del menu
		//Hovers Menu
		$("#menu dt").hover(
			function() {
				var subMenu = $(this).attr('view');
				var pos = $(this).position();
				var leftPos = menuPos.left + pos.left;
				$('#'+subMenu).css('left', leftPos+"px");
				$('#'+subMenu).show(aDurationAni);
				subMenuArr[subMenu] = false;	
				$("a", this).animate({color: '#9b8965'}, aDurationAni);
			},
			function() {
				var subMenu = $(this).attr('view');
				setTimeout(function(){$.hideSubMenu(subMenu);},500)
				$("a", this).animate({color: '#808080'}, aDurationAni);
			}
		);	
		
		
		$(".sub-menu").hover(
			function() {subMenuArr[$(this).attr('id')] = true;},
			function() {subMenuArr[$(this).attr('id')] = false;	$.hideSubMenu($(this).attr('id'));}
		);
		
		$(".sub-menu dt").hover(
			function() {$("a", this).animate({color: '#9b8965'}, aDurationAni)},
			function() {$("a", this).animate({color: '#FFF'}, aDurationAni)}
		);
		
		$.showSubMenu = function(subMenu){
			$('#'+subMenu).show(aDurationAni);
		};
		
		$.hideSubMenu = function(subMenu){
			if(subMenuArr[subMenu] == false){$('#'+subMenu).hide(aDurationAni);}
		};
		
		$('#right-menu-icons img').hover(
				function(){
					$(this).stop();
					$('#'+$(this).attr('view')).stop();
					$(this).animate({left: "9px"}, aDurationAni);
						if ($.browser.version == "8.0"){
							$('#'+$(this).attr('view')).css({'height': "26px"});
							$('#'+$(this).attr('view') + " img").css({'position': 'relative', 'top': '4px'});
							$('#'+$(this).attr('view') + " img").stop();
							$('#'+$(this).attr('view') + " img").animate({opacity: 1}, aDurationAni);
						}
					$('#'+$(this).attr('view')).animate({opacity: 1}, aDurationAni);
					if ($.browser.version == "6.0" | $.browser.version == "7.0"){	
						$('#'+$(this).attr('view')).css('left', "-5px");
					}
				},
				function(){
					$(this).stop();
					$('#'+$(this).attr('view')).stop();
					$(this).animate({left: "0px"}, aDurationAni);
						if ($.browser.version == "8.0"){
							$('#'+$(this).attr('view') + " img").stop();
							$('#'+$(this).attr('view') + " img").animate({opacity: 0}, aDurationAni);
						}
					$('#'+$(this).attr('view')).animate({opacity: 0}, aDurationAni);
					if ($.browser.version == "6.0" | $.browser.version == "7.0"){
						$('#'+$(this).attr('view')).css('left', "300px");
					}
				}
		);
		
		/*if(pageName == "certificaciones"){
			$('#cert-ico').unbind('mouseover mouseleave click');
			$('#cert-ico').css('left','9px');
			$('#cert').css('opacity','1');
		}
		
		if(pageName == "singluten"){
			$('#sin-gluten-ico').unbind('mouseover mouseleave click');
			$('#sin-gluten-ico').css('left','9px');
			$('#sin-gluten').css('opacity','1');
		}
		
		if(pageName == "marcamdd"){
			$('#mmd-ico').unbind('mouseover mouseleave click');
			$('#mmd-ico').css('left','9px');
			$('#mmd').css('opacity','1');
		}*/
		
		switch(pageName)
		{
			case 'certificaciones':	
				$('#cert-ico').unbind('mouseover mouseleave click');
				$('#cert-ico').css('left','9px');
				$('#cert').css('opacity','1');	
			break;
			
			case 'singluten':	
				$('#sin-gluten-ico').unbind('mouseover mouseleave click');
				$('#sin-gluten-ico').css('left','9px');
				$('#sin-gluten').css('opacity','1');
			break;
			
			case 'marcamdd':	
				$('#mmd-ico').unbind('mouseover mouseleave click');
				$('#mmd-ico').css('left','9px');
				$('#mmd').css('opacity','1');
			break;
		}
		
	});
	//Load Header
	
	//Footer
	var colorErrorFot   = "#e93430";
	var colorRestoreFot = "#3C3C3C"
	$.nombreFot = "";
	$.emailFot	= "";
	$.asuntoFot = "";
	$.mensajeFot= "";
	

	$.textCamposFooterArr = new Array();
	
	//Load Footer
	$.get('footer.html', function(data){
		//Insertamos en contenido al final del main
		$('body').append(data);
		
		$("#footer .cell a").hover(
			function() {$(this).animate({color: '#A71732'}, aDurationAni);},
			function() {$(this).animate({color: '#FFF'}, aDurationAni);}
		);
		
		$("#langLink a").hover(
			function() {
				if($(this).attr('id') != pageLang){
					$(this).animate({color: '#A71732'}, aDurationAni);
				}
			},
			function() {
				if($(this).attr('id') != pageLang){
					$(this).animate({color: '#BFBFBF'}, aDurationAni);
				}
			}
		);
		
		$("#"+pageLang).css('color', '#A71732');
		
		//Campos Footer
		$.nombreFot = $('#nombreFot');
		$.emailFot	= $('#emailFot');
		$.asuntoFot = $('#asuntoFot');
		$.mensajeFot= $('#mensajeFot');
		
		//Mensajes de error del formulario
			//Por defecto en español
			$.fotNameMsg = "Introduzca su nombre";
			$.fotEmailMsg = "Introduzca su dirección de email";
			$.fotRmailMsgInvalid = "Introduzca una dirección de email válida";
			$.fotSubjectMsg = "Introduzca el asunto";
			$.fotQueryMsg = "Introduzca un Mensaje";
			$.fotSending = "Enviando consulta"
			$.fotOk = "Mensaje enviado con éxito<br />En breve nos pondremos en contacto con usted";
			$.fotError = "Hubo un error al enviar su mensaje<br />Por favor intentelo de nuevo o póngase en contacto con info@juanlunaslu.com";
			
			switch ($.pageLang) {		
				case "pt":
					$.fotNameMsg = "Digite seu nome";
					$.fotEmailMsg = "Digite seu e-mail";
					$.fotRmailMsgInvalid = "Por favor, um e-mail válido";
					$.fotSubjectMsg = "Por favor, informe o assunto";
					$.fotQueryMsg = "Por favor, escreva uma mensagem";
					
					$.fotSending = "Envio de mensagem"
					$.fotOk = "Mensagem enviada com sucesso<br />Logo entraremos em contato";
					$.fotError = "Houve um erro ao enviar a sua mensagem<br />Por favor, tente novamente ou contate info@juanlunaslu.com";
				break;
				
				case "en":
					$.fotNameMsg = "Please enter your name";
					$.fotEmailMsg = "Please enter your email";
					$.fotRmailMsgInvalid = "Please a valid email address";
					$.fotSubjectMsg = "Please enter the subject";
					$.fotQueryMsg = "Please enter a message";
					
					$.fotSending = "Sending message"
					$.fotOk = "Message sent successfully<br />Shortly we will contact you";
					$.fotError = "There was an error sending your message<br />Please try again or contact info@juanlunaslu.com";
				break;
					
				case "fr":
					$.fotNameMsg = "S'il vous plaît entrer votre nom";
					$.fotEmailMsg = "S'il vous plaît entrer votre email";
					$.fotRmailMsgInvalid = "S'il vous plaît une adresse email valide";
					$.fotSubjectMsg = "S'il vous plaît saisir le sujet";
					$.fotQueryMsg = "S'il vous plaît entrer un message";
					$.fotSending = "Envoi du message"
					$.fotOk = "Message envoyé avec succès<br />Peu de temps nous prendrons contact avec vous";
					$.fotError = "Il y avait une erreur d'envoyer votre message<br />S'il vous plaît essayez de nouveau ou contactez info@juanlunaslu.com";
				break;
				
				case "de":
					$.fotNameMsg = "Bitte geben Sie Ihren Namen";
					$.fotEmailMsg = "Bitte geben Sie Ihre E-Mail";
					$.fotRmailMsgInvalid = "Bitte eine gültige Email-Adresse";
					$.fotSubjectMsg = "Bitte geben Sie den Betreff";
					$.fotQueryMsg = "Bitte geben Sie eine Nachricht";
					$.fotSending = "Nachricht wird gesendet"
					$.fotOk = "Nachricht erfolgreich gesendet<br />In Kürze werden wir Ihnen in Verbindung setzen";
					$.fotError = "Es wurde ein Fehler beim Senden Ihrer Nachricht<br />Bitte versuchen Sie es erneut oder wenden info@juanlunaslu.com";
				break;
			}
		
		
		footerfocus($.nombreFot);
		footerfocus($.emailFot);
		footerfocus($.asuntoFot);
		footerfocus($.mensajeFot);
		$('#btnSendFooter').click(function(){ submitFormFooter();	});
	});
	//Load Footer
	
	//Contacto FOOTER
	function submitFormFooter(){
		
		if(validateFot() == true){
			$('#mensajeFot').hide('fade', 300, function(){
				$('#footerSendStatus').show('fade', 300);
			});		
			$('#send_status_img_footer').attr('src', '../img/loadingFooter.gif');
			$('#send_status_message_footer').html($.fotSending);
			
			jQuery.ajax({
				  type: "POST",
				  url: "../mail-footer.php",
				  dataType: "html",
				  data: "nombre=" + $.nombreFot.val()
				   + "&email=" + $.emailFot.val()
				   + "&asunto=" + $.asuntoFot.val()
				   + "&consulta=" + $.mensajeFot.val()
				   + "&idioma=" + $.pageLang,
				  success: function(response){				
				    $('#send_status_img_footer').attr('src', '../img/ok.png');
					$('#send_status_message_footer').html($.fotOk);
				  },
				  error: function(response){
				  	$('#send_status_img_footer').attr('src', '../img/error.png');
					$('#send_status_message_footer').html($.fotError);
				  }
			 });
			 				
		}
			return false;
		
	};
	
	function validateFot(){
		var errorStatus = true;
		
		if($.nombreFot.val() == ""){
			$.textCamposFooterArr[$.nombreFot.attr('id')] = "error";
			errorFooter($.nombreFot, $.fotNameMsg);
			errorStatus = false;				
		}
		
		if($.emailFot.val() == ""){
			$.textCamposFooterArr[$.emailFot.attr('id')] = "error";
			errorFooter($.emailFot, $.fotEmailMsg);
			errorStatus = false;				
		}
		else{
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
				if(reg.test($.emailFot.val()) == false) {
					$.textCamposFooterArr[$.emailFot.attr('id')] = $.emailFot.val();
					errorFooter($.emailFot, $.fotRmailMsgInvalid);
					errorStatus = false;
			    }
			}
		
		if($.asuntoFot.val() == ""){
			$.textCamposFooterArr[$.asuntoFot.attr('id')] = "error";
			errorFooter($.asuntoFot, $.fotSubjectMsg);
			errorStatus = false;				
		}
		
		
		if($.mensajeFot.val() == ""){
			$.textCamposFooterArr[$.mensajeFot.attr('id')] = "error";
			errorFooter($.mensajeFot, $.fotQueryMsg);
			errorStatus = false;				
		}
		
		
		return errorStatus;
	}
	
	function errorFooter(campo, errorMsg){
		
		campo.animate({
			'borderColor': colorErrorFot
		}, '300');
		
		$(campo).val(errorMsg);
		
			/*if($('#errorAdvertise').css('opacity') == "0"){
				$('#errorAdvertise').animate({
					'opacity': 1
				});
			}*/
	}
	
	function footerfocus(campo){
		campo.focus(function(){
			if($.textCamposFooterArr[campo.attr('id')] == "error"){
				campo.val("");
				$.textCamposFooterArr[campo.attr('id')] = "";
				campo.animate({
					'borderColor': colorRestoreFot
				}, '300');					
			}
			else if($.textCamposFooterArr[campo.attr('id')] != ""){						
				campo.val($.textCamposFooterArr[campo.attr('id')]);
				$.textCamposFooterArr[campo.attr('id')] = "";
				campo.animate({
					'borderColor': colorRestoreFot
				}, '300');
			}
				
				/*if($('#errorAdvertise').css('opacity') == "1"){
					$('#errorAdvertise').animate({
						'opacity': 0
					});
				}*/
		})
	}
	
		
});

$(window).load(function(){
	if ($.browser.version == "6.0" | $.browser.version == "7.0"){
		$(".img-background").css('left', '0px');
	}
	
	if ($.browser.version == "8.0"){
		$('.right-menu .text img').css('opacity', '0');
	}
	
});

/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)
http://rafael.adm.br/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

