/**
 * jqMenu - jQuery plugin
 * @version: 1.0 (2011/07/07)
 * @requires jQuery v1.5.2 or later 
 * @author Richard Vacchino Marceau
 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
**/


(function($) {
	
	// Globals
	var opts = new Array();	
	
	$.fn.jqMenu = $.fn.jqMenu = function(options){
		
		// Intiallisation
		init = function(el){
		
			//Options
			opts[el.id] = $.extend({}, $.fn.jqMenu.defaults, options); //opts[el.id].width);
			
			
			switch(opts[el.id].type)
			{
				case 'mega':
					if(opts[el.id].effect == 'slide') {
						$.megaSlide(el);
					}	
				break;
				
				case 'list':
				  $.list(el);
				break;
				  
				default:
				  $.slide(el);
			}
			
		};
		
		
		// Mega dropdown menu
		$.megaSlide = function(el) {
		
			var h  =  opts[el.id].height;
			var w  =  opts[el.id].width;
			var l  =  opts[el.id].speed;
			var iw =  opts[el.id].initialWidth; 	// initialWidth
			var bc =  opts[el.id].buttonClass; 		// Class of the button
			var ac =  opts[el.id].activeClass; 		// Class for active button
			var dc =  opts[el.id].dropDownClass; 	// Class of the dropDown to animate
			
			var delay =  opts[el.id].delay;
			var sub = '.'+opts[el.id].dropDownClass; 
			
			//close the sub element
			$('#'+el.id+' .'+opts[el.id].dropDownClass).height(0);
			$('#'+el.id+' .'+opts[el.id].dropDownClass).width(iw);
			
			
			
			$('#'+el.id+' li.'+bc).hover(function() {
			
				var position = $(this).offset()
				
				$(this).addClass(ac);
				$(this).siblings().removeClass(ac);
				$(this).stop();
				$(this).siblings().children(sub).stop();
				$(this).siblings().children(sub).stop().height(0);
				$(this).siblings().children(sub).stop().width(iw);
				
				if($(this).children(sub).length > 0) {
				
					$(this).children(sub).stop()
					
					
					if (position.left + w >= $(document).width()) {
					
						$(this).children(sub).css({
							left : 'auto',
							right: 0
						});
						
					}
					else {
					
							$(this).children(sub).css({
								left : 0,
								right: 'auto'
							});
					}
					
					$(this).children(sub).stop().delay(delay).animate({'height':h},l,function(){
							$(this).animate({'width':w},l)
					});
				}
				
			},function(){
				$(this).siblings().stop().children(sub).height(0);
				$(this).siblings().removeClass(ac);
				
				if($(this).children(sub).length > 0){
				
					$(this).children(sub).stop().delay(delay).animate({'width':iw},l,function(){
					
							$(this).animate({'height':0},l,function(){
							
								$(this).parent().removeClass(ac);
								
							});
							
					});
				}
				else {
				
					$(this).removeClass(ac);
					
				}
					
			});
		}
		
		
		// UL list drop down menu
		$.list = function(el) {
		
			var l  =  opts[el.id].speed;
			var ac =  opts[el.id].activeClass; 		// Class for active button
			var d  =  opts[el.id].delay;
			
			// Hide submenu
			$('#'+el.id).find('li ul').hide();
			
			var tUl = $('#'+el.id).find('li')
			
			tUl.each(function() {
				
				var se = $(this)
				var timer;
				
				$(this).hover(function() {
					se.addClass(ac)
					se.children('ul').fadeIn(l)
					clearTimeout(timer);
					
				},function() {
				
					timer = setTimeout(function(){
    					se.removeClass(ac)
						se.children('ul').fadeOut(l)
					},d)
					
				})
				
			})
			
			
		}
		
		this.each (
			function(){ init(this); }
		);	
	};
	
	
	
	// default values
	$.fn.jqMenu.defaults = {
		type:           'mega',     // (mega, list) Type of menu 
		speed: 			300,		// speed of the animation
		initialWidth:   10, 		// Width before enlargement
		width: 			520, 		// width of submenu
		height: 		600, 		// height of submenu
		delay: 			200, 		// delay before desapearence
		effect: 		'slide', 	// slide, fade, etc.
		dropDownClass: 	'sub', 		//Class for the dropdown elements
		activeClass:    'open',   // Class active for the button
		buttonClass: 	'bt' 		// Class for the button
	};
	
})(jQuery);
