/**
 * @author Ideatronic
 */

var Tips = new Class({
	options:{
		openMethod:'mouseenter',
		tipClassName: '.tip',
		tipContainerClassName: '.tipContainer',
		duration: 400
	},
	initialize:function(options){
		this.tips = new Array();
		this.tipsContainer = new Array();
		
		this.getTips();
		this.createFx();
		this.attachTipEvents();
	},
	getTips:function(){
		var $this = this;
		this.tips = $$(this.options.tipClassName);
		this.tips.each(function(tip,index){
			if(tip.getElement($this.options.tipContainerClassName))
				$this.tipsContainer[index] = tip.getElement($this.options.tipContainerClassName);
		});
	},
	createFx:function(){
		var $this = this;
		this.tipsContainer.each(function(tipContainer){
			var duration = $this.options.duration;
			if(window.ie){
				duration = 0;	
			}
			if(!window.ie){
				tipContainer.setStyle('opacity',0);
			}
			tipContainer.fx = new Fx.Style(tipContainer,'opacity',{wait:500,duration:duration,
				onStart:function(){
					if (this.element.display == 'block') {
						this.element.style.display = 'block';
					}					
				},
				onComplete:function(){
					if (this.element.display == 'none') {
						this.element.setStyle('display', 'none');
					}
				}
			});
		})
	},
	attachTipEvents:function(){
		var $this = this;
		this.tips.each(function(item,index){
			item.addEvent($this.options.openMethod, function(e){
				var ev = new Event(e);
				ev.stopPropagation();
				$this.tipsContainer[index].display = 'block';
				//$this.tipsContainer[index].fx.stop();
				$this.tipsContainer[index].fx.start(0,1);
			});
			item.addEvent('mouseleave',function(e){
				var ev = new Event(e);
				ev.stopPropagation();
				$this.tipsContainer[index].display = 'none';
				//$this.tipsContainer[index].fx.stop();
				$this.tipsContainer[index].fx.start(1,0);
			})
		})
	}
});
Tips.implement(new Options);
