/*!
 * FeaturedWorks (jQuery slider)
 * Developed for modostudio.eu
 * Copyright (c) 2011 Gian Carlo Mingati 
 * Version: 1.1 (15-NOV-2011)
 */

jQuery.extend( jQuery.easing, // from the jquery.easing plugin
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});


function featured(){

	var speed = 8000;
	//console.log(speed);
	var run = setInterval('rotate()', speed);	
	$('#featured').append('<div id="ui"><a id="l" href="#"><\/a><a id="r" href="#"><\/a><\/div>');
	var item_width = $('#featured li').outerWidth(true); 
	shuffle($('#featured li'));
	$('#featured li:last').before($('#featured li:first'));
	
	$('#featured li p').each(function(index) {
		$(this).css({'top': -8-$(this).outerHeight(true)});
	});
	$('#featured ul').css({"width" : item_width * $('#featured ul li').size(), 'left' : -item_width});
	$('#featured #ui, #featured p').css({opacity: '0.3'});
	$('#featured').hover(
	  function () {
		$(this).find('#ui, p').animate({ opacity: '1.0'}, 450, 'easeInOutExpo');
		},
	  function () {
		$(this).find('#ui, p').animate({ opacity: '0.5'}, 450, 'easeInOutExpo'); 		
		}
	);		
	$('#featured #ui a#l:not(:animated)').hover(
	  function () {
		$(this).animate({ width: '90px'}, 0, 'easeInOutExpo', function(){
			$(this).append("<img style='position: relative; top: 5px; left: 25px; width: 60px; height: 31px' src='" + $('#featured li:first').find("img").attr("src") + "'/>");
		});        
		},
	  function () {
	    $(this).children('img').remove();	
		$(this).animate({ width: '30px', left: '8px'}, 0, 'easeInOutExpo', function(){
			$('#featured #ui a#l').children('img').remove();
		});    		
		}
	);	
	$('#featured #ui a#r:not(:animated)').hover(
	  function () {
		$(this).css({'backgroundPosition':'-120px center'});
		$(this).animate({ width: '90px', left: '612px'}, 0, 'easeInOutExpo', function(){
			$(this).append("<img style='position: relative; top: 5px; left: 5px; width: 60px; height: 31px' src='" + $('#featured li:eq(2)').find("img").attr("src") + "'/>");		
		}); 	    
		},
	  function () {
	    $(this).children('img').remove();	
		$(this).animate({ width: '30px', left: '672px'}, 0, 'easeInOutExpo', function(){
			$(this).css({'backgroundPosition':'-180px center'});	
			$('#featured #ui a#r').children('img').remove();
		});   
		}
	);	
	var left_value = item_width * (-1); 
	
	$('#featured #ui a#l').click(function() {	
		var left_indent = parseInt($('#featured ul').css('left')) + item_width;    		
		$('#featured ul:not(:animated)').animate({ left: left_indent}, 900, 'easeInOutExpo', function(){	
			$('#featured li:first').before($('#featured li:last')); 
			$('#featured ul').css({'left' : left_value});
			$('#featured #ui a#l img').attr("src",$('#featured li:first').find("img").attr("src"));
		});     
		return false;
  
	});
	$('#featured #ui a#r').click(function() {
		var left_indent = parseInt($('#featured ul').css('left')) - item_width;
		$('#featured ul:not(:animated)').animate({ left: left_indent}, 900, 'easeInOutExpo', function(){
			$('#featured li:last').after($('#featured li:first'));
			$('#featured ul').css({'left' : left_value});
			$('#featured #ui a#r img').attr("src",$('#featured li:eq(2)').find("img").attr("src"));				
		});  
		return false;
	});        

	
	$('#featured').hover(
		function() {
			clearInterval(run);
		}, 
		function() {
			run = setInterval('rotate()', speed);	
		}
	); 

 /**/
};

 function rotate() {
	$('#featured #ui a#r').click();
}
function shuffle(a) {
	var i = a.size();
	while (--i) {
		var j = Math.floor(Math.random() * (i));
		var tmp = a.slice(i, i+1);
		a.slice(j, j+1).insertAfter(tmp);
	}
}
