function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var itemWidth = 0;
var navWidth = 0;
var currIndex = 0;
var currPos = 0;
var initPos = 0;
var leftTimeout;
var rightTimeout;

function onInit()
{	
	$("#folioNav").css("left", 0);
	
	$("#scLeft").mouseover(scRight);
	$("#scLeft").mouseout(function(){clearTimeout(rightTimeout)});
	
	$("#scRight").mouseover(scLeft);
	$("#scRight").mouseout(function(){clearTimeout(leftTimeout)});
	
	$("#folioNav").children("li").each(function(i){
		itemWidth = $(this).width();
		navWidth += (itemWidth+14);
	});
	
	$("#folioNav").children("li").mouseover(showNav);
	$("#folioNav").children("li").mouseout(hideNav);
	
	$("#folioNav").width(navWidth);
	/*
	if (navWidth < 587)
	{
		$("#scLeft").children("img").attr("src","/img/arr_left_off.gif");
	}
	$("#scRight").children("img").attr("src","/img/arr_right_off.gif");
	*/
	
	$("#folioNav").children("li").children("ul").children("li").children("a").each(function(i){
		if( $(this).attr("href") == window.location.href ) 
		{
			$(this).attr("class","active");
			$(this).parent().parent().parent().attr("class","active");
		}
	});
	
	$("#folioNav").children("li").filter(".active").children("ul").show();
	
	if (parseInt(readCookie("retouchingPos")) < 0) currPos = parseInt(readCookie("retouchingPos"));
	
	$("#folioNav").css("left", currPos);
}

function showNav()
{
	$(this).children("ul").show();
}

function hideNav()
{
	$(this).children("ul").hide();
	$("#folioNav").children("li").filter(".active").children("ul").show();
}

function scLeft()
{
	if (currPos > (-navWidth + 577))
	{
		$("#scLeft").children("img").attr("src","/img/arr_left.gif");
		
		currPos = parseInt($("#folioNav").css("left").split('px')[0]);
	
		$("#folioNav").css("left",function(){
			currPos -= 3;
			$("#folioNav").css("left", currPos+'px');
		});
		createCookie("artdirectionPos", currPos);
		leftTimeout = setTimeout(scLeft, 10);
	}
	else
	{
		$("#scRight").children("img").attr("src","/img/arr_right_off.gif");		
	}
}

function scRight()
{
	if (currPos != 0)
	{
		$("#scRight").children("img").attr("src","/img/arr_right.gif");
		
		currPos = parseInt($("#folioNav").css("left").split('px')[0]);
	
		$("#folioNav").css("left",function(){
			currPos += 3;
			$("#folioNav").css("left", currPos+'px');
		});
		
		createCookie("artdirectionPos", currPos);
		rightTimeout = setTimeout(scRight, 10);
	}
	else
	{
		$("#scLeft").children("img").attr("src","/img/arr_left_off.gif");
	}
}

$(document).ready(onInit);