/*
Name: Slider
Author: Luca Dioli
Company: Ander-Web
*/

var akExtend = function(){
	var args = arguments;
	if (args.length == 1) args = [this, args[0]];
	for (var prop in args[1]) args[0][prop] = args[1][prop];
	return args[0];
};
function akClass() { }
akClass.prototype.construct = function() {};
akClass.extend = function(def) {
  var classDef = function() {
      if (arguments[0] !== akClass) { return this.construct.apply(this, arguments); }
  };
  var proto = new this(akClass);
  akExtend(proto,def);
  classDef.prototype = proto;
  classDef.extend = this.extend;      
  return classDef;
};


var slider = akClass.extend({	
	construct : function(options) {
		var defaults = {
			'sliderId' : 'slider',
			'speed' : 500,
			'sliderDirection' : 'horizontal',
			'slideFade' : false,
			'fadeArrows' : false,
			'nextArrow' : false,
			'previousArrow' : false,
			'numbers' : false,
			'index' : false,
			'autoslide' : false,
			'rewind' : false,
			'debug' : false
		};
		
		this.opts = $.extend(defaults, options);
		
    	this.sliderId = this.opts.sliderId;	
    	this.sliderObj = $('#'+this.sliderId);
    	this.sliderUl = $('#'+this.sliderId+' .itemSet');
    	
    	this.itemWidth = parseInt(this.sliderObj.css('width'));	
    	this.items = this.sliderUl.children().size();
    	this.itemsWidth = this.items * this.itemWidth;
    	
    	this.position = 0;
    	this.slide = 1;

    	this.newPosition;
    	
    	this.stop = false;
    	this.moving = false;
    	this.direction;
    	
    	this.slides = new Array(this.items);
	
	
		this.disableAutoslide = false;
	
	
		this.init();
	},
   	
   	init : function (){
   		
   		if(this.opts.sliderDirection == 'horizontal'){
   			this.sliderUl.css({'width':this.itemsWidth+'px'});
   		}
   		else{
   			this.sliderUl.css({'width':this.itemWidth+'px'});
   		}
   		
   		for(i=0;i<this.slides.length;i++){
   			this.slides[i] = (i+1);
   		}
   		
   		/*if(this.opts.previousArrow || this.opts.nextArrow){
   			this.initArrows();
		}*/

   		if(this.opts.numbers){
   			this.initNumbers();
   		}
   		
   		if(this.opts.indexOf){
   			this.setIndexOf();
   		}
   		
   		if(this.opts.autoslide){
   			this.autosliding();
   		}
   		
   	},
   	
   	initArrows : function (){
   	
   		if(this.opts.previousArrow){
   			$('#'+this.opts.previousArrow.id).attr('title',this.opts.previousArrow.title).click(function (){
   				if(!this.moving){this.previousItem()}
   			});
   		}
   		
   		if(this.opts.nextArrow){
   			$('#'+this.opts.nextArrow.id).attr('title',this.opts.nextArrow.title).click(function (){
   				if(!this.moving){this.nextItem()}
   			});
   		}
   		
   	},
   	
   	initNumbers : function (){
   		
   		var html = "";
   		var separator = (this.opts.numbers.separator) ? this.opts.numbers.separator : "&nbsp;";
   				
   		for(i=0;i<this.items;i++){
   			if(i > 0){
   				html += separator;
   			}
   						
   			if((i) == this.position){
   				 html += "<div title='"+(i+1)+"' class='on'>"+(i+1)+"</div>";
   			}
   			else{
   				html += "<div title='"+(i+1)+"'>"+(i+1)+"</div>";
   			}
   		}		
   				
   		$("#"+this.opts.numbers.id).html(html); 
   		
   		
   		var thisOpts = this.opts;
   		var t = this;
   		
   		$('#'+this.opts.numbers.id+' div').click(function (){
   			if(!t.isMoving()){
	        	var nextSlide = parseInt($(this).attr("title"));
	        	
	        	if(t.opts.rewind){
	        		t.newPosition = '-'+ ((nextSlide-1) * t.itemWidth);
	        		t.slide = nextSlide;
	        		t.position = (nextSlide-1);
	        		
	        		t.moveItem();
	        		
	        	}
        	}
        	/*else{
   				for(i=0;i<t.slides.length;i++){
   					if(t.slides[i] == 1){
   						p = i;
   						break;
   					}
   				}
   				
   				for(i=0;i<p;i++){

					var ele = $("#"+t.sliderId+" .itemSet li:first-child").remove();
					t.sliderUl.append(ele);

					ele = t.slides.shift();
					t.slides.push(ele);
   				}
   				
   				if(t.opts.sliderDirection == 'horizontal'){
   					t.sliderUl.css('left','-'+((t.slide-1)*t.itemWidth)+'px');
   				}
   				else{
   					t.sliderUl.css('top','-'+((t.slide-1)*t.itemWidth)+'px');
   				}
   				
   				t.newPosition = '-'+ ((t.nextSlide-1) * t.itemWidth);
   				
   				t.slide = t.nextSlide;
   				t.position = t.nextSlide - 1;
   				
   				t.moveItem();
        	}*/
    						
        })
   	
   	},
   	
   	setIndexOf : function(){
   		var separator = (this.opts.indexOf.separator) ? this.opts.indexOf.separator : "&nbsp;/&nbsp;";
   		var html = this.slide+''+separator+''+this.items;
   	
   		$('#'+this.opts.indexOf.id).html(html);
   	},
   	
   	selectNumber : function(){
   		$("#"+this.opts.numbers.id+" div[class=on]").removeClass();
   		$("#"+this.opts.numbers.id+" div:eq("+(this.slide-1)+")").toggleClass("on");
   	},

	autosliding : function (){
   		
   		var t = this;
   		
   		var autoslide = function (){
			if(!t.disableAutoslide){
		    	if(t.stop == false){
		    		if(t.opts.autoslide.direction && t.opts.autoslide.direction == "left"){
		    			t.previousItem();
		    		}
		    		else{
		    			t.nextItem();
		    		}
		    	}
	
	    		setTimeout(autoslide, t.opts.autoslide.timeout);
    		}
    	};
   		
    	setTimeout(autoslide, this.opts.autoslide.delay);

    	this.sliderObj.hover(function (){t.stop = true},function (){t.stop = false});
				
		if(this.opts.nextArrow){
     			$("#"+this.opts.nextArrow.id).hover(function (){t.stop = true},function (){t.stop = false});
     		}
     		
     		if(this.opts.previousArrow){
     			$("#"+this.opts.previousArrow.id).hover(function (){t.stop = true},function (){t.stop = false});
     		}
   	
   	},

   	moveItem : function (){
		
		this.moving = true;
		
   		if(this.opts.slideFade){
   			if(this.opts.sliderDirection == 'horizontal'){
   				var t = this;
    			this.sliderUl.animate({'left': this.newPosition+'px','opacity': this.opts.slideFade.minOpacity},this.opts.speed,function (){
   					t.moving = false;
   					t.stopMoving();
   				}).animate({'opacity': '1'},(this.opts.speed/2));
   			}
   			else{
   				var t = this;
   				this.sliderUl.animate({'top': this.newPosition+'px','opacity': this.opts.slideFade.minOpacity},this.opts.speed,function (){
   					t.moving = false;
   					t.stopMoving();
   				}).animate({'opacity': '1'},(this.opts.speed/2));
   			}
   		}
   		else{
   			if(this.opts.sliderDirection == 'horizontal'){
   				var t = this;
    			this.sliderUl.animate({'left': this.newPosition+'px'},this.opts.speed,function (){
    				t.moving = false;
    				t.stopMoving();
    			});
   			}
   			else{
   				var t = this;
   				sliderUl.animate({'top': this.newPosition+'px'},this.opts.speed,function (){
   					t.moving = false;
   					t.stopMoving();
   				});
   			}
   		}
		
   		if(this.opts.numbers){
   			this.selectNumber();
   		}
   		
   		if(this.opts.indexOf){
   			this.setIndexOf();
   		}
   		
   		if(this.opts.debug){
   			console.log("slide:"+this.slide);
   			console.log("position:"+this.position);
   			console.log("slides"+this.slides);
   		}
   		
   	},
   	
   	stopMoving : function () { this.moving = false },
   	
   	nextItem : function (){
		
		direction = 'next';
		
   		if((this.position+1) < this.items){
   			this.newPosition = '-='+this.itemWidth;
     			this.position++;
     			this.slide++;
   		}
   		else if(this.opts.rewind){
   			this.newPosition = '0';
     			this.position = 0;
     			this.slide = 1;
   		}
   		else{
   			this.rebuildItems('right');
   			this.newPosition = '-='+this.itemWidth;
   			this.slide++;
   		}
   		
   		if(this.slide > this.items){
   			this.slide = 1;
   		}
   		
   		this.moveItem();
   		
   	},
   	
   	previousItem : function (){
   	
   		this.direction = 'previous';
   	
   		if(this.position > 0){
   			this.newPosition = '+='+this.itemWidth;
     			this.position--;
     			this.slide--;
   		}
   		else if(this.opts.rewind){
   			this.newPosition = "-="+((this.items-1)*this.itemWidth);
     			this.position = (this.items-1);
     			this.slide = this.items;
   		}
   		else{
   			this.rebuildItems('left');
   			this.newPosition = '+='+this.itemWidth;
   			this.slide--;
   		}
   		
   		if(this.slide < 1){
   			this.slide = this.items;
   		}
   		
   		this.moveItem();
   	},
   	
   	rebuildItems : function (direction){
   		
   		var relativePosition = this.getPosition();
		
		if(direction == 'right'){
			var ele = $("#"+this.sliderId+" .itemSet li:first-child").remove();

			if(this.opts.sliderDirection == 'horizontal'){
 					this.sliderUl.css('left',(relativePosition+this.itemWidth)+'px');
 				}
 				else{
 					this.sliderUl.css('top',(relativePosition+this.itemWidth)+'px');
 				}
 				
			this.sliderUl.append(ele);
			
			ele = this.slides.shift();
			this.slides.push(ele);
			
			this.position = this.items-1;

		}
		else if(direction == 'left'){
			var ele = $("#"+this.sliderId+" .itemSet li:last-child").remove();
			
			if(this.opts.sliderDirection == 'horizontal'){
 					this.sliderUl.css('left',(relativePosition-this.itemWidth)+'px');
 				}
 				else{
 					this.sliderUl.css('top',(relativePosition-this.itemWidth)+'px');
 				}
			
			this.sliderUl.prepend(ele);
			
			ele = this.slides.pop();
			this.slides.unshift(ele);
			
			this.position = 0;
		}
   	
   	},

   	getPosition : function (){
   		if(this.opts.sliderDirection == 'horizontal'){
   			return parseInt(this.sliderUl.css('left'));
   		}
   		else{
   			return parseInt(this.sliderUl.css('top'));
   		}
   	},
   	
   	isMoving : function (){
   		return this.moving;
   	}
});
