$(document).ready(function(){
	var menuTimers = [];

	/* Rollover del menu principal */
	$('#topmenu > ul > li').mouseover(function(e){
		$(this).parent().parent().addClass($(this).attr('id').split('-')[1]);
		$(this).addClass('hover');
		var index = $('#topmenu > ul > li').index(this);
		if (menuTimers[index])
		{
			clearTimeout(menuTimers[index]);
		}

	}).mouseout(function(e){
		var li = $(this);
		var index = $('#topmenu > ul > li').index(this);
		menuTimers[index] = setTimeout(function(){
			li.parent().parent().removeClass(li.attr('id').split('-')[1]);
			li.removeClass('hover');
		},100);
	});

	/* Dropdown selector de rallyes */
	$('ul.dropdown').addClass('collapsed');
	$('ul.dropdown li').hide();
	//$('ul.dropdown li:first-child').show().addClass('active');
	var activeItem = $('ul.dropdown li.active');
	var activeItemIndex = $('ul.dropdown li').index(activeItem);

	if ( activeItemIndex != 0 )
	{
		$('table.clasificacion').hide();
		$('table#c-rallye-'+activeItemIndex).show();
	}
	activeItem.show();
	$('ul.dropdown li').mouseover(function(){
		$(this).addClass('hover');
	}).mouseout(function(){
		$(this).removeClass('hover');
	});

	$('ul.dropdown li').click(function(e){
		var ul = $(this).parent();
		// cerrar
		if ( !ul.hasClass('collapsed') )
		{
			ul.parent().css({position: 'static'});
			ul.css({position: 'static', display: 'inline'});
			ul.find('li').hide().removeClass('active');
			ul.addClass('collapsed');
			$(this).show().addClass('active');
			$(this).parent().trigger('change',[$(this)]);
			e.stopPropagation();
		}
	});

	$('ul.dropdown').click(function(e){
		var ul = $(this);

		// desplegar
		if ( ul.hasClass('collapsed') )
		{
			ul.parent().css({position: 'relative'});
			ul.css({position: 'absolute', display: 'block',left: 172});
			ul.find('li').show();
			ul.removeClass('collapsed');
			e.stopPropagation();
		}
	});


	$('#rally_selector').bind('change',function(e, li){
		var index = $(li).parent().find('> li').index(li);
		$('table.clasificacion').hide();
		$('table#c-rallye-'+index).show();
	});


	/* Month pager */
	$('div.monthpager a.back').click(function(){
		var currentLi = $('div.monthpager li.active');
		var monthList = $('div.monthpager li');
		var calendarIndex = monthList.index(currentLi);
		if ( calendarIndex > 0 )
		{
			var calendars = $('.calendar-container');
			currentLi.removeClass('active');
			$(monthList.get(calendarIndex-1)).addClass('active');
			calendars.addClass('hidden');
			$(calendars.get(calendarIndex-1)).removeClass('hidden');
		}
	});
	$('div.monthpager a.forward').click(function(){
		var currentLi = $('div.monthpager li.active');
		var monthList = $('div.monthpager li');
		var calendarIndex = monthList.index(currentLi);
		if ( calendarIndex < monthList.length - 1)
		{
			var calendars = $('.calendar-container');
			currentLi.removeClass('active');
			$(monthList.get(calendarIndex+1)).addClass('active');
			calendars.addClass('hidden');
			$(calendars.get(calendarIndex+1)).removeClass('hidden');
		}
	});

	$('#homeflashcontainer').flash({
		swf: typeof ultimoVideoYT == 'undefined' ? '' : ultimoVideoYT,//'http://www.youtube.com/v/x0SJU3l71hs&hl=es&fs=1&',
		height: 351,
		width: 580
	});

	$('.homevideo .video a').click(function(e){
		var url = $(this).attr('href');
		$('#homeflashcontainer').flash({
			swf: url,
			height: 351,
			width: 580
		});
		e.stopPropagation();
		return false;
	});

	// pesta�as de seccion

	var sectionTabs = $('.section .section-top ul li');
	var backgroundYOffset = ( sectionTabs.index($('.section .section-top ul li.active')) + 1 ) * 38 * -1;
	$('.section .section-top ul').css({backgroundPosition: '0 '+backgroundYOffset+'px'});
	$('.section .section-top ul li.active a').click(function(e) { e.stopPropagation(); return false; });

	sectionTabs.mouseover(function(){
		var currentTab = sectionTabs.index(this);
		var currentBackgroundYOffset = (currentTab + 1) * 38 * -1;
		$(this).parent().css({backgroundPosition: '0 '+ currentBackgroundYOffset + 'px'});
	}).mouseout(function(){
		$(this).parent().css({backgroundPosition: '0 '+backgroundYOffset+'px'});
	});

 	/*       Comentado para anular el enlace del panel de clasificacion TOP-10
	$('table.clasificacion').click(function(){
		var lnk = $('.dropdown li.active a').attr('href');
		location.href = lnk;
	});*/

	// link aviso legal
	$('#avisolegal a').click(function(e){
		var url = $(this).attr('href');
		var w = 450;
		var h = 430;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=yes,resizable=no';
		window.open(url,'Aviso_legal',wfeatures);
		e.stopPropagation();
		return false;
	});

	// link novedades en tu e-mail
	$('#menu-novedades a').click(function(e){
		var url = $(this).attr('href');
		var w = 480;
		var h = 380;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=no,resizable=no';
		window.open(url,'Novedades',wfeatures);
		e.stopPropagation();
		return false;
	});

	// link desde promociones.php para aputnarse a la newsletter
	$('#promo-newsletter').click(function(e){
		var url = $(this).attr('href');
		var w = 480;
		var h = 380;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=no,resizable=no';
		window.open(url,'Novedades',wfeatures);
		e.stopPropagation();
		return false;
	});

	// link bases notariales promo
	$('#bases-promocion').click(function(e){
		var url = $(this).attr('href');
		var w = 450;
		var h = 430;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=yes,resizable=no';
		window.open(url,'Aviso_legal_promocion',wfeatures);
		e.stopPropagation();
		return false;
	});

	// link form promo
	$('a.form-promocion').click(function(e){
		var url = $(this).attr('href');
		var w = 910;
		var h = 620;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=yes,resizable=no';
		window.open(url,'Promocion',wfeatures);
		e.stopPropagation();
		return false;
	});
	
	// link ganadores promo costabrava
	$('#ganadores_promo_costabrava').click(function(e){
		var url = $(this).attr('href');
		var w = 450;
		var h = 378;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=yes,resizable=no';
		window.open(url,'ganadores_promo_costabrava',wfeatures);
		e.stopPropagation();
		return false;
	});

	// link ganadores promo shalymar
	$('#ganadores_promo_shalymar').click(function(e){
		var url = $(this).attr('href');
		var w = 450;
		var h = 378;
		var x = screen.width/2 - w/2;
		var y = screen.height/2 - h/2;
		wfeatures = 'width='+w+',height='+h+',left='+x+',top='+y+',scrollbars=yes,resizable=no';
		window.open(url,'ganadores_promo_shalymar',wfeatures);
		e.stopPropagation();
		return false;
	});

});

$(document).click(function(){
	$('ul.dropdown').css({position: 'static', display: 'inline'}).parent().css({position: 'static'});
	$('ul.dropdown').addClass('collapsed');
	$('ul.dropdown li').hide();
	$('ul.dropdown li.active').show();
});