(function($){

	function Dropdown(item){
		this.onItem = false;
		this.onSubNav = false;
		this.isOpen = false;
		this.item = $(item);
		this.subnav = this.item.siblings("ul");
		
		// bind events & start
		this.init();
		
		return this;
	}
	
	Dropdown.prototype.start = function(){
		var self = this;
		
		this.timer = window.setInterval(function(){
			if(!self.onItem && !self.onSubNav && self.isOpen){
				self.stop();
			}
		}, 200);
	}
	
	Dropdown.prototype.stop = function(){
		window.clearInterval(this.timer);
		this.subnav.hide();
		this.isOpen = false;
		this.item.removeClass("active");
	}
	
	Dropdown.prototype.init = function(){
		var self = this;
		
		// bind to the main item
		this.item.bind("mouseenter mouseleave", function(e){
			
			// assume no
			self.onItem = false;
			
			if(e.type === "mouseenter"){
				$(this).addClass("active");
				self.start();
				self.subnav.show();
				self.onItem = true;
				self.isOpen = true;
			}
		});
		
		// bind to subnav
		this.subnav.bind("mouseenter mouseleave", function(e){
			self.onSubNav = (e.type === "mouseenter") ? true : false;
		});
	}
	
	// use this dropdown class on each navigation link that has a sibling subnav
	$("#navigation > li a:has(~ ul)").each(function(){
		return new Dropdown(this);
	});
	
})(jQuery);
