$(function() {
	// truncate for short description
	$("span.ensemble-product-pDescription .short-description").truncate(40);
	
	$(".ensemble-p-container").each(function(i) {
		
		// setup the swatch image functionality
		$(".swatches",this).click(function() {
			$(".ensemble-product").eq(i).attr("src", $(this).attr("name"));
			$(".ensemble-product").eq(i).attr("alt",$(this).attr("alt"));
		});
		
		// setup the drop-down functionality
		$(".ensemble-product-options select",this).each( function() {
			$(this).change(function() { setTimeout( function() { setImage(i); }, 1); });
		});
	
	});
	
});

function setImage(i) {
	$(".ensemble-product-options select", $(".ensemble-p-container").eq(i)).each(function() {
		if( ($(this).attr("id").indexOf("Color")) != -1 && ( $(this).val() != "" ))
		{
			// grab the color value, chang the main image with it.
			swatchName = $(this).val();
			$(".ensemble-product").eq(i).attr("src", $(".swatches[@alt=" + swatchName + "]",$(".ensemble-p-container").eq(i)).attr("name"));
			$(".ensemble-product").eq(i).attr("alt", swatchName);
		}
	});
};

function shareWithFriend(obj) {
	var oldHREF = $(obj).attr("href");
	var tempHREF = $(obj).attr("href");
	var styleColor = $("#the-ensemble-image").attr("src");
	styleColor = styleColor.substring( styleColor.lastIndexOf('/') + 1,styleColor.length - 1) 
	styleColor = styleColor.split('_')[0];
	tempHREF = tempHREF + "&stylecolor=" + styleColor + "&colorname=" + $("#the-ensemble-image").attr("alt");		
	$(obj).attr("href",tempHREF);
	popupWindow(obj,'emailFriend','location=0,status=0,menubar=0,toolbar=0,resizable=0,width=550,height=525');
	$(obj).attr("href",oldHREF);
};

// brian reindel plugin for truncate
jQuery.fn.truncate = function(max,settings) {
    settings = jQuery.extend({
        chars: /\s/,
        trail: "..."
    }, settings);
    return this.each(function(){
        v = jQuery.trim(jQuery(this).text());
        if (v.length > max) {
            while (max > 0) {
                c = v.charAt(max);
                if (c.match(settings.chars)) {
                    jQuery(this).html(v.substring(0,max) + settings.trail);
                    break;
                }
                max--;
            }
        }
    });
};

function setAddTo(addToType) {
	document.productForm.addTo.value = addToType;
}

