(function($){
    $.submenu = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el;
        
        // Add a reverse reference to the DOM object
        base.$el.data("submenu", base);
		base.options = $.extend({},$.submenu.defaultOptions, options);
		var isAnimationGoingon = false;
        
        base.init = function(){
        	if($.browser.msie && $.browser.version != 8){
        		var scrrenWidth = $(window).width();
        		$(base.options.menuClass , base.$el).css({'width' : scrrenWidth});
				$(window).resize(function(){					
					var scrrenWidth = $(window).width();
					$(base.options.menuClass , base.$el).css({'width' : scrrenWidth});
				});
        	}
            var count = 0;
			
			$(base.options.anchorClass , base.$el).each(function(){
				$(this).attr('id' , 'link-'+count);
				$(this).next().css('zIndex' , '-1');
				if($(this).parent().hasClass('active'))
				{
					$(this).parent().addClass('current');
					var nextElem = $(this).next();
					var height = nextElem.height();
					var top = nextElem.position().top;
					var newTop = parseInt(height) + parseInt(top);
					nextElem.css({ 'top' : newTop , 'zIndex' : 1});
				}			
				count++;
			});
			base.log(base.options.setInitHandler);
			
			if(base.options.setInitHandler)
				$(base.options.anchorClass , base.el).bind('initHandler' , base.options.initHandler);

			$(base.$el).delegate(base.options.anchorClass , 'click' , function(){	
				base.log(base.options.setInitHandler+" <=> "+isAnimationGoingon);
				if(!base.options.setInitHandler)	
				{
					if(!isAnimationGoingon)
						base.closeOtherMenu($(this));
				}
				else
				{
					base.log("Id is "+$(this).attr('id'));
					$("#"+$(this).attr('id') , base.el).trigger('initHandler' , $(this));
					$("#"+$(this).attr('id') , base.el).unbind('initHandler');
				}
			});
				
			
            // Put your initialization code here
        };
		
		base.closeOtherMenu = function(obj){
			/*obj.unbind('click');*/
			isAnimationGoingon = true;
			base.log('Close menu');
			//var subMenu = base.$el.children('li.active').children(base.options.menuClass);
			var subMenu = base.$el.children('li.current').children(base.options.menuClass);
			//base.log(subMenu.length);
			/*subMenu.css({'float' : 'none'});*/
			if(subMenu.length > 0)
			{
				var submenuHeight = subMenu.height();
				var submenuTop = subMenu.position().top;
				var newTop = parseInt(submenuTop) - parseInt(submenuHeight);
				subMenu.css({ 'zIndex' : -1});
				//subMenu.parent().toggleClass('active');		
				subMenu.parent().toggleClass('current');		
				subMenu.animate({ 'top' : newTop } , base.options.speed , function(){
					//base.log("Closed");
					var clickedId = obj.attr('id').substring(5);
					var animationId = $(this).prev().attr('id').substring(5);
					base.log(clickedId+" "+animationId);
					if(clickedId != animationId)
					{							
						base.openMenu(obj);
					}
					else
					{
						isAnimationGoingon = false;
					}
				});
			}
			else
			{
				base.openMenu(obj);
			}
		};
		
		base.openMenu = function(obj){
			base.log(obj.attr('class'));
			//obj.parent().toggleClass('active');
			obj.parent().toggleClass('current');
			
			var nextElem = obj.next(base.options.menuClass);
			var height = nextElem.height();
			var top = nextElem.position().top;
			var newTop = parseInt(height) + parseInt(top);			
			nextElem.animate({ 'top' : newTop } , base.options.speed , function(){
				//base.log('Bind '+obj.attr('id'));								
				/*obj.live('click' , function(){					
					base.log('Clicked');
					base.closeOtherMenu($(this));
				});*/
				nextElem.css({ 'zIndex' : 1});
				isAnimationGoingon = false;
			});
			
			
			//nextElem.css({ 'top' : newTop , 'zIndex' : 1});
			
		}
		
		base.log = function(m){
			//if(!$.browser.msie)
				//console.log(m);			
		};
        
        // Sample Function, Uncomment to use
        // base.functionName = function(paramaters){
        // 
        // };
        
        // Run initializer
        base.init();
    };
    
    $.submenu.defaultOptions = {
		menuClass : '.submenu',
		container: '.submenu-container',
		anchorClass : '.nav-link',
		setInitHandler : false,
		initHandler : function(event){},
		speed: 600
    };
    
    $.fn.submenu = function(options){
        return this.each(function(){
            (new $.submenu(this, options));
        });
    };
    
    // This function breaks the chain, but returns
    // the submenu if it has been attached to the object.
    $.fn.getsubmenu = function(){
        return this.data("submenu");
    };
    
})(jQuery);
