// JavaScript Document
var current_id = 0;
var image_set_info = new Array();
var title_div_el = "";
var text_div_el = "";
var image_el = "";
var image_url_el = "";
var header_url_el = "";
//var more_link_el = "";
var img_change_buffer = 3; //Seconds before the next image shows.
var auto_start_buffer = 2; //Seconds before the images/text start rotating on their own on intervals of img_change_buffer
var auto_rotate_state = false; //True if currently rotating automatically - false if not currently rotating automatically
var auto_rotate_allowed = true; //True if the script is supposed to force an auto rotate. - False if not supposed to force an auto rotate.
var time_elapsed;
var img_change_timer; // The variable containing our main image change buffer timer
var auto_start_timer;

function captureUserAction()
{
	//Each time a button is pressed	or some other action (such as a rollover) is detected then this function can be called
	//and the auto rotate state will be set to false.
	stopTimers();
	startAutoStartTimer();
}

function startImageChangeTimer()
{
	if(auto_rotate_allowed == true)
	{
		img_change_timer = setTimeout('showNextImageSet(1)', img_change_buffer*1000); //Get the next image automatically every time_to_live seconds
	}
}

function startAutoStartTimer()
{
	if(auto_rotate_allowed == true)
	{
		auto_start_timer = setTimeout('startImageChangeTimer()', auto_start_buffer*1000); //Get the next image automatically every time_to_live seconds
	}
}

function stopTimers()
{
	clearTimeout(img_change_timer);
	clearTimeout(auto_start_timer);
}

function startPage()
{
	//Dynamic stream inserted here
	image_set_info[0] = new Array();image_set_info[0]['image_url'] = "/front_image/12";image_set_info[0]['title'] = "American Indian";image_set_info[0]['department_url'] = "/department/369";image_set_info[0]['text'] = "<p>Click on the Image for Great Deals.</p>";image_set_info[1] = new Array();image_set_info[1]['image_url'] = "/front_image/17";image_set_info[1]['title'] = "Warner Brothers";image_set_info[1]['department_url'] = "/department/370";image_set_info[1]['text'] = "<p>Limited Edition Autographed Wayne Gretsky Cel</p>";image_set_info[2] = new Array();image_set_info[2]['image_url'] = "/front_image/19";image_set_info[2]['title'] = "The Gerber Knife";image_set_info[2]['department_url'] = "/department/373";image_set_info[2]['text'] = "<p>The Gerber Knife says Quality. Get your Paraframe or Multi Tool and Smile.</p>";image_set_info[3] = new Array();image_set_info[3]['image_url'] = "/front_image/16";image_set_info[3]['title'] = "Home Decor";image_set_info[3]['department_url'] = "/department/1";image_set_info[3]['text'] = "&nbsp;";image_set_info[4] = new Array();image_set_info[4]['image_url'] = "/front_image/4";image_set_info[4]['title'] = "The Kitchen";image_set_info[4]['department_url'] = "/department/3";image_set_info[4]['text'] = "<p><strong><font size=\"2\"></font></strong></p><p>For&nbsp;People who enjoy Cooking.</p>";image_set_info[5] = new Array();image_set_info[5]['image_url'] = "/front_image/10";image_set_info[5]['title'] = "Leather";image_set_info[5]['department_url'] = "/department/5";image_set_info[5]['text'] = "<p>Offers unbelievable prices on Quality Leather</p>";image_set_info[6] = new Array();image_set_info[6]['image_url'] = "/front_image/5";image_set_info[6]['title'] = "The Kitchen";image_set_info[6]['department_url'] = "/department/3";image_set_info[6]['text'] = "<p><font size=\"2\"></font></p><p>&nbsp;For the Cooking Artist</p>";image_set_info[7] = new Array();image_set_info[7]['image_url'] = "/front_image/13";image_set_info[7]['title'] = "Swiss Army Knife";image_set_info[7]['department_url'] = "/department/372";image_set_info[7]['text'] = "<p>&nbsp;Swiss Army - For when you want the best.</p>";image_set_info[8] = new Array();image_set_info[8]['image_url'] = "/front_image/6";image_set_info[8]['title'] = "Leather";image_set_info[8]['department_url'] = "/department/5";image_set_info[8]['text'] = "<p>Unbelievable prices on Quality Leather<strong>.</strong></p>";image_set_info[9] = new Array();image_set_info[9]['image_url'] = "/front_image/1";image_set_info[9]['title'] = "Knives & Swords";image_set_info[9]['department_url'] = "/department/2";image_set_info[9]['text'] = "<p>HOT&nbsp;Brands like <strong>SOG, Cold Steel, Browning&nbsp;</strong>and<strong>&nbsp;Winchester.</strong></p>";image_set_info[10] = new Array();image_set_info[10]['image_url'] = "/front_image/7";image_set_info[10]['title'] = "Home Decor";image_set_info[10]['department_url'] = "/department/1";image_set_info[10]['text'] = "<p><strong>&nbsp;</strong></p>";
	
	//These elements weren't created until their part of the page loaded so let's fill their variables now.
	title_div_el = document.getElementById('title');
	text_div_el = document.getElementById('text');
	image_el = document.getElementById('image');
	image_url_el = document.getElementById('image_url');
	header_url_el = document.getElementById('header_url');
	//more_link_el = document.getElementById('more_link');
	
	//Set our initial image set content
	updateImageSet(current_id);
	startAutoStartTimer();
	
}

function updateImageSet(id_value)
{
	current_id = id_value;
	image_el.src = image_set_info[id_value]['image_url'];
	image_url_el.href = image_set_info[id_value]['department_url'];
	header_url_el.href = image_set_info[id_value]['department_url'];
	//more_link_el.href = image_set_info[id_value]['department_url'];
	replaceNode(title_div_el, image_set_info[id_value]['title']);
	replaceNode(text_div_el, image_set_info[id_value]['text']);
}

function showNextImageSet(auto_request)
{
	if(!auto_request)
	{
		captureUserAction();
	}
	else
	{
		startImageChangeTimer();
	}
	
	if(current_id == (image_set_info.length-1))
	{
		new_id = 0;
	}
	else
	{
		new_id = current_id+1;
	}
	updateImageSet(new_id);
}

function showPrevImageSet(auto_request)
{
	if(!auto_request)
	{
		captureUserAction();
	}
	else
	{
		startImageChangeTimer();
	}
	
	if(current_id == 0)
	{
		new_id = image_set_info.length-1;
	}
	else
	{
		new_id = current_id-1;
	}
	updateImageSet(new_id);
}

function replaceNode(el, text)
{
	if(el != null)
	{
		el.innerHTML = text;
	}
}