/***
*
*  Light Collage:  
*  
***/
var LightCollage= new Class({
        //
		//  initialize
		//
		Implements: [Options,EventManager],
        options: {
                on_opacity: 1,
                off_opacity: .25,
				button_on_opacity: 1,
                button_off_opacity: 0,
				button_wait:2500,
				fade_time: 750,
				slide_time: 600,
				init_left: 0,
				init_blocks: "all",
				smoothing: 1,
				leftButton_id: "leftButton",
				rightButton_id: "rightButton",
				buttonFade: false,
				maxLeft:null,
				minLeft:null,
				speed:20
		},
        initialize: function(container,blocks,options){
                this.setOptions(options)
                this.container = document.id(container)
				this.container.width = 0
                this.blocks = $$(blocks)
                this.blocks.each(function(elm){
						var out = this.options.init_blocks != "all"
						if (out) this.isFaded == true
                        this.setblock(elm,out)
                }.bind(this))
				this.container.set("tween",{duration:this.options.slide_time})
				this.slideTo(this.options.init_left)
				if (this.options.init_blocks != "all" && this.options.init_blocks != "none") this.setCurrent(this.options.init_blocks)
						
        },
		setMax: function(num){
				this.options.maxLeft = num
		},
		setMin: function(num){
				this.options.minLeft = num
		},
        setblock: function(elm,out){
				elm.set("tween",{duration:this.options.fade_time,link:'ignore'})
				if (out) {
						elm.setStyle("opacity",this.options.off_opacity)
				}
				if (this.container.width < elm.getStyle("left").toInt() + elm.width){
						this.container.width = elm.getStyle("left").toInt() + elm.width
				} 			
        },
		addSlideButtons: function(left,right,btnbox){
				this.leftButton = new Element("div",{id:this.options.leftButton_id})
				this.rightButton = new Element("div",{id:this.options.rightButton_id})
				this.leftButton.setStyle("cursor","pointer")
				this.rightButton.setStyle("cursor","pointer")
				if (left) this.leftButton.set("html",left)
				if (right) this.rightButton.set("html",right)
				if (this.options.buttonFade){
						this.leftButton.setStyle("opacity",this.options.button_off_opacity)
						this.rightButton.setStyle("opacity",this.options.button_off_opacity)
						this.leftButton.set("tween",{duration:330,link:'ignore'})
						this.rightButton.set("tween",{duration:330,link:'ignore'})
				} else {
						this.leftButton.setStyle("opacity",this.options.button_on_opacity)
						this.rightButton.setStyle("opacity",this.options.button_on_opacity)
				}
				document.id(btnbox).grab(this.leftButton)
				document.id(btnbox).grab(this.rightButton)
				this.slideOn(btnbox)
		},
		enableSlideButtons: function(left,right,btnbox){
				this.pushEvents(this.leftButton,"mousedown",function(){
						var down = function(){
								document.id(document.body).addListener("mouseup",function(){$clear(this.mdp)}.bind(this))
								this.buttonDown(1)
						}.bind(this)
						this.mdp = down.periodical(35)
				}.bind(this),"slide_buttons")
				this.pushEvents(this.rightButton,"mousedown",function(){
						var down = function(){
								document.id(document.body).addListener("mouseup",function(){$clear(this.mdp)}.bind(this))
								this.buttonDown(-1)
						}.bind(this)
						this.mdp = down.periodical(35)
				}.bind(this),"slide_buttons")
		},
		//
		// actions
		//
		buttonDown: function(sgn){
				newx = this.container.getStyle("left").toInt() + sgn * this.options.speed
				if (this.options.maxLeft) newx = Math.min(this.options.maxLeft,newx)
				if (this.options.minLeft) newx = Math.max(this.options.minLeft,newx)
				this.container.setStyle("left",newx)
		},
		slide: function(sgn){
				var pgwidth = Browser.Engine.trident ? 900 : window.innerWidth.toInt() 
				var newx = (this.container.getStyle("left").toInt() + sgn * pgwidth * .6)
				this.slideTo(newx)
		},
		slideTo: function(newx){
				newx = newx.toInt()
				if (newx != this.container.getStyle("left").toInt()){
						if (this.options.maxLeft) newx = Math.min(this.options.maxLeft,newx)
						if (this.options.minLeft) newx = Math.max(this.options.minLeft,newx)
						this.container.tween("left",newx)
						return true
				} else {
						return false
				}
		},
		newBlocks: function(blks,lft){
				this.fadeCurrent()
				if (!!(lft || lft === 0) && this.slideTo(lft)) var delay = this.options.smoothing * this.options.slide_time
				else if (this.current) var delay = this.options.smoothing * this.options.fade_time
				else var delay = 0
				var twn_in = function(){
						this.current = []
						this.getArray(blks).each(function(elm){
								this.current.push(elm)
								elm.tween("opacity",this.options.on_opacity)
						}.bind(this))
				}.bind(this)		
				twn_in.delay(delay)
		},
		fadeCurrent: function(){
				if (this.current) {
						this.current.each(function(elm){
								elm.tween("opacity",this.options.off_opacity)
						}.bind(this))
				}
		},
		fadeAll: function(type,nobtns){
				if (type == "in" && this.isFaded){
						this.isFaded = false
						this.blocks.each(function(elm){
								elm.tween("opacity",this.options.on_opacity)
						}.bind(this))
						if (!this.options.buttonFade && !nobtns){
								this.leftButton.setStyle("opacity",this.options.button_on_opacity)
								this.rightButton.setStyle("opacity",this.options.button_on_opacity)
						}
				} else if (type != "in" && !this.isFaded) {
						this.isFaded = true
						this.blocks.each(function(elm){
								elm.tween("opacity",this.options.off_opacity)
						}.bind(this))
						this.leftButton.setStyle("opacity",0)
						this.rightButton.setStyle("opacity",0)
						
				}
				
		},
		//
		// utils
		//
		slideOn: function(btnbox){
				if (!this.isOn){
						this.isOn = true
						this.enableSlideButtons()
						if (this.options.buttonFade){
								this.pushEvents(btnbox,"mousemove",function(e){
										this.resetButtons()	
								}.bind(this),"slide_buttons")
						}
				}
		},
		slideOff: function(){
				if (this.isOn){
						this.isOn = false
						this.pullEvents("slide_buttons")
				}
		},
		resetButtons: function(){
				this.pullDelays("buttons_out")
				this.leftButton.tween("opacity",this.options.button_on_opacity)
				this.rightButton.tween("opacity",this.options.button_on_opacity)
				this.pushDelays(function() {
						this.leftButton.tween("opacity",this.options.button_off_opacity)
						this.rightButton.tween("opacity",this.options.button_off_opacity)
				},this.options.button_wait,this,"buttons_out")
		},
		getArray: function(blks){
				if ($type(blks) == "string"){
							blks = $$(blks)
				} else { 
						if ($type(blks) != "array"){
								blks = [blks]
						}
						blks.each(function(elm,idx){
								if ($type(elm) == "number") blks[idx] = this.blocks[elm]
								else if ($type(elm) == "string") blks[idx] = document.id(elm)		
						}.bind(this))
				}		
				return blks
		}
});
