$(function(){
	ant=1;
	sig=2;
	act=0;
	pres=0;
	prod=1;
	// Obtención de la URL.                                 
	url = document.location.href ;     
	url_ant = document.referrer;
	// División en trozos con la barra como delimitador.    
	partes = url.split('/'); 
	partes_ant = url_ant.split('/'); 
	// Obtención del nombre de la página y sus parámetros.                    
	pagina = partes[partes.length-1];
	pagina_ant = partes_ant[partes_ant.length-1];
	
	if (pagina=="" || pagina=="index.php"){
	  $("#box_banner").everyTime(4500, "fade", function() {												   
		$("#banner"+ant).fadeOut("slow",function(){
			$("#banner"+ant).fadeIn("slow");
			$("#submenu_"+ant).css("background","url(images/b-submenu_2.png)");
		});
		$("#submenu_"+ant).css("background","url(images/b-submenu.png)");
		ant = sig;
		sig = sig+1;
		if (sig==5){ant=4;sig=1;}
	  });
	}
	
	if (pagina=="productos.php"){
	  $("#box_productos").everyTime(4500, function() {												   
		$("#prod_imgs").fadeOut("slow",function(){
			$("#prod_imgs").css("background","url(images/prod_"+prod+".jpg)");
		});
		$("#prod_imgs").fadeIn("slow");
		prod = prod+1;
		if (prod==6){prod=1;}
	  });
	}
	
	
	$("#submenu_1").click(function() {
	  $("#box_banner").stopTime("fade");
	  $("#banner"+ant).fadeOut("slow",function(){
			$("#banner1").fadeIn("slow");
			$("#submenu_"+ant).css("background","url(images/b-submenu.png)");
			$("#submenu_1").css("background","url(images/b-submenu_2.png)");
			ant = 1;
			sig = ant+1;
		});
	  
	});
	
	$("#submenu_2").click(function() {
	  $("#box_banner").stopTime("fade");
	  $("#banner"+ant).fadeOut("slow",function(){
			$("#banner2").fadeIn("slow");
			$("#submenu_"+ant).css("background-image","url(images/b-submenu.png)");
			$("#submenu_2").css("background-image","url(images/b-submenu_2.png)");
			
			ant = 2;
			sig = ant+1;
	   });
	  $("#banner").css("overflow","hiden");
	});
	
	$("#submenu_3").click(function() {
	  $("#box_banner").stopTime("fade");
	  $("#banner"+ant).fadeOut("slow",function(){					    
			$("#banner3").fadeIn("slow");
			$("#submenu_"+ant).css("background","url(images/b-submenu.png)");
			$("#submenu_3").css("background","url(images/b-submenu_2.png)");
			ant = 3;
			sig = ant+1;
		});
	});
	
	$("#submenu_4").click(function() {
	  $("#box_banner").stopTime("fade");
	  $("#banner"+ant).fadeOut("slow",function(){
			$("#banner4").fadeIn("slow");
			$("#submenu_"+ant).css("background","url(images/b-submenu.png)");
			$("#submenu_4").css("background","url(images/b-submenu_2.png)");
			ant = 4;
			sig = 1;
		});
	  
	});
	
	
	
	margin = 0;
	
	$("#inicio").click(function() {
		pres=1;
	});
	$("#quienes").click(function() {
		pres=1;
	});
	$("#productos").click(function() {
		pres=1;
	});
	$("#servicios").click(function() {
		pres=1;
	});
	$("#ubicacion").click(function() {
		pres=1;
	});
	$("#clientes").click(function() {
		pres=1;
	});
	$("#contactenos").click(function() {
		pres=1;
	});
	
	switch(pagina_ant){
		case "": 
			$("#line").css({"background-color":"#f26937", "width":"46px", "margin-left":"0px"}); break;
		case "index.php": 
			$("#line").css({"background-color":"#f26937", "width":"46px", "margin-left":"0px"}); break;
		case "quienes.php": 
			$("#line").css({"background-color":"#003ca0", "width":"117px", "margin-left":"49px"}); break;
		case "productos.php": 
			$("#line").css({"background-color":"#003ca0", "width":"71px", "margin-left":"169px"}); break;
		case "servicios.php": 
			$("#line").css({"background-color":"#003ca0", "width":"66px", "margin-left":"243px"}); break;
		case "ubicacion.php": 
			$("#line").css({"background-color":"#003ca0", "width":"72px", "margin-left":"312px"}); break;
		case "clientes.php": 
			$("#line").css({"background-color":"#003ca0", "width":"61px", "margin-left":"387px"}); break;
		case "contactenos.php": 
			$("#line").css({"background-color":"#003ca0", "width":"84px", "margin-left":"451px"}); break;
	}
	
	switch(pagina){
		case "": 
			$("#bgmenu").css("background-position","0px 0px");
			$("#inicio").animate({color:'#f26937'},150);
			pag = 1;
			m = "0px"; 
			w = "46px";
			bg = "#f26937"; 
			$("#submenu_1").css("background","url(images/b-submenu_2.png)");
			$("#submenu_2").css("background","url(images/b-submenu.png)");
			$("#submenu_3").css("background","url(images/b-submenu.png)");
			$("#submenu_4").css("background","url(images/b-submenu.png)");
			$("#inicio").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;	
		case "index.php": 
			$("#bgmenu").css("background-position","0px 0px");
			$("#inicio").animate({color:'#f26937'},150);
			pag = 1;
			m = "0px"; 
			w = "46px";
			bg = "#f26937";
			$("#submenu_1").css("background","url(images/b-submenu_2.png)");
			$("#submenu_2").css("background","url(images/b-submenu.png)");
			$("#submenu_3").css("background","url(images/b-submenu.png)");
			$("#submenu_4").css("background","url(images/b-submenu.png)");
			$("#inicio").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;	
		case "quienes.php": 
			$("#bgmenu2").css("background-position","0px -13px");
			$("#quienes").animate({color:'#003ca0'},150);
			pag = 2;
			m = "49px"; 
			w = "117px";
			bg = "#003ca0";
			$("#quienes").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
		case "productos.php": 
			$("#bgmenu2").css("background-position","0px -26px");
			$("#productos").animate({color:'#003ca0'},150);
			pag = 3;
			m = "169px"; 
			w = "71px";
			bg = "#003ca0";
			$("#productos").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
		case "servicios.php": 
			$("#bgmenu2").css("background-position","0px -39px");
			$("#servicios").animate({color:'#003ca0'},150);
			pag = 4;
			m = "243px"; 
			w = "66px";
			bg = "#003ca0";
			$("#servicios").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
		case "ubicacion.php": 
			$("#bgmenu2").css("background-position","0px -52px");
			$("#ubicacion").animate({color:'#003ca0'},150);
			pag = 5;
			m = "312px"; 
			w = "72px";
			bg = "#003ca0";
			$("#ubicacion").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
		case "clientes.php": 
			$("#bgmenu2").css("background-position","0px -65px");
			$("#clientes").animate({color:'#003ca0'},150);
			pag = 6;
			m = "387px"; 
			w = "61px";
			bg = "#003ca0";
			$("#clientes").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
		case "contactenos.php": 
			$("#bgmenu2").css("background-position","0px -78px");
			$("#contactenos").animate({color:'#003ca0'},150);
			pag = 7;
			m = "451px"; 
			w = "84px";
			bg = "#003ca0";
			$("#contactenos").oneTime(200, function() {
				$("#line").animate({backgroundColor:bg, width:w,marginLeft:m},150);
			});
			break;
	}

	 $().ajaxStart(function() {
		$('#form').hide();
		$('#result').hide();
		$('#contacto_p').show();
        $('#loading').show();
    }).ajaxStop(function() {
        $('#loading').hide();
		$('#contacto_p').show();
        $('#result').fadeIn('slow');
    });
	 
    $('#form').submit(function() {
		if (envia){					   
			$.ajax({
				type: 'POST',
				url: $(this).attr('action'),
				data: $(this).serialize(),
				success: function(data) {
					$('#result').html(data);
				}
			})
			return false;
		}
    }); 

	
	$("#mapsite").click(function() {
		$("#mapa").slideToggle("medium");
	});	
	
	
	$("#inicio").hover(function() {	
		   act=1;
		   $("#inicio").animate({color:'#f26937'},150);
		   $("#line").stop().animate({width:'46px',marginLeft:'0px',backgroundColor:'#f26937'},150);	
	  }, function(){
		  		act=0;
				if (pag!=1){
					$("#inicio").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	  });
	
	$("#quienes").hover(function() {	
		   act=1;
		   $("#quienes").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'117px',marginLeft:'49px'},150);
	  }, function(){
		  		act=0;
				if (pag!=2){
					$("#quienes").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	 });
	
	$("#productos").hover(function() {
		   act=1;
		   $("#productos").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'71px',marginLeft:'169px'},150);
	  }, function(){
		  		act=0;
				if (pag!=3){
					$("#productos").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	 });
	
	$("#servicios").hover(function() {	
		   act=1;				
		   $("#servicios").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'66px',marginLeft:'243px'},150);
	  }, function(){
		  		act=0;
				if (pag!=4){
					$("#servicios").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	 });
	
	$("#ubicacion").hover(function() {
		   act=1;
		   $("#ubicacion").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'72px',marginLeft:'312px'},150);
	  }, function(){
		  		act=0;
				if (pag!=5){
					$("#ubicacion").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	});
	
	$("#clientes").hover(function() {	
		   act=1;
		   $("#clientes").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'61px',marginLeft:'387px'},150);
	  }, function(){
		  		act=0;
				if (pag!=6){
					$("#clientes").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	 });
	
	$("#contactenos").hover(function() {
		   act=1;
		   $("#contactenos").animate({color:'#003ca0'},150);
		   $("#line").stop().animate({backgroundColor:'#003ca0',width:'84px',marginLeft:'451px'},150);
	  }, function(){
		  		act=0;
				if (pag!=7){
					$("#contactenos").animate({color:'#667777'},500);
				}
				$(this).oneTime(350, function() {
					if (act==0 && pres==0){
		   				$("#line").stop().animate({backgroundColor:bg, width:w,marginLeft:m},150);
					}
				});
	});
	
	
	

});
