/*
 * content slider V1.81 for tagesschau.de
 * by Sascha Kluger
 * http://www.spinningairwhale.com
*/
(function($) {
    
    // init:
    $(window).load(function() {
    $(".contentSliderNoscript").each( function() {
	    $(this).removeClass("contentSliderNoscript");
	    $(this).addClass("contentSlider");
	});
	$(".invisibleSlider").each( function() {
	    $(this).removeClass("invisibleSlider");
	    $(this).addClass("visibleSlider");
	});
	$(".contentSlider").each( function() { 
		if ($(this).children('.navi').length==0) {
			$(this).append('<div class="navi"><div class="naviLeft">&nbsp;</div><div class="naviRight">&nbsp;</div></div><div class="clearMe"></div>');
		}
	    $(this).tss();
	});
    });

    $.fn.tss = function(args) {
        var slider = new $tcs($(this));
	slider.init();
    }
    
    $.tagesContentSlider = function(destObj) {
	
	this.show =  3;
	this.viewport = destObj.find('.viewPort');
	this.navi = destObj.find('.navi');
	this.belt = this.viewport.find('.contentSliderUl');
	this.pagerC = this.viewport.find('.pager');
	this.leftButton = this.navi.find('.naviLeft');
	this.rightButton = this.navi.find('.naviRight');
	this.itemCount = this.belt.find().length;
	this.itemWidth = 162;
	this.itemBuffer = [];
	this.inAnimation = false;
	this.pager = $(document.createElement('ul'));
	this.pagerC.html('').append( this.pager );
	this._moveDest = null;
	this._currentItem = null;
	this._initalLength = 0;

	
	// buffer content, build pager, set item dimensions
	this.init = function() {
	    // this is no slider
	    try {
		this._initalLength = this.belt.children().length
		if (this.belt.children().length<=this.show) {
		    this.belt.css('left',0);
		    this.belt.width(this.belt.parent().width());
		    this.leftButton.addClass('noPfeilLeft');
		    this.rightButton.addClass('noPfeilRight');
		    return;
		}
	    } catch(e) {};
	    
	    var ref = this;
	    this.belt.width(this.belt.parent().width()+(3*this.itemWidth)+"px").children().each(
		function(i, e) {
		    ref.itemBuffer.push($(this).width(ref.itemWidth+'px').attr('rel', i));		
		    $(this).remove();	
		    ref.pager.append(
			$('<li>&nbsp;</li>').attr('rel', i).click( 
			    function() {
				ref._moveDest = parseInt($(this).attr('rel'));
				ref.moveTo();
			    }
			)
		    );	    
		}	  
	    )
	    
	    if (this._initalLength==0) return;

	    // inital view:
	    // just one more as desired - copy
	    this.itemBuffer.unshift(ref.itemBuffer.pop());
	    if (this._initalLength==this.show+1) {
		this.itemBuffer.push(this.itemBuffer[0].clone());
	    }	    
	    
	    // click left
	    this.rightButton.click(function(){ref.moveRight();});    
	    
	    // click right
	    this.leftButton.click(function(){ref.moveLeft();});    
    
	    this.buildSlider();    
	    
	};
	
	this.moveLeft = function() {
	    var ref = this;
	    if (ref.inAnimation===true) return;
	    ref.inAnimation=true;

	    if (this._initalLength==this.show+1) {
		this.itemBuffer.pop();
	    }
	    
	    ref.itemBuffer.unshift(ref.itemBuffer.pop());
	    
	    if (this._initalLength==this.show+1) {
		this.itemBuffer.push(this.itemBuffer[0].clone());
	    }
	    
	    ref.belt.animate({left:'+='+ref.itemWidth}, {complete: function() {ref.buildSlider()}});
	};

	this.moveRight = function() {
	    var ref = this;
	    if (ref.inAnimation===true) return;
	    ref.inAnimation=true;
	    
	    if (this._initalLength==this.show+1) {
		this.itemBuffer.shift();
	    }
	    
	    ref.itemBuffer.push(ref.itemBuffer.shift());
	    
	    if (this._initalLength==this.show+1) {
		this.itemBuffer.unshift(this.itemBuffer[this.itemBuffer.length-1].clone());
	    }	    
	    
	    ref.belt.animate({left:'-='+ref.itemWidth}, {complete: function() {ref.buildSlider()}});
	};
	
	this.moveTo = function() {
	    var ref = this;
	    var first = parseInt(ref.itemBuffer[1].attr('rel'));
	    var direction = (ref._moveDest>=ref._currentItem) ? 'right' : 'left';
	    
	    if (ref._moveDest==first || ref._moveDest==undefined) return false;
	    ref.inAnimation=true;
	    
	    ref.belt.animate({left: ((direction=='right') ? '-=' : '+=' )+ref.itemWidth}, {
		easing: ( (ref._moveDest-1==first || ref._moveDest+1==first)  ? 'swing' : 'linear'),
		complete: function() {
		    if (ref._moveDest!=first && ref._moveDest!=undefined) {
			ref.buildSlider();
			if (direction=='right') {
		    	    if (ref._initalLength==ref.show+1) {
				ref.itemBuffer.shift();
			    }
			    ref.itemBuffer.push(ref.itemBuffer.shift());  // right:
			    if (ref._initalLength==ref.show+1) {
				ref.itemBuffer.unshift(ref.itemBuffer[ref.itemBuffer.length-1].clone());
			    }	 			    
			}
			else {
			    if (ref._initalLength==ref.show+1) {
				ref.itemBuffer.pop();
			    }	    			    
			    ref.itemBuffer.unshift(ref.itemBuffer.pop()); // left
			    if (ref._initalLength==ref.show+1) {
				ref.itemBuffer.push(ref.itemBuffer[0].clone());
			    }			    
			}
			ref.buildSlider();	  
			ref.moveTo(true);
			return;
		    }
		},
		duration: 100
	    });

	};
	
	// build belt & pager
	this.buildSlider = function() {
	    var ref = this;
	    var relations = [];
	    ref.inAnimation=false;
	    ref.belt.html('').css('left',-ref.itemWidth);
	    for (var i=0; i<=ref.show+1; i++) {
		if (ref.itemBuffer[i]==undefined) continue;
		if (i<ref.show) relations.push(ref.itemBuffer[i+1].attr('rel'));
		ref.belt.append(ref.itemBuffer[i]);
	    }
	    
	    ref._currentItem =  parseInt(ref.itemBuffer[1].attr('rel'));
	    ref.pager.children().each(
		function(i, e) {
		    var newClass = ($.inArray($(this).attr('rel'), relations)==-1) ? 'pagerInaktiv' : 'pagerAktiv';
		    $(this).removeClass('pagerAktiv').removeClass('pagerInaktiv');
		    $(this).addClass(newClass);
		}
	    
	    )
	};

    }
    var $tcs = $.tagesContentSlider;
    $tcs.fn = $tcs.prototype;

    
})(jQuery);
