/**
 * @overview	
 * @using prototype
 * @using scriptaculous
 */

Sureflix.UI.currentDropdownMenu = null;

Sureflix.UI.DropdownMenu = Class.create({

	initialize: function(titleElement, menuElement) {
		this.titleElement = $(titleElement);
		this.menuElement = $(menuElement);
		this.index = -1;
		this.entryCount = 0;
		this.hideTimeout = null;
		this.effectDuration = 0.5;

		this.menuElement.hide();

		this.setupMenu();

		Event.observe(this.titleElement, "mouseover", this.onMouseOver.bindAsEventListener(this));
		Event.observe(this.titleElement, "mouseout", this.onMouseOut.bindAsEventListener(this));
		Event.observe(this.menuElement, "mouseout", this.onMouseOut.bindAsEventListener(this));
	},


	setupMenu: function() {
		Element.cleanWhitespace(this.menuElement);
		Element.cleanWhitespace(this.menuElement.down());
		if (this.menuElement.firstChild && this.menuElement.down().childNodes) {
			this.entryCount = this.menuElement.down().childNodes.length;
			for (var i = 0; i < this.entryCount; i++) {
				var entry = this.getEntry(i);
				entry.entryIndex = i;
				Event.observe(entry, "mouseover", this.onHover.bindAsEventListener(this));
			}
		}
	},

	setCurrent: function(cssRule) {
		this.menuElement.down().childElements().each(function(item) {
			item.removeClassName("current");
		});

		var currentItem = this.menuElement.down(cssRule);
		if (currentItem) {
			currentItem.addClassName("current");
		}
	},


	onMouseOver: function(event) {
		this.cancelHide();
		this.show();
	},


	onMouseOut: function(event) {
		this.cancelHide();
		this.index = -1;
		this.render();
		this.hideTimeout = setTimeout(this.hide.bind(this), 500);
	},


	onHover: function(event) {
		this.cancelHide();

		var element = Event.findElement(event, 'LI');
		if (this.index != element.entryIndex) {
			this.index = element.entryIndex;
			this.render();
		}
		Event.stop(event);
	},

	show: function() {
		if (Sureflix.UI.currentDropdownMenu != null && Sureflix.UI.currentDropdownMenu != this) {
			Sureflix.UI.currentDropdownMenu.hideImmediate();
		}
		Sureflix.UI.currentDropdownMenu = this;

		this.cancelHide();

		//Element.addClassName(this.titleElement.down(), "selected");

		if (Element.getStyle(this.menuElement, 'display') == 'none') {
			if (!this.menuElement.style.position || this.menuElement.style.position == 'absolute') {
				this.menuElement.style.position = 'absolute';
				Position.clone(this.titleElement, this.menuElement, {
					setHeight: false,
					setWidth: false,
					offsetTop: this.titleElement.offsetHeight + 2
				});

				//make sure that menu does not extend beyond right edge of window
				var windowWidth = SxUi.GetWindowWidth();
				if (this.menuElement.getStylePixels("left") + this.menuElement.getWidth() > windowWidth) {
					this.menuElement.moveTo(windowWidth - this.menuElement.getWidth(), null);
				}
			}
			if (Sureflix.UI.useEffects) {
				Effect.Appear(this.menuElement, {
					duration: this.effectDuration //, queue: { position: 'end', scope: this.menuElement.id }
				});
			} else {
				this.menuElement.show();
			}
		}

		if (!this.iefix && (Prototype.Browser.IE) && (Element.getStyle(this.menuElement, 'position') == 'absolute')) {
			new Insertion.After(this.menuElement, '<iframe id="' + this.menuElement.id + '_iefix" ' +
			'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
			'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
			this.iefix = $(this.menuElement.id + '_iefix');
		}
		if (this.iefix)
			setTimeout(this.fixIEOverlapping.bind(this), 50);
	},


	fixIEOverlapping: function() {
		Position.clone(this.menuElement, this.iefix, {
			setTop: (!this.menuElement.style.height)
		});
		this.iefix.style.zIndex = 1;
		this.menuElement.style.zIndex = 2;
		Element.show(this.iefix);
	},


	hide: function() {
		this.cancelHide();

		//Element.removeClassName(this.titleElement.down(), "selected");

		if (Element.getStyle(this.menuElement, 'display') != 'none') {
			if (Sureflix.UI.useEffects) {
				new Effect.Fade(this.menuElement, {
					duration: this.effectDuration //, queue: { position: 'end', scope: this.menuElement.id }
				})
			} else {
				this.menuElement.hide();
			}
		}

		if (this.iefix) {
			Element.hide(this.iefix);
		}
	},


	hideImmediate: function() {
		this.cancelHide();

		//Element.removeClassName(this.titleElement.down(), "selected");

		this.menuElement.hide()

		if (this.iefix) {
			Element.hide(this.iefix);
		}
	},


	cancelHide: function() {
		if (this.hideTimeout) {
			clearTimeout(this.hideTimeout);
			this.hideTimeout = null;
		}

		//var queue = Effect.Queues.get(this.menuElement.id);
		//queue.each(function(effect) { effect.cancel(); });
	},


	render: function() {
		for (var i = 0; i < this.entryCount; i++) {
			this.index == i ? Element.addClassName(this.getEntry(i), "selected") : Element.removeClassName(this.getEntry(i), "selected");
		}
	},


	getEntry: function(index) {
		return this.menuElement.down().childNodes[index];
	},


	getCurrentEntry: function() {
		return this.getEntry(this.index);
	}
});