﻿var peaPods = {
    cache: []
};

$(function () {
    peaPods.rollOvers();
    peaPods.cartSummary();
    peaPods.selectedTab();
});

peaPods.preLoad = function(path) {
    peaPods.cache.push($("<img src='" + path + "' />"));
}

peaPods.rollOvers = function() {
    $(".roll-over").each(function() {
        var before = $(this).attr("src");
        var after = before.replace(/\./, "-over.");
        
        peaPods.preLoad(after);
    
        $(this).hover(function() {
            $(this).attr("src", after);
        }, function() {
            $(this).attr("src", before);
        });
    });
};

peaPods.cartSummary = function () {
    var empty = $(".cartsummary_empty");
    var full = $(".cartsummary_full");
    var output = $("#Display_Cart_Summary");

    if (empty.size() > 0) {
        output.html("Your cart is empty.");
    } else if (full.size() > 0) {
        var pattern = /\(Your shopping cart contains (\d+) items* for a total cost of \$(\d+.*\d*)\)/;
        var match = pattern.exec(full.html());

        output.html(match[1] + " items | $" + parseInt(match[2]).toFixed(2));
    }
}

peaPods.selectedTab = function () {

    // If already set, don't change it
    if ($(".nav_selected").length > 0) {
        return;
    }

    // Find the one matching our current URL
    $("li.nav").each(function (index) {
        var target = $("a", this).attr("href");
        var current = window.location.toString();

        if (current.indexOf(target) == (current.length - target.length)) {
            $(this).addClass("nav_hover nav_selected");
        }
    });
};

