//Monkeypatch for IE6
if (!Array.indexOf){
    Array.prototype.indexOf = function(obj) {
        for (var i = 0; i < this.length; ++i) {
            if (this[i] == obj) {
                return i;
            }
        }
        return -1;
    }
}

//preload images
$j('.ro').each(function () {
    $j('<img/>').appendTo('body')
          .css({ display: "none" })
          .attr('src', $j(this).attr('src').replace(/([^.]*)\.(.*)/, "$1_over.$2"));
    $j('<img/>').appendTo('body')
          .css({ display: "none" })
          .attr('src', $j(this).attr('src').replace(/([^.]*)\.(.*)/, "$1_pushed.$2"));
});

//hover them
$j('.ro').hover(
    function () {
        t = $j(this);
        t.attr('src', t.attr('src').replace(/([^._]*)\.(.*)/, "$1_over.$2"));
    },
    function () {
        t = $j(this);
        t.attr('src', t.attr('src').replace(/([^.]*)_[^_]*\.(.*)/, "$1.$2"));
    }
).mousedown(
    function () {
        t = $j(this);
        t.attr('src', t.attr('src').replace(/([^.]*)_[^_]*\.(.*)/, "$1_pushed.$2"));
    }
).mouseup(
    function () {
        t = $j(this);
        t.attr('src', t.attr('src').replace(/([^.]*)_[^_]*\.(.*)/, "$1.$2"));
    }
);

function getIndexLessThanOrEqualTo(arr, desiredIndex) {
    var index = desiredIndex;
    while (index > 0) {
        if (arr.hasOwnProperty(index)) {
            return {index: index, value: arr[index]};
        }
        --index;
    }
    return null;
};

$j(function() {
    if (typeof(ccyCode) === "undefined" || ccyCode === null || ccyCode === "") {
        ccyCode = "USD";
    }
    if (typeof(ccySymbol) === "undefined" || ccySymbol === null || ccySymbol === "") {
        ccySymbol = "$";
    }
    $j('.currency').each(function () { $j(this).html(ccySymbol); });
    $j(".reflectorPrice").each(function() {
        try {
            var classes = $j(this).attr("class").split(" ");
            var indexOfReflectorPrice = classes.indexOf("reflectorPrice");
            if (indexOfReflectorPrice === -1) {
                throw "Not found";
            }
            var parts = classes[indexOfReflectorPrice + 1].split("_");
            if (parts.length < 2) {
                throw "Invalid class";
            }
            var productName = parts[0];
            var users = parts[1];
            var pricesForProduct = pricesLookup[productName];
            if (pricesForProduct === null || pricesForProduct === undefined) {
                throw "No information for " + productName;
            }
            var priceStructure = getIndexLessThanOrEqualTo(pricesForProduct, users);
            var value;
            switch (parts.length) {
                case 2:
                    if (priceStructure.index === users) {
                        value = priceStructure.value.Price;
                    } else {
                        value = priceStructure.value.Price * users / priceStructure.index;
                    }
                    break;
                case 3:
                    if (parts[2] === "percentsaving") {
                        value = Math.round(100 - ((pricePerLicense / (pricesLookup[productName][1].Price * users)) * 100));
                        if (isNaN(value)) {
                            value = 0;
                        }
                        break;
                    }
            }
            $j(this).html(Math.ceil(value));
        } catch (e) {}
    });
});

// // Menu highlighting done in JS rather than serverside
// if ($j('#support').length > 0)
    // $j('#supportmenu').addClass('selected');
// else if ($j('#product').length > 0 || $j('#comparemain').length > 0)
    // $j('#downloadmenu').addClass('selected');

