/**
* Prepare for and preload images for the carousel
*/
// number of images that have been preloaded
var loadedQty = 0;
// function to run jcarousel once all images have loaded
function imageLoaded(max, d) {
	loadedQty = loadedQty + 1;
	if (loadedQty == max) {
		try {
			$(d).find('ul').jcarousel({ wrap:'circular', auto:'3' });
			$(d).find('img').unbind('load');
		}
		catch (e) {
			$(d).prepend(e.description);
		}
	}
}
// jquery to preload the images for the carousel
$(function() {
	$('.divCallToActionTop').each(function() {
		var d = $(this);
		$(d).css({height:'250px', overflow:'hidden'});
		if (!theImages.length) {
			return false;
		}
		$(d).append('<ul></ul>');
		var max = theImages.length;
		$.each(theImages, function(key, value) {
			if (key == 0) { var s = ''; }
			else { var s = ' style="display:none;" '; }
			$(d).find('ul').append('<li style="list-style:none;"><a href="' + theLinks[key] + '"><img src="' + value + '" alt="Skins4Things" border="0" /></a></li>');
			$(d).find('img:last').load(function() {
				imageLoaded(max, d);
			});
		});
	});
});


/**
* Image preloading function from Macromedia.
* Extracted from templates on 110107 by AH, 1PCS Ltd
*/
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


/**
* Code to set up the menu. 
* Extracted from the templates on 110107 by AH, 1PCS Ltd
*/
// jquery onload func
$(document).ready
(
	function()
	{
		// set up click event handlers for menus
		$("#menu > ul > li > div > a").click(function()
		{
			// get id of current menu - next sibling element to link
			// eg:
			//    <a href="graham_menu.html">Menu1</a> - this is the link that was clicked
			//    <ul id="subMenu1"> - this is the element we want to show - the next one

			var thisMenuID = $(this).parent().next().attr("id");


			// show this menu
			showMenuWithEffect(thisMenuID);

			// set cookie with id                
			$.cookie("currentMenuID", thisMenuID);

			
		});


// set up click event handlers for menus
		$("#menu > ul > li > div > img").click(function()
		{
			// get id of current menu - next sibling element to link
			// eg:
			//    <a href="graham_menu.html">Menu1</a> - this is the link that was clicked
			//    <ul id="subMenu1"> - this is the element we want to show - the next one

			var thisMenuID = $(this).parent().next().attr("id");

			// show this menu
			// showMenuWithEffect(thisMenuID);
			toggleMenuWithEffect(thisMenuID);

			// set cookie with id                
			$.cookie("currentMenuID", thisMenuID);

	return false;

			
		});

		// check cookie for id - if not set subMenu1 by default
		var currentMenuID = $.cookie("currentMenuID");

		if(currentMenuID != null)
		{
			// show current menu
			showMenuWithEffect(currentMenuID);

			// debug out for current menu id
		   
//$("#currentMenuLabel").text($.cookie("currentMenuID"));
		}
		
	}
);

function showMenu(menuID)
{
	// hide all menus
	//$("#menu ul ul").filter("[id!='" + menuID + "']").hide();

	// show this one
	$("#" + menuID).show();
}

function showMenuWithEffect(menuID)
{
	// hide all menus
	//$("#menu ul ul").filter("[id!='" + menuID + "']").slideUp();

	// show this one
	$("#" + menuID).slideDown();
}

 function toggleMenuWithEffect(menuID)
{
	// hide all menus
	//$("#menu ul ul").filter("[id!='" + menuID + "']").slideUp();

	// show this one
	$("#" + menuID).slideToggle();
}


/***************************************************************************/
/**
* Functions taken from the catalog template for the large overlay images.
* Extracted on 110107 by AH, 1PCS Ltd
*/
<!-- Copyright 2006 Bontrager Connection, LLC -->

var cX = 0; var cY = 0;


function GetCursorPos(evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : null);

	cX = mouseX(evt);
	cY = mouseY(evt);
	
	//writetostatus("x=" + cX + ",y=" + cY + ",scrollX=" + getScrollX() + ",scrollY=" + getScrollY());
}


document.onmousemove = GetCursorPos


function getScrollX()
{
	var x = 0;

	if(window.pageYOffset)
	{
		x = window.pageXOffset;
	}
	
	else if(document.documentElement.scrollTop && document.documentElement.scrollTop > 0)
	{
		x = document.documentElement.scrollLeft;
	}
	
	else if(document.body.scrollTop)
	{
		x = document.body.scrollLeft;
	}
	
	return x;

}

function getScrollY()
{
	var y = 0;

	if(window.pageYOffset)
	{
		y = window.pageYOffset;
	}
	
	else if(document.documentElement.scrollTop && document.documentElement.scrollTop > 0)
	{
		y = document.documentElement.scrollTop;
	}
	
	else if(document.body.scrollTop)
	{
		y = document.body.scrollTop;
	}
	
	return y;

}


function mouseX(evt) 
{
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) 
    {
		if (evt.pageX) return evt.pageX;
		else if (evt.clientX)
		   return evt.clientX + (document.documentElement.scrollLeft ?
		   document.documentElement.scrollLeft :
		   document.body.scrollLeft);
		else return null;
	}
	return null;
}

function mouseY(evt) 
{
	evt = (evt) ? evt : ((window.event) ? window.event : "")
    if (evt) 
    {
		if (evt.pageY) return evt.pageY;
		else if (evt.clientY)
		   return evt.clientY + (document.documentElement.scrollTop ?
		   document.documentElement.scrollTop :
		   document.body.scrollTop);
		else return null;
	}
	return null;
}


function writetostatus(input)
{
	window.status=input
	return true
}

function AssignPosition(d) 
{

	if(cX == null || cY == null)
		return;

	var w = 0, h = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) 
	{
	    //Non-IE
	    w = window.innerWidth;
	    h = window.innerHeight;
	    
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
	    //IE 6+ in 'standards compliant mode'
	    w = document.documentElement.clientWidth;
	    h = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
	    //IE 4 compatible
	    w = document.body.clientWidth;
	    h = document.body.clientHeight;
  	}

	
	var dw = 405;
	var dy = 300;
	
	
	if((cX - getScrollX()) >= w/2)
	{
		d.style.left = (cX-30-dw) + "px";
	}
	else
	{
		d.style.left = (cX+30) + "px";
	}
	
	if((cY - getScrollY()) >= h/2)
	{
		d.style.top = (cY-40-dy) + "px";
		
	}
	else
	{
		d.style.top = (cY+40) + "px";
	}

	//d.style.left = (cX+30) + "px";
	//d.style.top = (cY+40) + "px";
}


function HideContent(d) 
{
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}

function ShowContent(d) 
{
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}

function ReverseContentDisplay(d) 
{
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") { dd.style.display = "block"; }
	else 
	{ dd.style.display = "none"; }
}

