// remap jQuery to $
(function($){})(window.jQuery);


$(document).ready(function(){$("input:text, textarea, input:password").each(function(){if(this.value=='')this.value=this.title;});$("input:text, textarea, input:password").focus(function(){if(this.value==this.title)this.value='';});$("input:text, textarea, input:password").blur(function(){if(this.value=='')this.value=this.title;});$("input:image, input:button, input:submit").click(function(){$(this.form.elements).each(function(){if(this.type=='text'||this.type=='textarea'||this.type=='password'){if(this.value==this.title&&this.title!=''){this.value='';}}});});});

/* trigger when page is ready */
$(document).ready(function (){
	
	//Empresa
	$('#more-empresa-trigger').toggle(function(){
		$(this).addClass('open');
		$('#menu.empresa').css('background-position', '50% -274px');
		$('#empresa-txt  p.to-hide').removeClass('hidden');
		$('#menu.empresa').animate({
			height: '550px'
		}, 'normal');
	}, function() {
		$(this).removeClass('open');
		$('#empresa-txt  p.to-hide').addClass('hidden');
		if($.browser.msie)
		{
			$('#menu.empresa').animate({
				height: '114px',
				'background-position-y': 0
			}, 'normal');
		}
		else 
		{
			$('#menu.empresa').animate({
				height: '114px',
				backgroundPosition: '50% 0'
			}, 'normal');
		}
	});
	
	$('#menu-flotante ul li').hover(function(){
		$(this).find('span').show();
	}, function(){
		if( ! $(this).find('a').hasClass('selected') )
		{
			$(this).find('span').hide();
		}
	});
	
	$('#menu-flotante > ul li a').live('click', function(e){
		e.preventDefault();
		var $this = $(this);
		$('#menu-flotante > ul li a').next().hide();
		$('#menu-flotante > ul li a').removeClass('selected');
		$this.addClass('selected');
		$this.next().show();
		
		return false;
	});
	
	
	/********************/
	
	
	// contact form
	$('#submit-form-contact').click(function(e){
		
		e.preventDefault();
		
		var send = true;
		var nombrec = $('#nombrec').val();
		var emailc = $('#emailc').val();
		var asuntoc = $('#asuntoc').val();
		var mensajec = $('#mensajec').val();
		
		var data = "nombre="+nombrec+"&email="+emailc+"&asunto="+asuntoc+"&mensaje="+mensajec;
		
		//validation
		if(nombrec == "" || emailc == "" || mensajec == "" || asuntoc == 0 )
		{
			send = false;
			alert('Verifique que los campos no se encuentren vacíos.');
		}else if( ! /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/.test(emailc))
		{
			alert('Ingrese el Email correctamente.');
			send = false;
		}
		
		if(send)
		{
			$.ajax({
				url : '_/includes/send_mail.php',
				data:  data,
				dataType: 'text',
				type : 'post',
				success: function(response){
					$('#contacto-1').fadeOut('normal', function(){
						$('#mensajec').val("Mensaje");
						$('#nombrec').val("Nombre");
						$('#emailc').val("Email");
						$('#asuntoc option[value=0]').attr('selected', true);
						$('#asuntoc').next().find('.select-title-trigger').text('Asunto');
						$('#contacto-2').fadeIn('normal');
					});
					
				},
				error: function (){
					alert('El servidor no responde... intenta de nuevo.');
				}
			});
		}
		else
		{
			if(mensajec == "")
			{
				$('#mensajec').val("Mensaje");
			}
			
			if(nombrec == "")
			{
				$('#nombrec').val("Nombre");
			}
			
			if(emailc == "")
			{
				$('#emailc').val("Email");
			}
			
			if(asuntoc == "")
			{
				$('#asuntoc').val("Asunto");
			}
		}
		
	});
	
	$('#enviar-mensaje').live('click', function(e){
		e.preventDefault();
		$('#contacto-2').fadeOut('normal', function(){
			$('#contacto-1').fadeIn('normal');
		});
		
	});
	
	$('#enviar-mensaje-franquicias').live('click', function(e){
		e.preventDefault();
		$('#fran-gracias').fadeOut('normal', function(){
			$('#fran2 > form').fadeIn('normal');
		});
		
	});
	
	
	// Newsletter form
	$('#submit_newsletter').click(function(e){
		
		e.preventDefault();
		
		var send = true;
		var newsletter = $('#newsletter').val();
		var data = "email="+newsletter;
		
		if( ! /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/.test(newsletter))
		{
			alert('Ingrese el Email correctamente.');
			send = false;
		}
		
		
		
		if(send)
		{
			$.ajax({
				url : '_/includes/send_newsletter.php',
				data:  data,
				dataType: 'text',
				type : 'post',
				success: function(response){
					alert('Registrado correctamente.');
					if(parseInt(response) == 1)
					{
						$('#newsletter').val("Escribe tu Correo Electrónico");
					}
				},
				error: function (){
					alert('El servidor no responde... intenta de nuevo.');
				}
			});
			
		}
		else
		{
			if(newsletter == "")
			{
				$('#newsletter').val("Escribe tu Correo Electrónico");
			}
		}
	});
	

	// Newsletter form
	$('#form-franquicia').click(function(e){
		
		e.preventDefault();
		
		var send = true;
		var nombre = $('#nombre').val();
		var email = $('#email').val();
		var telefono = $('#telefono').val();
		var celular = $('#celular').val();
		var ciudad = $('#ciudad').val();
		var estado = $('#estado').val();
		var mensaje = $('#mensaje').val();
		var data = "nombre="+nombre+"&email="+email+"&telefono="+telefono+"&celular="+celular+"&ciudad="+ciudad+"&estado="+estado+"&mensaje="+mensaje;
		
		//validation
		if(nombre == "" || email == "" || mensaje == "" || telefono == "" || estado == "" || celular  == "" || ciudad == "")
		{
			send = false;
			alert('Verifique que los campos no se encuentren vacíos.');
		}else if( ! /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/.test(email))
		{
			alert('Ingrese el Email correctamente.');
			send = false;
		}
		
		
		
		if(send)
		{
			$.ajax({
				url : '_/includes/send_mail-franquicia.php',
				data:  data,
				dataType: 'text',
				type : 'post',
				success: function(response){
					//alert('Solicitud enviada correctamente.');
					if(parseInt(response) == 1)
					{
						$('#fran2 > form').fadeOut('normal', function(){
							$('#nombre').val("Nombre");
							$('#email').val("Email");
							$('#telefono').val("Teléfono Fijo(lada)");
							$('#celular').val("Celular");
							$('#ciudad').val("Ciudad");
							$('#estado').val("Estado");
							$('#mensaje').val("Mensaje");
							$('#fran-gracias').fadeIn('normal');
						});
					}
				},
				error: function (){
					alert('El servidor no responde... intenta de nuevo.');
				}
			});
			
		}
		else
		{
			if(email == "")
			{
				$('#email').val("Email");
			}
			
			if(nombre == "")
			{
				$('#nombre').val("Nombre");
			}
			
			if(telefono == "")
			{
				$('#telefono').val("Teléfono Fijo(lada)");
			}
			
			if(celular == "")
			{
				$('#celular').val("Celular");
			}
			if(ciudad == "")
			{
				$('#ciudad').val("Ciudad");
			}
			if(estado == "")
			{
				$('#estado').val("Estado");
			}
			if(mensaje == "")
			{
				$('#mensaje').val("Mensaje");
			}
		}
	});
	
	if(document.getElementById('slides-wok'))
	{
		$('#slides-wok') 
		.before('<div id="nav-wok">') 
		.cycle({ 
			fx:     'fade', 
			speed:  'normal', 
			timeout:  0, 
			sync: 0,
			pager:  '#nav-wok' 
		});
	}
	
	if(document.getElementById('slides-salad'))
	{
		$('#slides-salad') 
		.before('<div id="nav-salad">') 
		.cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout:  0, 
			sync: 0,
			pager:  '#nav-salad' 
		});
	}
	
	if(document.getElementById('slides-pasta'))
	{
		$('#slides-pasta') 
		.before('<div id="nav-pasta">') 
		.cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout:  0,
			sync: 0,
			pager:  '#nav-pasta' 
		});
	}
	
	if(document.getElementById('menu-slider'))
	{
		$('#menu-slider').cycle({ 
				fx:     'fade', 
				speed:  'fast', 
				timeout: 0, 
				next:   '#next-menu-slide', 
				prev:   '#prev-menu-slide' 
			});
	}
	
	//Show the Gallery
	$('#info-footer > a').click(function(e){
		
		e.preventDefault();
		
		var $this		= $(this);
		
		var width = parseInt($(window).width());
		
		var lightbox_w = 732;//lightbox width
		var lightbox_h = $(document).height() + 'px';
		
		var lightbox_y = (parseInt(width - lightbox_w) / 2) + 'px';
		
		$('#lightbox-overlay').css({
			height: lightbox_h,
			opacity: '0.8'
		}).show();
		
		$('#lightbox').css({
			left: lightbox_y
		}).fadeIn('normal');
		
		
		if($.browser.webkit)
		{
			$('#lightbox').css('top', '80px');
		}
		
		//Load the map
		if(document.getElementById('map-ajax-container'))
		{
			//carga el mapa
			$.ajax({
					url : '_/data/locations.php',
					type : 'post',
					dataType : 'text',
					data : 'id=1',
					success : function(data){
						if(data != "")
						{
							$('#map-ajax-container').html(data);
							$(document).scrollTop(0);
						}
					},
					error: function(){
						
					},
					complete : function() {
						if($('#image-location').attr('src') == "")
						{
							$('#btn-camera').hide();
						}
						else {
							$('#btn-camera').show();
						}
					}
			});
		}
		
		
	
		
	});
	
	
	//Show the Gallery
	$('#gal2 ul li.enabled a').click(function(e){
		
		e.preventDefault();
		
		var $this		= $(this);
		var target= $this.attr('href');
		
		var width = parseInt($(window).width());
		var height = parseInt($(document).height());
		
		var lightbox_w = 732;//lightbox width
		
		var lightbox_y = (parseInt(width - lightbox_w) / 2) + 'px';
		
		$('#lightbox-overlay').css({
			height: height+'px',
			opacity: '0.8'
		}).show();
		
		$('#gal2 ul li.enabled a').removeClass('selected');
		
		$this.addClass('selected');
		
		$('#lightbox-gallery').css({
			left: lightbox_y
		}).show();
		
		$.ajax({
			type: 'get',
			url: target,
			dataType: 'text',
			success: function(data) {
				$('#gallery-ajax').html(data);
				$(document).scrollTop(0);
			},
			error: function(){
				
			}
			
		});
		
		if($.browser.webkit)
		{
			$('#lightbox-gallery').css('top', '80px');
		}
		
	});
	
	///////////////////////////////////////////////////////////////////////////////video 
	$('#ver-video').click(function(e){
		
		e.preventDefault();
		
		var $this		= $(this);
		var target= $this.attr('href');
		
		var width = parseInt($(window).width());
		var height = parseInt($(document).height());
		
		var lightbox_w = 732;//lightbox width
		
		var lightbox_y = (parseInt(width - lightbox_w) / 2) + 'px';
		
		$('#lightbox-overlay').css({
			height: height+'px',
			opacity: '0.8'
		}).show();
		
		$('#gal2 ul li.enabled a').removeClass('selected');
		
		$this.addClass('selected');
		
		$('#lightbox-video').css({
			left: lightbox_y
		}).show();
		
		$.ajax({
			type: 'get',
			url: target,
			dataType: 'text',
			success: function(data) {
				$('#gallery-ajax-video').html(data);
				$(document).scrollTop(0);
			},
			error: function(){
				
			}
			
		});
		
		if($.browser.webkit)
		{
			$('#lightbox-gallery').css('top', '80px');
		}
		
	});
	
	
	//////////////////////////////////////////////////////////////////////////////////////////////////
	
	
	//Show the Menu Gallery
	$('#menu-slider > div > ul > li > a').click(function(e){
		
		e.preventDefault();
		
		$('#gallery-ajax').html("");
		
		var $this		= $(this);
		var target= $this.attr('href');
		
		var width = parseInt($(window).width());
		var height = parseInt($(document).height());
		
		var lightbox_w = 732;//lightbox width
		
		var lightbox_y = (parseInt(width - lightbox_w) / 2) + 'px';
		
		$('#lightbox-overlay').css({
			height: height+'px',
			opacity: '0.8'
		}).show();
		
		
		
		
		
		$('#lightbox-gallery-menu').css({
			left: lightbox_y,
			top : '1390px'
		}).show();
		
		$.ajax({
			type: 'get',
			url: target,
			dataType: 'text',
			success: function(data) {
				$('#gallery-ajax').html(data);
				//$(document).scrollTop(1437);
			},
			error: function(){
				
			}
			
		});
		
	});
	
	//Show the prev image
	/*$('#prev-image').click(function(e){
		e.preventDefault();
		
		var total_images= $('#gallery nav a').length;
		var $this		= $(this);
		var image_index	= parseInt($('#gallery nav a.selected').index());
		
		if(image_index  >= 1 )
		{
			var image_target= $('#gallery nav a:eq('+(image_index-1)+')').attr('href');
			$('#lightbox-gallery > img').attr('src', image_target);
			$('#gallery nav a').removeClass('selected');
			$('#gallery nav a:eq('+(image_index-1)+')').addClass('selected');
		}
		
	});
	
	//Show the next image
	/*$('#next-image').click(function(e){
		e.preventDefault();
		
		var total_images= $('#gallery nav a').length;
		var $this		= $(this);
		var image_index	= parseInt($('#gallery nav a.selected').index());
		
		if(image_index  < (total_images-1) )
		{
			var image_target= $('#gallery nav a:eq('+(image_index+1)+')').attr('href');
			$('#lightbox-gallery > img').attr('src', image_target);
			$('#gallery nav a').removeClass('selected');
			$('#gallery nav a:eq('+(image_index+1)+')').addClass('selected');
		}
	});*/
	
	//Close the lightbox
	$('.close-lightbox').click(function(e){
		e.preventDefault();
		$('.lightbox').fadeOut('normal');
		$('#gal2 ul li.enabled a').removeClass('selected');
	});
	
	
	$('.scroll-pane').jScrollPane(
	{
			autoReinitialise: true
		}
	);
	
	//Lightbox Maps
	$('#locations').change(function(){
		var $this = $(this);
		var value = $this.val();
		
		
		$.ajax({
			url : '_/data/locations.php',
			type : 'post',
			dataType : 'text',
			data : 'id='+value,
			success : function(data){
				if(data != "")
				{
					$('#map-ajax-container').html(data);
				}
			},
			error: function(){
				
			},
			complete : function() {
				if($('#image-location').attr('src') == "")
				{
					$('#btn-camera').hide();
				}
				else {
					$('#btn-camera').show();
				}
			}
		});
	});
	
	$('#btn-camera').live('click', function(e){
		
		e.preventDefault();
		
		$('#image-location').removeClass('hidden');
		
	});
	
	$('#btn-magnify').live('click', function(e){
		
		e.preventDefault();
		
		$('#image-location').addClass('hidden');
		
	});
	
});


// optional triggers

$(window).load(function() {
	
	//hide the text from menu-flotante
	$('#menu-flotante > ul li span').hide();
	
	
	
	var leftScroll = ($(window).width() - 431) / 2;
	
	//Scroll follow
	$("#menu-flotante").css('left', leftScroll+'px');
	
	//Init the Custom Select
	$('select[data-custom-select=true]').each(function(index) {
		
		var $this = $(this);
		var $parent= $this.parent();
		var $first_text = ($this.find('option:first').text() != "") ? $this.find('option:first').text() : "Selecciona";
		
		//hide  select by default
		$this.hide();
		
		var indicator = '<span class="select-indicator-arrow"></span>';
		var anchor = '<a class="select-trigger" href="#" title="">'+'<span class="select-title-trigger">'+$first_text+'</span>'+indicator+'</a>';
		var container_anchor = '<div class="select-container-trigger">'+anchor+'</div>';
		
		var options = "";
		
		$this.find('option').each(function(index2){
			if(index2 == 0)
			{
				var value = $(this).val();
				var text = $(this).text();
				options += "<li style='display: none;'><a href='#' data-select-value='"+value+"' title=''>"+text+"</a>";
			}
			else
			{
				var value = $(this).val();
				var text = $(this).text();
				options += "<li><a href='#' data-select-value='"+value+"' title=''>"+text+"</a>";		
			}
		});
		
		var container_options = '<div class="select-container-options scroll-pane-select"><ul>'+options+'</ul></div>';
		
		$('<div class="select-box">'+container_anchor+container_options+'</div>')
			.insertAfter($this)
			.show();
		
	});
	
	//show or Hide the list with the list of options
	$('.select-trigger').live('click', function(e){
		
		e.preventDefault();
		
		var $this = $(this);
		var $container_list = $this.parent().parent().find('.select-container-options');
		var $list = $container_list.find('ul');
		
		if($container_list.is(':visible'))
		{
			$container_list.toggle();
		}
		else
		{
			
			//Globals
			var api = $('.scroll-pane-select').jScrollPane(
					{
						showArrows:false,
						maintainPosition: true,
						autoReinitialise: true
					}
			).data('jsp');
			
			api.reinitialise();
			//Hide all the list options
			$('.select-container-options').hide();
			
			var height = $this.height()+'px';
			
			//Show the list
			$container_list
				.css({
					top : '39px',
					height: '150px'
				}).show();
		}
	});
	
	//Select the option and hide the list
	$('.select-container-options  ul li a').live('click', function(e){
	 	
		e.preventDefault();
		
		var $this = $(this);
		
		var index_option	= $this.parent().index();
		var select			= $this.closest('.select-box').prev().attr('id');
		var text 			= $('#'+select).find('option:eq('+index_option+')').text();
		var trigger			= $this.closest('.select-box').find('.select-trigger');
		
		//clean all the selected options
		$('#'+select).find('option').attr('selected', false);
		
		//select the option clicked
		$('#'+select).find('option:eq('+index_option+')').attr('selected', true);
		
		//change the name
		trigger.find('.select-title-trigger').text(text);
		
		//hide the list
		$this.closest('.select-box').find('.select-container-options').hide();
		
		//Trigger the change Event
		$('#'+select).trigger('change');
	});
	
	
	
});

$(window).resize(function() {
	
});


