/*
*	@title			jag.dropper v1.0 - 2010.07.08
*	@author			james austin greene aka graf
*	@contact		dropper@jamesaustingreene.com
*	@description	Drop menu creator built on jquery 1.4.3.
*					Uses nested list objects (ul->li) to establish higherarchy.
*					Uses 'recursion' and can handle more levels than you'll need.
*					Please send feedback for improvements, enhancements!
*/
(function($){										//compatibility, allow the use of $ in plugin code
	$.fn.dropper = function(options){										//main function declaration
		var dropperDefaults = $.extend({},$.fn.dropper.defaults, options);	//combine default options with user specific options
		var dropperControlObjectsArray = new Array();
		var dropperLevels=-1;
		var dropperCurrentObj = this;
		function dropperOpen(e){
			for(var i=e.data.level; i>-1; i--){
				window.clearTimeout(dropperControlObjectsArray[e.data.level].timer);
				dropperControlObjectsArray[e.data.level].timer=null;
			}
			dropperClose(e.data.level);
			dropperControlObjectsArray[e.data.level].obj=$(this).children("ul").css('visibility','visible').css('display','block');
		}
		function dropperTimerSet(e){
			dropperControlObjectsArray[e.data.level].timer=window.setTimeout(function(){dropperClose(e.data.level);}, dropperControlObjectsArray[e.data.level].timeOut);
		}
		function dropperClose(level){
			if(dropperControlObjectsArray[level].obj)
				dropperControlObjectsArray[level].obj.css('visibility','hidden').css('display','none');
		}
		function dropperManager(_timeout,_timer,_obj){
			this.timeOut=_timeout;
			this.timer=_timer;
			this.obj=_obj;
		}
		while(dropperCurrentObj.length){			//this is where we setup our stage and bind all functions needed
			dropperLevels++;
			dropperControlObjectsArray[dropperLevels] = new dropperManager(parseInt(dropperDefaults.dropperTimeout/(dropperLevels+1)), 0, 0);
			var currentClass = "";
			for(var i=0; i<dropperLevels; i++) currentClass = currentClass + 'Sub';
			if(typeof(dropperDefaults['dropper' + currentClass + 'Class'])== 'undefined')
				currentClass = 'SubSub';
			dropperCurrentObj.addClass(dropperDefaults['dropper' + currentClass + 'Class']);
			if(dropperLevels>1)dropperCurrentObj.parent('li').addClass(dropperDefaults['dropperArrowClass']);
			dropperCurrentObj.children('li').bind('mouseover', {level:dropperLevels}, dropperOpen);
			dropperCurrentObj.children('li').bind('mouseout', {level:dropperLevels}, dropperTimerSet);
			dropperCurrentObj=dropperCurrentObj.children('li').children('ul');
		}
		dropperDefaults.dropperFinished();
		return this;
	};
	$.fn.dropper.defaults = {						//set default parameters
		dropperClass:'dropperMenu',					//class given to top lewel navigation
		dropperSubClass:'dropperSubMenu',				//class given to first level drop down
		dropperSubSubClass:'dropperSubSubMenu',		//class given to second level drop down
		dropperSubSubSubClass:'dropperSubSubSubMenu',	//class given to items (li) that have a submenu
		dropperArrowClass:'dropperArrow',				//timeout befor menu closes on mouseoff (sub menus divide this incrementally)
		dropperTimeout:400,
		dropperFinished:function(){}
	};
})(jQuery);
