displayImg = 'stage';
timer      = 2000;
currImg    = 0;
prevImg    = imageSlides.length - 1;
nextImg    = 1;
delay      = null;
pauseAtStart = false;
var paused = false;


function init() {
    if (document.getElementById) {
        slideShow(currImg);
        
        $("#customPortfolio .pause").show();
        
    }
}

function thumbFocus() {
    prevImg = (currImg ? currImg-1 : imageSlides.length-1);
    if (currImg > (imageSlides.length-1))
	{
		nextPage();
	}
	else
	{
		nextImg = currImg+1;
	}
	
    for (var i=0;i<imageSlides.length;i++) {
		
	if (i == currImg) 
	{
		className = 2;
		document.getElementById("thumb_"+i).style.clip = 'rect(16px, 72px, 52px, 38px)';
	}
	
	else if (i == nextImg && i>0)
	{	
		className = 1;
		document.getElementById("thumb_"+i).style.clip = 'rect(18px, 70px, 50px, 40px)';
	} 
	else
	{
		className = 3;
		document.getElementById("thumb_"+i).style.clip = 'rect(18px, 70px, 50px, 40px)';
	}

        document.getElementById("slide_"+i).className = 'thumb_' + className;
		document.getElementById("slide_"+i).style.display = 'block';
    }
}

function slideShow(newImg) {
    clearTimeout(delay);
    currImg = newImg;
    nextImg = currImg;

    thumbFocus();
    
    if( imageSlides[currImg] )
    {
    
	    
	
	    document.getElementById(displayImg).src = imageSlides[currImg].src;
	    document.getElementById(displayImg).width = imageSlides[currImg].width;
	    document.getElementById("footer").style.clear = "both";
		
		//Detect IE5.5+
		var version=0;
		if (navigator.appVersion.indexOf("MSIE")!=-1)
		{
			temp=navigator.appVersion.split("MSIE");
			version=parseFloat(temp[1]);
			
			if (version==0)
			{
				document.getElementById("content").style.marginBottom = imageSlides[currImg].height +  'px';
			}
			
			
		}
		
	 
	
		//for (var i=0;i<imageSlides.length;i++) {
			//document.getElementById("slide_"+i).style.display = 'block';
		//}
	
		document.getElementById("loading").style.display = 'none';
	
		
		
		if( $("#customPortfolio").length )
		{
			var elements= CB_Cookie.get('customPortfolio');
			var inCustomPortfolio = false;
			if(elements && elements.length)
			{
				for(i in elements) 
				{
					if( elements[i]["id"] == imageSlides[currImg].imageId )
					{
						inCustomPortfolio = true;
						break;
					}
				}
			}
			
			if( inCustomPortfolio )
			{
				$("#customPortfolio a.addItem").hide();
				$("#customPortfolio a.removeItem").show();
			}
			else
			{
				$("#customPortfolio a.addItem").show();
				$("#customPortfolio a.removeItem").hide();
			}
		}
		
    }
	
    if( ! ( pauseAtStart ) )
    {
    	delay = setTimeout('slideShow(nextImg)',timer);
    	// restart();
    	
    }
    else
    {
    	// pause();
    	pauseAtStart = false;
    }
    
}

function pause() {
    clearTimeout(delay);
    delay = null;

}

function restart() {
	if(!paused)
	{
		delay = setTimeout('slideShow(nextImg)',timer);
    
		$("#customPortfolio .pause").show();
		$("#customPortfolio .resume").hide();
	}
}

function updateButtons()
{
	
}

$(window).load( function() {
	
	$("#customPortfolio a.addItem").click( 
		function()
		{
			// CB_Cookie.del('customPortfolio');
			var element = {
					href 		: window.location.href.toString() ,
					current 	: currImg ,
					id			: imageSlides[currImg].imageId ,
					src			: imageSlides[currImg].src
			};
			
			var elements= CB_Cookie.get('customPortfolio');
			if( !elements )
			{
				elements = new Array();
			}
			elements[ elements.length ] =  element;
			CB_Cookie.set('customPortfolio', elements);
			
			
			
			// $.get( "lightbox.php?ajax=addCustomPortfolioItem&id="+imageSlides[currImg].imageId+"&current="+currImg );
			
			$("#customPortfolio a.addItem").hide();
			$("#customPortfolio a.removeItem").show();			
			
			imageSlides[currImg].inCustomPortfolio = true;
			return false;
		}
	);
	$("#customPortfolio a.removeItem").click(
		function ()
		{
			var elements= CB_Cookie.get('customPortfolio');
			if( elements && elements.length )
			{
				for( i in elements)	if( elements[i].id == imageSlides[currImg].imageId )
				{
					elements.splice(i,1);
					break;
				}
			}
			CB_Cookie.set('customPortfolio', elements);
			
			
			
			// $.get( "lightbox.php?ajax=removeCustomPortfolioItem&id="+imageSlides[currImg].imageId );
			
			$("#customPortfolio a.addItem").show();
			$("#customPortfolio a.removeItem").hide();			
			
			imageSlides[currImg].inCustomPortfolio = false;
			return false;
		}
	);
	// pause portfolio when user moves over "add / remove to/from custom portfolio" link
	$("#customPortfolio a.removeItem,#customPortfolio a.addItem").mouseover( function()
	{
		pause();
		
	}).mouseout( function(){
		restart();
	});
	
	$("#customPortfolio a.pause").click( function()
	{
		paused = true;
		pause();
		
	    $("#customPortfolio .pause").hide();
	    $("#customPortfolio .resume").show();		
	    
		return false;
	});
	$("#customPortfolio a.resume").click( function()
	{
		paused = false;
		restart();
	    
		return false;
	});	
	
} );
