// ./_js/main_menu.js 

// - execute when document is ready
$(document).ready(function(){
	
	
	// - initialize the main menu
	mainMenuInit();
});



// - main menu functionality


// -- object to store vars
var v_mm = new Object();


// -- vars (config)
v_mm.tier2 = { // tier 2
	"speed" : 250, // (miliseconds) speed of transitions
	"wait" : 350 // (miliseconds) to wait for tier2 to close
};


// -- vars (private)
v_mm.tier2.active_item = 0; // which tier2 item is currently active?


// -- initialize the main menu
function mainMenuInit() {
	
	// --- loop each tier1 submenu parent li
	$('ul#mm_tier1 > li.submenu_parent').each(function(index) {
		index++;
		v_mm.tier2['timeout_'+index] = 0; // set a timeout interval for each tier2 nav
		
		
		// ---- set the width of div.cover
		$(this)
			.find('div.cover:first')
				.width($(this).find('a:first').innerWidth(true));
		
		
		$(this)		
			// ---- set hover handler
			.hover(function() {
				var t_which = $(this).attr('rel');
				
				if(t_which != v_mm.tier2.active_item) {
					mainMenuTier2Hide(v_mm.tier2.active_item, 1);
					v_mm.tier2.active_item = t_which;
				}
				
				clearTimeout(v_mm.tier2['timeout_'+t_which]);
				
				if($(this).hasClass('active')) return;
							
				$(this)
					.addClass('active')
					.find('div.cover:first')
						.removeClass('hidden');
						
				$(this).find('div.mm_tier2:first')
						.slideDown(v_mm.tier2.speed);
			}, function() {
				var t_which = $(this).attr('rel');
				v_mm.tier2['timeout_'+t_which] = setTimeout(function() {
					mainMenuTier2Hide(t_which);
				}, v_mm.tier2.wait);					
			})		
			// ---- add a rel for reference
			.attr('rel', index)
			// ---- loop the tier2 <ul>, separating its li's into 3 columns
			.find('ul:first')
				.each(function() {
					var t_children = $(this).find('li').size();
					var t_max_per_col = Math.ceil(t_children / 2);
					var t_ul_index = 0;
					var t_li_index = 0;
					
					$(this).find('li').each(function() {
						if(!(t_li_index % t_max_per_col)) {
							t_ul_index++;
							$(this).parents(':eq(1)')
								.append(
									$('<ul/>')
										.attr('rel', t_ul_index)
								);
						}
						
						t_li_index++;
						var t_link = $(this).html();
						
						$(this).parents(':eq(1)').find('ul[rel="'+t_ul_index+'"]')
							.append(
								$('<li/>').html(t_link)
							);
					});
				})
			.remove();
	});
	
	
	// --- loop features
	for(var i in mm_tier2_features) {
		var t_html = '<div class="feature"><a href="%URL%" title="%TITLE%"><img src="%IMG%" alt="%TITLE%" /></a><h4><a href="%URL%">%TITLE%</a></h4></div>';
		t_html = t_html.replace(/%URL%/g, mm_tier2_features[i].link);
		t_html = t_html.replace(/%TITLE%/g, mm_tier2_features[i].title);
		t_html = t_html.replace(/%IMG%/g, mm_tier2_features[i].image);
		
		$('ul#mm_tier1').find('div[rel="s_'+mm_tier2_features[i].section_id+'"]:first')
			.append(t_html);
		
	}
}

// -- hides tier2 menu items
function mainMenuTier2Hide(which, t_speed) {
	
	if(!t_speed) t_speed = v_mm.tier2.speed;
	
	$('ul#mm_tier1 > li.submenu_parent[rel='+which+'] > div.mm_tier2:first')
		.slideUp(t_speed, function() {
			$(this).parent()
				.removeClass('active')
				.find('div.cover:first')
						.addClass('hidden');
				if(v_mm.tier2.active_item == which) v_mm.tier2.active_item = 0;				
		});
}


function createTier2Feature(which) {
		
}
