var Strgb_dropdown = Class.create();
Strgb_dropdown.prototype = {
	initialize: function(el, options) {

		this.el = $(el);
		
		this.stopper = 0;

		this.showEvent = this.show.bindAsEventListener(this);
		this.hideEvent = this.hide.bindAsEventListener(this);
		this.stopEvent = this.stop.bindAsEventListener(this);
		
		//this.childElement = this.el.select('ul.level2');
		if (this.el.childNodes[1]) {
			this.childElement = this.el.childNodes[1];
		}
		
		/*for(attribute in this.childElement) {
			//console.log(typeof(this.childElement));
			console.log("ATT: "+attribute);
			console.log("VAL: "+this.childElement[attribute]);
		}*/
		
		this.initialized = false;
		this.setOptions(options);
		
		
		if (this.el.childNodes[1]) {
			Event.observe(this.el, "mouseover", this.showEvent);
			Event.observe(this.el, "mouseout", this.hideEvent);
			
			Event.observe(this.childElement, "mouseout", this.hideEvent);
			Event.observe(this.childElement, "mouseover", this.stopEvent);
		}
		
	},

	setOptions: function(options) {
		this.options = {
			delay: 250,
			appearDuration: .25,
			disappearDuration: .25
		};
		Object.extend(this.options, options || {});
	},
	
	stop: function(e) {
		this.stopper++;
	},
	
	show: function(e) {
		this.stop();
		var myStopper = this.stopper;
		if (!this.initialized) {
			this.timeout = window.setTimeout(this.appear.bind(this,myStopper), this.options.delay);
		}
	},
	
	hide: function(e) {
		this.stop();
		var myStopper = this.stopper;

		if (this.initialized) {
			this.timeout = window.setTimeout(this.disappear.bind(this,myStopper), this.options.delay);
		}
	},
	
	appear: function(stopper) {
		if (this.stopper == stopper) {
			new Effect.BlindDown(this.childElement, { duration: this.options.appearDuration });
			this.initialized = true;
			this.stop();
		}
	},
	
	disappear: function(stopper) {
		if (this.stopper == stopper) {
			new Effect.Fade(this.childElement, { duration: this.options.disappearDuration });
			this.initialized = false;
			this._clearTimeout(this.timeout);
		}
	},
	
	_clearTimeout: function(timer) {
		clearTimeout(timer);
		clearInterval(timer);
		return null;
	}
}



Event.onReady(function() {
	$$("li.level1").each(function(link) {
		new Strgb_dropdown(link, {});
	});
});

Event.onReady(function() {
	$$("div.customkeyv-pre-nav").each(function(link) {
		new Strgb_dropdown(link, {});
	});
});