/**
 * JavaScript behaviors for the product controller.
 *
 * @copyright  Copyright (c) 2005-2008 Found Line, Inc. (http://www.foundline.com/)
 * @version    $Id: product.js 196 2008-09-08 17:36:12Z bradley.holt $
 */

$("document").ready(function() {
    $("img.add-button-disabled").hide();
    var locationParamsAndValues = window.location.pathname.split("/");
    var action = locationParamsAndValues[2];
    var initialParam = locationParamsAndValues[3]
    var initialValue = locationParamsAndValues[4];
    locationParamsAndValues = locationParamsAndValues.splice(5);
    var newLocation = locationParamsAndValues.join("/");
    if (newLocation.length > 1) {
        window.location = "/product/" + action + "/" + initialParam + "/" + initialValue + "/#" + newLocation;
    } else {
        $.history.init(function (hash) {
            if (hash.length > 0) {
                var paramsAndValues = hash.split("/");
                var params = new Array();
                var param;
                var isParam = true;
                for (var i in paramsAndValues) {
                    var paramOrValue = paramsAndValues[i];
                    if (isParam) {
                        param = paramOrValue;
                    } else {
                        params[param] = paramOrValue;
                    }
                    isParam = !isParam;
                }
                for (var i in params) {
                    //$(":radio.product-attr-" + i).removeAttr("checked");
                    $(":radio#product-attr-" + params[i]).attr("checked", "checked");
                }
                var sizeValueId = extractAttributeValue($(":radio.product-attr-size:checked").get(0).id);
                var colorValueId = extractAttributeValue($(":radio.product-attr-color:checked").get(0).id);
                var productImage = "/media/product/" + initialValue + "." + sizeValueId + "." + colorValueId + ".png";
                if (jQuery.browser.msie == true && parseFloat(jQuery.browser.version) < 7 && $("img.product-image").eq(0).hasClass("png") == true) {
                    $("img.product-image").eq(0).css("filter", 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + productImage + '",sizingMethod="scale")');
                } else {
                    $("img.product-image").get(0).src = productImage;            
                }
            }            
        });
    }
	$(".attribute :radio:checked").parent().next().addClass("checked");
    //change event is more correct but does not work correctly in IE (surpise!)
	$(".attribute :radio").click(function() {
        $(this).parent().parent().children().removeClass("checked");
		$(this).parent().next().addClass("checked");
		var attribute = extractAttributeValue($(this).attr("class"));
        var value = extractAttributeValue($(this).attr("id"));
        var paramsAndValues = window.location.hash.substr(1).split("/");
        var params = new Array();
        var param;
        var isParam = true;
        for (var i in paramsAndValues) {
            var paramOrValue = paramsAndValues[i];
            if (isParam) {
                param = paramOrValue;
            } else {
                params[param] = paramOrValue;
            }
            isParam = !isParam;
        }
        params[attribute] = value;
        var newLocation = "";
        for (var i in params) {
            newLocation = newLocation + i + "/" + params[i] + "/";
        }
        if (newLocation.length > 1) {
            $.history.load(newLocation);
        }
    });
});

function extractAttributeValue(attr) {
    var attributeSplit = attr.split("-");
    attributeSplit.shift();
    attributeSplit.shift();
    return attributeSplit.join("-");
}