// JavaScript Document

 var $j = jQuery.noConflict();

$j(document).ready(function() {

	$j('#sponsor-logo').cycle({
		fx: 'fade'
	});
	
	$j(".video-div").each(function() { 
		var videoWidth = $j(this).find('object').width();
		console.log(videoWidth);
		$j(this).find(".caption").css('width', videoWidth);
	});


	var startDay = $j(".countdown-start").text().substr(0,2);
	var startMonth = $j(".countdown-start").text().substr(3,3);
	var startYear = $j(".countdown-start").text().substr(7,4);
//	console.log(Number(startDay), startMonth, startYear);
	
	switch(startMonth) {
		case "Jan":
			startMonth = 0;
			break;
		case "Feb":
			startMonth = 1;
			break;
		case "Mar":
			startMonth = 2;
			break;
		case "Apr":
			startMonth = 3;
			break;
		case "May":
			startMonth = 4;
			break;
		case "Jun":
			startMonth = 5;
			break;
		case "Jul":
			startMonth = 6;
			break;
		case "Aug":
			startMonth = 7;
			break;
		case "Sep":
			startMonth = 8;
			break;
		case "Oct":
			startMonth = 9;
			break;
		case "Nov":
			startMonth = 10;
			break;
		case "Dec":
			startMonth = 11;
			break;
		default: 
			break;
	}
	var dateRemaining = Date.UTC(Number(startYear), Number(startMonth), Number(startDay))/1000 ;




	//var daysLeft = countdown(1343390400);
	var daysLeft = countdown(dateRemaining);
	$j("#days p").html(daysLeft);
	
	
	$j('.imageScroller').each(
function( intIndex ){
	/*
	console.log($j(this));
	var wrap = $j(this).find('a').find('img'); // your span or other wrapper element
	var link = wrap.parent('a').parent();

	link.prepend(wrap);
	$j(this).find('a').remove();
	*/

	$j(this).find('img').hide(); // initially hide all the images that will appear in the gallery. <- this allows the 
									// User to still see all the images if they do not have javascript

	//$jimageList = $j(this).children().filter('img'); // Finds all the images in the div and store in the array
	$jimageList = $j(this).find('img'); // Finds all the images in the div and store in the array

	$jnumImages = $jimageList.length; // set the variables numImages equal to the number of elements found 
	
	$jnumImages--; //because we have put the images into a zero indexed array we need to make corrections to the num of children
	$jimgNum = 0; // set initial value to xero

	$j(this).append('<a href="#" class="prevImage"><span>Prev Image</span></a><a href="#" class="nextImage"><span>Next Image</span></a><p class="imageCounter"></p>'); //add the gallery controls + images counter
	$j('.imageCounter').append(" "+String($jimgNum+1)+" of "+String($jnumImages+1)); // adds the number section of the counter

	imageShow($jimageList[$jimgNum]); //set initial image to show
	if($jnumImages>0) {
	$j('.nextImage').css('opacity',0.5); //set opacity of image controls to 50%
	$j('.prevImage').css('opacity',0.5);
	} else {
		$j('.nextImage').hide();
		$j('.prevImage').hide();
		$j('.imageCounter').hide();
	}
	$j(this).find('a').hover (
		function() {
			$j(this).fadeTo("fast", 0.9); //fade in on rollover
		}, function() {
			$j(this).fadeTo("fast", 0.5); // fade out on rolloff
		}
	);
});
	

$j('.nextImage').click( function() {
	//$j(this).preventDefault();
	if($jimgNum < $jnumImages) { // if we aren't viewing the last image view the next image
		$jimgNum++;
	}	else {
		$jimgNum = 0; // otherwise we'll view the first
	}
	$j('.feature img').hide(); // hide the image
	imageShow($jimageList[$jimgNum]); // show the next image
return false;
});


$j('.prevImage').click( function() { //same as nextImage button just in reverse

	if($jimgNum > 0) {
		$jimgNum--;
	}	else {
		$jimgNum = $jnumImages;
	}
	$j('.feature img').hide();
	imageShow($jimageList[$jimgNum]);
	return false;

	});

});

imageShow = function(img) { // takes a ref to an image and displays it, and updates the image counter
	$j(img).show();
	$j('.imageCounter').empty();
	$j('.imageCounter').append(" "+String($jimgNum+1)+" of "+String($jnumImages+1));
	$j('.imageCounter').css('top', 10);
	var leftDistance = ($j(img).width()-40);
	if (leftDistance > 472) {
		leftDistance = 472;
	}
	//$j('.nextImage').css('left', ($j(img).width()-40));
	$j('.nextImage').css('left', leftDistance);
};

countdown = function(time) {
	var currentTime = new Date()
	var today = currentTime.getTime();
	var diffTime = (time - (today / 1000));
	diffTime = Math.round(diffTime/86400);
	//console.log(diffTime);
	if(diffTime > 0) {
	diffTime = diffTime + " DAYS TO GO";
	} else {
	diffTime = "WAKA RACING";	
	}
	return diffTime;
}
