/*	Author: Tashreek Jackson
 *	Overview: jQuery Functions.	
*/

$(document).ready(function() {	
	
	//********** Toggle Results Button Images **********//
	$(".stage-results").click(function () {
		$(this).siblings('a').removeClass('overall-results-btn-active');			
		$(this).addClass('stage-results-active');	
    });
	
	$(".overall-results-btn").click(function () {
		$(this).siblings('a').removeClass('stage-results-active');									  
		$(this).addClass('overall-results-btn-active');	
    });
						   
	//********** Slideshow Cycles **********//
	$('#intro-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#intro-gallery-controls',
		timeout: 5000
	});
	
	/*$('.gallery-items').cycle({
		fx:     'scrollHorz', 
		pager:  '#gallery-controls',
		timeout: 5000
	});*/
	
	$('#team-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#team-gallery-controls',
		timeout: 5000,
		after:   onAfter1
	});
	
	$('#team2-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#team2-gallery-controls',
		timeout: 5000,
		after:   onAfter2
	});
	
	$('#team3-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#team3-gallery-controls',
		timeout: 5000,
		after:   onAfter3
	});
	
	$('#team4-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#team4-gallery-controls',
		timeout: 5000,
		after:   onAfter4
	});
	
	$('#team5-gallery-items').cycle({ 
		fx:     'scrollHorz', 
		pager:  '#team5-gallery-controls',
		timeout: 5000,
		after:   onAfter5
	});
	
	// Gallery Caption events
	function onAfter1() {$('#team-driver').html('<a><strong>' + this.alt + '</strong></a>'); }
	function onAfter2() {$('#team2-driver').html('<a><strong>' + this.alt + '</strong></a>'); }
	function onAfter3() {$('#team3-driver').html('<a><strong>' + this.alt + '</strong></a>'); }
	function onAfter4() {$('#team4-driver').html('<a><strong>' + this.alt + '</strong></a>'); }
	function onAfter5() {$('#team5-driver').html('<a><strong>' + this.alt + '</strong></a>'); }
	
	// this allows the page to scroll with easing
	$.localScroll();
	
	//Stage Results Utility 
	//var stagesResults = $("div#results-box").html()
	$(".results-boxes").each(function () {
		$(this).parent().children("#stored-results").html($(this).html());
	});

	$(".overall-results-btn").click(function () {									  
		var q = window.location.search.substring(1);
		if (q != '') q = '?'+q;
		var stagediv = this;
      	$.ajax({
		  url: '/dakar/overall_results/'+q,
		  cache: false,
		  success: function(html){
			$(stagediv).parent().parent().parent().children("div#results-box").html(html);
		  }
		});
    });
	
	$(".stage-results").click(function () {
		//$("div#results-box").html(stagesResults);
		results_parent = $(this).parent().parent().parent();
		results_parent.children("div#results-box").html(results_parent.children("#stored-results").html());
    });
	
	//Stage Pagination
	$(".stagelist2").hide();
	$(".prev-stage").hide();
	$(".next-stage").click(function () {
		$(".stagelist1").hide();
		$(".stagelist2").show();
		$(".next-stage").hide();
		$(".prev-stage").show();
		return false;
    });
	$(".prev-stage").click(function () {
		$(".stagelist1").show();
		$(".stagelist2").hide();
		$(".next-stage").show();
		$(".prev-stage").hide();
		return false;
    });
	
	//Gallery Pagination
	$(".gallerylist2").hide();
	$(".prev-gallery").hide();
	$(".next-gallery").click(function () {
		$(".gallerylist1").hide();
		$(".gallerylist2").show();
		$(".next-gallery").hide();
		$(".prev-gallery").show();
		return false;
    });
	$(".prev-gallery").click(function () {
		$(".gallerylist1").show();
		$(".gallerylist2").hide();
		$(".next-gallery").show();
		$(".prev-gallery").hide();
		return false;
    });
	
	// tabbing system
	$("#race-tabs").tabs("#race-content > .race-item", {effect: 'fade'});
	$("#team-tabs").tabs("#team-content > .team-item", {effect: 'fade'});
	$("#stage-tabs").tabs("#stage-container > .stage", {effect: 'fade'}).history();
	$("#gallery-tabs").tabs("#gallery-container > .gallery-block", {effect: 'fade'}).history();
	//$("#gallery-tabs").tabs("#gallery-container > #gallery", {effect: 'fade'});
		
	$('.accordion').accordion();

});