// With this divMenu object, 'false' means the menu elements are not nested.
var divMenu = new FSMenu('divMenu', false, 'visibility', 'visible', 'hidden');
divMenu.cssLitClass = 'highlighted';
divMenu.animInSpeed = 1;
divMenu.animOutSpeed = 1;
// The other optional settings and animation functions will work here too.

var arrow_on  = '/images/categories_topnav_arrow_on.gif';
var arrow_off = '/images/categories_topnav_arrow_off.gif';
var img_arrow_on  = new Image(); img_arrow_on.src = arrow_on;
var img_arrow_off = new Image(); img_arrow_off.src = arrow_off;

$(function(){
	
	//setup mainnav(topnav) cellclick and rollovers
	$('.main_nav_row > td').each(function(){
		var td = $(this);
		var category_url = $('a', td).attr('href');
		var is_first     = ($('.main_nav_row > td').index(td) == 0) ? 1 : 0;
		//alert('is first? ' + is_first + 'index was:' + $('.main_nav_row > td').index(this) );
		if (category_url) {
			td.bind('mouseover', {'url':category_url,'is_first':is_first}, function(e){
				$('.img_link_cell img', $(this)).attr('src', arrow_on);
				this.className = e.data['is_first'] ? 'main_nav_bg_leftcell_on' : 'main_nav_bg_on';
				this.style.cursor = 'pointer'; 
			});
			td.bind('mouseout', {'url':category_url}, function(e){
				$('.img_link_cell img', $(this)).attr('src', arrow_off);
				this.className = '';
				this.style.cursor = 'default'; 
			});
			//alert('binding td click to this url: ' + category_url);
			td.bind('click', {'url':category_url}, function(e){
				window.location.href = e.data['url'];
			});
		}

		//dropdown menu triggers
		var menu_div = td.attr('id') + '_menu';
		td.bind('mouseover', {'menu_div':menu_div}, function(e){
			divMenu.show(e.data['menu_div'], this, 0, 46);
		});
		td.bind('mouseout', {'menu_div':menu_div}, function(e){
			divMenu.hide(e.data['menu_div']);
		});
	});
	
	//set cell click on the cart button/area thing
	$('#main_nav_cart').bind('mouseover', function(e){
		this.style.cursor = 'pointer'; 
	})
	$('#main_nav_cart').bind('mouseout', function(e){
		this.style.cursor = 'default'; 
	})
	$('#main_nav_cart').bind('click', function(e){
		var show_cart_url = $('a', $(this)).attr('href');
		window.location.href = show_cart_url; 
	})

	//fix widths of menu containers.
	$('.menudiv').each(function() {
		var menucols = $('div.menudiv_column_container', $(this));
		var cols_count = menucols.length;
		var cols_width = $(menucols[0]).width();
		var menu_width = cols_count * (cols_width+0);
		$(this).width(menu_width);
	});
	
	//bind hover and click stuff for dropdown menuitems.
	$('div.menudiv_menuitem').each(function(){
		var menuitem_div = $(this);
		menuitem_div.bind('mouseover', function(e){
			$(this).addClass('menudiv_menuitem_hover');
			this.style.cursor = 'pointer'; 
		});
		menuitem_div.bind('mouseout', function(e){
			$(this).removeClass('menudiv_menuitem_hover');
			this.style.cursor = 'default'; 
		});
		var category_url = $('a', menuitem_div).attr('href');
		if (category_url) {
			menuitem_div.bind('click', {'url':category_url}, function(e){
				window.location.href = e.data['url'];
			});
		}
	});
				
	//setup leftnav cellclick
	$('.leftnav_cell').each(function(){
		var td = $(this);
		var nav_url = $('a', td).attr('href');
		var live_link = $('td.nav_1_on', td).length ? 1 : 0;
		
		//alert('is first? ' + is_first + 'index was:' + $('.main_nav_row > td').index(this) );
		if (nav_url) {
			td.bind('mouseover', function(e){
				$('td.nav_1_off', $(this)).attr({'class':'nav_1_on','id':'nav_1_on'});
				this.style.cursor = 'pointer'; 
			});
			td.bind('mouseout', {"live_link":live_link}, function(e){
				if (!e.data['live_link']) {
					$('td.nav_1_on', $(this)).attr({'class':'nav_1_off','id':'nav_1_off'});
				}
				this.style.cursor = 'default'; 
			});
			td.bind('click', {'url':nav_url}, function(e){
				window.location.href = e.data['url'];
			});
		}
	});
	
});

function cart_items() {
	//cart items updater.
	var items_nocache_random = Math.round(100000*Math.random());
	var items_url = "/hprf.pl?rm=cart_items&no_cache=" + items_nocache_random;
	$.getJSON(items_url, function(data){
		//get the cart items number span tag and replace the text.
		$('.cart_num_items').text(data['cart_num_items']);
//the stuff commented out below was to show/hide the login/logout text link at the top which has now been switched to a permanent login link and thus the ajax stuff / showhide stuff here is not needed.
//		if (data['logged_in_cx_id']) {
//			$('#client_login_placeholder').hide();
//			$('#client_login_button').hide();
//			$('#client_logout_button').show();
//		} else {
//			$('#client_login_placeholder').hide();
//			$('#client_logout_button').hide();
//			$('#client_login_button').show();
//		}
	});
}