//--------------------Slideshow------------------------
function initSlideShow() 
{	
	var canRotate = true;
	window.addEventListener('focus', function() {
    	canRotate = true;
    	clearInterval(timer);
	    timer = setInterval(rotate,delay);
	},false);
	
	window.addEventListener('blur', function() {
	    canRotate = false;
	    clearInterval(timer);
	},false);

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $('.window').width();
	var imageSum = $('.image_reel img').length;
	var imageReelWidth = imageWidth * imageSum;
	
	
	//add the page pointers
	for (i = 0; i < imageSum; i++)
	{
		$('.paging').append('<a href="#" rel="' + i + '">&nbsp;</a>');
	}
	var $active = $('.paging a:first');
	//Show the paging and activate its first link
	$('.paging').show();
	$('.paging a:first').addClass('active');
	$('#slideshow_label').text($('.image_reel a:eq(0)').children('.label_text').text());
	$('#slideshow_label').attr('href', $('.image_reel a:eq(0)').attr('href'));
	//Adjust the image reel to its new size
	$('.image_reel').css({'width' : imageReelWidth});
	
	var activeID = 0; 
	var targetID = 1;
	var image_reelPosition = 0; 
	var delay = 5000;
	var timer = setInterval(rotate,delay);    
	
	//Paging  and Slider Function
	function rotate()
	{
		if(canRotate == false) return;
	    image_reelPosition = targetID * imageWidth;
	    if (targetID == 0 && activeID != 0)  
	    {
	    	$('.image_reel').fadeOut(500, function()
	    	{
	    		$('.image_reel').css('left', -image_reelPosition);
	    		$('.image_reel').fadeIn(500, function()
	    		{
	    			afterRotate();
	    		});
	    	});
	    	return;
	    }
	    //Slider Animation
	    $('.image_reel').animate({
	        left: -image_reelPosition
	    }, 900, 'swing', afterRotate);
	}; 
	
	function afterRotate()
    { 
        $active = $('.paging a:eq('+targetID+')'); 
        activeID = parseFloat($active.attr('rel'));
        
		$('.paging a').removeClass('active'); 
   		$active.addClass('active'); 
		
		if ($active.next().length > 0) targetID = activeID +1;
		else targetID = 0;
		
		$('#slideshow_label').text($('.image_reel a:eq('+activeID+')').children('.label_text').text());
        $('#slideshow_label').attr('href', $('.image_reel a:eq('+activeID+')').attr('href'));
        //timer = setTimeout(rotate,5000); //Trigger the paging and slider function
    }
	
	//On Hover
	$(".main_view").hover(function() {
	    clearInterval(timer); //Stop the rotation
	}, function() {
		clearInterval(timer);
	    timer = setInterval(rotate,delay); //Resume rotation timer
	});	
	
	//On Click
	$(".paging a").click(function() {
		if (activeID == parseFloat($(this).attr('rel'))) return false;
	    targetID = parseFloat($(this).attr('rel')); //Activate the clicked paging
	   
	    //Reset Timer
	    clearInterval(timer);
	    timer = setInterval(rotate,delay);
	    rotate(); //Trigger rotation immediately
	    return false; //Prevent browser jump to link anchor
	});
}

function initWorkSlideShow() 
{
	$('.work_pic[src="null"]').remove();
	$('.work_pic').each(function(i)
	{
		$(this).attr('rel', i);
	});
	var itemWidth = 588;
	var totalWidth = $('.work_pic').length * itemWidth;
	var currentID = 0;
	var maxItems = $('.work_pic').length;
	
	for (i = maxItems -1; i >= 0; i--)
	{
		$('#work_slideshow_paging').append('<div rel="' + i + '"></div>');
	}
	$('#work_slideshow_paging div:last').addClass('active');
	
	$('#work_slideshow_rail').width(totalWidth);
	$('#work_slideshow_back_btn').hide();
	if (totalWidth == 1) 
		$('#work_slideshow_fwd_btn').hide();
	
	
	$('#work_slideshow_fwd_btn').click(onFwd);
	$('#work_slideshow_back_btn').click(onBack);
	
	function onFwd()
	{
		currentID ++;
		$('#work_slideshow_paging div').removeClass('active');
		$('#work_slideshow_paging div[rel="'+ currentID +'"]').addClass('active');
		
		if (currentID >= maxItems -1) 
			$('#work_slideshow_fwd_btn').hide();
		if (currentID >= 1) 
			$('#work_slideshow_back_btn').show();
			
		$('#work_slideshow_rail').animate(
		{
	        left: -currentID * itemWidth
	    }, 500);
	    
	    return false; //Prevent browser jump to link anchor
	}
	
	function onBack()
	{
		currentID --;
		$('#work_slideshow_paging div').removeClass('active');
		$('#work_slideshow_paging div[rel="'+ currentID +'"]').addClass('active');
		
		if (currentID <= 0) 
			$('#work_slideshow_back_btn').hide();
		if (currentID >= 1) 
			$('#work_slideshow_fwd_btn').show();
			
		$('#work_slideshow_rail').animate(
		{
	        left: -currentID * itemWidth
	    }, 500);
	    
	    return false; //Prevent browser jump to link anchor
	}
	
}
