(function($) {

	var opt 		= new Array;
	var played		= new Array;
	var oimg		= new Array;
	var odet		= new Array;
	var olink		= new Array;
	var currentpos	= new Array;
	var autoInterval = new Array;
	var scrollInterval = new Array;
	var in_animate = false;
	var moveScroll = false;
	var acceleration = 0;
	var imgLength;
	var mousePlace;
	 
	$.fn.ssslider= $.fn.ssslider = function(options){
		
		init = function(tg){
			opt[tg.id] = $.extend({}, $.fn.ssslider.defaults, options);
			played[tg.id]		= new Array();
			oimg[tg.id]			= new Array();
			odet[tg.id]			= new Array();
			olink[tg.id]		= new Array();
			currentpos[tg.id]	= 0;

			$('#'+opt[tg.id].idSlide).find('img').remove(); //Hide All images
			$('#'+tg.id).css('width', $('#'+tg.id+' li').length*opt[tg.id].PLwidth+'px');
			$('#'+tg.id+' li').find('img').css('opacity', 0.60);
			
			$.each($('#'+tg.id+' li'), function(i,item){ //grab the data for each image & description.
				played[tg.id][i]	= $(item);
				oimg[tg.id][i] 		= $(item).find('.timg').text();
				odet[tg.id][i] 		= $(item).find('.dfors').html();
				olink[tg.id][i] 	= $(item).find('.dlink').text();
			});
			
			$.LoadAllImages(tg);
		};
		
		$.firstPlay = function(tg){
			in_animate = true;
			
			$('#'+opt[tg.id].idSlide).css('background-image', 'none');//remove the preloader image
			$('#'+opt[tg.id].idSlide).find('li').css('display', 'block');
			$('#'+opt[tg.id].idSlide+' li').find('img').css('display', 'block');
			$('.timg, .dfors').remove();
			played[tg.id][0].find('img').animate({opacity : 1.00}, opt[tg.id].animateSpeed);
			played[tg.id][0].addClass('marked');
			$('#'+opt[tg.id].idSlide+' ul').animate({ left : '0px'}, {queue:false, duration:opt[tg.id].animateSpeed, easing: 'easeInOutExpo'});
			in_animate = false;
			
			$.scrollLists(tg); //scrolling function
			$.handlerClick(tg); //handle the item click
							
			if( opt[tg.id].autoPlay ) //if autoplay == true, do the animation.
			$.transition(tg);
		}
		
		$.scrollLists = function(tg){

			$('#sp_slider').mouseover(function(e){
				mousePlace = 'next';
				if(!moveScroll){
					scrollInterval[tg.id] = setInterval(function() { $.scrollPlay(tg, mousePlace); }, 30);
					moveScroll = true;
				}
			});
			
			$('#sn_slider').mouseover(function(e){
				mousePlace = 'previous';
				if(!moveScroll){
					scrollInterval[tg.id] = setInterval(function() { $.scrollPlay(tg, mousePlace); }, 30);
					moveScroll = true;
				}
			});
			
			$('#sp_slider,#sn_slider').bind('mouseleave', function()
			{ 
				clearInterval( scrollInterval[tg.id] ); 
				moveScroll = false;
				acceleration = 0;
			});
			
			$('#sp_slider,#sn_slider').bind('click', function()
			{
				return false;
			});
	
		};
		
		$.scrollPlay = function(tg, mousePlace){
			var movement = 0, modifier = 10, leftNow = parseInt($('#'+tg.id).css('left'));

			acceleration = acceleration <= 2 ? acceleration + 0.5 : acceleration;
				if(mousePlace == 'next'){	
					movement = 180;
					posNow = leftNow + (movement/modifier);
							
					if(leftNow < 0)
					{	
						if (posNow > 0) posNow = 0;
						$('#'+tg.id).css({left:posNow + "px"});
					}
				} else if(mousePlace == 'previous') {	
					movement = 180;
					posNow = leftNow + (movement/modifier * -1);
							
					if((leftNow * -1) < (opt[tg.id].PLwidth * $('#'+tg.id+' li').length) - opt[tg.id].Lwidth){
						if (posNow * -1 > (opt[tg.id].PLwidth * $('#'+tg.id+' li').length) - opt[tg.id].Lwidth) posNow = opt[tg.id].Lwidth - (opt[tg.id].PLwidth * $('#'+tg.id+' li').length);		
						$('#'+tg.id).css({left:posNow + "px"});
					}
				} else { acceleration = 0; }

		};
		
		// transitions
		$.transition = function(tg){
			autoInterval[tg.id] = setInterval(function() { $.tp(tg) }, opt[tg.id].delay);		
		};
		
		//animate process
		$.tp = function(tg, direction){

				if(typeof(direction) == "undefined")
					currentpos[tg.id]++;
				else
					currentpos[tg.id] = direction;

				if(currentpos[tg.id] == played[tg.id].length ) {
					currentpos[tg.id] = 0;
				}	

				if(currentpos[tg.id] == -1){
					currentpos[tg.id] = played[tg.id].length-1;
				}
				
				in_animate = true;
				
				$('.marked').find('img').animate({opacity : 0.6}, opt[tg.id].animateSpeed);
				$('.marked').removeClass('marked');
				played[tg.id][currentpos[tg.id]].find('img').animate({opacity : 1.00}, opt[tg.id].animateSpeed);
				played[tg.id][currentpos[tg.id]].addClass('marked')
				$('#'+opt[tg.id].idSlide+' ul').animate({left : '-' + opt[tg.id].Swidth*(currentpos[tg.id]) + 'px'}, {queue:false, duration:opt[tg.id].animateSpeed, easing: 'easeInOutExpo'});
				
				if($('#'+tg.id+' li').length > 4 ){
					if(!moveScroll){
						if(currentpos[tg.id] > 3){
							$('#'+tg.id).animate({'left' : '-' + opt[tg.id].PLwidth*(currentpos[tg.id]-3) + 'px'}, {queue:false, duration:opt[tg.id].animateSpeed, easing: 'easeInOutExpo'});
						}
						if(currentpos[tg.id] < 4 ){
							$('#'+tg.id).animate({'left' : '0px'}, {queue:false, duration:opt[tg.id].animateSpeed, easing: 'easeInOutExpo'});
						}
						if(currentpos[tg.id] == played[tg.id].length-1 ){
							$('#'+tg.id).animate({'left' : opt[tg.id].Lwidth - (opt[tg.id].PLwidth * $('#'+tg.id+' li').length)+'px'}, {queue:false, duration:opt[tg.id].animateSpeed, easing: 'easeInOutExpo'});
						}
					}
				}
				in_animate = false;
		};
		
		$.LoadAllImages = function(tg){

			imgLength = $('#'+tg.id+' li').length;
			$('#'+opt[tg.id].idSlide).append('<ul id="slide-play"></ul>');
			
				$('#'+tg.id+' li').each(function(i){
					var img = new Image();
					var imgSrc = oimg[tg.id][i];
					
					$(img).css('display', 'none').load(function(){
						$(img).addClass('semi_img').addClass('image_ke'+i).appendTo($('body'));

						if( $('.semi_img').length == imgLength ){
							
							for(k=0;k<imgLength;k++){
								var topo = $('.image_ke'+k);
								var inDdsc = '<div id="inlight'+k+'"></div>';
								
								if( odet[tg.id][k] ){
									inDdsc = '<div id="inlight'+k+'" class="feat_desc">'+odet[tg.id][k]+'</div>';
								}
								
								$('#slide-play').append('<li style="display:none;">'+inDdsc+'</li>');
								
								if( olink[tg.id][k] ){
									$(topo).clone().insertBefore($('#inlight'+k)).wrap('<a class="sslink" href="'+olink[tg.id][k]+'"></a>');
								} else {
									$(topo).clone().insertBefore($('#inlight'+k));
								}
								topo.remove();
							}

							$('#slide-play').css({ width : opt[tg.id].Swidth*($('#'+tg.id+' li').length) + 'px', left : opt[tg.id].Swidth + 'px'});
							//Cufon.replace('.slider_det h2');
							$.firstPlay(tg);	
						}
					}).attr('src', imgSrc);
	
				});
		};

		
		$.handlerClick = function(tg){
			
			$.each($('#'+tg.id+' li'), function(i,it){
				$(it).click( function(e){
					e.preventDefault();
					if( !in_animate ){

						$.tp(tg,i);

						if( opt[tg.id].autoPlay ){
							clearInterval(autoInterval[tg.id]);
							$.transition(tg);
						}
					}
				});
			});
			
			if( opt[tg.id].autoPlay )
				if(opt[tg.id].hoverPause)$.pause(tg);
		};
		
		$.pause = function(tg){
			$('#'+opt[tg.id].idSlide+'').hover(function(){
				clearInterval(autoInterval[tg.id]);
			}, function(){
				clearInterval(autoInterval[tg.id]);
				$.transition(tg);
			});
		}

		this.each (
			function(){ init(this); }
		);


	};
	
	// default values
	$.fn.ssslider.defaults = {	
		delay : 5000, // delay between div/object
		animateSpeed : 900, //speed on amimation per div/object
		idSlide: "",
		Swidth : 930, //width of slider in pixels
		Lwidth : 640,
		PLwidth : 160,
		hoverPause : true,
		autoPlay : true
	};	
	
})(jQuery);
