var ScrollIt = new Class({

    options: {
		container			: null,
		leftHandle			: null,
		rightHandle			: null,
		navcounter			: null,
		separator			: "",
		startFrom			: 0,
		paddingLeft			: 0,
		paddingTop			: 0,
		duration			: 500,
		size				: [0,0],
		mousewheel			: false,
		mousewheelReverse	: false,
		scrollType			: "horizontal" // for future application of horizontal / vertical
	},
	
	initialize: function(options){
		this.setOptions(options)
	
		this.scrollFx		= null; 
		this.navIndex		= null;
		this.navTotal 		= null;
		this.container 		= $(this.options.container) || null;
		this.leftHandle 	= this.options.left;
		this.rightHandle 	= this.options.right;
		this.separator 		= this.options.separator || "&nbsp;/&nbsp;";
		this.startFrom 		= this.options.startFrom;
		this.duration		= this.options.duration;
		this.mousewheel		= this.options.mousewheel;
		this.scrollType		= (this.options.scrollType.toLowerCase() == "vertical") ? "vertical" : "horizontal";
		this.paddingLeft	= this.options.paddingLeft;
		this.paddingTop		= this.options.paddingTop;
		this.size 			= this.options.size;
		this.index 			= 0;
		this.numItems 		= 0;
		this.maskWidth 		= 0;
		this.maskHeight		= 0;
		this.offset			= 0;
		this.zeroLead		= "0000000000000000"; // enough to cover even the most big photo gallery! (I HOPE!!!)
		this.scrollDiv 		= null;
		
		if (this.container != null){
			this.maskWidth 			= this.size[0];
			this.maskHeight			= this.size[1] || "auto";
			this.numItems 			= this.container.getChildren().length;
			
			if (this.scrollType == "horizontal") var width = (this.numItems * this.maskWidth)  + (this.numItems * this.paddingLeft);
			else var height = (this.numItems * this.maskHeight) + (this.numItems * this.paddingTop);

			// create subContainer div
			this.scrollDiv = new Element("div",{"id":"scrolldiv","style":"width:"+this.maskWidth+"px,height:"+this.maskHeight+"px"}),
						
			// moving all children of container into scrollDiv
			this.scrollDiv.adopt(this.container.getChildren());
			
			// injecting scrolDiv into DOM
			this.container.adopt(this.scrollDiv);
			
			// create scroll Effect
			this.scrollFx 				= new Fx.Tween(this.scrollDiv,{duration:this.duration});
			this.scrollFx.running		= false;
			this.scrollFx.classHandle	= this;
						
			// set divs properties
			this.container.setStyle("width",this.maskWidth)
			this.container.setStyle("height",this.maskHeight)
			this.container.setStyle("overflow","hidden");
			
			if (this.scrollType == "horizontal") this.scrollDiv.setStyle("width",width )
			else this.scrollDiv.setStyle("height",height )

			this.scrollDiv.setStyle("overflow","hidden");

/******* NAVIGATION BUTTONS **********/

			// navigation buttons
			if(this.leftHandle = $(this.leftHandle)) {
				var eventBinded = this.prev.bind(this);
				this.leftHandle.set("href","javascript:void(0)");
				this.leftHandle.addEvent("click",eventBinded);
				this.leftHandle.store("status",true);
				this.leftHandle.store("href",this.leftHandle.getProperty("href"));
			}
			
			if(this.rightHandle = $(this.rightHandle)) {
				var eventBinded = this.next.bind(this);
				this.rightHandle.set("href","javascript:void(0)");
				this.rightHandle.addEvent("click",eventBinded);
				this.rightHandle.store("status",true);
				this.rightHandle.store("href",this.rightHandle.getProperty("href"));
			}
			
			// navigation counter
			if ((navBar = $(this.options.navcounter)) && (this.numItems > 1)) {
				this.navIndex	= new Element("span",{"id":this.options.navcounter+"-navIndex","class":"navcounter","html":this.getNavigationIndex()});
				this.navTotal 	= new Element("span",{"id":this.options.navcounter+"-navTotal","class":"navcounter","html":this.numItems});
				navBar.adopt(
							 this.navIndex,
							 new Element("span",{"html":this.separator}),
							 this.navTotal
							 );
			}

/******* /NAVIGATION BUTTONS **********/

/******* EVENTS ASSIGNMENT **********/
			this.scrollFx.addEvent("complete",function(){
				this.running = false;
				if (this.classHandle.onMotionFinished) this.classHandle.onMotionFinished();
			});
			

			if (this.mousewheel){
				this.onMouseWheelBinded = this.onMouseWheel.bind(this);
				this.container.addEvent("mousewheel",this.onMouseWheelBinded);
			}
			
/******* /EVENTS ASSIGNMENT **********/

			// ---> START APPLICATION
			if (this.numItems > 1){
				if (this.startFrom > 0) this.goTo(this.startFrom);
				this.checkButtons();
			} else this.hideNavigationButtons();
			
			
			// setting float of elements
			if (this.scrollType == "horizontal") {
				this.scrollDiv.getChildren().each(function(item,index){
													item.setStyle("float","left");
													item.setStyle("overflow","hidden");
													item.setStyle("width",this.maskWidth - this.paddingLeft);
													item.setStyle("padding-left",this.paddingLeft);
												},this);
			} else {
				this.scrollDiv.getChildren().each(function(item,index){
													item.setStyle("overflow","hidden");
													item.setStyle("height",this.maskHeight - this.paddingTop);
													item.setStyle("padding-top",this.paddingTop);
												},this);
				
				}
			
		}
		else this.hideNavigationButtons();
	},
	
	prev: function(){
		this.goTo(this.index-1);
	},
		
	next: function(){
		this.goTo(this.index+1);
	},
		
	last: function(){
		this.goTo(this.numItems-1);
	},
	
	first: function(){
		this.goTo(0);
	},

	goToPage: function(index){
		this.goTo(index-1);
	},
	
	goTo: function(index){
		if(this.scrollFx != null && !this.scrollFx.running && !isNaN(index) && index >= 0 && index <= this.numItems-1){
			this.scrollFx.running = true;
			this.index = index;			
			this.offset = -(((this.scrollType == "horizontal") ? this.maskWidth : this.maskHeight) * index) ;
			this.scrollFx.start((this.scrollType == "horizontal") ? "margin-left": "margin-top",this.offset);
			this.checkButtons();
			if (this.navIndex != null) this.navIndex.set("html",this.getNavigationIndex());
		}		
	},

	
	checkButtons: function(){
		// back button
		if (this.leftHandle != null) {
			if (this.index <= 0) this.deactivateButton(this.leftHandle);
			else if (this.leftHandle.retrieve("status") == false) this.activateButton(this.leftHandle);
		}
		
		// next button
		if (this.rightHandle != null) {
			if (this.index >= this.numItems-1) this.deactivateButton(this.rightHandle);
			else if (this.rightHandle.retrieve("status") == false) this.activateButton(this.rightHandle);
		}
	},
	
	activateButton: function(button){
		if (button != null) {
			button.setProperty("href",button.retrieve("href"));
			button.removeClass("inactive");	
			button.store("status",true);
		}
	},
	
	deactivateButton: function(button){
		if (button != null) {
			button.removeProperty("href");
			button.addClass("inactive");
			button.store("status",false);
		}
	},
	
	hideNavigationButtons: function(){
		if(this.leftHandle = $(this.leftHandle)) this.leftHandle.setStyle("display","none");
		if(this.rightHandle = $(this.rightHandle)) this.rightHandle.setStyle("display","none");		
	},
	
	getNavigationIndex: function(){		
		return new String(this.zeroLead+(this.index+1)).slice(-(new String(this.numItems).length));
	},
/******* EVENTS DECLARATION **********/
	onMouseWheel: function(e){
		if(e.wheel > 0) (this.options.mousewheelReverse) ? this.next() : this.prev();
		else (this.options.mousewheelReverse) ? this.prev() : this.next();
	}
/******* /EVENTS DECLARATION **********/
});
ScrollIt.implement(new Options, new Events);