/**
* @author    JoomlaShine.com http://www.joomlashine.com
* @copyright Copyright (C) 2008 - 2011 JoomlaShine.com. All rights reserved.
* @license   GNU/GPL v2 http://www.gnu.org/licenses/gpl-2.0.html
*/

// menu plugin
(function($){
var subnav 		= new Array();
$.fn.jsnBuildMenu = function(options) {
	
	subMenuSetup($(this));
	
	function subMenuSetup(element) {
		if(element != undefined && element != null)
		{
			element.children().each(function() {
				if ($(this).get(0).tagName.toLowerCase() == 'li') {
					var li = $(this);			
					li.children().each(function() {				
						if ($(this).get(0).tagName.toLowerCase() == 'ul') {
							var ul = $(this);
							ul._id = subnav.length+1;						
							ul.parent = element;
							subnav.push (ul);
							jsnInit(ul);
							li.bind('mouseenter', function() {
								jsnSetActive(li);
								jsnShow(ul, 0);
							});
							li.bind('mouseleave', function() {	
								jsnSetDeactive(li);
								jsnHide(ul, options.delay);								
							});
							subMenuSetup(ul);
							li.hasSub = 1;
						}
					});
					if (!li.hasSub) {
						li.bind('mouseenter', function() {					
							jsnSetActive(li);
						});
						li.bind('mouseleave', function() {
							jsnSetDeactive(li);
						});
					}				
				}
			});
		}
	}
	
	
	function jsnSetActive(li) {
		li.addClass('sfhover');
	}

	function jsnInit(ul) {
		duration = (options.duration == undefined)?250:options.duration;
		ul.orientation = (options.orientation == undefined)?'h':options.orientation;
		ul.mw = ul.width();
		ul.mh = ul.height();		
		ul.css('left', '-999em');
		
		if (ul.parent._id || ul.orientation == 'v') {
			ul.animate({
				width: "0"
			}, duration, function() {
				if (ul.status == 'jsnHide') {
					ul.css('left', '-999em');
					ul.hidding = 0;
				}
				ul.animating = false;
				ul.css('overflow', '');
			});
		} else {
			ul.animate({
				height: "0"
			}, duration, function() {
				if (ul.status == 'jsnHide') {
					ul.css('left', '-999em');
					ul.hidding = 0;
				}
				ul.animating = false;
				ul.css('overflow', '');
			});
		}
	}

	function jsnSetDeactive(li) {
		li.removeClass('sfhover');
	}
	function jsnHideAll(ul) {
		for (var i=0;i<subnav.length; i++) {
			if (!jsnIsChild(ul, subnav[i])) {			
				jsnHide(subnav[i], 0); 
			}
		}
	}
	function jsnIsChild(ul, _obj) {
		var chk = false;
		while (ul.parent) {
			if (ul._id == _obj._id) {
				return true;
			}
			ul = ul.parent;
		}
		return false;
	}

	function jsnAnim(ul) {		
		if (
			(ul.status == 'jsnHide' && ul[0].style.left != 'auto') ||
			(ul.status == 'jsnShow' && ul[0].style.left == 'auto' && !ul.hidding)
		) return;
		
		if (ul.status == 'jsnShow') {
			ul.hidding = 0;
			jsnHideAll(ul); 
			ul.css('left','auto'); 
			ul.stop(true, true);
			if (ul.parent._id || ul.orientation == 'v') {
				ul.animate({
					width: ul.mw
				}, duration, function() {
					ul.animating = false;
					ul.css('overflow', '');
				});
			} else {
				ul.animate({
					height: ul.mh
				}, duration, function() {
					ul.animating = false;
					ul.css('overflow', '');
				});
			}
		} else {
			ul.hidding = 1;
			ul.stop(true, true);
			if (ul.parent._id || ul.orientation == 'v') {
				ul.animate({
					width: 0
				}, duration, function() {
					ul.animating = true;
					ul.css('overflow', 'hidden');
				});
			} else {
				ul.animate({
					height: 0
				}, duration, function() {
					ul.animating = true;
					ul.css('overflow', 'hidden');
				});
			}
		}
	}
	
	function jsnHide(ul, ddelay) {
		ul.status = 'jsnHide';
		clearTimeout(ul.timeout);
		if (ddelay != undefined && ddelay > 0 && !ul.animating) {	
			ul.timeout = setTimeout(function(){jsnAnim(ul);}, ddelay);
		} else {
			jsnAnim(ul);
		}
	}
	function jsnShow(ul, ddelay) {
		ul.status = 'jsnShow';
		clearTimeout(ul.timeout);
		if (ddelay != undefined && ddelay > 0 && !ul.animating) {
			ul.timeout = setTimeout(function(){jsnAnim(ul);}, ddelay);
		} else {
			jsnAnim(ul);
		}
	}
	
};
})(jQuery);
