var _timer = null;
var _IE7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;

var core = {
	menu:{
		init:function(){
			$("#menu li").click(function(e) {
				core.menu.active(e.currentTarget.id, false);
			});
			core.over.init();
			core.buinzootv.init();
		},
		active:function(id, instant){
			if(id != core.menu.old){
				
				core.menu.old = id;
				id = id.substr(1,1);
				
				$('#m1, #m2, #m3').addClass('off');
				$('#m'+id).removeClass('off');			
				
				if(instant){
					$('#back-sub-menu .b1, #back-sub-menu .b2, #back-sub-menu .b3').hide();
					$('#back-sub-menu .b'+id).fadeIn();
				}else{
					$('#back-sub-menu .b1, #back-sub-menu .b2, #back-sub-menu .b3').fadeOut();
					$('#back-sub-menu .b'+id).fadeIn();
				}
				
				$('#sub-menu .m1, #sub-menu .m2, #sub-menu .m3').css({display:"none"});
				$('#sub-menu .m'+id).css( {display:"inline-block"} ); // revisar Css
				
				$('#sub-menu').removeClass('m1 m2 m3');
				$('#sub-menu').addClass('m'+id);
			}
		},
		old:null,
		selected:null
	},
	homebanner:{
		init:function(){
			var a = $("#homebanners .menu a");
						
			core.homebanner.current = $("#homebanners .menu #b1");
				
			if(a.length>0){
				$.each(a, function(k, v) {										
					$(v).mouseup(function(e){
						clearInterval(_timer);
						_timer= null;
						
						if ( _timer == null ) _timer = setInterval("core.homebanner.setBanner()",10000);
						core.homebanner.showBanner(e.currentTarget);
					});
				});
			}
			_timer = setInterval("core.homebanner.setBanner()",10000);
		},
		showBanner:function(e) {
			if(core.homebanner.current_id != e.id) {
				$('body').css('overflow-x', 'hidden');
				$(e).addClass('selected');
				
				$(core.homebanner.current).removeClass('selected');
				core.homebanner.current = e;
				
				var id = e.id;
				if (!_IE7) {
					$('#homebanners-bkgs #b1, #homebanners-bkgs #b2, #homebanners-bkgs #b3, #homebanners-bkgs #b4').fadeOut();
					$('#homebanners-bkgs #'+id).fadeIn();
												
					$('#homebanners li#'+core.homebanner.current_id).animate({ marginLeft:'-960px', opacity:0}, 1000, function() { $(this).css({ display:"none" }) });
					
					$('#homebanners li#'+id).css({ display:"block", marginLeft:'960px'});
					$('#homebanners li#'+id).animate({marginLeft:'0px', opacity:1}, 1000, function() { $('body').css('overflow-x', 'visible')});
				} else {
					$('#homebanners-bkgs #b1, #homebanners-bkgs #b2, #homebanners-bkgs #b3, #homebanners-bkgs #b4').fadeOut();
					$('#homebanners-bkgs #'+id).fadeIn();
												
					$('#homebanners .banners').find('#' + core.homebanner.current_id).animate({ marginLeft:'-1200px' }, 1000, function() { 
						$(this).css({ display:"none" });
						$('#homebanners .banners').find('#'+id).css( { display:"block", marginLeft:'960px'} );
						$('#homebanners .banners').find('#'+id).animate({marginLeft:'0px'}, 1000, function() { $('body').css('overflow-x', 'visible'); });
					});
				}										
				core.homebanner.current_id = id;
			}			
		},
		setBanner:function() {
			var a = $("#homebanners .menu a");
			var _str;
						
			if ( core.homebanner.current_id  == "b"+a.length) {
				core.homebanner.showBanner($('#homebanners .menu').find('#b1')[0]);
			} else {
				_str = core.homebanner.current_id.substr(1);
				_str++;
				_str = "#b" + _str;
				
				// console.log($('#homebanners .menu').find(_str)[0]);
				// core.homebanner.showBanner($(_str)[0]);
				core.homebanner.showBanner($('#homebanners .menu').find(_str)[0]);
			}
		},
		current:null,
		current_id:"b1"
	},
	over:{
		init:function(){
			var a = $(".over");
			if(a.length>0){
				$.each(a, function(k, v) {					
					var i = $(v).find('img')[0];
					i.rel = i.src;					
					$(i).mouseover(function(e){
						var p1 = i.rel.substr(i.rel.lastIndexOf("."));
						var p2 = i.rel.substr(0, i.rel.lastIndexOf("."));
						var over = p2+"_over"+p1;
						e.currentTarget.src = over;
					});
					$(i).mouseout(function(e){
						e.currentTarget.src = e.currentTarget.rel;
					});
				});
			}
			
			//OVER TAB READ MORE
			var a = $(".tab");
			if(a.length>0){
				$.each(a, function(k, v) {									
					$(v).mouseover(function(e){ $(e.currentTarget).addClass('over-b'); });
					$(v).mouseout(function(e){ $(e.currentTarget).removeClass('over-b'); });
				});
			}
			
			//REST READ MORE
			var a = $(".read-more");
			if(a.length>0){
				$.each(a, function(k, v) {									
					$(v).mouseover(function(e){ $(e.currentTarget).addClass('mod'); });
					$(v).mouseout(function(e){ $(e.currentTarget).removeClass('mod'); });
					
				});
			}
		
		}
	},
	buinzootv:{
		init:function(){
			core.buinzootv.dom = $('#buinzootv-single');
			if(core.buinzootv.dom.length > 0){
				core.buinzootv.dom = core.buinzootv.dom[0] ;
				core.buinzootv.deploy();
			}
		},
		deploy:function(){
			
			dom = core.buinzootv.dom;
			container = $(dom).find('.container')[0];
			$(dom).find('ul').css({width:($(dom).find('ul>li').length*($(dom).find('li:first').outerWidth(true)))});
			
			$(dom).find('.ar_rigth').mouseover(function(e){ core.buinzootv.int = setInterval(function(){ container.scrollLeft+=12; }, 33); });
			$(dom).find('.ar_rigth').mouseout(function(e){ clearInterval(core.buinzootv.int); });
			
			$(dom).find('.ar_left').mouseover(function(e){ core.buinzootv.int = setInterval(function(){ container.scrollLeft-=12; }, 33); });
			$(dom).find('.ar_left').mouseout(function(e){ clearInterval(core.buinzootv.int); });
			
		},
		int:null,
		dom:null
	}
}

var section = {
	comollegar:{
		currentTargetA: null, 
		currentTargetB: null,
		
		init:function(){
			$(".planificar, .vuelveconsulta").click(function(e){
				$('#auto-norte, #auto-sur,#bus-norte, #bus-sur, #tren-norte, #tren-sur, #error').hide();
								
				if (section.comollegar.currentTargetA != null && section.comollegar.currentTargetB != null) {
					switch( section.comollegar.currentTargetA.currentTarget.id ) {
						case "auto" :
							switch ( section.comollegar.currentTargetB.currentTarget.id ) {
								case "norte" :
									$('#auto-norte').show();
									break;
								case "sur" :
									$('#auto-sur').show();
									break;
							}
							break;
						case "bus" :
							switch ( section.comollegar.currentTargetB.currentTarget.id ) {
								case "norte" :
									$('#bus-norte').show();
									break;
								case "sur" :
									$('#bus-sur').show();
									break;
							}
							break;
						case "tren" :
							switch ( section.comollegar.currentTargetB.currentTarget.id ) {
								case "norte" :
									$('#tren-norte').show();
									break;
								case "sur" :
									$('#tren-sur').show();
									break;
							}
							break;
					}
				} else {
					$('#error').show();
				}
				$("#content.comollegar #step2, #content.comollegar #step1").toggle();
			});

			var a = $("#step1 .a a");
			if(a.length > 0){
				$.each(a, function(k, v) {					
					var i = $(v).find('img')[0];
					i.rel = i.src;					
					$(i).mouseover(function(e){
						var p1 = i.rel.substr(i.rel.lastIndexOf("."));
						var p2 = i.rel.substr(0, i.rel.lastIndexOf("."));
						var over = p2+"_over"+p1;
						e.currentTarget.src = over;
					});
					$(i).mouseout(function(e){
						if ( section.comollegar.currentTargetA != null ) {
							if ( section.comollegar.currentTargetA.currentTarget.id != e.currentTarget.id )
								e.currentTarget.src = e.currentTarget.rel;
						} else {
							e.currentTarget.src = e.currentTarget.rel;
						}
					});
				});
			}	

			a = $("#step1 .b a");
			if(a.length > 0){
				$.each(a, function(k, v) {					
					var i = $(v).find('img')[0];
					i.rel = i.src;					
					$(i).mouseover(function(e){
						var p1 = i.rel.substr(i.rel.lastIndexOf("."));
						var p2 = i.rel.substr(0, i.rel.lastIndexOf("."));
						var over = p2+"_over"+p1;
						e.currentTarget.src = over;
					});
					$(i).mouseout(function(e){
						if ( section.comollegar.currentTargetB != null ) {
							if ( section.comollegar.currentTargetB.currentTarget.id != e.currentTarget.id )
								e.currentTarget.src = e.currentTarget.rel;
						} else {
							e.currentTarget.src = e.currentTarget.rel;
						}
					});
				});
			}
						
			$("#step1 .a a img").click(function(e) {
				if ( section.comollegar.currentTargetA != null )
					if (e.currentTarget.id != section.comollegar.currentTargetA.currentTarget.id)
						section.comollegar.currentTargetA.currentTarget.src = section.comollegar.currentTargetA.currentTarget.rel;
				section.comollegar.currentTargetA = e;
			});
			
			$("#step1 .b a img").click(function(e) {
				if ( section.comollegar.currentTargetB != null )
					if (e.currentTarget.id != section.comollegar.currentTargetB.currentTarget.id)
						section.comollegar.currentTargetB.currentTarget.src = section.comollegar.currentTargetB.currentTarget.rel;
				section.comollegar.currentTargetB = e;
			});
		}
	}
}
var viajeempresa = {

	submit:function(form) {		
		// Ocultando el mensaje de alerta de datos
		$('response_msg').hide();
		$('#contact_alert_msg').hide();
		$('#send_alert_error_msg').hide();
		$('#send_alert_ok_msg').hide();
		
		// variables a enviar
		var _valido = true;
		var _nombre = form.nombre.value;
		var _email = form.email.value;
		var _telefono = form.telefono.value;
		var _empresa = form.empresa.value;
		var _telefonoempresa = form.telefonoempresa.value;
		var _fechareserva = form.fechareserva.value;
		var _comentario = form.comentario.value;
		
		// validando los datos necesarios	
		if ( _nombre.length < 1 ) {
		    form.nombre.focus();
		    _valido = false;
    	} 
    	else if (!_email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i) ) {
		    form.email.focus();
		    _valido = false;
    	}
    	else if ( _empresa.length < 1 ) {
		    form.empresa.focus();
		    _valido = false;
    	}
    	else if ( _telefonoempresa.length < 1 ) {
		    form.telefonoempresa.focus();
		    _valido = false;
    	}
    	// verificando que los campos sean v‡lidos
    	if (_valido) {
    		// enviando el mensaje
    		$('#response_msg').html("Enviado el mensaje...");
    		$('#response_msg').fadeIn('slow');
    		var datastr ='nombre=' + _nombre + '&email=' + _email + '&telefono=' + _telefono + '&empresa=' + _empresa + '&telefonoempresa=' + _telefonoempresa + '&fechareserva=' + _fechareserva + '&comentario=' + _comentario;
    		viajeempresa.send(datastr);
    	}
    	else { 
    		$('#contact_alert_msg').html("Debes llenar todos los campos obligatorios."); 
    		$('#contact_alert_msg').fadeIn("slow"); 
    	}
  		
    	return false;
	},
	send:function(data) {
		$.ajax({
				type: "POST",
				url: template_url + "/scripts/viajeempresasm.php",
				data: data,
				cache: false,
				timeout: 2000,
				success: function(response) {
					$('#response_msg').hide();
					if (response == "true")
						$('#send_alert_ok_msg').fadeIn("slow");
					else 
						$('#send_alert_error_msg').fadeIn("slow");
				},
				error: function (errorReq, textStatus, errorThrown) { 
					alert("Error: " + errorReq + " " + textStatus + " " + errorThrown);	
				}
		});
	}
}

$(function() {
 	$(".social-networks a").hover(function() {
  		$(this).children("span").fadeIn();
 	}, function() {
 		$(this).children("span").hide();
 	});
 	
 	$("#sidebar_sn a").hover(function() {
  		$(this).children("span").fadeIn();
 	}, function() {
 		$(this).children("span").hide();
 	});
});

$(document).ready(function() {
	core.menu.init();
	if (activemenu == "") activemenu = "m1";
	core.menu.active(activemenu, true);
	if (submenu != "") $("#sub-menu li#" + submenu + " a").addClass('over');
	if (  typeof( window[ 'banner' ] ) != "undefined"  ) { core.homebanner.init(); }
	
	// Ocultamos todos los elementos con la clase msg_body
  	$(".msg_body").hide();
  	
  	$(".msg_head").click(function(e) {
    	$(this).next(".msg_body").slideToggle(600, function() {
    		
    		if($(this).is(':visible')) {
    			$(e.target).parent().find("span").html(" [Click para cerrar]");
    		} else {
    			$(e.target).parent().find("span").html(" [Click para abrir]");
    		}
    	});
  	});
});


