/*
 * Droppy 0.1.2
 * (c) 2008 Jason Frame (jason@onehackoranother.com)
 */
$.fn.droppy = function(options){
	options = $.extend({speed: 250}, options || {});
	this.each(function(){
		var root = this, zIndex = 1000;
		function getSubnav(ele){
			//alert("getSubnav : "+ele.nodeName+":"+$(ele).attr("class"));
			if (ele.nodeName.toLowerCase() == 'li') {
				//var subnav = $('* ul', ele);
				var subnav = $("> div.box-sub",ele);
				//alert("subnav : "+$(subnav).attr("class"));
				return subnav.length ? subnav[0] : null;
			} else {
				return ele;
			}
			
		}

		function getActuator(ele) {
			//alert("getActuator : "+ele.nodeName+":"+$(ele).attr("class"));
			if (ele.nodeName.toLowerCase() == 'div.box-sub') {
				return $(ele).parents('li')[0];
			} else {
				return ele;
			}
		}

		function hide() {
			//alert("hide : "+$(this).attr("class"));
			var subnav = getSubnav(this);
			if(!subnav) return;
			$.data(subnav, 'cancelHide', false);
			setTimeout(function(){
				if (!$.data(subnav, 'cancelHide')) {
					//$(subnav).slideUp(options.speed);
					$(subnav).stop(true,true);
					$(subnav).hide(options.speed);
				}
			}, 50);
		}

		function show() {
			//alert("show : "+$(this).attr("class"));
			var subnav = getSubnav(this);
			if (!subnav) return;
			$.data(subnav, 'cancelHide', true);
			//$(subnav).css({"z-index":++zIndex}).slideDown(options.speed);
			$(subnav).stop(true,true);
			$(subnav).css({"z-index":++zIndex}).show(options.speed);
			//alert("kk : " + this.nodeName.toLowerCase());
			if(this.nodeName.toLowerCase() == 'div.box-sub') {
				var li = getActuator(this);
				//alert("kk : " + li);
				$(li).addClass('hover');
				$('> a', li).addClass('hover');
			}
		}

		$('div.box-sub, li', this).hover(show, hide);
		$('li', this).hover(
			function() {
				//alert("li : hover");
				$(this).addClass('hover'); $('> a', this).addClass('hover'); },
			function() {
				//alert("li : rollout");
				$(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
		);
	});
};

