//  K3carousel 0.1
//  by Nicolas Janik - http://www.k3media.com
//  and Danny Turcotte - http://www.k3media.com
//  2008-09-15
//
//  Licensed under the Creative Commons Attribution 3.0 License
//  http://creativecommons.org/licenses/by/3.0/
//

var k3Carousel = Class.create();  
k3Carousel.prototype = {
 
 
	initialize: function(ul, viewportSize, options) {
		
		this.ul = $(ul);
		if(!this.ul){
			return false;
		}
		this.viewportSize = viewportSize;
		
		this.optionsDefault = $H({
			direction:'horizontal',
			nbVisible:0,
			class_previous_button:'previous_button',
			class_previous_button_disabled:'previous_button_disabled',
			class_next_button:'next_button',
			next_button_content:'Next',
			previous_button_content:'Previous',
			class_next_button_disabled:'next_button_disabled',
			class_container:'container',
			scrollInc:0
			

			
		});
		
		this.options=this.optionsDefault.merge(options);
		
		
				
		var masterNode = Builder.node("div",{id:"master_"+this.ul.id,style:'float:left;clear:both;'},[
			new Element('div',{className:this.options.get('class_previous_button'), style:'float:left;cursor:hand;cursor:pointer;'}).update(this.options.get("previous_button_content")),
			new Element("div",{className:this.options.get('class_container'), style:'float:left; overflow:hidden; position:relative; width:'+this.viewportSize+"px"}),
			new Element("div",{className:this.options.get('class_next_button'), style:'float:left;cursor:hand;cursor:pointer;'}).update(this.options.get("next_button_content"))
		]);
		Element.insert(this.ul, {after:masterNode});
		Element.insert(masterNode,{after:new Element("div",{style:"clear:both;"})});
		this.ul = Element.remove(this.ul);
		
	
		
		this.container = Element.down(masterNode,'div.' + this.options.get('class_container'));
		this.lis = this.ul.select('li');
		this.container.appendChild(this.ul);

		
		if(this.options.get("direction")=='horizontal'){
			
			Element.setStyle(this.ul,{width:"10000px", padding:"0", margin:"0",position:'relative'});
			
			this.liWidthMax = 0;
			
			this.lis.each(function(li){
				
				Element.setStyle(li,{float:"left", textAlign:"center", listStyleType:"none"});
				
				var width = Element.getWidth(li);
				if(width > this.liWidthMax){
					this.liWidthMax = width;
				}
			}.bind(this));
			x=this;
			this.nbVisible = this.calculateMaxVisibleElement();
			this.liMargin =  Math.floor(  (this.viewportSize - (this.nbVisible*this.liWidthMax))/this.nbVisible/2);


			this.lis.each(function(li){
				Element.setStyle(li,{width:this.liWidthMax+"px",paddingLeft:this.liMargin+'px',paddingRight:this.liMargin+'px'});
				var div = Builder.node("div",{});
				x=li;
				var children = li.childNodes;
				
				var a=[];
				for(var i=children.length-1; i>=0 ;  i--){
					a.push(children[i].parentNode.removeChild(children[i]));
				}
				
				for(var i=a.length-1; i>=0; i--){
					div.appendChild(a[i]);
				}
				
				li.appendChild(div);


			}.bind(this));	
			
			
			
			
		}
		
		this.carousel = new UI.Carousel("master_"+this.ul.id,{
													direction:this.options.get("direction"),
													previousButton:'.'+this.options.get('class_previous_button'),
													nextButton:'.'+this.options.get('class_next_button'),
													scrollInc:this.scrollInc()
													});
	},
	
	
	calculateMaxVisibleElement: function(){
		var maxPossible =  Math.floor(this.viewportSize / this.liWidthMax);
		if(this.options.get('nbVisible') > maxPossible || this.options.get('nbVisible') == 0){
			return maxPossible < this.lis.length ? maxPossible : this.lis.length;	
		}
		return this.options.get('nbVisible') < this.lis.length ? this.options.get('nbVisible') : this.lis.length;	
		
	},
	
	scrollInc: function(){
		var nbVisible = this.calculateMaxVisibleElement();
		
		if(this.options.get('scrollInc')==0 || this.options.get('scrollInc') > nbVisible){

			
			return nbVisible;
		}
		return 	this.options.get('scrollInc');
	
	}   
    
}
