// 
//  IceMenu - A module that allow to make a animated dropdown navigation system.
// 
//  @copyright	Copyright (C) 2008 IceTheme. All Rights Reserved
//  @license	Copyrighted Commercial Software 
//  @author     IceTheme (icetheme.com)

 
 var IceMenu = new Class({
	options: {
		bgiframe: true,
		hoverClass: 'sfHover',
		delay: 500,
		animate: {
			props: ['opacity', 'height'],
			opts: Class.empty
		}
	},
	initialize: function (b, c) {
		this.setOptions(c);
		if (window.ie6) this.options.delay = 50;
		this.element = $(b);
		var width = 970;//parseInt(this.element.getStyle('width'));
		var childs = this.element.getChildren();
		var cw = parseInt(width/childs.length);
		var lastcw = width - cw*(childs.length-1);
		childs.each(function(el,i){
			if(i == childs.length-1)
			el.setStyle('width',lastcw);
			else
			el.setStyle('width',cw);
		});
		this.element.getElements('li').each(function (a) {
			a.addEvents({
				'mouseover': this.over.bind(this, a),
				'mouseout': this.out.bind(this, a)
			});
		},
		this)
	},
	over: function (b) {
		$clear(b.sfTimer);
		if (!b.hasClass(this.options.hoverClass)) {
			if (window.ie6) {
				var c = b.getProperty('class').split(" ");
				var d = this.options.hoverClass;
				c = c.filter(function (y) {
					return ! y.test("-" + d)
				});
				c.each(function (a) {
					if (b.hasClass(a)) b.addClass(a + "-" + d)
				},
				this);
				var e = c.join("-") + "-" + d;
				if (!b.hasClass(e)) b.addClass(e)
			}
			b.addClass(this.options.hoverClass);
			var f = b.getElement('ul');
			if (f) {
				if (this.options.bgiframe) f.bgiframe({
					opacity: false
				});
				f.animate(this.options.animate)
			}
			b.getSiblings().each(function (a) {
				a.removeClass(this.options.hoverClass)
			},
			this)
		}
	},
	out: function (e) {
		var f = this.options.hoverClass;
		e.sfTimer = (function () {
			if (window.ie6) {
				var b = e.getProperty('class').split(" ");
				b = b.filter(function (y) {
					return y.test("-" + f)
				});
				b.each(function (a) {
					if (e.hasClass(a)) e.removeClass(a)
				},
				this);
				var c = b.join("-") + "-" + f;
				if (!e.hasClass(c)) e.removeClass(c)
			}
			e.removeClass(f);
			var d = e.getElement('iframe');
			if (d) d.remove()
		}).delay(this.options.delay, this)
	}
});
IceMenu.implement(new Options);
Element.extend({
	animate: function (b) {
		if (!this.Fx) {
			this.Fx = this.effects(b.opts);
			this.now = this.getStyles.apply(this, b.props);
			this.FxEmpty = {};
			for (var i in this.now) this.FxEmpty[i] = 0
		}
		if (b.props.contains('height') || b.props.contains('width')) {
			this.setStyle('overflow', 'hidden');
			this.getParents('ul').each(function (a) {
				a.setStyle('overflow', 'visible')
			})
		}
		this.Fx.set(this.FxEmpty).start(this.now)
	},
	getParents: function (a) {
		var b = [];
		var c = this.getParent();
		while (c && c !== document) {
			if (c.getTag().test(a)) b.push(c);
			c = c.getParent()
		}
		return b
	},
	getSiblings: function () {
		var a = this.getParent().getChildren();
		a.splice(a.indexOf(this), 1);
		return a
	}
});

