/*
Script: slideBox-v1.0.js
	slideBox - slideBox version 1.0

License:
	MIT-style license.

Copyright:
	Copyright 2008
	Trebbers
	http://www.trebbers.nl

Based on MooTools v1.2.1 Core
	[The MooTools production team](http://mootools.net/developers/).

More information @ www.mootools.nl or http://mootools.trebbers.nl

Keep smiling ;)
*/

var slideBox = new Class({

	Implements: [Events, Options],

	options: {
		className:'slideBox',
		prevArrow:'-previous',
		nextArrow:'-next',
		removeArrows:true,
		fadeArrows:false,
		startOpacity:0.5,
		endOpacity:1,
		mouseoverBox:true,
		startClass:'normal',
		endClass:'over',
		speed:5,
		transition:Fx.Transitions.Quart.easeOut
	},

	initialize: function(element,options){
		element = $(element);
		this.setOptions(options);		
		this.active = false;

		this.up = element.getElements('div[class=' + this.options.className + '-previous]');
		this.up = this.up[0].getElements('a');
		this.up = this.up[0];
		
		this.down = element.getElements('div[class=' + this.options.className + '-next]');
		this.down = this.down[0].getElements('a');
		this.down = this.down[0];
		
		this.wrapper = element.getElements('div[class=' + this.options.className + '-wrapper]');
		this.wrapperH = this.wrapper[0].getStyle('height').toInt();
		
		this.slider = this.wrapper[0].getElements('div[class=' + this.options.className + '-slider]');
		this.slider = this.slider[0];
		
		
		if(this.options.removeArrows) this.removeArrows();
		if(this.options.fadeArrows) this.fadeArrows();
		if(this.options.mouseoverBox) this.mouseoverBox();
		
		this.clickEvent(element);
	},
	
	removeArrows: function() {
		this.start = this.slider.getStyle('top').toInt();			
		if(this.start==0)
		 this.up.getParent().setStyle('display','none');
		else
		 this.up.getParent().setStyle('display','block');
		this.height = this.slider.getSize().y;
		//this.last = ((this.height/this.wrapperH)-1)*this.wrapperH;
		if (this.height <= this.wrapperH)
		 this.down.getParent().setStyle('display','none');
		else
		 this.down.getParent().setStyle('display','block');
		 //alert(this.last+" - "+this.wrapperH);
		//alert(JSON.encode(this.slider.getCoordinates()));
	},
	
	fadeArrows: function(){
		this.up.setStyle('opacity',this.options.startOpacity);
		this.down.setStyle('opacity',this.options.startOpacity);
		
		this.up.addEvent('mouseenter', this.up.fade.bind(this.up,[this.options.endOpacity]));
		this.down.addEvent('mouseenter', this.down.fade.bind(this.down,[this.options.endOpacity]));
		
		this.up.addEvent('mouseleave', this.up.fade.bind(this.up,[this.options.startOpacity]));
		this.down.addEvent('mouseleave', this.down.fade.bind(this.down,[this.options.startOpacity]));
	},
	
	mouseoverBox: function(){
		$$('.' + this.options.className + '-slider UL LI').each(function(element,index){
			element.addClass(this.options.startClass);
			element.addEvent('mouseenter',function(){
				element.addClass(this.options.endClass);
				element.removeClass(this.options.startClass);
			}.bind(this));
			element.addEvent('mouseleave',function(){
				element.addClass(this.options.startClass);
				element.removeClass(this.options.endClass);
			}.bind(this));
		}.bind(this));
	},
	
	setArrows: function() {
		this.current = this.slider.getStyle('top').toInt();
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);
		if(this.current==0) { 
			this.up.getParent().setStyle('display','none');
			this.down.getParent().setStyle('display','block');
		} else if(this.current > this.last) {
			this.up.getParent().setStyle('display','block');
			this.down.getParent().setStyle('display','block');
		} else {
			this.up.getParent().setStyle('display','block');
			this.down.getParent().setStyle('display','none');
		}
	},
	
	clickEvent: function(element){		
		this.height = this.slider.getSize().y;		
		this.slideFx = new Fx.Tween(this.slider,{
			duration:(this.options.speed*100),
			transition:this.options.transition,
			wait:false,
			onComplete:function(){
				this.active = false;
				if(this.options.removeArrows) this.setArrows();
			}.bind(this)
		});
		
		this.up.addEvent('click',function(e){
			var e = new Event(e).stop();
			if(this.active==false) {
				this.scrollUp();
			}
		}.bind(this));
		
		this.down.addEvent('click',function(e){
			var e = new Event(e).stop();
			if(this.active==false) {
				this.scrollDown();
			}
		}.bind(this));
	},
	
	scrollDown: function(){		
		this.now = this.slider.getStyle('top').toInt();		
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);
		if(this.now > this.last) {
			this.active = true;
			this.slideFx.start('top',(this.slider.getStyle('top').toInt()-this.wrapperH)+'px');
		}
	},
	
	scrollUp: function() {
		this.now = this.slider.getStyle('top').toInt();
		this.last = 0-(((this.height/this.wrapperH)-1)*this.wrapperH);		
		if(this.now < 0) {
			this.active = true;
			this.slideFx.start('top',(this.now+this.wrapperH)+'px');
		}
	}

});
