$(document).ready(function()
{
	$("div#color-select a").click(function()
	{
		var largePath = $(this).attr("href");
		var radioID = $(this).attr("title");		
		var popupImage = $(this).attr("rel");

		$("#product-image").attr({ src: largePath });
		$("#click_product_image_link").attr({ href: popupImage });
		$("#enlarge_image_link").attr({ href: popupImage });
		setCheckedValue(document.forms['addToCartForm'].elements['color'], radioID);
		return false;
	});

	$("div#color-select-radio input").click(function()
	{
		anchorSource = "div#color-select a[title=" + $(this).attr("value") + "]";
		var largePath = $(anchorSource).attr("href");
		var popupImage = $(anchorSource).attr("rel");
		
		$("#product-image").attr({ src: largePath });
		$("#click_product_image_link").attr({ href: popupImage });
		$("#enlarge_image_link").attr({ href: popupImage });
	});	
	
	$("a#click_product_image_link, a#enlarge_image_link").click(function()
	{	
		var largePath = $(this).attr("href");
		openWin(largePath, 520, 720);
		return false;
	});
});

function setCheckedValue(radioObj, newValue) 
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
