﻿$(document).ready(function() {
    $("#optionsPanel").hide();
    $("#interiorPanel").hide();
    /* Alert to fix IE6 issues 
    alert("The Cirrus Aircraft Configurator is for reference only.\n\nPlease contact your sales person for up-to-date pricing and configuration.");
    */
    /* Set Fixed Header Scrolltable Style */
    $(".navButton").bind("mouseover", function() {
        $(this).css("color", "#0000ff");
    }).bind("mouseout", function() {
        $(this).css("color", "#000000");
    });
    $("#exteriorNav").bind("click", function() {
        $("#divPrevious").hide();
        $(".navButton").removeClass("selected");
        $(this).addClass("selected");
        $(".configuratorBottom div.bottomPanel").hide();
        $("#exteriorPanel").show();
    }).click();
    $("#interiorNav").bind("click", function() {
        $("#divNext").show();
        $("#divPrevious").show();
        $(".navButton").removeClass("selected");
        $("#optionsNav").removeClass("inactive").removeAttr("disabled");
        $(this).addClass("selected");
        $(".configuratorBottom div.bottomPanel").hide();
        $("#interiorPanel").show();
    });
    $("#optionsNav").bind("click", function() {
        $("#divNext").hide();
        $("#divPrevious").show();
        $(".navButton").removeClass("selected");
        $("#summaryNav").removeClass("inactive");
        $(this).addClass("selected");
        $(".configuratorBottom div.bottomPanel").hide();
        $("#optionsPanel").show();
    });
    
    /* Process selected color */
    $("div.extColorSwatch").bind("click", function() {
        $("div.extColorSwatch").removeClass("selected");
        $(this).addClass("selected");
        $.get("/WebServices/AircraftConfigurator/ExteriorStripeBoxHandler.ashx?m=" + querystring("m") + "&c=" + querystring("id", $("img", this).attr("src")), function(results) {
            $("#exteriorStripeBoxPH").html(results);
            $("#exteriorStripeBoxPH div").bind("click", function() {
                $("#exteriorStripeBoxPH div").removeClass("selected");
                $(this).addClass("selected");
                $("#imgConfiguratorPictureFade").attr("src", $("#imgConfiguratorPicture").attr("src"));
                $("#imgConfiguratorPicture").fadeOut("slow", function() {
                    $(this).attr("src", "/WebServices/AircraftConfigurator/ExteriorImageHandler.ashx?m=" + querystring("m") + "&c=" + querystring("id", $("div.extColorSwatch.selected img").attr("src")) + "&s=" + querystring("id", $("#exteriorStripeBoxPH div.selected img").attr("src"))).fadeIn("slow");
                    $("input[name='exteriorPicture']").val($(this).attr("src"));
                });
                $("#stripePackage").html("<img style=\"height:30px\" src=\"" + $("img", this).attr("src") + "\" alt=\"\" />");
                $("input[name='stripePackage']").val($("img", this).attr("src"));
                $("#summaryNav").addClass("inactive");
                $("#optionsNav").addClass("inactive");
                $("#baseStripeName").html($("#exteriorStripeBoxPH div.selected .extStripeName").html());
                
                /* Retrieve the appropriate interior colors */
                $.get("/WebServices/AircraftConfigurator/InteriorStripeBoxHandler.ashx?m=" + querystring("m") + "&c=" + querystring("id", $("div.extColorSwatch.selected img").attr("src")) + "&s=" + querystring("id", $("#exteriorStripeBoxPH div.selected img").attr("src")), function(results) {
                    $("#interiorStripeBoxPH").html(results + "<div style=\"clear:both\"></div>");
                    
                    $("div.intSwipeSwatch").bind("click", function() {
                        $("div.intSwipeSwatch").removeClass("selected");
                        $(this).addClass("selected");
                        $("#baseInteriorName").html($(".intColorName", this).html());
                        $("#interiorPackage").html("<img style=\"height:40px\" src=\"" + $("img", this).attr("src") + "\" alt=\"\" />");
                        calculateWeightAndPrice();
                        var selectedInterior = $(this);
                        $("input[name='interiorColor']").val($("img", selectedInterior).attr("src"));
                        $("input[name='additionalInteriorCost']").val($("input", selectedInterior).val());
                    });
                    $("#interiorStripeBoxPH div:first").click();
                });
            });
            $("#exteriorStripeBoxPH div:first").click();
        });
        $("#baseColor").html("<img style=\"height:30px\" src=\"" + $("img", this).attr("src") + "\" alt=\"\" />");
        $("#baseColorName").html($("#exteriorColorBoxPH div.selected .extColorName").html());
        var selectedColor = $("div.extColorSwatch.selected");
        $("input[name='baseColor']").val($("img", selectedColor).attr("src"));
        $("input[name='additionalPaintCost']").val($("input", selectedColor).attr("value"));
        calculateWeightAndPrice();
    });
    $("#exteriorColorBoxPH div:first").click();
    
    /* Set hover row style */
    $("tbody.scrollContent tr").bind("mouseover", function() {
        $(this).css("background-color", "#ddd");
    }).bind("mouseout", function() {
        $(this).css("background-color", "#fff");
    }).bind("click", function() {
        var cb = $("input[@type=checkbox]", this);
        if (!cb.attr("disabled")) {
            if (cb.attr("checked"))
                cb.removeAttr("checked");
            else
                cb.attr("checked", "true");
            /* Do the Weight and Total Calculations */
            calculateWeightAndPrice();
        }
    });
    
    $("tbody.scrollContent tr input[@type=checkbox]").bind("mouseup", function() {
        $(this).parent().click();
    });
    
        
    /* Weight and Total Calculation Function */
    function calculateWeightAndPrice() {
        var weight = 0;
        var price = 0;
        $("tbody.scrollContent tr input[@type=checkbox][@checked]").each(function() {
            price += parseInt($("td.priceColumn", $(this).parent().parent().parent()).text().replace(/,/g,""));
            var tmpWeight = $("td.weightColumn", $(this).parent().parent().parent()).text().replace(/,/g,"");
            if (tmpWeight != "-")
                weight += parseInt($("td.weightColumn", $(this).parent().parent().parent()).text());
        });
        if($(".intSwipeSwatch.selected input").attr("value")){
            price += parseInt($(".intSwipeSwatch.selected input").attr("value"));
        }
        weight += parseInt($("div.extColorSwatch.selected input.weight").val());
        price += parseInt($("#exteriorColorBoxPH div.selected input").attr("value"));
        $("#totalPrice").text(formatNumber((parseInt($("#basePrice").text().replace(/\$/g,"").replace(/,/g,"")) + price).toString(),"$"));
        $("#totalWeight").text(formatNumber((parseInt($("#baseWeight").text().replace(/ lbs/g,"").replace(/,/g,"")) + weight).toString(),"") + " lbs");
    }
    
    $(".divPrevious").bind("click", function() {
        switch($(".configuratorNav div.selected").text()) {
            case "Interior":
                $("#exteriorNav").click();
                break;
            case "Options":
                $("#interiorNav").click();
                break;
            case "Summary":
                $("#optionsNav").click();
                break;
        }
    }).bind("mouseover", function() {
        $(this).css("font-weight", "bold");
    }).bind("mouseout", function() {
        $(this).css("font-weight", "normal");
    });
    $(".divNext").bind("click", function() {
        switch($(".configuratorNav div.selected").text()) {
            case "Exterior":
                $("#interiorNav").click();
                break;
            case "Interior":
                $("#optionsNav").click();
                break;
            case "Options":
                $("#summaryNav").click();
                break;
        }
    }).bind("mouseover", function() {
        $(this).css("font-weight", "bold");
    }).bind("mouseout", function() {
        $(this).css("font-weight", "normal");
    });
    function formatNumber(nStr, symbol) {
        nStr += "";
        x = nStr.split('.');
        x1 = x[0]
        x2 = x.length > 1 ? '.' + x[1] : '';
        var rgx = /(\d+)(\d{3})/;
        while (rgx.test(x1)) {
            x1 = x1.replace(rgx, '$1' + ',' + '$2');
        }
        return symbol + x1 + x2;
    }
    
    function querystring(key, url) {
        if (typeof url == "undefined")
            url = window.location.href;
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var results = regex.exec(url);
        if( results == null )
            return "";
        else
            return results[1];
    }
});
