////////////////////
//// set speeds ////
////////////////////
var wait_to_load = 500;		// wait time to load promos after page loads
var change_speed = 500;	// fade promo speed
var rotation_speed = 6000;	// rotation speed
var link_width = 31; 		// length of the link divs + 1 for border on right

////////////////
////  paths ////
////////////////
var site_path = "http://www.southernpoliceequipment.com"
var img_path = site_path + "/images/hp_rotate/"

////////////////
//// arrays ////
////////////////
var promo_link_Array = new Array();
var promo_image_Array = new Array();

///////////////////////////////////////
//// array of promo images to show ////
///////////////////////////////////////
function build_img_array()
{
	//promo_image_Array.push(img_path + "hp_eotech.jpg");
	promo_image_Array.push(img_path + "free-police-hats.jpg");
	promo_image_Array.push(img_path + "511-multi-tool.jpg");
	promo_image_Array.push(img_path + "free-streamlight-holder.jpg");
	promo_image_Array.push(img_path + "hot-item.jpg");
	//promo_image_Array.push(img_path + "truspec-24-7-pants.jpg");
	promo_image_Array.push(img_path + "ameriglo-lightsticks.jpg");
	promo_image_Array.push(img_path + "free-511-hat.jpg");
	//promo_image_Array.push(img_path + "free-511-hat-and-knive.jpg");
	promo_image_Array.push(img_path + "coast_p5-c2.jpg");
	promo_image_Array.push(img_path + "safariland_free_hat.jpg");
	promo_image_Array.push(img_path + "511_KIWI.jpg");
	promo_image_Array.push(img_path + "free-vic-light.jpg");
	promo_image_Array.push(img_path + "free-timberland-hat.jpg");
	promo_image_Array.push(img_path + "free-otis-hat.jpg");
}

//////////////////////////////
//// array of promo links ////
//////////////////////////////
function build_link_array()
{
	//promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=86");
	promo_link_Array.push("");
	promo_link_Array.push("");
	promo_link_Array.push(site_path + "/deals/hp-streamlight-free-holder.asp");
	promo_link_Array.push(site_path + "/shop/default.asp?h=c&c=52&id=1361");
	promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=10&id=1302");
	//promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=26&bc=12");
	promo_link_Array.push("");
	promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=90&id=1300");
	promo_link_Array.push(site_path + "/shop/default.asp?h=c&c=17&id=1296");
	promo_link_Array.push("");
	promo_link_Array.push("");
	promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=95");
	promo_link_Array.push(site_path + "/shop/default.asp?h=b&b=3");
	
}

////////////////////////////////////////////
//// array holding position of rotation ////
////////////////////////////////////////////
var pos_rotation_Array = new Array();
pos_rotation_Array[0] = "0";

//////////////////
//// scroller ////
//////////////////
scrollStep = 2
timerLeft = ""
timerRight = ""
function toLeft(id)
{
	document.getElementById(id).scrollLeft = 0
}
function toRight(id)
{
	document.getElementById(id).scrollLeft = document.getElementById(id).scrollWidth
}
function scrollDivLeft(id)
{
	clearTimeout(timerRight) 
	document.getElementById(id).scrollLeft += scrollStep
	timerRight = setTimeout("scrollDivLeft('"+id+"')",10)
}
function scrollDivRight(id)
{
	clearTimeout(timerLeft)
	document.getElementById(id).scrollLeft -= scrollStep
	timerLeft = setTimeout("scrollDivRight('"+id+"')",10)
}
function stopMe()
{
	clearTimeout(timerRight) 
	clearTimeout(timerLeft)
}

///////////////////////////////////////////////////
//// start the homepage promos after page load ////
///////////////////////////////////////////////////
function hp_promos()
{
	build_img_array();
	build_link_array();
	var howmany = promo_image_Array.length - 1;
	var randomMenu = Math.floor(Math.random() * howmany);
	pos_rotation_Array.splice(0,1,randomMenu);
	var show_content = setTimeout("start_promos("+parseInt(randomMenu)+")",wait_to_load);
}

function scroll_to_active_link(pos)
{
	// scroll link into view if needed
	if(document.getElementById("blend_links"))
	{
		var max_count = promo_image_Array.length - 1;
		document.getElementById("blend_links").scrollLeft += (pos * link_width)
	}
}

//////////////////////////////////////////////////
//// call functions to load and rotate promos ////
//////////////////////////////////////////////////
function start_promos(randomMenu)
{
	show_hide_promo_divs();
	set_blenddiv_start_background(parseInt(randomMenu));
	build_promo_scroll_links(parseInt(randomMenu));
	set_blenddiv_anchors(parseInt(randomMenu));
	start_rotation(parseInt(randomMenu));
	
	scroll_to_active_link(randomMenu)
}

//////////////////////////////////////////////
//// start animation / rotation of promos ////
//////////////////////////////////////////////
function start_rotation(randomMenu)
{
	menuInterval = setInterval("rotate_promo_image("+parseInt(randomMenu)+");", rotation_speed);
}

///////////////////////
//// stop rotation ////
///////////////////////
function stop_rotate()
{
	clearInterval(menuInterval);
}

///////////////////////////////////////////
//// rotate the promo images and links ////
///////////////////////////////////////////
function rotate_promo_image(randomMenu)
{
	try
	{
		var max_count = promo_image_Array.length - 1
		var next_id = parseInt(pos_rotation_Array[0])
		if(next_id < max_count)
		{
			next_id = next_id + 1
		}
		else
		{
			next_id = 0
		}
		pos_rotation_Array.splice(0,1,next_id); 
		swap_promo_image(next_id)
	}
	catch(err)
	{
		txt="There was an error with the promotion rotator on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		alert(txt);
		stop_rotate();
	}
}

///////////////////////////////////////////////////////////////
//// swap out the promo image and update active promo link ////
///////////////////////////////////////////////////////////////
function swap_promo_image(id)
{
	// change image
	blendimage("blenddiv","blendimage",""+ promo_image_Array[id] +"",""+ change_speed +"");
	// change link
	set_blenddiv_anchors(id)
	// change promo scroll menu
	build_promo_scroll_links(id);
	// scroll link into view if needed
	scroll_to_active_link(id)
}

////////////////////////
//// set promo link ////
////////////////////////
function set_blenddiv_anchors(id)
{
	var getlink = promo_link_Array[id]
	var blenddiv_anchors = document.getElementById("blenddiv_anchors");
	blenddiv_anchors.style.display = "block";
	if(getlink != "")
	{
		//blenddiv_anchors.style.display = "block";
		document.getElementById("blenddiv_anchors").href = getlink;
	}
	else
	{
		//blenddiv_anchors.style.display = "none";
		document.getElementById("blenddiv_anchors").href = "javascript:void(0);";
	}
}

/////////////////////////////////////
//// preview promo in div window ////
/////////////////////////////////////
function promo_preview(img_pos,action,evt)
{
	var str = "";
	if(action == "show")
	{
		var getimg = promo_image_Array[img_pos];
		var y = getY(document.getElementById("hp_promo_container")) + 109;
		var x = getX(document.getElementById("hp_promo_container")) + 1;
		document.getElementById("ippreview").src = getimg
		document.getElementById("div_promo_preview").style.top = y + "px";
		document.getElementById("div_promo_preview").style.left = x + "px";
		document.getElementById("div_promo_preview").style.display = "block";
	}
	if(action == "hide")
	{
		document.getElementById("div_promo_preview").style.display = "none";
		document.getElementById("ippreview").src = "/images/page_elements/loading_promo_preview.gif"
		document.getElementById("scroller").scrollLeft = 0;
	}
}

///////////////////////////////////////////////////////
//// build scroll links to navigate between promos ////
///////////////////////////////////////////////////////
function build_promo_scroll_links(id)
{
	var i = 0;
	var max_count = promo_image_Array.length - 1;
	var set_scroll_width = (max_count + 1) * link_width;
	var str = ""
	
	if(max_count >= 12)
	{
	str += "<div id=\"scrollleft\">"
	//str += "<a href=\"javascript:void(0);\" title=\"Click to scroll left.\" onmousedown=\"scrollDivRight('blend_links');\" onmouseup=\"stopMe();\" onmouseover=\"stop_rotate();\" onmouseout=\"start_rotation();\">&lt;</a>"
	str += "<a href=\"javascript:void(0);\" title=\"Click to scroll left.\" onmousedown=\"scrollDivRight('blend_links');\" onmouseup=\"stopMe();\">&lt;</a>"
	str += "</div>"
	str += "<div id=\"scrollright\">"
	//str += "<a href=\"javascript:void(0);\" title=\"Click to scroll right.\" onmousedown=\"scrollDivLeft('blend_links');\" onmouseup=\"stopMe();\" onmouseover=\"stop_rotate();\" onmouseout=\"start_rotation();\">&gt;</a>"
	str += "<a href=\"javascript:void(0);\" title=\"Click to scroll left.\" onmousedown=\"scrollDivLeft('blend_links');\" onmouseup=\"stopMe();\">&lt;</a>"
	str += "</div>"
	str += "<div id=\"blend_links\">"
	}
	
	
	str += "<div id=\"scroller\">"
	for(i = 0; i <= max_count; i++)
	{
		str += "<div id=\"link"+ i +"\" class=\"blend_div_links\">"
		str += "<a href=\"javascript:void(0);\""
		str += " id=\"a"+i+"\" name=\"a"+i+"\""
		str += " onclick=\"javascript:promo_preview('"+i+"','hide',event);swap_promo_image(" + i + ");\""
		str += " onmouseover=\"javascript:stop_rotate();"
		//str += " onmouseover=\""
		if(i != id)
		{
			str += "promo_preview('"+i+"','show',event);"
		}
		str += "\""
		str += " onmouseout=\"javascript:start_rotation();"
		//str += " onmouseout=\""
		if(i != id)
		{
			str += "promo_preview('"+i+"','hide',event);"
		}
		str += "\""
		if(i == id)
		{
			str += " class=\"blend_active_link\""
		}
		str += " title=\"Click to view promotion.\">"
		str += i + 1
		str += "</a>"
		str += "</div>"
	}
	str += "</div>"
	str += "</div>"
	
	//alert(str)
	document.getElementById("blenddiv_links").innerHTML = str;
	document.getElementById("scroller").style.width = set_scroll_width + "px";
}

//////////////////////////////////////////////////////////////////
//// set the background image to the first image in the array ////
//////////////////////////////////////////////////////////////////
function set_blenddiv_start_background(randomMenu)
{
	document.getElementById('blenddiv').style.backgroundImage = 'url('+ promo_image_Array[parseInt(randomMenu)] +')';
	document.getElementById('blenddiv').innerHTML = "<img src=\""+ img_path + promo_link_Array[parseInt(randomMenu)] + "\" id=\"blendimage\" alt=\"\" />";
}

//////////////////////////////
//// show hide promo divs ////
//////////////////////////////
function show_hide_promo_divs()
{
	var hp_loading = document.getElementById("hp_loading");
	var blenddiv = document.getElementById("blenddiv");
	var blenddiv_links = document.getElementById("blenddiv_links");
	hp_loading.style.display = "none";
	blenddiv.style.display = "block";
	blenddiv_links.style.display = "block";
}

/////////////////////////////
//// get x y coordinates ////
/////////////////////////////
function getX(obj){
	return obj.offsetLeft + (obj.offsetParent ? getX(obj.offsetParent) : obj.x ? obj.x : 0);
}        
function getY(obj){
	return (obj.offsetParent ? obj.offsetTop + getY(obj.offsetParent) : obj.y ? obj.y : 0);
}