$(function(){
	
	$("#sponsors").slides({
		effect:'fade',
		fadeEasing: "easeInOutQuint",
		fadeSpeed:750,
		generatePagination:false,
		play:5000,
		prev: 'sponsor-prev',
		next: 'sponsor-next',
		container:'slides'
	});
	
	$("#feature").slides({
		effect:'fade',
		fadeSpeed:750,
		generatePagination:false,
		paginationClass: 'slider-pagination',
		currentClass: 'current',
		autoHeight:false,
		play:6000,
		crossfade:true,
		container:'slide-container',
	});
	
	$("#behind-the-wheel").slides({
		effect:'fade',
		fadeSpeed:750,
		generatePagination:false,
		paginationClass: 'slider-pagination',
		currentClass: 'current',
		autoHeight:false,
		play:6000,
		crossfade:true,
		container:'slide-container',
	});
	
	var totalEvents = $('.event-states-list li').length;
	var nextEvent = $('.event-states-list li.past-event').length-1;
	
	if (nextEvent - totalEvents < 5){
		startFrom5 = totalEvents - 5;
	} else {
		startFrom5 = nextEvent;
	}	
	
	// OVERRIDE: forcing start at beginning, will
	// need to reset this logic when events start
	// rolling out.
	$('#tour-dates #carousel-5').jCarouselLite({
		btnNext: "#tour-next",
		btnPrev: "#tour-prev",
		visible: 5,
    // start: startFrom5,
		start: 0,
		circular: false,
		speed: 300
	});
	
	$('#gallery #carousel-circular').jCarouselLite({
		btnNext: "#gallery-next",
		btnPrev: "#gallery-prev",
		visible: 5,
		circular: true,
		speed: 300
	});
	
	
	$(".modal").fancybox({
		'width': '80',
		'height': '75%',
		'scrolling': 'yes',
		'autoScale': false,
		'padding': '0',
		'margin': '0',
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'type': 'iframe',
		'centerOnScroll': true
	});
	
	$("a.gallery").fancybox({
		'transitionIn' : 'elastic',
		'transitionOut': 'elastic',
		'speedIn': 200,
		'speedOut': 200,
		'overlayShow': false,
		'titlePosition': 'over',
		'titleFormat': function(title) {
	    	return '<span id="fancybox-title-over">' + title + '</span>';
		}
	});
	
	$('.rumble a img').jrumble({
		'rangeX': 3,
		'rangeY': 3,
		'rangeRot': 3,
		'rumbleSpeed': 80
	});
	
	$(".jiggle").mouseover(function(){
		$(this).children('img.arrow').effect("bounce", { direction:'left', times:3 }, 300);
	});
	
	$(".bounce").mouseover(function(){
		$(this).children('img.arrow').effect("bounce", { direction:'up', times:2 }, 400);
	});
	
	$('#offsite-events a:not([href])').addClass('closed');
	
	// Offset the event bubble
	$('li.event-city').each(function(index){
		if ($(this).css('background-position') == '0% 50%'){
			$(this).addClass('to-right');
		} else {
			$(this).addClass('to-left');
		}	
	});
	
	// Redirect countdown
	var count = $('p.countdown span').data('count');
	countdown = setInterval(function(){
		$("p.countdown span").html(count);
		if (count == 0) {window.location = $('p.countdown span').data('url');}
		count--;
	}, 1000);
		
	$('li.event-city').each(function(){
		// options
		var distance = 10;
		var time = 250;
		var hideDelay = 200;
		var hideDelayTimer = null;
		
	    // tracker
		var beingShown = false;
		var shown = false;
		
		var trigger = $('.trigger', this);
		var popup = $('.balloon-details', this).css('opacity', 0);
		
		// set the mouseover and mouseout on both element
		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer); // stops the hide event if we move from the trigger to the popup element
			
			// don't trigger the animation again if we're being shown, or already visible
			if (beingShown || shown) {
				return;
			} else {
			beingShown = true;
			
			// reset position of popup box
			popup.css({
				bottom: 18,
				display: 'block'
			})
			
			// (we're using chaining on the popup) now animate it's opacity and position
			.animate({
				bottom: '+=' + distance + 'px',
				opacity: 1
			}, time, 'swing', function() {
				// once the animation is complete, set the tracker variables
				beingShown = false;
				shown = true;
			});
		}
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer); // reset the timer if we get fired again - avoids double animations
			
			// store the timer so that it can be cleared in the mouseover if required
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				popup.animate({
					bottom: '+=' + distance + 'px',
					opacity: 0
				}, time, 'swing', function () {
					shown = false; // once the animate is complete, set the tracker variables
					popup.css('display', 'none'); // hide the popup entirely after the effect (opacity alone doesn't do the job)
				});
			}, hideDelay);
		});
	});
	
});
