 var objMain;
$(document).ready(function() {
	var sfEls = $('.ja-megamenu li.mega');
	sfEls.each (function() {
	    var a=$(this).find('a.mega'); 
		if ((a) && $(this).children(a)) $(this).attr('a', a);
		else $(this).attr('a', null);
	});
	sfEls.each (function(){
		$(this).bind('mouseover', function(e) {		
			if ($(this).attr('class')=='group') 
			    return;
			//clearTimeout($(this).timer);
			jaMegaHoverOutOther($(this));
			if ($(this).attr('class').indexOf('haschild')!=-1) 
			{
			    $(this).removeClass('haschild').addClass('haschild-over');
			}
			else $(this).addClass ('over');
			if ($(this).find('a')) {
				$(this).find('a').addClass ('over');
			}
		});
		$(this).bind('mouseout', function(e) {
		    objMain= $(this);
		    $(this).timer=500;		    
		    jaMegaHoverOut(objMain);
			 
		});
	});
	    sfEls.each (function() {
	            $(this).removeClass('active');
	            $(this).find('a').attr('class','mega');
	        });
	        sfEls.each (function() {
	            var link=$(this).find('a').attr('href');
	            var currentpageLink=""+$(document).attr('location');
	            
	            if(currentpageLink.toLowerCase().indexOf(link.toLowerCase())!=-1)
	            {
	                $(this).addClass('active');
	                $(this).find('a').attr('class','mega active');

	            }
	        });
	        
	function jaMegaHoverOut(e) {
		//clearTimeout($(e).timer);
		if ($(e).attr('class').indexOf('haschild-over')!=-1) 
		{
		    $(e).removeClass ('haschild-over')
		    $(e).addClass ('haschild');
		}
		else 
	        $(e).removeClass ('over');
		if ($(e).find('a')) {
			$(e).find('a').removeClass ('over');
		}
	}
	function jaMegaHoverOutOther(el) {
		sfEls.each (function() {
			if ($(this) != el && !$(this).children (el)) {
				if ($(this).attr('class').indexOf('haschild-over')!=-1)
					$(this).removeClass ('haschild-over').addClass ('haschild');
				else
					$(this).removeClass ('over');
				if ($(this).find('a')) {
					$(this).find('a').removeClass ('over');
				}
			}
		});
	}
});
