$(document).ready(function(){	
		$("#promoImages").decodeSlider( { sliderWidth: 800, perView: 5 } );
	});	

(function($) {

	$.fn.decodeSlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId:          '#slideBack',
			nextId:          '#slideForward',
			perView:         1,
			sliderWidth:     0,
			vertical:        false,
			speed:           1000,
			auto:            false,
			pause:           2000,
			continuous:      true
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var slider = $(this); 				
			var n = $("li", slider).length;
			var w = $("li", slider).width(); 
			var h = $("li", slider).height(); 
			slider.width( (options.sliderWidth > 0 ? options.sliderWidth : options.perView*w) );
			slider.height(h);
			slider.css({overflow: 'hidden'});
			var ts = Math.ceil(n/options.perView) -1;
			$('#decodeDebug').append('<p>ts = '+ ts +'</p>');
			var ls = n - (Math.floor(n/options.perView) * options.perView);
			$('#decodeDebug').append('<p>ls = '+ ls +'</p>');
			var lm = (slider.width()/options.perView)*ls*-1;
			$('#decodeDebug').append('<p>lm = '+ lm +'</p>');
			var t = 0;
			$("ul", slider).css('width',(options.sliderWidth > 0 ? n*(slider.width()/options.perView) : n*w));			
			if(!options.vertical) $("li", slider).css('float','left');
				
			$("a#"+options.nextId).click(function(){		
				animate("next",true);
				return false;
			});
			$("a#"+options.prevId).click(function(){		
				animate("prev",true);				
				return false;
			});	
/*
			$("a","#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("a","#"+options.lastId).click(function(){		
				animate("last",true);				
			});		
*/
			
			function animate(dir,clicked){
				var ot = t;		
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
			    
			    $('#decodeDebug').css({ 'clear': 'both', 'width': '100%', 'display': 'block' }).append('<p>t = '+ t +'</p>');
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(n > options.perView) {
    				if(!options.vertical) {
    					p = (t*slider.width()*-1);
    					if(t==ts && lm<0) {
    					   p = (((t-1)*slider.width())*-1)+lm;
    					}
    			         $('#decodeDebug').append('<p>p = '+ p +'</p>');
    					$("ul",slider).animate(
    						{ marginLeft: p }, 
    						speed
    					);				
    				} else {
    					p = (t*slider.height()*-1);
    					$("ul",slider).animate(
    						{ marginTop: p }, 
    						speed
    					);					
    				};
				}
				
/*
				if(!options.continuous && options.controlsFade){					
					if(t==ts){
						$("a","#"+options.nextId).hide();
						$("a","#"+options.lastId).hide();
					} else {
						$("a","#"+options.nextId).show();
						$("a","#"+options.lastId).show();					
					};
					if(t==0){
						$("a","#"+options.prevId).hide();
						$("a","#"+options.firstId).hide();
					} else {
						$("a","#"+options.prevId).show();
						$("a","#"+options.firstId).show();
					};					
				};				
*/
			};
			
		});
	  
	};

})(jQuery);







/*
$(document).ready(function() {
    document.slider.total = Math.ceil($('#sliderBed').children().length / document.slider.perView);
      
    var lastSlide = $('#sliderBed').children().length - (Math.floor($('#sliderBed').children().length / document.slider.perView) * document.slider.perView)
    if(lastSlide != 0) {
        document.slider.lastMovement = (document.slider.movement / document.slider.perView) * lastSlide;
    } else {
        document.slider.lastMovement = document.slider.movement;
    }

    document.slider.manageClickers();
    
    $('#slideBack').css({cursor: 'pointer'}).click(function() {
        var newPosition = ((document.slider.position -2) * document.slider.movement) * -1;
        $("#sliderBed").animate({ left: newPosition + "px", top: 0 }, {duration: 1000, easing: "easeOutExpo"});
        document.slider.position -= 1;
        document.slider.manageClickers();
    });
    
    $('#slideForward').css({cursor: 'pointer'}).click(function() {
        var newPosition = (document.slider.position * document.slider.movement) * -1;
        if((document.slider.position +1) == document.slider.total) {
            newPosition = (((document.slider.position -1) * document.slider.movement) + document.slider.lastMovement) * -1;
        }
        $("#sliderBed").animate({ left: newPosition + "px", top: 0 }, {duration: 1000, easing: "easeOutExpo"});
        document.slider.position += 1;
        document.slider.manageClickers();
    });

});

document.slider = {
    perView: 4,
    singleWidth: 148,
    position: 1,
    total: 0,
    movement: 630,
    manageClickers: function() {
        if(document.slider.position == 1) {
            $('#slideBack').hide();
        } else {
            $('#slideBack').show();
        }
    
        if(document.slider.position == document.slider.total) {
            $('#slideForward').hide();
        } else {
            $('#slideForward').show();
        }
    }
}
*/