﻿GScript=Object.extend({
	Fade: null
}, window.GScript || {});

var Fade=Class.create({
	elementStatus: [],
	
	mouseOverListener: null,
	mouseOutListener: null,
	
	overOpacity: 1,
	outOpacity: 0.5,
	overDuration: 0.5,
	outDuration: 0.5,
	overDelay: 0,
	outDelay: 1000,
	
	initialize: function(){
		this.registerMouseOverListener();
		this.registerMouseOutListener();
	},
	
	registerMouseOverListener: function(event){
		if(!this.mouseOverListener){
			this.mouseOverListener = (function(event){
				var param = Utility.getParam(Event.element(event), 'fade');
		        if(param != null){
		        	event.stop();
		        	var targetID = this.getTargetID(param);
					this.elementStatus[targetID] = setTimeout(function(){
						Utility.setOpacity(targetID, {
							duration: param.overDuration == null ? this.overDuration : param.overDuration,
							to: param.overOpacity == null ? this.overOpacity : param.overOpacity
						});
					}, param.overDelay == null ? this.overDelay : param.overDelay * 1000);
		        }
			}).bind(this);
			document.observe('mouseover', this.mouseOverListener);
		}
	},
	
	registerMouseOutListener: function(event){
		if(!this.mouseOutListener){
			this.mouseOutListener = (function(event){
				var param = Utility.getParam(Event.element(event), 'fade');
		        if(param != null){
		        	event.stop();
		        	var targetID = this.getTargetID(param);
					this.elementStatus[targetID] = setTimeout(function(){
						Utility.setOpacity(targetID, {
							duration: param.outDuration == null ? this.outDuration : param.outDuration,
							to: param.outOpacity == null ? this.outOpacity : param.outOpacity
						});
					}, param.outDelay == null ? this.outDelay : param.outDelay * 1000);
		        }
			}).bind(this);
			document.observe('mouseout', this.mouseOutListener);
		}
	},
	
	getTargetID: function(param){
		var targetID = param.target == null ? param.trigger.identify() : param.target.identify();
		if(this.elementStatus[targetID]){
			clearTimeout(this.elementStatus[targetID]); // a system function
		}
		return targetID;
	}
}); 


if($$('body')[0]){
	if(!GScript.Fade){
		GScript.Fade = new Fade();
	}
}
else{
	document.observe('dom:loaded', function(){
		if(!GScript.Fade){
			GScript.Fade = new Fade();
		}
	});
}

