// JavaScript Document
function xtractFile(data){
var m = data.match(/(.*)\/([^\/\\]+)(\.\w+)$/);
if(m == null) { m = "null"; }
return {path: m[1], file: m[2], extension: m[3]}
}

jQuery(document).ready(function() 
{
/*	$('#menu ul').hide();
	$('#menu li a').click(
		function() {
			$(this).next().slideToggle('normal', function(){ });
			$(this).toggleClass("openCat_link");
			$(this).parent().toggleClass("openCat");
		}
	);
	*/
	
	$('#menu ul').each( 
		function()
		{ 
			$(this).parent().addClass("openCat");
			$(this).prev().addClass("openCat_link");
		}
	);
	// highlight the current side-bar subnav link.
	if (document.getElementById("main_nav")) {
		var sublinks = document.getElementById("main_nav").getElementsByTagName("a");
		for (var i = 0; i < sublinks.length; i++)
		{ if (sublinks[i].href == document.location.href) { sublinks[i].parentNode.className += "_current"; } }
	}
	/*/
	* Check the height of the 6 homepanels & unify their heights, to make it easier to arrange them on the page,
	* regardless of the content within them.
	/*/
	if(document.getElementById("productDetail_container"))
	{	
		var getDivs = document.getElementById("productDetail_container").getElementsByTagName("div");

		var currTallest = 0;
		var totalHeight = 0;
		var theRows = [];
		var rowCheck = 0;
		// Go through each row (assumed to be of 3 panels) and grab the largest height
		for(var i = 0; i < getDivs.length; i++)
		{
			if(getDivs[i].className == "prodDetail_prodTitle")
			{
				if(getDivs[i].offsetHeight > currTallest ) { currTallest =  getDivs[i].offsetHeight}
				if( (i+1)%4 == 0)
				{
					theRows[rowCheck] = currTallest;
					totalHeight += currTallest;
					rowCheck++;
					currTallest = 0; // Reset the tallest each new row.
				}
			}
		}
		// Reset the values, iterate through the panels and set the height
		rowCheck = 0;
		for(var x = 0; x < getDivs.length; x++)
		{
			if(getDivs[x].className == "prodDetail_prodTitle")
			{
				getDivs[x].style.height = theRows[rowCheck]+"px";
				if( (x+1)%4 == 0) { rowCheck++ }
			}
		}
	} /* END HEIGHT CHECK */
	

	$.fn.equalHeights=function() {
		var maxHeight=0;this.each(function(){ 
			if (this.offsetHeight>maxHeight) {
				maxHeight=this.offsetHeight;}
			}
			);
			this.each(function(){ 
			$(this).height(maxHeight + "px"); 
				if (this.offsetHeight>maxHeight) {  
				$(this).height((maxHeight-20-(this.offsetHeight-maxHeight))+"px"); }
				
				}
			);
	};
	
	
	$(".header").click(function() {
		$("#leftCol").css("height", "");
		$("#rightCol").css("height", "");
	}
	);
	
	
	
    $('.showHide a.header').click(function() {
        var $nextDiv = $(this).next();
        var $parentDiv = $(this).parent();
        var $visibleSiblings = $('.showHide div:visible');
        $visibleSiblings.hide();
        
        if($(this).next(':visible')) {
            $nextDiv.hide();
        }
                
        if($(this).next(':hidden')) {
            $nextDiv.show();
        }
        else {
            $visibleSiblings.hide();
 
            
        }
        $("#rightCol,#leftCol").equalHeights();
    
	});
	
	$('.showHide div').hide();
	$("#leftCol,#rightCol").equalHeights();
	
	$("table.img_thumb img").each(function()
	{ 
		$(this).height() > 260 ? $(this).css({"width" : "auto", "height" :260 }) : null;
		//$(this).width() < 166 ? $(this).css({"width" : "auto", "height" : "auto" }) : $(this).css({"width" : 166, "height" : "auto" });
		//$(this).css({"marginTop" : ((260 - $(this).height()) / 2) });
	});

	//adjustProducts(".indivProd_container .wraptocenter");
	
});	

/* BASIC SHOW / HIDE DIVS */
function showHide(divID) {
/*
  var item = document.getElementById(divID);
    $(".showHide div:visible").toggleClass("open");
    $(".showHide div:visible").hide();
	item.className=(item.className=='closed')?'open':'closed';
	$("#rightCol,#leftCol").equalHeights();
	*/
}

$(window).load(function() {
	// *------------------------------------------------------------------------------------------------------- *
	// 	Shrink images down to fit within their wrappers.
	// 	USAGE: 	The parameters of this function is a string list - ala jQuery - of
	// 			the IDs and classes of the various wrappers surrounding the images you wish to shrink
	//adjustThumbs(".indivRange_container .wraptocenter, #mainProduct_image");
	
	// *------------------------------------------------------------------------------------------------------- *
});

function adjustProducts(strArgs)
{
	var imgWrapperWidth, imgWidth, widthDiff;
	var imgWrapperHeight, imgHeight, heightDiff;
	
	jQuery(strArgs).each(function() 
	{
		imgWrapperWidth = jQuery(this).width();
		imgWidth = jQuery(this).find("img").width();
		imgWrapperHeight = jQuery(this).height();
		imgHeight = jQuery(this).find("img").height();
		
		//alert(imgWrapperWidth + ", " + imgWidth + " " + imgWrapperHeight + ", " + imgHeight);
		
		if(imgWidth > imgWrapperWidth)
		{
			jQuery(this).find("img").css({
				"width" : imgWrapperWidth, 
				"height" : "auto"
			});
		}
		var newHeight = jQuery(this).find("img").attr("height");
		jQuery(this).find("img").css({ "margin-top" : (imgWrapperHeight - newHeight )/2 });

});

}


// Adjust various images based on the wrappers containing them. 
// Call function as follows: adjustThumbs(strArgs);
// Where strArgs is a string list of the classes / IDs of the wrappers
function adjustThumbs(strArgs)
{
	var imgWrapperWidth, imgWidth, widthDiff;
	var imgWrapperHeight, imgHeight, heightDiff;
	
	
	jQuery(strArgs).each(function() 
	{
		imgWrapperWidth = jQuery(this).width();
		imgWidth = jQuery(this).find("img").width();
		//alert(imgWrapperWidth + ", " + imgWidth);

		imgWrapperHeight = jQuery(this).height();
		imgHeight = jQuery(this).find("img").height();
		
		var widthProp = imgWidth / imgWrapperWidth;
		var heightProp = imgHeight / imgWrapperHeight;
		
		if(widthProp > 1 && ((1 / widthProp) * imgHeight) > imgWrapperHeight)
		{
			jQuery(this).find("img").css({"width" : parseInt((1 / widthProp) * imgWidth), "height" : parseInt((1 / widthProp) * imgHeight) });
		}
		
		if(heightProp > 1 && ((1 / heightProp) * imgWidth) > imgWrapperWidth)
		{
			jQuery(this).find("img").css({"width" : parseInt((1 / heightProp) * imgWidth), "height" : parseInt((1 / heightProp) * imgHeight) });
		}
		
		widthDiff = jQuery(this).find("img").width() - imgWrapperWidth;
		heightDiff = jQuery(this).find("img").height() - imgWrapperHeight;
		jQuery(this).find("img").css({"margin-left" : parseInt(-(widthDiff / 2)) });
		jQuery(this).find("img").css({"margin-top" : parseInt(-(heightDiff / 2)) });
	});
}

