// JavaScript Document -- handles the scroll
$(function() {
	  $('#spotlight_1').spotlight({displayTime: 4});
	});
window.onload = init;

var index = 0;
var y = 0;
var num = 100;
var over = false;
var first = true;

// adds mouseover and text change effect for scrolling
function init()
{
	num = $(".spotlight_item").length;
	//alert("total number of items" + num);
	
	$(".spotlight_item").mouseover( function (){
		//$(".on").css("color","#dd0000");
		$(".on").css("border","1px solid #dd0000");
		//removeHandlers();
	});
	$(".spotlight_item").mouseout( function (){
		$(".on").css("border","1px solid black");
	});
	$(".spotlight_items").mouseover( function (){
		over = true;
	});
	$(".spotlight_items").mouseout( function (){
		over = false;
	});
	$(".spotlight_item").eq(2).mouseover(next);
	//$(".spotlight_item").eq(2).css("border","1px solid green");
	//needed to fix ie6 image flicker problem with background images
	try {
  		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}

//remove mouseovers from certain images in scroller
function removeHandlers()
{
	$(".spotlight_item").eq(index+2).unbind("mouseover", next); 
	//$(".spotlight_item").eq(index+2).css("border","1px solid black");
	$(".spotlight_item").eq(index).unbind("mouseover", prev);
	//$(".spotlight_item").eq(index).css("border","1px solid black");
	/*var counter = 0;
	while ( counter < num)
	{
		$(".spotlight_item").eq(counter).unbind("mouseover", prev);
		$(".spotlight_item").eq(counter).unbind("mouseover", next);
		$(".spotlight_item").eq(counter).css("border","1px solid black");
		counter++;
	}*/
}

//move image in scrollbar and add handlers
function next()
{
	if ( index+1 < num-2)
	{
		removeHandlers();
		y = y-76;
		$(".spotlight_item").eq(0).animate({ 
			marginTop: ""+y+"px"
		}, 1500 );
		index++;
		if ( index == $(".spotlight_item").length-3) {
			setTimeout('$(".spotlight_item").eq(index).mouseover(prev);', 1500);
		}
		else {
			setTimeout('$(".spotlight_item").eq(index+2).mouseover(next);$(".spotlight_item").eq(index).mouseover(prev);', 1500);	
		}
	}

}

//move image in scrollbar and add handlers
function prev()
{
	if ( index-1 >= 0 )
	{
		removeHandlers();
		y = y+76;
		$(".spotlight_item").eq(0).animate({ 
			marginTop: ""+y+"px"
		}, 1500 );
		index--;
		if (index == 0){
			setTimeout('$(".spotlight_item").eq(index+2).mouseover(next);', 1500);
		}
		else{
			setTimeout('$(".spotlight_item").eq(index+2).mouseover(next);$(".spotlight_item").eq(index).mouseover(prev);', 1500);
		}
	}
}

//move image in scrollbar and add handlers
function backToTop()
{
	removeHandlers();
	y = 0;
	$(".spotlight_item").eq(0).animate({ 
		marginTop: ""+y+"px"
	}, 1500 );
	index = 0;
	var counter = 0;
	while ( counter < num)
	{
		$(".spotlight_item").eq(counter).unbind("mouseover", prev);
		$(".spotlight_item").eq(counter).unbind("mouseover", next);
		counter++;
	}
	setTimeout('$(".spotlight_item").eq(2).mouseover(next);', 1500);
}
