/***********************************
Controls the js based fallbacks
for the flash slideshows
***********************************/

//Global speeds in ms
var auto_advance_slide_delay = 7000;
var slide_crossfade_speed = 1000;
var adjust_and_fade_animation_delay = 0;

//Global Vars
var is_idevice;
var slide_timer;
var current_slide;
var function_array = new Array();
var tar_slideshow = document.getElementById("slideshow");



jQuery(document).ready
(
	function($)
	{
		//Is the output device an iDevice?
		is_idevice = navigator.userAgent.match(/iPad/i) != null;

		//If so, slow down the framerate to 100ms (10fps)
		if (is_idevice)
			jQuery.fx.interval = 100;


		//Set up the ribbon (if a setup_ribbon functione exists)
		if (typeof(setup_ribbon) == 'function')
		{
			setup_ribbon();
		}
		else
		{
			//alert("NO RIBBON");
		}

		//Construct the nav
		construct_nav();
		
		
		//Set up click event for all slides
		$(".slide").click
		(
			function() 
			{
				document.location.href = dcr_data[current_slide][1];
			}
		);

		//Begin auto-advancing slides
		next_slide();
	}
);




function construct_nav()
{
		////////////////////////////////
		//Left Arrow
		////////////////////////////////
		var left_arrow = $("<div>" + "<img src=\"" + left_arrow_src + "\">" + "</div>");
		left_arrow.css("margin-top", "1px");
		left_arrow.click
		(
			function ()
			{
				prev_slide();
			}
		);
		$("#nav").append(left_arrow);


		////////////////////////////////
		//Slide Buttons
		////////////////////////////////
		for (var x=0; x<dcr_data.length; x++)
		{
			var t = $("<div class=\"number_button\" onclick='go_to_slide(" + x +");'>" + (x+1) + "</div>");

			$("#nav").append(t);
		}


		////////////////////////////////
		//Right arrow
		////////////////////////////////
		var right_arrow = $("<div>" + "<img src=\"" + right_arrow_src + "\">" + "</div>");
		right_arrow.css("margin-right", "0px");
		right_arrow.css("margin-top", "1px");
		right_arrow.click
		(
			function ()
			{
				next_slide();
			}
		);
		$("#nav").append(right_arrow);


		//Reset the width of the nav
		var width = (dcr_data.length + 2) * 13;
		$("#nav").css("width", width + "px");
}



//Sets up a timer to call itself every so often.
//Shows the next slide in the dcr_data array.
function auto_advance_slide()
{
	next_slide();


}

function next_slide()
{
	go_to_slide(current_slide + 1);
}

function prev_slide()
{
	go_to_slide(current_slide - 1);
}


//Go to a slide
function go_to_slide(i)
{

	clearTimeout(slide_timer);


	//If current slide is undefined then start at the beginning.
	if(typeof(current_slide) == 'undefined')
	{
		current_slide = 0;
	}
	else
	{
		//Otherwise, set it to i and bound to the length of the dcr_data array.
		if (i >= dcr_data.length)
		{
			i = 0;
		}
		else if (i < 0)
		{
			i = dcr_data.length - 1;
		}

		//Set the current slide
		current_slide = i;
	}

	try
	{
		//Get the name of the current slide (It will be something like "promo_celebrate_home_02.swf")
		function_array[current_slide]();
		

		
		
		/*
		tar_slideshow.onClick = 
		function()
		{
			alert("Monday, March 21, 2011 10:05 AM");
			document.location = dcr_data[current_slide][1];
		}
		*/
	}
	catch(err)
	{
		//Handle errors here
		//alert("Current Slide: " + current_slide + "\n" + err);
		
		tar_slideshow.innerHTML = "Current Slide: " + current_slide + " " + err + "\n<br/>" + tar_slideshow.innerHTML;
	}

	//Set the background on the currently selected button
	$(".number_button").each
	(
		function(i,e)
		{
			if (i == current_slide)
				$(this).css("background", "white");
			else
				$(this).css("background", "none");
		}
	);

	slide_timer = setTimeout(auto_advance_slide , auto_advance_slide_delay);
}




//Crossfades slides
function slide_transition(t)
{

	//Walk the list of DOM objects with the "slide" class.
	$('.slide').each
	(
		function(i,e)
		{
			//If it has an opacity greater than 0
			if ($(this).css('opacity') > 0)
			{
				//Fade it out.
				$(this).animate
				(
					{opacity: 0},
					slide_crossfade_speed,
					'swing',
					function()
					{

					}
				);
			}
		}
	);


	//Fade in the target slide
	$(t).animate
	(
		{opacity: 1},
		slide_crossfade_speed,
		'swing',
		function()
		{

		}
	);
}


//Adjusts an object`s opacity and one other named property.
function adjust_and_fade(target, property, delay_in_ms, length_in_ms, initial_prop_val, final_prop_val, initial_opacity, final_opacity)
{
	delay_in_ms += adjust_and_fade_animation_delay;

	//Set initial property
	$(target).css(property, initial_prop_val);
	$(target).css("opacity", initial_opacity);


	//Create a final proeprty value list
	var final_property_value_list =
	{
		opacity: final_opacity
	};
	final_property_value_list[property] = final_prop_val;


	//Animate the properties to their final values
	$(target).delay(delay_in_ms).animate
	(
		final_property_value_list,
		length_in_ms,
		'swing',
		function()
		{

		}
	);
}



