var scrollHandle;
var startHandle;
var isAnimating;

$(document).ready(
	function()
	{
		bindHover();
		jQuery();
		$("#rightCntr p").each(
			function()
			{
				if ($(this).find("img").length < 3)
				{
					$(this).find("img").addClass("noborder").addClass("notopmargin");
				}
			}
		);
        
        if($('#slider').length != 0) 
        {
		    $("#slider .slides:not(:eq(1))").css("opacity", 0.5);
		    
        	$(window).load(
        		function()
        		{	
					startScrolling();
					setButtons();
				}
			);
        }
	}
);

//Zoom
jQuery(function($) 
{
	$('a[rel*=magnify]').magnify(
	{
		lensWidth: 160, // width of the lens
		lensHeight: 160, // height of the lens
		link: false, // clicking in the lens goes to the large image
		delay: 0 // time to wait before the lens is shown
	});
});

//hover
function bindHover()
{
	$(".hover").each(
		function()
		{
			$(this).find("img:eq(1)").hide();
		}
	);
	
	$(".hover").hover(
		function()
		{
			$(this).find("img:eq(1)").show();
			$(this).find("img:eq(0)").hide();
		},
		function()
		{
			$(this).find("img:eq(0)").show();
			$(this).find("img:eq(1)").hide();
		}
	);
}



//////////////////////////////////////////////////////
// Header scroller
//////////////////////////////////////////////////////

function startScrolling()
{
	scrollHandle = setInterval("scrollSlide('left', 2000)", 6000);	
}

function scrollSlide(sDir, iTime)
{
	if ($("#slider").queue("fx").length == 0)
	{	
		doSwitch(sDir);
		
		if (sDir == 'left')
		{

			var slideID = $("#slider .slides:eq(2)").attr("id");
	
		    $("#"+slideID).animate({opacity: 1}, iTime);
		    $("#slider .slides:not(#"+slideID+")").animate({opacity: 0.5}, iTime);
		    
			$("#slider").animate(	{left: -1580}, 
									iTime,
									function()
									{
										$("#slider .slides:first").remove();
										$("#slider").css("left", -790); 

										$("#rotator-navigation .active").removeClass('active');										
										$("#rotator-navigation ."+slideID).addClass('active');
									}
			);
	
	
		}
		else
		{
			var slideID = $("#slider .slides:eq(1)").attr("id");
	
		    $("#"+slideID).animate({opacity: 1}, iTime);
		    $("#slider .slides:not(#"+slideID+")").animate({opacity: 0.5}, iTime);
		
			$("#slider").animate(	{left: -790}, 
									iTime,
									function()
									{
										$("#slider .slides:last").remove();
										
										$("#rotator-navigation .active").removeClass('active');										
										$("#rotator-navigation ."+slideID).addClass('active');
									}
			);
		}
	}
}

function doSwitch(direction)
{
	if(direction == "right")
	{
		$("#slider .slides:last").clone().prependTo("#slider");
		$("#slider").css("left", -1580);
	}
	else
	{
		$("#slider .slides:first").clone().appendTo("#slider");
	}
}

function setButtons()
{
	$("a#btnLeftScroll").click(
		function()
		{
			scrollSlide("right", 700);
			clearInterval(scrollHandle);
			clearTimeout(startHandle);
			startHandle = setTimeout("startScrolling()", 6000);
			
			return false;
		}
	);
	
	$("a#btnRightScroll").click(
		function()
		{
			scrollSlide("left", 700);
			clearInterval(scrollHandle);
			clearTimeout(startHandle);
			startHandle = setTimeout("startScrolling()", 6000);
			
			return false;
		}
	);
}
