/*
	Author: The Bridge
	Copyright (c): 2011 The Bridge
	Created: 29-11-2011
	Last Modified: 09-12-2011
	Name: Slider
	Description: Content slider with animated content
*/

(function($) {
	$.fn.bridgeSlider = function(options){
		var defaults = {			
			speed: 800,
			auto: true,
			staticPause: 8000, // Time in seconds for items that don't have an inner animate
			innerPause: 4000, // Time in seconds before the inner animation is invoked
			pause: 12000, // Time in seconds before item change
			continuous: true,
			clicks: false
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width(); 
			var h = $("li", obj).height(); 
			var clickable = true;
			var ts = s-1;
			var t = 0;
			var lastItemData = [];
			$("ul", obj).css('width',s*w);
			
			if(options.continuous){
				$("ul", obj).prepend($("ul li:last-child", obj).clone().css("margin-left","-"+ w +"px"));
				$("ul", obj).append($("ul li:nth-child(2)", obj).clone());
				$("ul", obj).css('width',(s+1)*w);
			};
			
			if(options.clicks){
				$("#arrow_right").css({'cursor' : 'pointer'}).click(function(){
					top.location.href = $("#li_"+t).attr("title");
				});
				$("li", obj).each(function(){
					$(this).css({'cursor' : 'pointer'}).click(function(){
						top.location.href = $(this).attr("title");
					});
				});
			};		
			
			function adjust(){
				if(t > ts) t=0;	
				if(t<0) t=ts;	
				$("ul",obj).css("margin-left",(t*w*-1));
				clickable = true;
				if(options.numeric) setCurrent(t);
			};
			
			function animate(dir,clicked){
				if (clickable){
					clickable = false;
					var ot = t;				
					switch(dir){
						case "next":
							t = (ot>=ts) ? (options.continuous ? t+1 : ts) : t+1;						
							break; 
						case "prev":
							t = (t<=0) ? (options.continuous ? t-1 : 0) : t-1;
							break; 
						case "first":
							t = 0;
							break; 
						case "last":
							t = ts;
							break; 
						default:
							t = dir;
							break; 
					};	
					var diff = Math.abs(ot-t);
					var speed = diff*options.speed;						
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						{ queue:false, duration:speed, complete:adjust }
					);	
					
					if(clicked) clearTimeout(timeout);
					if(options.auto && dir=="next" && !clicked){
						var duration = options.speed+options.pause;
						if(!$("#li_"+t).hasClass('slide_static')){ 
							setTimeout(function(){innerAnimate()}, options.innerPause);
						}
						else{
							duration = options.speed+options.staticPause;	
						}
						timeout = setTimeout(function(){
							animate("next",false);
						},diff*duration);
					};
				};
			};
			
			function innerAnimate(){
				if(lastItemData.length > 0) innerAnimateReset();
				var obj = $("#li_"+t+" > .slide_main");
				var obj2 = $("#li_"+t+" > .slide_text");
				var obj3 = $("#li_"+t+" > .slide_title_vert");
				lastItemData[0] = obj.width();
				lastItemData[1] = obj.height();
				lastItemData[2] = parseInt(obj.css("top"));
				lastItemData[3] = parseInt(obj.css("left"));
				lastItemData[4] = t;
				var newW = (t==3) ? 255 : 540;
				var newL = (t==3) ? (parseInt(obj.css("left"))-230) : 0;
				var percentage = 100*(newW/lastItemData[0]);
				var newH = Math.round((percentage/100)*lastItemData[1]);
				var newT = Math.round((248-newH)/2);
				
			    obj.animate({ 'width' : newW+'px', 'height' : newH+'px', 'left' : newL, 'top' : newT+'px' }, { duration: 400, queue: false });
			    obj2.animate({'left' : '480px'}, { duration: 400, queue: false });
				obj3.animate({'opacity' : '.2'}, { duration: 400, queue: false });
			};
			
			function innerAnimateReset(){
				var obj = $("#li_"+lastItemData[4]+" > .slide_main");
				var obj2 = $("#li_"+lastItemData[4]+" > .slide_text");
				var obj3 = $("#li_"+lastItemData[4]+" > .slide_title_vert");
				obj.css({ 'width' : lastItemData[0]+'px', 'height' : lastItemData[1]+'px', 'top' : lastItemData[2]+'px', 'left' : lastItemData[3]+'px' });
				obj2.css({'left' : '960px' });
				obj3.css({'opacity' : '1'});
				lastItemData = [];
			}
			
			var timeout;
			
			if(options.auto){;
				setTimeout(function(){innerAnimate()}, options.innerPause);
				timeout = setTimeout(function(){		
					animate("next",false);
				},options.pause);
			};		
		});
	};
})(jQuery);




