// JavaScript Document
var sub_menu;
var rollover = false;
var img_link = '';
	
function rollover_fn(object) {
	$('#sub_menu').show();
	
	$('#menu ul li a').removeClass('active');
	$(object).children('a').addClass('active');
		
	if ($(object).children('ul').length > 0) {
		var subs = Array('', '', '', '');
		var sub_index = 1;
		
		
		$('#sub_menu').removeClass('industry_bg');
		var wclass = $(object).attr('class');
		if ( wclass == 'industry' || wclass == 'lending_a_hand' || wclass == 'ayan') {
			/*img_link = $('#sub_menu').css('background-image');
			$('#sub_menu').css('background-image', 'none');*/
			
			$('#sub_menu').addClass('industry_bg');
		}
		
		rollover = true;
		//$('#sub_menu').html('<ul>'+$(this).children('ul').html()+'</ul>');
		
		var x = 1;
		var by = 2;
		
		if ($(object).children('ul').children('li').length > 9) {
			by = 4;
		} else if ($(object).children('ul').children('li').length > 6) {
			by = 3;
		}
		
		
		$(object).children('ul').children('li').each(function() {
			sub_index = Math.ceil(x / by);
			subs[sub_index] += '<li>'+$(this).html()+'</li>';
			
			x++;
		});
		
		var html = '';
		var class_name = '';
		for (var y = 1; subs[y] != undefined; y++) {
			if ($(object).children('ul').attr('class')) class_name = $(object).children('ul').attr('class'); else class_name = '';
			
			html += '<ul class="'+ class_name +'">'+subs[y]+'</ul>';
		}
		$('#sub_menu').html(html);
		
		$('#sub_menu').show();
		
		if (sub_seccion != '' && seccion != 'industry') {
			$('#sub_menu ul li a[@href=http://localhost:8080/mann_india/'+seccion+'/'+sub_seccion+'.html]').addClass('selected');
		}
		
	} else {
		rollover = true;
		
		if (sub_seccion != '') {
			$('#sub_menu').html('');
		} else {
			$('#sub_menu').hide();
		}
	}
}

function rollout() {
	//alert(sub_seccion);
	
	if (rollover == false) {
		if (sub_seccion != '') {
			rollover_fn($('#menu ul li.'+seccion));
		} else {
			$('#menu ul li a').removeClass('active');
			$('#sub_menu').hide();
			
			$('#sub_menu').html('');
		}
	}
}
	
$(document).ready(function() {	
	
	$('#menu ul').children('li').hover(function() { 
		if ($(this).parent('ul').parent('div').attr('id') == 'menu') {
			rollover_fn($(this));
		}
	}, function() {
		//$('#sub_menu').css('background-image', img_link);
		rollover = false;
		setTimeout(rollout, 500);
		//$('#sub_menu').append('<span>OUT</span>');
	});
	
	$('#sub_menu').hover(function() {
		rollover = true;
		/*if ($(this).children('ul').attr('class')) {
			img_link = $('#sub_menu').css('background-image');
			$('#sub_menu').css('background-image', 'none');
		}*/
	}, function() {
		
		if (sub_seccion != '') {
			
			
			//$('#sub_menu').html('');
			
		} else {
			rollover = false;
			$('#menu ul li a').removeClass('active');			
			$('#sub_menu').hide();
		}
	});
	
	
	
});