(function($) {
	var TrueContentSlider = function(tcs, options)
	{
		var o = $.extend({}, $.fn.trueCS.defaults, options);
		$tcs = $(tcs);
		
		var $tcs_container		= $tcs.children('.trueCS_container'),
			$tcs_slides			= $tcs_container.find('li'),
			total_elems			= $tcs_slides.length;
			
			
		var vars = {
            currentSlide: 0,
            nextSlide: 0,
            totalSlides: total_elems,
            running: false,
            paused: false,
            stop: false
        };
		
		$tcs.data('trueCS:vars', vars);
		
		$tcs_container.css({
			width:	o.width+'px',
			height:	o.height+'px'
			}).show();
		$tcs_container.find('.trueCS_slides').css({
			width:	(o.width*total_elems)+'px',
			height:	o.height+'px'
			})
		$tcs_slides.css({
			width:	o.width+'px',
			height:	o.height+'px'
			});
		$tcs_slides.each(function(index, element){
			// image Element adjust Orginal
			$(element).children('.trueCS_image').css({
				position:	'absolute',
				left:		o.paddingLeft+'px',
				top:		o.paddingTop+'px'
			});
			
			// Header Element adjust Orginal
			if($(element).children('h3'))
			{
				$(element).children('h3').css({
				position:		'absolute',
				left:			(o.spacing+o.paddingLeft+$(element).children('.trueCS_image').outerWidth())+'px',
				top:			o.paddingTop+'px'
				});
			}
			
			// Content Element Adjust Orginal
			if($(element).children('.trueCS_content'))
			{
				$(element).children('.trueCS_content').css({
					position:		'absolute',
					left:			(o.spacing+o.paddingLeft+$(element).children('.trueCS_image').outerWidth())+'px',
					top:			(o.paddingTop+$(element).children('h3').outerHeight())+'px'
				});
			}
			// Saving Element Orginal Informations to Var
			$(element).children('h3, .trueCS_content').each(function(){
				var oVars = {
					pos: 	$(this).position(),
					width: 	$(this).outerWidth(),
					height:	$(this).outerHeight(),
				};
				$(this).data('oVars', oVars);	
				$(this).css('overflow', 'hidden');
			});
			$(element).children('.trueCS_image').each(function(){
				var oVars = {
					pos: 	$(this).position(),
					width: 	$(this).outerWidth(),
					height:	$(this).outerHeight(),
				};
				$(this).data('oVars', oVars);	
				$(this).css('overflow', 'hidden');
				$(this).parent().find('.trueCS_image_shadow').css({top:oVars.height+oVars.pos.top+10, left:oVars.pos.left, width:oVars.width});
			});
			// Add shadow to image
		});
		
		// Adjusting timer
		var timer = 0;
        if(o.autoSlider>0 && vars.totalSlides > 1){
            timer = setInterval(function(){ go($tcs, $tcs_slides, o, vars.nextSlide, 'next'); }, o.autoSlider);
        }
		
		if(o.pauseOnHover){
            $tcs.hover(function(){
                vars.paused = true;
                clearInterval(timer);
                timer = '';
            }, function(){
                vars.paused = false;
                //Restart the timer
                if(timer == '' && !o.autoSlide){
                    timer = setInterval(function(){ go($tcs, $tcs_slides, o, vars.nextSlide, 'next'); }, o.autoSlider);
                }
            });
        }
		
		if(o.useDirection)
		{
			$tcs.append('<div class="truecs_direction"><a class="truecs_prev"></a><a class="truecs_next"></a></div>');
			$tcs.find('.truecs_prev').live('click', function(){
				if(vars.running) return false;
                clearInterval(timer);
                timer = '';
                var goSlideNo = vars.currentSlide-1;
				if(goSlideNo<0)
					goSlideNo=vars.totalSlides-1;
				go($tcs, $tcs_slides, o, goSlideNo, 'prev');
			});
			
			$tcs.find('.truecs_next').live('click', function(){
				if(vars.running) return false;
                clearInterval(timer);
                timer = '';
				var goSlideNo = vars.currentSlide+1;
				if(goSlideNo>=vars.totalSlides)
					goSlideNo=0;
				go($tcs, $tcs_slides, o, goSlideNo, 'next');
			});
		}
		
		if(o.useNavigation)
		{
			$tcs.append('<div class="truecs_navigation"></div>');
			for(i=0; i<vars.totalSlides; i++)
				$tcs.find('.truecs_navigation').append($('<a class="truecs_navitem" rel="'+i+'"><span>'+(i+1)+'</span></a>'));
				$tcs.find('.truecs_navigation a').live('click', function(){
					if(vars.running) return false;
					if($(this).attr('rel')==vars.currentSlide) return false; 
					clearInterval(timer);
					timer = '';
					go($tcs, $tcs_slides, o, parseInt($(this).attr('rel')), '');
			});
		}
		
		var go = function($tcs, $tcs_slides, o ,no, direction)
		{
			var vars = $tcs.data('trueCS:vars');
			var firstSlide = (vars.currentSlide==vars.nextSlide)?true:false;
				
			vars.currentSlide = no;
			vars.nextSlide = no+1;
			
			if(vars.nextSlide>=vars.totalSlides)
				vars.nextSlide = 0;
			
			vars.running = true;
			var imgVars = $($tcs_slides[no]).children('.trueCS_image').data('oVars');
			$($tcs_slides[no]).children('.trueCS_image').css('clip', 'rect(0px, '+imgVars.width+'px, '+imgVars.height+'px, '+imgVars.width+'px)').stop(true,true).animate({left:'+=50', opacity:'0'},0);
			$($tcs_slides[no]).children('.trueCS_image_shadow').css('clip', 'rect(0px, '+imgVars.width+'px, 17px, '+imgVars.width+'px)').stop(true,true).animate({left:'+=50', opacity:'0'},0);
			
			if($($tcs_slides[no]).children('h3').length>0)
			{
				var h3oVars = $($tcs_slides[no]).children('h3').data('oVars');
				$($tcs_slides[no]).children('h3').css('clip', 'rect(0px, 0px, '+h3oVars.height+'px, 0px)').stop(true,true).animate({opacity:'0'},0);
			}
			
			if($($tcs_slides[no]).children('.trueCS_content').length>0)
			{
				var contoVars = $($tcs_slides[no]).children('.trueCS_content').data('oVars');
				$($tcs_slides[no]).children('.trueCS_content').css('clip', 'rect(0px, 0px, '+contoVars.height+'px, 0px)').stop(true,true).animate({opacity:'0'},0);
			}
			
			$($tcs_slides[no]).parent().stop(true,true).animate({left: (o.width*no*-1)}, 400, 'easeOutQuad', function(){
				$($tcs_slides[no]).children('.trueCS_image').stop(true,true).animate({left:'-=50', opacity:'1', clip: 'rect(0px, '+imgVars.width+'px, '+imgVars.height+'px, 0px)'}, 500, 'easeOutQuad'); 
				$($tcs_slides[no]).children('.trueCS_image_shadow').stop(true,true).animate({left:'-=50', opacity:'1', clip: 'rect(0px, '+imgVars.width+'px, 17px, 0px)'}, 500, 'easeOutQuad'); 
								
				if($($tcs_slides[no]).children('h3').length>0)
					$($tcs_slides[no]).children('h3').stop(true,true).delay(200).animate({opacity:'1', clip: 'rect(0px, '+h3oVars.width+'px, '+h3oVars.height+'px, 0px)'}, 800, 'easeOutQuad'); 
				
				if($($tcs_slides[no]).children('.trueCS_content').length>0)					
					$($tcs_slides[no]).children('.trueCS_content').stop(true,true).delay(400).animate({opacity:'1', clip: 'rect(0px, '+contoVars.width+'px, '+contoVars.height+'px, 0px)'}, 500, 'easeOutQuad'); 
				
				
				if(o.useNavigation)
				{
					$tcs.find('.truecs_navigation a').removeClass('truecs_navitem_active');
					$tcs.find('.truecs_navigation a[rel='+no+']').addClass('truecs_navitem_active');
				}
				
				vars.running = false;
			});
			
		}
		
		go($tcs, $tcs_slides, o, vars.currentSlide, 'next');
		o.afterLoad.call(this);
	}
	
	$.fn.trueCS = function(options){
        return this.each(function(key, value){
            var slider = $(this);
            if (slider.data('trueCS')) return slider.data('trueCS');
            var trueCS = new TrueContentSlider(this, options);
            slider.data('trueCS', trueCS);
        });
	};
	
	$.fn.trueCS.defaults = 
	{
		width: 960,
		height: 440,
		autoSlider: 5000,
		pauseOnHover: true,
		useDirection: true,
		useNavigation: true,
		paddingLeft: 40,
		paddingTop: 70,
		spacing:20,
		afterLoad: function(){}
	};
})(jQuery);
