$(function(){
	// Detect IE6
	var ie6=$.browser.msie&&($.browser.version == "6.0")&&!window.XMLHttpRequest;
	
	// Shopping cart drop down menu
	$('#sc_menu').hover(function(){//Over
		$('#sc_menu_dropdown').show();
	}, function(){//Out
		$('#sc_menu_dropdown').hide();
	});
	
	// Find the body color
	var bodyBgColor = $('body').css('background-color');
	var bodyColor = $('body').css('color');
	$('.side_nav .selected a').css('color', bodyColor);
	
	if(bodyBgColor == 'transparent') {
		$('.side_nav .selected a').css('background-color', '#FFF');
	} else {
		$('.side_nav .selected a').css('background-color', bodyBgColor);		
	}; 
	
	// TOOLTIP IMAGES FOR PRODUCT THUMBNAILS
	$('.product_box .screenshot').tooltip({ 
	    delay: 600,
	    track: true, 
	    opacity: false,  
	    showURL: false,
	    bodyHandler: function() { 
	        return $("<img/>").attr("src", this.rel).height('400px').width('400px').addClass("product_preview"); 
	    }
	});
	
	// PRODUCT GRAPHIC ZOOM IMAGE HOVER SWAP
	/* ##### Swaps product image with one refrenced in the link's REL attribute on mouseover ##### */
	var originalImage; // remember the product image src
	var currentImage; // the img tag that's moused over
	var currentProduct; // the value of the link's REL attribute
	var artBgndColor; // the art preview's bakground color
	$('#main_content .product_box').not('#product_extra_images .product_box') //exclude additional product view images
	.mouseover(function () {
		originalImage = this.getElementsByTagName("img")[0].src;
		currentImage = this.getElementsByTagName("img")[0];
		currentProduct = this.getElementsByTagName("a")[0].rel;
		currentImage.src = currentProduct; // swap the image
		if (this.getElementsByTagName("a")[0].rev.length > 1) {
			currentImage.style.background = this.getElementsByTagName("a")[0].rev;
		}
	}).mouseout(function () {
		currentImage.src = originalImage; // swap the image
		if (ie6) {
			currentImage.style.background = '#ffffff';
		} else {
			currentImage.style.background = 'url(/resources/images/backgrounds/product_image_box.png) no-repeat top left';
		}
	});
	
	// IE6 Hack to fall back to displaying the sharp cornered background boxes
	if (ie6) {
		$('#main_content .product_box img').not('#product_extra_images .product_box img').css('background', '#ffffff');
	}
	
	// PRELOAD ART PREVIEW IMAGES
	jQuery.preLoadImages('rel', $('#main_content .product_box a'));
	
	// STORE FILTER DROPDOWN
	$('#store_filter').change(function () {
		if ($(this).val() == '') {
			location.href='index.php?main_page=index&manufacturers_id=' + mfrId + '&filter_id=' + catId + zenIdFilter;
		} else {
			location.href='index.php?main_page=advanced_search_result&keyword='+ escape($(this).val()) + '&manufacturers_id=' + mfrId + '&categories_id=' + catId + '&filter_id=' + catId + zenIdFilter;
		}
	});
	
});

// IMAGE PRELOADING FUNCTION
/* The first argument is the attribute that contains the image path
 * All of the other arguments are jquery selectors
 */
(function($) {
  var cache = [];
  $.preLoadImages = function() {
	var attrName = arguments[0];
    for (var i = 1; i<arguments.length; i++) {
    	arguments[i].each( function (index) {
    		var cacheImage = document.createElement('img');
    		cacheImage.src = $(this).attr(attrName);
    		cache.push(cacheImage);	
    	});
    }
  }
})(jQuery)