// Dropdown menu class
var tgmDropmenuClass = Class.create({
	className:'tgmDropmenuClass',
	visible:false,
	linkEl:null,
	foldoutEl:null,
	mainEl:null,
	currentClass:null,
	activeClass:'',
	inFoldout:false,
	closeTimer:null,

	initialize:function(mainEl, mainObject, el)
	{
		mainObject.log('tgmDropmenu initialized');

		var currentClass = mainObject.getClassesWithout(el, 'menu');
		var foldoutEl    = el.down('span.dropmenu');
		if (foldoutEl != null) {
			this.mainEl       = mainEl;
			this.foldoutEl    = foldoutEl;
			this.linkEl       = el;
			this.currentClass = currentClass;

			if (foldoutEl.getAttribute('id') == null) {
				foldoutEl.id = 'dropmenu_foldout_'+Math.round(Math.random()*10000);
			}

			// Mouse over and out
			this.linkEl.observe('mouseover', this.open.bindAsEventListener(this));
			this.linkEl.observe('mouseout', this.mouseout.bindAsEventListener(this));

			this.foldoutEl.observe('mouseover', this.foldoutMouseover.bindAsEventListener(this));
			this.foldoutEl.observe('mouseout', this.foldoutMouseout.bindAsEventListener(this));

			// Event listener for toggle element
			this.linkEl.observe('click', this.toggle.bindAsEventListener(this));

			el.show();
		}
		mainObject.observeEvent('onDocumentClick', this.close.bindAsEventListener(this));
		mainObject.observeEvent('onBeforeSuggestOpen', this.close.bindAsEventListener(this));
		mainObject.observeEvent('onDetailMenuClick', this.close.bindAsEventListener(this));
		mainObject.observeEvent('onBeforeLayerOpen', this.close.bindAsEventListener(this));
	},

	handleDocumentClick:function(e)
	{
		this.close(e);
	},

	mouseout:function(e)
	{
		var el = Event.element(e);
		if(this.closeTimer != null) {
			window.clearTimeout(this.closeTimer);
		}
		this.closeTimer = window.setTimeout(function() {
			this.close(e);
		}.bind(this), 100);
	},

	foldoutMouseover:function(e)
	{
		this.inFoldout = true;
		if(this.closeTimer != null) {
			window.clearTimeout(this.closeTimer);
		}
	},

	foldoutMouseout:function(e)
	{
		var realFoldout = Event.element(e);

		if(realFoldout.id != this.foldoutEl.id) {
			return;
		}
		this.inFoldout = false;
		if(this.closeTimer != null) {
			window.clearTimeout(this.closeTimer);
		}
		this.closeTimer = window.setTimeout(function() {			
			this.close();
		}.bind(this), 100);
	},

	open:function(e, callback)
	{
		if(this.visible) return;

		// Close all open drop menus
		tgmMain.fireEvent('onBeforeLayerOpen');
		this.linkEl.up('li').addClassName(this.activeClass);
		tgmMain.fireEvent('onBeforeDropmenuOpen');

		// Bugfix: IE has problems with alpha-tranparency
		if (Prototype.Browser.IE) {
			this.foldoutEl.show();
			this.visible = true;
			if (callback != null) {
				callback();
			}
			tgmMain.fireEvent('onDropmenuOpen');
		} else {
			tgmMain.elementAppear(this.foldoutEl, function() {
				if (callback != null) {
					callback();
				}
				this.visible = true;
				tgmMain.fireEvent('onDropmenuOpen');
			}.bindAsEventListener(this), 0.2);
		}
	},

	close:function(e, callback)
	{
		if (!this.visible) return;
		/*var el = Event.element(e);
		//alert("hallo"+el);
		// @todo: error occurs by mouseout of the dropmenu with an nolink-element
		if(el.tagName.toLowerString() == 'a') {
			if (el.hasClassName('nolink')) {
				Event.stop(e);
				return;
			}
		}*/

		this.linkEl.up('li').removeClassName(this.activeClass);
		tgmMain.fireEvent('onBeforeDropmenuClose');

		// Bugfix: IE has problems with alpha-tranparency
		if (Prototype.Browser.IE) {
			this.foldoutEl.hide();
			this.visible = false;
			if (callback != null) {
				callback();
			}
			tgmMain.fireEvent('onDropmenuClose');
		} else {
			tgmMain.elementFade(this.foldoutEl, function() {
				this.visible = false;
				if (callback != null) {
					callback();
				}
				tgmMain.fireEvent('onDropmenuClose');
			}.bindAsEventListener(this), 0.2);
		}
	},

	toggle:function(e)
	{
		if (this.visible == true) {
			this.close(e);
		} else {
			this.open(e);
		}
	}
});

