/**
 * @author Administrator
 */
var OverflowBox = new Class({
	options:{
		outerBoxId:'',	//tylko jeżeli chcemy używać do id nie klas
		innerBoxId:'',  // - || -
		outerBoxClass:'.outerBox',
		innerBoxClass:'.innerBox',
		eventType:'none', // wybierz typ eventu rozwiniecia boxa - 'click', 'mouseenter','none' jeżeli none nie ma efektu rozwijania
		indicatorClass:'' // clasa obiektu pokazywanego przy wykryciu overflowa
	},
	initialize: function(options){
		this.setOptions(options);
		this.type = 'type1';
		if(this.options.eventType == 'none'){
			this.type1(); 
			return
		}
		if(this.options.eventType!='none' && this.options.indicatorClass==''){
			this.type2(); 
			return;
		}
		else 
			this.type3(); 
	},
	getObjects: function(){
		if(this.options.outerBoxId=='' || this.options.innerBox==''){
			// pobierz tylko klasy
			this.outerBox = $$(this.options.outerBoxClass);
			this.innerBox = $$(this.options.innerBoxClass);
			return;
		}
		else {
			// pobierz tylko jeden element bo id
			this.outerBox = $(this.options.outerBoxId);
			this.innerBox = $(this.options.innerBoxId);
		}
	},
	type1: function(){
		var $this = this;
		this.getObjects();
		if ($type(this.outerBox) == 'array') {
			$$(this.options.indicatorClass).each(function(item,i){
				if($this.chckOverflow($this.outerBox[i],$this.innerBox[i])[0]=='true'){
					item.setStyle('display','block')										//pokaż linka
				}
			});
			return;	
		}
		else {
			if(this.chckOverflow($this.outerBox,$this.innerBox)[0]=='true'){
				$$(this.options.indicatorClass)[0].setStyle('display','block')
			}
		}
	},
	type2: function(){
		var $this = this;
		this.getObjects();
		var lastHeight = [];
		var FX = [];
		if ($type(this.outerBox) == 'array'){
			this.outerBox.each(function(item,i){
				if ($this.chckOverflow($this.outerBox[i], $this.innerBox[i])[0] == 'true') {
					lastHeight[i] = item.getSize().size.y;
					FX[i] = new Fx.Style(item, 'height',{transition:Fx.Transitions.Expo.easeInOut});
					$this.innerBox[i].addEvent($this.options.eventType, function(){
							$this.lastIndex = i;
							FX[i].stop();
							FX[i].start($this.outerBox[i].getSize().size.y, $this.innerBox[i].getSize().size.y);
					})
					$this.outerBox[i].addEvent('mouseleave',function(){
						if(item.getSize().size.y > (lastHeight[i]+12)){
							FX[i].stop();
							FX[i].start($this.outerBox[i].getSize().size.y, lastHeight[i]);
						}
					})
				}
			});
		}
		else{
			var FX = new Fx.Style($this.outerBox.id,'height');
			$this.outerBox.addEvent(this.options.eventType,function(){
				FX.stop();
				FX.start($this.outerBox.getSize().size.y, $this.innerBox.getSize().size.y);
			})
			
		}
	},
	type3: function(){
		var $this = this;
		this.getObjects();
		var lastHeight = [];
		var indicators = $$(this.options.indicatorClass);
		var FX = [];
		if ($type(this.outerBox) == 'array') {
			this.outerBox.each(function(item, i){
				if ($this.chckOverflow($this.outerBox[i], $this.innerBox[i])[0] == 'true') {
					lastHeight[i] = item.getSize().size.y;
					FX[i] = new Fx.Style(item, 'height',{transition:Fx.Transitions.Expo.easeInOut});
					indicators[i].setStyle('display', '');
					indicators[i].addEvent($this.options.eventType, function(){
						if($this.outerBox[i].getSize().size.y < $this.innerBox[i].getSize().size.y){
							$this.lastIndex = i;
							FX[i].stop();
							FX[i].start($this.outerBox[i].getSize().size.y, $this.innerBox[i].getSize().size.y);
						}
					});
					$this.outerBox[i].addEvent('mouseleave',function(){
						if($this.lastIndex == i && item.getSize().size.y > (lastHeight[i]+12)){
							FX[i].stop();
							FX[i].start($this.outerBox[i].getSize().size.y, lastHeight[i]);
						}
					})
				}
			});
		}
		else {
			
			if(this.chckOverflow($this.outerBox,$this.innerBox)[0]=='true'){
				$$(this.options.indicatorClass)[0].setStyle('display','');
				var FX = new Fx.Style($this.outerBox, 'height');
				indicators[0].addEvent(this.options.eventType,function(){
					FX.stop();
					FX.start($this.outerBox.getSize().size.y, $this.innerBox.getSize().size.y);
				})
			}
		}
	},
	chckOverflow: function(outer,inner){
		if (outer.getSize().size.y < inner.getSize().size.y) {
			var diff = inner.getSize().size.y - outer.getSize().size.y;
			return ['true',diff];
		}
		else return ['false',0]
	}
});
OverflowBox.implement(new Options);

