﻿
function FormatCurrency(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) { i = 0.00; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) { s += '.00'; }
    if (s.indexOf('.') == (s.length - 2)) { s += '0'; }
    s = minus + s;
    return s;
}

function UpdateCart() {
    var totalPrice = 0.0;
    var totalProducts = 0;
    var cart = "";
    if ($(".componentSelected").size() > 0) {
        $(".componentSelected").each(function(i) {
            if (this.getAttribute("productName") != null && this.getAttribute("productName") != "") {
                totalProducts += 1;
                cart += "<tr><td>";

                if (this.getAttribute("productLink") != null && this.getAttribute("productLink") != "") {
                    cart += "<a target=\"_blank\" class=\"gbProductName\" title=\"View Full Product Page\" href=\"" + this.getAttribute("productLink") + "\">";
                }
                cart += this.getAttribute("productName");
                if (this.getAttribute("productLink") != null && this.getAttribute("productLink") != "") {
                    cart += "</a>";
                }

                cart += "</td><td class=\"gbProductPrice\">";
                if (this.getAttribute("productPrice") != null) {
                    cart += "<span class=\"price\"> $" + FormatCurrency(this.getAttribute("productPrice"));
                    totalPrice += parseFloat(this.getAttribute("productPrice"));
                }
                cart += "</span><a class='component a" + this.getAttribute("categoryId") + " componentSelected gbClearBtn' id='A1' title='Remove Product From Cart' categoryId='" + this.getAttribute("categoryId") + "'><strong>&nbsp;</strong></a>"
                cart += "</td></tr>";
            }
        });
        if (totalProducts > 0) {
            cart = "<table class=\"gbCart\">" + cart;
            cart += "<tr><td class=\"gbTotal\">Sub Total:" + "</td><td class=\"gbPrice\">$" + FormatCurrency(totalPrice) + "</td></tr>";
            cart += "</table>";
            $("#cartWrap").show();
        }
        else {
            cart = "<table>" + cart;
            cart += "<tr><td><p><em>Your Shopping Cart is Empty.</em></td></tr>";
            cart += "</table>";
            $("#cartWrap").show();
        }
    }
    $("#cart").html(cart);
}

this.selectComponent = function() {
    $("div").click(function(e) {
        var $tgt = $(e.target);
        if ($tgt.hasClass("component") || $tgt.parent().hasClass("component")) {
            if (!$tgt.hasClass("component")) $tgt = $tgt.parent();

            var catClass = "." + $tgt.attr("categoryId");
            $(catClass).addClass("hidden");
            if ($tgt.attr("imageDivId")) {
                var id = "#" + $tgt.attr("imageDivId");
                $(id).removeClass("hidden");
            }
            var anchorClass = ".a" + $tgt.attr("categoryId");
            $(anchorClass).removeClass("componentSelected");
            $tgt.addClass("componentSelected");

            var quantityClass = ".qty" + $tgt.attr("categoryId");
            $(quantityClass).val("");

            var quantityId = "#qty" + $tgt.attr("imageDivId");
            $(quantityId).val("1");

            var checkboxClass = ".sel" + $tgt.attr("categoryId");
            $(checkboxClass).attr("checked", false);

            var checkboxId = "#sel" + $tgt.attr("imageDivId");
            $(checkboxId).attr("checked", true);

            UpdateCart();
        }
    });
};        
$(document).ready(function(){
    selectComponent();


    // Top Navigation Drop Down Menu
    $("ul.subnav").parent().append("<span></span>");
	
    $("ul.topnav li span").click(function() { 
	    $(this).parent().find("ul.subnav").slideDown('fast').show();
	    $(this).parent().hover(function() {
	    }, function(){	
		    $(this).parent().find("ul.subnav").slideUp('slow'); 
	    });

	    }).hover(function() { 
		    $(this).addClass("subhover");
	    }, function(){
		    $(this).removeClass("subhover");
    });	


	//Tabbed Navigation
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active content
		return false;
	});    
    
});