//////////////////////////////////////////////////////
//
//	The purpose of this file is to
//	apply a hover class on mouseover
//	on the <li> elements in the nav.
//	This is because IE doesn't put
//	the pseudo class :hover on <li> elements
//
//	This is code that uses the
//	jquery javascript library (http://jquery.com/)
//
//////////////////////////////////////////////////////

//keeps track of current position
var curr = 0;
var LEFT = 'left';
var RIGHT = 'right';
var scrolling = 0;

//amount the animation should step at any one time
var step = 10; 


//get the sum of all the list elements
function getscrollingwidth()
{
	$('.scrollingnav ul li').each(function(){
		
		scrolling= this.clientWidth + scrolling;
	});
	
}

function checkbounds(direction)
{
	var screen = $('.scrollscreen').width();
	
	if(screen >= scrolling)
	{
		//$('#debug')[0].innerHTML = $('#debug')[0].innerHTML + 'screen = ' + screen + ' scrolling = ' + scrolling + ' curr = ' + curr+ ' direction = ' + direction +' <br/>' ;
		if(curr - step < 0 & direction == LEFT)
			return false;
		else if( (curr + step) > (screen - scrolling) & direction == RIGHT )
			return false;
		
	}
	else if(screen < scrolling)
	{
		//$('#debug')[0].innerHTML = $('#debug')[0].innerHTML + 'screen = ' + screen + ' scrolling = ' + scrolling + ' curr = ' + curr+ ' direction = ' + direction +' <br/>' ;
		if(screen - curr >= scrolling & direction == LEFT)
			return false;
		else if( curr + step > 0 & direction == RIGHT)
			return false;
	}
	return true;
	
}
function animateMe(direction)
{
		//$('#debug')[0].innerHTML = $('#debug')[0].innerHTML + 'animate <br/>';
		if(direction == 'left')
		{
			if(!checkbounds(LEFT) )
				return false;
			curr = curr - step;
			
		}
		else
		{
			if(!checkbounds(RIGHT) )
				return false;
			curr = curr + step;
		}
		$('.scrollingnav').animate({left: curr}, 100);
}
	
$(window).load(function() {

	$('#pane1').jScrollPane({showArrows:false, scrollbarWidth: 12, dragMinHeight: 25, dragMaxHeight: 25});
	
	if( $('.scrollingnav .nav_level3 li').size() == 0)
		$('#scrollcontainer').hide();
	
	getscrollingwidth();
	var mousedown = 0;
	var next = 0;
	var prev = 0;
	var intervalID = 0;
	
	function checkforanimate()
	{
		if(mousedown == 1 & next == 1)
		{
			curr = curr - step;
			$('.scrollingnav').animate({left: curr}, 100);
			intervalID = setInterval("animateMe('left')",90);
		}
		else if(mousedown == 1 & prev == 1)
		{
			curr = curr + step;
			$('.scrollingnav').animate({left: curr}, 100);
			intervalID = setInterval("animateMe('right')",80);
		}
	}
	
	$("body").mousedown(function()
	{
		mousedown = 1;
		checkforanimate();
	});
	$("body").mouseup(function()
	{
		clearInterval(intervalID);
		mousedown = 0;
	});
	
	$(".next").hover(function()
	{
		next = 1;
		//checkforanimate();
	},
	function()
	{
		next = 0;
		clearInterval(intervalID);
		
	});
	
	$(".prev").hover(function()
	{
		prev = 1;
	},
	function()
	{
		prev = 0;
		clearInterval(intervalID);
		
	});
	
	//mountain navigation
	$(".navtop li.selected").each(function()
	{
		var li = $(this)[0];
		var classArray = li.className.split(' ');
		for(i=0;i<classArray.length;i++)
		{
			if(classArray[i].search('bg-') != -1)
			{
				str = 'url(cmsimages/' + classArray[i] + '.jpg)';
				$("#header")[0].style.backgroundImage = str;
			}
		}
	});


	
});

