var map,
	llMex = new google.maps.LatLng(23.6345010, -108.5527840),
	llTj = new google.maps.LatLng(32.508893, -116.985855),
	llGdl = new google.maps.LatLng(20.695105, -103.337448),
	foco = "images/foco_icon.png";
	
function initialize_map() {
    var config = {
      zoom: 4,
      center: llMex,
	  disableDefaultUI: false,
      mapTypeId: google.maps.MapTypeId.HYBRID
    };

	map = new google.maps.Map(document.getElementById("mapa"), config);
	
	var tj = new google.maps.Marker({ position: llTj, 
									  map: map,
									  title: "SanLight Tijuana",
									  icon: foco}),
									  
		gdl = new google.maps.Marker({ position: llGdl,
									   map: map,
									   title: "SanLight Guadalajara",
									   icon: foco});
											
	google.maps.event.addListener(gdl, 'click', function() {
		map.setCenter(llGdl);
		map.setZoom(19);
	});
	
	google.maps.event.addListener(tj, 'click', function() {
			map.setCenter(llTj);
		map.setZoom(19);
	});
	
	tj.setAnimation(google.maps.Animation.DROP);
	gdl.setAnimation(google.maps.Animation.DROP);
}

function clear_form(data){
	$('#contact_form').css('opacity',1);
	$('input:text, textarea').val('');
	$('#msj').html(data).addClass('success').slideDown('slow');
	
	setTimeout("$('#msj').fadeOut('slow');", 2000);
}

$(document).ready(function(){
	jQuery('.slider').fadeIn(300);
	
	if(document.getElementById("mapa"))
		initialize_map();
	
	if ($("#s4").length) {
		$.fn.cycle.defaults.timeout = 6000;
		$(function() {
			$('#s4').before('<div id="nav" class="nav">').cycle({
				fx:     'fade',
				speed:  'slow',
				timeout: 6000,
				pager:  '#nav'
			});
		});
	};

	$.fn.equalHeight = function() {
		var group = this;
		$(window).bind('resize', function(){
			var tallest = 0;
			$(group).height('auto').each(function() {
				tallest = Math.max(tallest, $(this).height());
			}).height(tallest);
		}).trigger('resize');
	}
					
	$(".lightbox_image").hover(function(){
		$(this).find("img").stop().animate({opacity:0.5}, "normal")
	}, function(){
		$(this).find("img").stop().animate({opacity:1}, "normal")
	});

	$('#toc a').click(function(){
		$.scrollTo( this.hash, 1500);
		$(this.hash).find('#options-examples').text( this.title );
		return false;
	});
	
	$(".height").equalHeight();
	
	jQuery('ul.menu').superfish();
	
	
	function validEmail(val){
		var atpos = val.indexOf('@'),
			dotpos = val.lastIndexOf(".");
			
		return (atpos<1 || dotpos<atpos+2 || dotpos+2>=val.length);
	}
	
	if ($("#contact_form").length) {
		var frm = $("#contact_form"),
			reqs = frm.find('.requerido'),
			msj = frm.prev('#msj'),
			msn = '';
		
		frm.bind('submit', function(e){
			e.preventDefault();
			
			msj[0].innerHTML = '';
			msn = '';
			
			for (var i = reqs.length - 1; i >= 0; i--){
				if( $.trim(reqs[i].value) == '' ){
					msn += 'El ' + reqs[i].id + ' es requerido <br />';
				}else if( reqs[i].id == 'correo' ){
					if(validEmail(reqs[i].value))
						msn += 'El correo es invalido. <br />';
				}
			};
			
			if(msn){
				msj.html(msn).slideDown('slow');
			}else{
				$.ajax({type: "POST",
					url: 'send.php',
					data: $(this).serialize(),
					beforeSend: function(){
						frm.fadeTo('slow', 0.3);
					},
					success: function(data){
						setTimeout('clear_form("'+data+'")', 3000);
					}
				});
			}
		});
	}
});
