// JavaScript Document
$(document).ready(function(){
	var browserwidth  = $(window).width();
	var ancho = browserwidth - $("#left").width();
	var numero = $("ul.sc_menu li").length;
	var anchoLi = $("ul.sc_menu li").width();
	numero = (numero * (anchoLi+1));
	
	if(numero)$("ul.sc_menu").css("width",numero+"px");
	if(ancho)$("#rigth").css("width",ancho+"px");	
	
	$(".tressesenta").click(function(){
		var id = $(this).attr("href");
				
		var currentslide = $('.supersize'+id+' img.cascolol');
		var conteo = $('.supersize'+id+' img').length;
			
		if(conteo>1){
			var nextslide =  currentslide.length ? currentslide.next() : $('.supersize'+id+' img:first');
					
			currentslide.removeClass('cascolol');
			nextslide.addClass('cascolol');

			currentslide.fadeOut(function(){
				nextslide.fadeIn();	
			});
			
			
			nextslide.css({'opacity':'1'});
					
			if ((conteo-1) == currentslide.attr("id") ){
				$('.supersize'+id+' img').eq(conteo-1).removeClass('cascolol');
				$('.supersize'+id+' img').eq(0).addClass('cascolol');
				$('.supersize'+id+' img').eq(conteo-1).fadeOut(function(){
					$('.supersize'+id+' img').eq(0).fadeIn();	
				});
				

				$('.supersize'+id+' img').eq(0).css({'opacity':'1'});
			}
		}
			
		return false;	
	});
	$(".casco").hover(function() {
		var id = $(this).attr("rel");
		var currentslide = $('.supersize'+id+' img.cascolol');
		currentslide.css({'opacity':'1'});
	}, function() {
		var id = $(this).attr("rel");
		var currentslide = $('.supersize'+id+' img.cascolol');
		currentslide.css({'opacity':'0.5'});
	});
	$(".tressesenta").hover(function() {
		var id = $(this).next(".casco").attr("rel");
		var currentslide = $('.supersize'+id+' img.cascolol');
		currentslide.css({'opacity':'1'});
	}, function() {
		var id = $(this).next(".casco").attr("rel");
		var currentslide = $('.supersize'+id+' img.cascolol');
		currentslide.css({'opacity':'0.5'});
	});
	
	$("#news").click(function(){
		var selector = $("#foot ul li a")
		if(subirFoot($(this))){
			$("#calendario-li").hide(1);
			$("#ofertas-li").hide(1);
			ajaxNews4();
			$(".news").show();
		}else{
			$("#calendario-li").show(1);
			$("#ofertas-li").show(1,function(){
				$("#foot ul li a").removeClass("active");
				$(".news").hide();
			});
		}
		
	})	
	$("#calendario").click(function(){
		if(subirFoot($(this))){
			$("#ofertas-li").hide(1);
			$("#news-li").hide(1);
			$(".calendario").show();
		}else{
			$("#ofertas-li").show(1);
			$("#news-li").show(1,function(){
				$("#foot ul li a").removeClass("active");
				$(".calendario").hide();
			});
		}
		
		$("#prev-calendario").hide();
		$("#next-calendario").hide();
		
	})
	$("#ofertas").click(function(){
		var selector = $("#foot ul li a")
		if(subirFoot($(this))){
			$("#calendario-li").hide(1);
			$("#news-li").hide(1);
			ajaxOfertas4()
			$(".ofertas").show();
		}else{
			$("#calendario-li").show(1);
			$("#news-li").show(1,function(){
				$("#foot ul li a").removeClass("active");
				$(".ofertas").hide();
			});
		}
	})
	
	$(".prev").click(function(e){
		/*if (this.id != 0){
			$(".calendar").hide();
			$("#calendar_"+this.id).show();
		}*/
		fragmentoTexto = this.id.split(':');
		
		var mes = parseInt(fragmentoTexto[0]);// mes
		var ano = parseInt(fragmentoTexto[1]);// año

		
		$.ajax({
			beforeSend : function (){
				$(".calendar").hide();
				$(".calendario").append('<div class="cargando"></div>');
				$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
			},			   
			type: "POST",
			url: "bin/newcalendario.ajax.php",
			data:"anoNuevo="+ano+"&mesNuevo="+mes,
			success: function(msg){
				$("#calendario-left").html(msg);
			},complete : function (){
				$(".cargando").remove();
				$(".calendar").fadeIn("slow",function(){$.getScript("js/general.js");});
			}
		});
	});		
	$(".next").click(function(e){
		/*if (this.id != 0){
			$(".calendar").hide();
			$("#calendar_"+this.id).show();							  		
		}*/
		fragmentoTexto = this.id.split(':');
		
		var mes = parseInt(fragmentoTexto[0]);// mes
		var ano = parseInt(fragmentoTexto[1]);// año

		
		$.ajax({
			beforeSend : function (){
				$(".calendar").hide();
				$(".calendario").append('<div class="cargando"></div>');
				$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
			},			   
			type: "POST",
			url: "bin/newcalendario.ajax.php",
			data:"anoNuevo="+ano+"&mesNuevo="+mes,
			success: function(msg){
				$("#calendario-left").html(msg);
			},complete : function (){
				$(".cargando").remove();
				$(".calendar").fadeIn("slow",function(){$.getScript("js/general.js");});
			}
		});
	});
	
	/*********** SCROLL PARA EL CATÁLOGO DE CASCOS ***********/
	//Get our elements for faster access and set overlay width
	var div 		= $('div.sc_menu'),
        div2        = $('ul.sc_menu li:gt(0) '),
		ul 			= $('ul.sc_menu'),
        liWidth     = $('ul.sc_menu li').width();
		ulPadding 	= 15;
	//Get menu width
	var divWidth 	= div.width();
	
	//Remove scrollbars
	div.css({overflow: 'hidden'});
	//Find last image container
	var lastLi 		= ul.find('li:last-child');
	//When user move mouse over menu
	div.mousemove(function(e){
		//As images are loaded ul width increases,
		//so we recalculate it each time
	
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

        if (e.pageX > 200){
                // Ultima.
                var left    = (e.pageX - div.offset().left) * (ulWidth-divWidth+500)  / divWidth;
                div.scrollLeft(left);
                lol = true;
                
        }else{
        	var left 	= (e.pageX - div.offset().left) * (ulWidth-divWidth)  / divWidth;
			div.scrollLeft(left);
        }
	
	/*	var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
		
		var left 	= (e.pageX - div.offset().left) * (ulWidth-divWidth)  / divWidth;
		div.scrollLeft(left);
	*/
	});
	
	
	$("#news-principal").live("click",function(){
		if ($("#news-principal").height()==118){
			$("#news-principal").animate({ 
				height: (193)
			}, 1500 );
			$("#old-texto").fadeOut("fast",function(){$("#news-texto").fadeIn(1500);});
			$(".news-secundarias").fadeOut(1500);
		}else{
			$("#news-texto").fadeOut("fast",function(){$("#old-texto").fadeIn(1500);});
			
			$(".news-secundarias").fadeIn(1500);
			$("#news-principal").animate({
				height: (118)
			}, 1500 );
		}
	});
	/*$("#news-principal,.news-secundarias,#mas-noticias").live("click",function(){
		if(subirFootExtra($("#news"))){
			$("#news-texto").fadeIn(1500);

		}else{
			$("#news-texto").fadeOut(1500);
		}		
	});*/
	
	
});
function newsPrincipal(){
			if ($("#news-principal").height()==118){
				$("#news-principal").animate({ 
					height: (193)
				}, 1500 );
				$("#old-texto").fadeOut("fast",function(){$("#news-texto").fadeIn(1500);});
				$(".news-secundarias").fadeOut(1500);
			}else{
				$("#news-texto").fadeOut("fast",function(){$("#old-texto").fadeIn(1500);});
				$(".news-secundarias").fadeIn(1500);
				$("#news-principal").animate({ 
					height: (118)
				}, 1500 );
			}
	}

function ajaxNews4(){
	$.ajax({
		beforeSend : function (){
			$("#news-left").hide();
			$("#publi").hide();
			$(".news").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/noticias.ajax.php",
		data:"all=todo",
		success: function(msg){
			$("#news-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#news-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			//slideNews();
		}
	});				
}
function ajaxOfertas4(){
	$.ajax({
		beforeSend : function (){
			$("#oferts-left").hide();
			$("#publi").hide();
			$(".ofertas").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/ofertas.ajax.php",
		data:"all=todo",
		success: function(msg){
			$("#oferts-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#oferts-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			//slideNews();
		}
	});				
}
function ajaxOfertas(idNews){
	$.ajax({
		beforeSend : function (){
			$("#oferts-left").hide();
			$("#publi").hide();
			$(".ofertas").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/ofertas.ajax.php",
		data:"id="+idNews,
		success: function(msg){
			$("#oferts-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#oferts-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			newsPrincipal();
			slideNews();
		}
	});				
}
function ajaxNews(idNews){
	$.ajax({
		beforeSend : function (){
			$("#news-left").hide();
			$("#publi").hide();
			$(".news").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/noticias.ajax.php",
		data:"id="+idNews,
		success: function(msg){
			$("#news-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#news-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			newsPrincipal();
			slideNews();
		}
	});				
}
function ajaxNews2(idNews){
	$.ajax({
		beforeSend : function (){
			$("#news-left").hide();
			$("#publi").hide();
			$(".news").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/noticias.ajax.php",
		data:"id2="+idNews,
		success: function(msg){
			$("#news-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#news-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			newsPrincipal();
		}
	});				
}
function ajaxOfertas2(idNews){
	$.ajax({
		beforeSend : function (){
			$("#oferts-left").hide();
			$("#publi").hide();
			$(".ofertas").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "700px",	"height" : "221px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/ofertas.ajax.php",
		data:"id2="+idNews,
		success: function(msg){
			$("#oferts-left").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#oferts-left").fadeIn("slow");
			$("#publi").fadeIn("slow");
			newsPrincipal();
		}
	});				
}
function ajaxDiaCalendario(fecha){
	$.ajax({
		beforeSend : function (){
			$("#calendario-rigth").hide();
			$(".calendario").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/calendario.ajax.php",
		data:"fecha="+fecha,
		success: function(msg){
			$("#calendario-rigth").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#calendario-rigth").fadeIn("slow",function(){slideCalendario();});
			
		}
	});				
}

function ajaxMesCalendario(anoActual,mesActual){
	$.ajax({
		beforeSend : function (){
			$("#calendario-rigth").hide();
			$(".calendario").append('<div class="cargando"></div>');
			$(".cargando").css({"display" : "block", "width" : "422px",	"height" : "192px", "float" : "left"});
		},			   
		type: "POST",
		url: "bin/calendario.ajax.php",
		data:"anoActual="+anoActual+"&mesActual="+mesActual,
		success: function(msg){
			$("#calendario-rigth").html(msg);
		},complete : function (){
			$(".cargando").remove();
			$("#calendario-rigth").fadeIn("slow",function(){slideCalendario();});
			
		}
	});
}

function subirFoot(id){
	if ($("#foot").css("height")=='31px'){
		$("#foot").animate({ 
			height: (252)
		}, 1500 );
		$(id).addClass("active");
		return true;
	}else{
		$("#foot").animate({ 
			height: (31)
		}, 1500 );
		return false
	}
}

function subirFootExtra(id){
	var altura 		= $("#news-principal").height();
	if ($("#foot").css("height")==252+'px'){
			$("#foot").animate({ 
				height: (altura+252)
			}, 1500 );
		return true;
	}else{
		$("#foot").animate({ 
			height: (252)
		}, 1500 );
		return false
	}
}

function slideCalendario(){
			$("#prev-calendario").show();
			$("#next-calendario").show();
			var currentPosition = 0;
			var slideHeight = 105;
			var slides = $('.eventos');
			var numberOfSlides = slides.length;
			

			
			// Wrap all .slides with #slideInner div
			slides.wrapAll('<div id="slideInner"></div>');
			// Float left to display horizontally, readjust .slides width
			
			// Set #slideInner width equal to total width of all slides
			$('#slideInner').css('height', slideHeight * numberOfSlides);

			// Hide left arrow control on first load
			manageControls(currentPosition);
			// Create event listeners for .controls clicks
			
				$('.control').bind('click', function(){
					// Determine new position

					currentPosition = ($(this).attr('id')=='next-calendario') ? currentPosition+1 : currentPosition-1;

					// Hide / show controls
					manageControls(currentPosition);

					// Move slideInner using margin-left
					$('#slideInner').animate({
						'marginTop' : slideHeight*(-currentPosition)
					});
				});
			
			// manageControls: Hides and Shows controls depending on currentPosition
			function manageControls(position){
				// Hide left arrow if position is first slide
				if((position==0)||(numberOfSlides<=1)){
					$('#prev-calendario').hide();
				}else{
					$('#prev-calendario').show();
				}
				// Hide right arrow if position is last slide
				if((position==numberOfSlides-2)||(numberOfSlides<=1)){
					$('#next-calendario').hide();
				}else{
					$('#next-calendario').show();
					
				}
			}
}

function slideNews(){
			var currentPosition = 0;
			var slideHeight = 22;
			var slides = $('.mas-noticias');
			var numberOfSlides = slides.length;
			
			// Wrap all .slides with #slideInner div
			slides
			.wrapAll('<div id="slideInner"></div>');
			// Float left to display horizontally, readjust .slides width
			
			// Set #slideInner width equal to total width of all slides
			$('#slideInner').css('height', slideHeight * numberOfSlides);

			// Hide left arrow control on first load
			manageControls(currentPosition);
			// Create event listeners for .controls clicks
			$('.control').bind('click', function(){
				// Determine new position
				currentPosition = ($(this).attr('id')=='next-noticias') ? currentPosition+1 : currentPosition-1;
				
				// Hide / show controls
				manageControls(currentPosition);
				// Move slideInner using margin-left
				$('#slideInner').animate({
					'marginTop' : slideHeight*(-currentPosition)
				});
			});
			// manageControls: Hides and Shows controls depending on currentPosition
			function manageControls(position){
				// Hide left arrow if position is first slide
				if((position==0)||(numberOfSlides<=1)){
					$('#prev-noticias').hide();
				}else{
					$('#prev-noticias').show();
				}
				// Hide right arrow if position is last slide
				if((position==numberOfSlides-9)||(numberOfSlides<=8)){
					$('#next-noticias').hide();
				}else{
					$('#next-noticias').show();
				}
			}
}
