function initializeProducts() {
    
    $(".thumbBoxInner").each(function(i){
    	
    	$thumbBox = $(this);
    	
    	$thumbBox.hover(
    		function() {
    			$(this).css({
    				backgroundImage: 'url(/images/products/bg-thumbGradientGreen.jpg)' // set bg image with js to fix lack of :hover support in IE6
    			});
    		},
    		function() {
    			$(this).css({
    				backgroundImage: 'url(/images/products/bg-thumbGradientGray.jpg)' // set bg image with js to fix lack of :hover support in IE6
    			});
    		}
    	);
		
		$thumbBox.click( function() {
			//alert( $('a', this).attr('href') );
			//$('a', this).click();
			window.location =  $('a', this).attr('href');
		});
		
    });

}

function categoryDropdowns() {
	$('#subcategoryBox').each(function(){
		var $menuBox = this;
		var selectedText = $('a.selected', $menuBox).text();
		
		var $menuTrigger = $('<div/>').attr({ className:'selectedText' })
							.append(
								$('<span />').attr({ className:'selectedTextInner' }).text(selectedText)
							).css({
								cursor: 'pointer'
							});
		var $menu = $('> ul', $menuBox).attr({id: 'subcategoryMenu'}).before($menuTrigger);
		
		var hideDelayTimer = null;
		var beingShown = false;
		var shown = false;	
	
		$([$menuTrigger.get(0), $menu.get(0)]).click(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			if (beingShown || shown) {
				$menu.slideUp(menuSpeed, function () {
					shown = false;
				});
			} else {
				// reset position of info box
				beingShown = true;
				
				$menu.slideDown(menuSpeed, function () {
					beingShown = false;
					shown = true;
				});
			}
			return false;
		});
		
		$([$menuTrigger.get(0), $menu.get(0)]).mouseover(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			return false;
		}).mouseout(function () {
			if (hideDelayTimer) clearTimeout(hideDelayTimer);
			hideDelayTimer = setTimeout(function () {
				hideDelayTimer = null;
				$menu.slideUp(menuSpeed, function () {
					shown = false;
				});
	
			}, hideDelay);
	
			return false;
		});
	
	});	
	
}

function updateLocationHash(parmString) {
	var newPageParms = mergeParms(window.location.hash, parmString);
	window.location.hash = '#' + removeMatchingParms(newPageParms, window.location.search);
}
function ajaxProductRefresh(parmArray){
	var parts = window.location.pathname.split("/");
	var file = parts[parts.length-1];
	
	$("#productThumbs").load("/ajaxproducts/" + file + "?" + makeParmString(parmArray),function (responseText) { 
		ajaxifyPageLinks();
		initializeProducts();	
		
		var parms = getParms(window.location.hash);
		var subcatMenu = $('#subcategoryMenu');
		var subcatMenuHide = $('#subcategoryMenuHide');
		if (subcatMenu != null && subcatMenuHide != null) {
			$('#subcategoryBox > .selectedText > .selectedTextInner').text($('#subcategoryMenuHide > li > .selected').text());
			subcatMenu.html(subcatMenuHide.html());
			ajaxifySubcategoryButtons();
		}
		
		var thumbSizeDefList = $('#thumbSizeDefList');
		var thumbSizeDefListHide = $('#thumbSizeDefListHide');
		if (thumbSizeDefList != null && thumbSizeDefListHide != null) {
			thumbSizeDefList.html(thumbSizeDefListHide.html());
			ajaxifySizeButtons();
		}	
		
		var discontinued = $('#discontinued');
		var discontinuedHide = $('#discontinuedHide');
		if (discontinued != null && discontinuedHide != null) {
			discontinued.html(discontinuedHide.html());
			ajaxifyDiscontinuedButton();
		}	
		
		
	
		
	});
}	
function discontinuedAnchorRefresh(parmArray) {
	if (parmArray.active == 'false') {
  		$("#discontinuedMessage").text('Discontinued products are now being shown.');
 		$("#discontinuedAnchor").text('Hide').removeClass("showToggled").addClass("hideToggled");
	} else {
     	$("#discontinuedMessage").text('Discontinued products are currently hidden.');
    	$("#discontinuedAnchor").text('Show').removeClass("hideToggled").addClass("showToggled");
	}
}
function thumbSizeDefRefresh(parmArray) {
	if (parmArray.imageSize == 'small') {
		$('#thumbPickSmall').addClass('selected');
		$('#thumbPickLarge').removeClass('selected');
	}
	else if (parmArray.imageSize == 'large') {
		$('#thumbPickLarge').addClass('selected');
		$('#thumbPickSmall').removeClass('selected');
	}
}
function supportTabRefresh(parmArray) {
	if (parmArray.support == 'open') {
		$("#tabBGmid").animate({opacity: 1.0}, 300);
        $("#tabBGmid").slideDown(menuSpeed);
        $("#supportTabAnchor").addClass("selected");
	} else {
		$("#tabBGmid").animate({opacity: 1.0}, 300);
        $("#tabBGmid").slideUp(menuSpeed);
        $("#supportTabAnchor").removeClass("selected");
	}
}
function ajaxifyPageLinks(){
	$('.pageBTN').click(function(event){
		event.preventDefault();
		refreshProducts(this.rel);
	});
}

function ajaxifySizeButtons(){
	$('.sizeBTN').click(function(event) {
		event.preventDefault();
		refreshAll(this.rel);
	});
}	

function ajaxifySubcategoryButtons(){
	$('.subcategoryBTN').click(function(event) {
		event.preventDefault();
		refreshProducts("pageNumber=1&subcategoryId=" + this.rel);
		
	});
}
function ajaxifyDiscontinuedButton(){
    $("#discontinuedAnchor").toggle(
	        function(event) {
	         event.preventDefault();
	         var hideAnchorText = $("#discontinuedAnchor").html();
	            if (hideAnchorText == 'Show') {
	            	refreshAll("pageNumber=1&active=false");
	            } else {
	            	refreshAll("pageNumber=1&active=true");
	            }
         }, function(event) {
         	event.preventDefault();
	         var hideAnchorText = $("#discontinuedAnchor").html();
	         if (hideAnchorText == 'Hide') {
	        	 	refreshAll("pageNumber=1&active=true");
	            } else {
	            	refreshAll("pageNumber=1&active=false");
	            }
         }
     );
}	
function ajaxifySupportTabAnchor() {
    $("#tabBGmid").hide();
    $("#supportTabAnchor").toggle(
           function(event) {
        	event.preventDefault();
            var hash = window.location.hash;
        	if(hash.match('support=open')) {
        		refreshSupportTab("support=closed");
        	}
        	else {
                var hideAnchorText = $("#discontinuedAnchor").html();
                if (hideAnchorText == 'Show') {
                	refreshAll('support=open&pageNumber=1&active=false');
                } else {
                	refreshAll("support=open");
                }
        	}
        }, function(event) {
        	event.preventDefault();
        	var hash = window.location.hash;
        	if(hash.match('support=open')) {
        		refreshSupportTab("support=closed");
        	}
        	else {
        		refreshSupportTab("support=open");
        	}
        }
    );
	
}
function getNewParms(parmString) {
	var base = mergeParms(window.location.search, window.location.hash);
	var parms = mergeParms(base, parmString);
	parms = getParms(parms);
	return parms;
}
function refreshSupportTab(parmString) {
	if (parmString == null || parmString == undefined || parmString == '' || parmString == '#' || parmString == '?') {
		return;
	}
	var parmArray = getNewParms(parmString);
	supportTabRefresh(parmArray);
	updateLocationHash(parmString);
}
function refreshAll(parmString) {
	if (parmString == null || parmString == undefined || parmString == '' || parmString == '#' || parmString == '?') {
		return;
	}
	var parmArray = getNewParms(parmString);
	ajaxProductRefresh(parmArray);
	discontinuedAnchorRefresh(parmArray);
	thumbSizeDefRefresh(parmArray);
	supportTabRefresh(parmArray);
	updateLocationHash(parmString);
}
function refreshProducts(parmString) {
	if (parmString == null || parmString == undefined || parmString == '' || parmString == '#' || parmString == '?') {
		return;
	}
	var parmArray = getNewParms(parmString);
	ajaxProductRefresh(parmArray);
	updateLocationHash(parmString);
}
$(document).ready(function() {
    initializeProducts(); // moved thumbbox initialization to separate function to call when ajax content is refreshed.
    categoryDropdowns();
    ajaxifySizeButtons();	
    ajaxifyDiscontinuedButton();
    ajaxifySupportTabAnchor();
    ajaxifySubcategoryButtons();
    ajaxifyPageLinks();
    refreshAll(window.location.hash);
});
