/**
 * @version		$Id$
 * @author		Pham Minh Tuan
 * @package		Joomla!
 * @subpackage	ZJ_Menu
 * @copyright	Copyright (C) 2008 - 2010 by Zerosoft. All rights reserved.
 * @license		http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL version 2
 */

var ZJ_MooMenu = new Class({
	options: {
		version: '1.2',
		hoverClass:	'sfhover',
		type: 'default',
		fx: {
			show: {
				transition_height: Fx.Transitions.linear,
				transition_width: Fx.Transitions.linear,
				duration_height: 300,
				duration_width: 300
			},
			hide: {
				transition_height: Fx.Transitions.linear,
				transition_width: Fx.Transitions.linear,
				duration_height: 300,
				duration_width: 300
			}
		}
	},
	
	initialize: function(element, options) {
		this.setOptions(options);
		this.subnav		= new Array();
		this.currentEl	= null;
		
		this.build($(element));
	},
	
	build: function(element) {
		$A($(element).childNodes).each(function(el) {
			if (el.nodeName.toLowerCase() == 'li') {
				el._hasSub = false;
				$A($(el).childNodes).each(function(el2) {
					if (el2.nodeName.toLowerCase() == 'ul') {
						$(el2)._id = this.subnav.length + 1;
						$(el2)._parent = $(element);
						this.subnav.push($(el2));
						this.init(el2); 
						
						el.addEvents({
							'mouseenter': function(event) {
								this.setActive(el);
								this.show(el2, 0);
								return false;
							}.bind(this),
							
							'mouseleave': function(event) {
								this.setDeactive(el);
								this.hide(el2, 20);
							}.bind(this)
						});
						
						this.build(el2);
						el._hasSub = true;
					}
				}.bind(this));
				if (!el._hasSub) {
					el.addEvents({
						'mouseenter': function(event) {
							this.setActive(el);
							return false;
						}.bind(this),
						
						'mouseleave': function(event) {
							this.setDeactive(el);
						}.bind(this)
					});
				}
			}
		}.bind(this));
	},
	
	init: function(el) {
		el._mw = el.offsetWidth;
		el._mh = el.offsetHeight;
		
		el._myFxShow = new Fx.Styles(el, {
			wait: false
		});
		
		el._myFxHide = new Fx.Styles(el, {
			wait: false
		});
		
		if (el._parent._id) {
			el._myFxShow.options.transition = this.options.fx.show.transition_width;
			el._myFxShow.options.duration = this.options.fx.show.duration_width;
			
			el._myFxHide.options.transition = this.options.fx.hide.transition_width;
			el._myFxHide.options.duration = this.options.fx.hide.duration_width;
			
			if (this.options.type == 'fade') {
				el.setStyle('opacity', 0);
			} else if (this.options.type == 'expand') {
				el.setStyles({
					'width': 0,
					'height': 0
				});
			} else {
				el.setStyle('width', 0);
			}
			
		} else {
			el._myFxShow.options.transition = this.options.fx.show.transition_height;
			el._myFxShow.options.duration = this.options.fx.show.duration_height;
			
			el._myFxHide.options.transition = this.options.fx.hide.transition_height;
			el._myFxHide.options.duration = this.options.fx.hide.duration_height;
			
			if (this.options.type == 'fade') {
				el.setStyle('opacity', 0);
			} else if (this.options.type == 'expand') {
				el.setStyles({
					'width': 0,
					'height': 0
				});
			} else {
				el.setStyle('height', 0);
			}
		}
		
		
		el.setStyle('left', '-999em');
		
		el._myFxShow.addEvent('onComplete', this.animComp.bind(el));
		el._myFxHide.addEvent('onComplete', this.animComp.bind(el));
	},
	
	animComp: function() {
		if (this._status == 'hide') {
			this.setStyle('left', '-999em');
			this.hidding = 0;
		}
		this.setStyle('overflow', '');
	},
	
	hideAll: function(el) {
		for (var i = 0; i < this.subnav.length; i++) {
			if (!this.isChild(el, this.subnav[i])) {
				this.hide(this.subnav[i], 0, 1);
			}
		}
	},
	
	isChild: function(el, subnav) {
		var obj = el;
		while (obj._parent) {
			if (obj._id == subnav._id) {
				return true;
			}
			obj = obj._parent;
		}
		return false;
	},
	
	hide: function(el, timeout) {
		this.currentEl = el;
		el._status = 'hide';
		clearTimeout(el._timeout);
		if (timeout) {
			el._timeout = setTimeout(this.anim.bind(this), timeout);
		} else {
			this.anim();
		}
	},
	
	show: function(el, timeout) {
		this.currentEl = el;
		el._status = 'show';
		clearTimeout(el._timeout);
		if (timeout) {
			el._timeout = setTimeout(this.anim.bind(this), timeout);
		} else {
			this.anim();
		}
	},
	
	setActive: function(el) {
		el.addClass(this.options.hoverClass);
	},
	
	setDeactive: function(el) {
		el.removeClass(this.options.hoverClass);
	},
	
	anim: function() {
		var el = this.currentEl;
		if ((el._status == 'hide' && el.style.left != 'auto') || (el._status == 'show' && el.style.left == 'auto' && !el._hidding))
			return;
		el.setStyle('overflow', 'hidden');
		if (el._status == 'show') {
			el._hidding = false;
			this.hideAll(el);
		}
		if (el._status == 'hide') {
			el._hidding = true;
			el._myFxHide.stop();
			el._myFxShow.stop();
			if (this.options.type == 'fade') {
				el._myFxHide.start({
					'opacity': 0
				});
			} else if (this.options.type == 'expand') {
				el._myFxHide.start({
					'width': 0,
					'height': 0
				});
			} else {
				if (el._parent._id) {
					el._myFxHide.start({
						'width': 0
					});
				} else {
					el._myFxHide.start({
						'height': 0
					});
				}
			}
		} else {
			el.setStyle('left', 'auto');
			el._myFxHide.stop();
			el._myFxShow.stop();
			if (this.options.type == 'fade') {
				el._myFxShow.start({
					'opacity': 1
				});
			} else if (this.options.type == 'expand') {
				el._myFxShow.start({
					'width': el._mw,
					'height': el._mh
				});
			} else {
				if (el._parent._id) {
					el._myFxShow.start({
						'width': el._mw
					});
				} else {
					el._myFxShow.start({
						'height': el._mh
					});
				}
			}
		}
	}
});

ZJ_MooMenu.implement(new Events, new Options);
