/**
* Ferry Brouwer
* Class MainDropdownMenu
* Show / Hide script of datalist (submenu filled with fredhopperdata)
* Donderdag 10 November 2009 11:51
*/
var show_delay = 200;
var hide_delay = 300;

function MainDropdownMenu(){
	this.menu_width;
	this.menu = new Array();
	this.global_over = false;
	this.over_element;
	
	$(document).ready(function(){
		mainCatMenu.menu_width = $('#categories_div').width();
		$.each($("#l-menu").children(), function(i, div){mainCatMenu.menu.push(div);});
		$.each($("#r-menu").children(), function(i, div){mainCatMenu.menu.push(div);});

		$.each(mainCatMenu.menu, function(i, div){
			div.list 	= $(div).children('div.list'); 
			div.fh 		= div.list.children('div.fredhopperdata');
			
			if(div.fh.html() != ''){
				(i == 0) ? div.first_item = true : div.first_item = false;
				div.list_width = mainCatMenu.getListWidth(div);
				mainCatMenu.reposition_list(div);
				mainCatMenu.setBannerHeight(div);
				
				//event MouseOver => show
				$(div).mouseenter(function(event){
					mainCatMenu.global_over 	= true; 
					mainCatMenu.over_element 	= div;
					
					var list_out = mainCatMenu.getAnyListOut();
					if(list_out){
						mainCatMenu.show(div, i);
					}else{
						var showtimeout = setTimeout(function(){
							clearTimeout(showtimeout);
							if(mainCatMenu.global_over && mainCatMenu.over_element == div)mainCatMenu.show(div, i);
						}, show_delay);
					}
				});

				//event MouseOut => hide
				$(div).mouseleave(function(event){
					if($(div).children().length == 2){
						$(div).children('div.item_right_over').addClass('item_right');
						$(div).children('div.item_right_over').removeClass('item_right_over');
						$(div).children('a').removeClass('over');
					}else{
						mainCatMenu.global_over = false;
						var hidetimeout = setTimeout(function(){
							clearTimeout(hidetimeout);
							if(!mainCatMenu.global_over)mainCatMenu.hide(div, i);
						},hide_delay);
					}
				});			
			}
			
		});	//end iterate array menu divs
	});//end onload document
}

/**
* @returns the width of the list inside the div
* @param div : the div that contains the list
*/
MainDropdownMenu.prototype.getListWidth = function(div) {
	var list_width = 0;
	if(typeof div.list == 'undefined')	div.list= $(div).children("div.list"); 
	if(typeof div.fh == 'undefined')	div.fh 	= div.list.children("div.fredhopperdata");
	
	for(var c=0; c<div.fh.children().length; c++){
		var child = $(div.fh.children()[c]);
		switch( child.attr('class') ){
			case 'leftColumn':
				var left_width = child.outerWidth() + parseInt(child.css('margin-left')) + parseInt(child.css('margin-right'));
				list_width += left_width;
			break;
			case 'firstRightColumn':
				var right_width = child.outerWidth() + parseInt(child.css('margin-left')) + parseInt(child.css('margin-right'));
				list_width += right_width;					
			break;
			case 'thirdColumn':
				var third_width = child.outerWidth() + parseInt(child.css('margin-left')) + parseInt(child.css('margin-right'));
				list_width += third_width;					
			break;
		}										  
	} 
	list_width += parseInt(div.fh.css('padding-left')) + parseInt(div.fh.css('padding-right'));
	list_width += parseInt(div.list.css('borderLeftWidth')) + parseInt(div.list.css('borderRightWidth'));
	return list_width;
}

/**
* Set banner height
*/
MainDropdownMenu.prototype.setBannerHeight = function(div) {
	var banners = $(div).find('div.flyout');
	var heighestBanner = 0;
	$.each(banners, function(index, _div){
		if($(_div).height() > heighestBanner) heighestBanner = $(_div).height();
	});
	$.each(banners, function(index, _div){
		$(_div).height(heighestBanner);
	});
}

/**
* Invoked by AjaxDataProvider Class
* Give a full list of brands in two columns
*/
MainDropdownMenu.prototype.extendBrands = function(div_id){
	if(typeof mainCatMenu.cachingCategories != 'undefined'){
		var cat = null;
		for(var i=0; i<mainCatMenu.cachingCategories.length; i++){
			if(mainCatMenu.cachingCategories[i].catId == div_id){
				cat = mainCatMenu.cachingCategories[i];
				break;
			}
		}
		
		if(cat){
			var div 				= cat.div;
			var list 				= cat.list;
			var rc					= cat.rc;
			var orig_html 			= cat.orig_html;
			var orig_list_properties= cat.orig_list_properties;
			var orig_rc_properties	= cat.orig_rc_properties;
			var new_html 			= cat.new_html;
			
			if(rc.html() != new_html){
				rc.html(new_html);
				rc.width(320);
				div.list = list;
				div.list_width = (orig_list_properties.width - orig_rc_properties.width) + 320;
				this.reposition_list(div);
				
				if(list.height() > orig_list_properties.height){
					$.each(rc.children("a"), function(i, element){
						$(element).css({"float":"left", "white-space":"normal", "padding-right":"20px", width:140});
					});
				}
			}
		}
	}
}

/**
* When brandlist is extended, this function sets the old HTML content back
* @param div : div which contains the brandlist (firstRightColumn) to set old HTML content back
*/
MainDropdownMenu.prototype.setOriginalHTML = function(div_id){
	if(typeof mainCatMenu.cachingCategories != 'undefined'){
		var cat = null;
		for(var i=0; i<mainCatMenu.cachingCategories.length; i++){
			if(mainCatMenu.cachingCategories[i].catId == div_id){
				cat = mainCatMenu.cachingCategories[i];
				break;
			}
		}
		
		if(cat){
			var div 				= cat.div;
			var rc					= cat.rc;
			var list 				= cat.list;
			var orig_html 			= cat.orig_html;
			var orig_list_properties= cat.orig_list_properties;
			var orig_rc_properties	= cat.orig_rc_properties;
			
			if(rc.html() != orig_html){
				rc.html(orig_html);
				rc.width(orig_rc_properties.width);
				div.list = list;
				div.list_width = orig_list_properties.width;
				$(list).width(div.list_width);
			}
		}
	}
}

/**
* Check if a list inside all the div children is out
* @returns Boolean
*/
MainDropdownMenu.prototype.getAnyListOut = function(){
	var list_out = true;
	var counter = 0;
	$.each(mainCatMenu.menu, function(i, div){
		var list = $(div).children("div.list");
		if(list.css('visibility') == 'hidden')counter++;
	});
	if(counter == mainCatMenu.menu.length)list_out = false;
	return list_out;
}

/**
* Show list
* @param element: div element
* @param number	: iterate number in menu of the element div
*/ 
MainDropdownMenu.prototype.show = function(element, number){
	//FIXME: version check for resize bug IE 7, must be a different approach
	//if( jQuery.browser.msie && Math.floor(jQuery.browser.version)=='7' )mainCatMenu.reposition_list(element);
	$(element).children('a').addClass('over');
	
	$(element).children('div.item_right').addClass('item_right_over');
	$(element).children('div.item_right').removeClass('item_right');
	
	if(element.list.css('visibility') == 'hidden')element.list.css({visibility:'visible'});
	$.each(mainCatMenu.menu, function(int, div){
		if(div != element){
			$(div.list).css({visibility:'hidden'});
			$(div).children('a').removeClass();
			$(div).children('div.item_right_over').addClass('item_right');
			$(div).children('div.item_right_over').removeClass('item_right_over');
	
			mainCatMenu.setOriginalHTML(div.id.replace(/^c_/, ""));
		}
	});
}

/**
* Hide list
* @param element: div element
* @param number	: iterate number in menu of the element div
*/ 
MainDropdownMenu.prototype.hide = function(element, number){ 	
	$(element).children('a').removeClass();

	$(element).children('div.item_right_over').addClass('item_right');
	$(element).children('div.item_right_over').removeClass('item_right_over');
	
	element.list.css({visibility:'hidden'});
	mainCatMenu.setOriginalHTML( element.id.replace(/^c_/, "") );
}

/**
* Reposition list 
* @param div : the div which contains the list
*/
MainDropdownMenu.prototype.reposition_list = function(div){
	div.list.css({left: $(div).offset().left});
	
	( div.list_width  < $(div).width() ) ? div.list.width( $(div).width() ) : div.list.width(div.list_width);
	div.list.css( {top:$('#categories_div').offset().top + $(div).height(), zIndex:999999} );
	
	//check if list is bigger than page, if so reposition the list
	var left_offset = div.list.offset().left - $('#page').offset().left;
	if( (left_offset + div.list_width) >= mainCatMenu.menu_width ){
		var left_position = ($('#categories_div').offset().left + $('#categories_div').innerWidth()) - div.list_width;
		div.list.css({left:left_position - 2});
	}
}

/**
* Resize window
* reposition all lists
*/
$(window).resize(function(){
	$.each(mainCatMenu.menu, function(i, div){
		if(typeof mainCatMenu.cachingCategories != 'undefined'){		
			for(var j=0; j<mainCatMenu.cachingCategories.length; j++){
				var cat = mainCatMenu.cachingCategories[j];
				if( cat.catId == div.id.replace(/^c_/, "") ){
					div.list = cat.list;
					div.list_width = (cat.orig_list_properties.width - cat.orig_rc_properties.width) + 320;
					break;
				}
			}
		}
		
		if(!isNaN(div.list_width)){
			mainCatMenu.reposition_list(div);
		}
	});
});

//Create an instance of the class
var mainCatMenu = new MainDropdownMenu();
