//-----------------------------------------------------------------------
// Copyright (C) Motorwebs Corporation. All rights reserved.
//-----------------------------------------------------------------------
// Configurator Main
 
var __config = null;
var __selectedNav = null;
var __selectedNavClass = "";
var __selectedSubNav = null;
var __selectedSubNavClass = "";
var __selectedColor = null;
var __selectedColorInterior = null;

var __stateHistory = [];
var __stateHistoryIndex = 0;

function pageLoad() {
    Sys.Application.add_navigate(onNavigate);
    __config = $find('config');
}

function pageUnload() {
    Sys.Application.remove_navigate(onNavigate);
}

function onSuccess(response, context, methodName) {
    __config.onServiceSuccess(response, context);
}

function onFailure(error, context, methodName) {
    var msg = "The " + methodName + " method called by " + context + " Failed! - " + error.get_message();
    alert(msg);
}

function onNavigate(sender, state) {

    if (location.hash != "") {
        // summary page
        if (location.hash == "#") {
            window.location.href = location.href.substring(0, location.href.indexOf("#"));
        }
        else {
           
            var history = $common.createHistoryObj(location.hash);
            var s = __stateHistory[history.index];
            if (s) {
               
                var request = new Motorwebs.NET35.VehicleConstruct.ConfigRequest();
                request.Type = history.type;
                request.ConfigState = s;

                setNav(history.type);
                setSubNav(history.type);

                switch (history.type) {
                    case "optionToggle":
                        setSubNav("options-subnav");
                        request.OptionCode = history.code;
                        break;
                    case "selectColorInterior":
                        var color = $get(history.colorID);
                        if (!color) {
                            setSubNav("colors-subnav");
                            request.Type = "colors-subnav";
                            VehicleConstruct.Configure(request, onSuccess, onFailure, "colors-subnav");
                            return;
                        }
                        setColorInterior(color);
                        request.ColorID = history.colorID;
                        break;
                    case "selectColor":
                        var color = $get(history.colorID);
                        if (!color) {
                            setSubNav("colors-subnav");
                            request.Type = "colors-subnav";
                            VehicleConstruct.Configure(request, onSuccess, onFailure, "colors-subnav");
                            return;
                        }
                        setColor(color);
                        request.ColorID = history.colorID;
                        break;
                    case "styleSelected":
                        request.StyleID = history.sid;
                        break;
                }

                VehicleConstruct.Configure(request, onSuccess, onFailure, history.type);                
            }
            else {

                // if not a object then is summary page
                var so = state.get_state();
                if (so.type == undefined) {
                    window.location.href = location.href.substring(0, location.href.indexOf("#"));
                    return;
                }

                var cs = __config.get_state();
                __stateHistory[__stateHistoryIndex] = cs.value;
                __stateHistoryIndex += 1;
               
                var request = new Motorwebs.NET35.VehicleConstruct.ConfigRequest();
                request.Type = so.type;
                request.ConfigState = cs.value;

                setNav(so.type);
                setSubNav(so.type);

                switch (so.type) {
                    case "optionToggle":
                        request.OptionCode = so.code;
                        break;
                    case "selectColorInterior":
                        setColorInterior($get(so.colorID));
                        request.ColorID = so.colorID;
                        break;
                    case "selectColor":
                        setColor($get(so.colorID));
                        request.ColorID = so.colorID;
                        break;
                    case "styleSelected":
                        request.StyleID = so.sid;
                        break;
                }

                VehicleConstruct.Configure(request, onSuccess, onFailure, so.type);
            }
        }
    }
}

function setNav(type) {
    switch (type) {
        case "editorials":
            if (__config.get_editorialsNav() != __selectedNav) {
                if (__selectedNav) __selectedNav.className = __selectedNavClass;
                __selectedNav = __config.get_editorialsNav();
                __selectedNav.className = "editorialsNavOn";
                __selectedNavClass = "editorialsNavOff";
            }
            break;
        case "standardOptions":
            if (__config.get_standardOptionsNav() != __selectedNav) {
                if (__selectedNav) __selectedNav.className = __selectedNavClass;
                __selectedNav = __config.get_standardOptionsNav();
                __selectedNav.className = "standardOptionsNavOn";
                __selectedNavClass = "standardOptionsNavOff";
            }
            break;
        case "techSpecs":
            if (__config.get_techSpecsNav() != __selectedNav) {
                if (__selectedNav) __selectedNav.className = __selectedNavClass;
                __selectedNav = __config.get_techSpecsNav();
                __selectedNav.className = "techSpecsNavOn";
                __selectedNavClass = "techSpecsNavOff";
            }
            break;
        case "consumerInfo":
            if (__config.get_consumerNav() != __selectedNav) {
                if (__selectedNav) __selectedNav.className = __selectedNavClass;
                __selectedNav = __config.get_consumerNav();
                __selectedNav.className = "consumerInfoNavOn";
                __selectedNavClass = "consumerInfoNavOff";
            }
            break;
        case "build":
            if (__config.get_buildNav() != __selectedNav) {
                if (__selectedNav) __selectedNav.className = __selectedNavClass;
                __selectedNav = __config.get_buildNav();
                __selectedNav.className = "buildNavOn";
                __selectedNavClass = "buildNavOff";
            }
           setSubNav("colors-subnav");
            break;
    }
}

function setSubNav(type) {
    var msg = null;
    switch (type) {
        case "colors-subnav":
            if (__config.get_colorsSubNav() != __selectedSubNav) {
                if (__selectedSubNav) __selectedSubNav.className = __selectedSubNavClass;
                __selectedSubNav = __config.get_colorsSubNav();
                __selectedSubNav.className = "colorsSubNavOn";
                __selectedSubNavClass = "colorsSubNavOff";
                msg = __config.get_subNavMessage();
                msg.className = "configSubNavMessage-colors";
            }
            break;
        case "options-subnav":
            if (__config.get_optionsSubNav() != __selectedSubNav) {
                if (__selectedSubNav) __selectedSubNav.className = __selectedSubNavClass;
                __selectedSubNav = __config.get_optionsSubNav();
                __selectedSubNav.className = "optionsSubNavOn";
                __selectedSubNavClass = "optionsSubNavOff";
                msg = __config.get_subNavMessage();
                msg.className = "configSubNavMessage-options";
            }
            break;
        case "quote-subnav":
            if (__config.get_quoteSubNav() != __selectedSubNav) {
                if (__selectedSubNav) __selectedSubNav.className = __selectedSubNavClass;
                __selectedSubNav = __config.get_quoteSubNav();
                __selectedSubNav.className = "quoteSubNavOn";
                __selectedSubNavClass = "quoteSubNavOff";
                msg = __config.get_subNavMessage();
                msg.className = "configSubNavMessage-quote";
            }
            break;
    }
}

function setColor(color) {
    // if color is already selected de-select it and return
    if (color == __selectedColor) {
        color.parentNode.className = "configColor";
        __selectedColor = null;
        return;
    }
    // de-select previous selected
    if (__selectedColor) __selectedColor.parentNode.className = "configColor";
    color.parentNode.className = "configColorSelected";

    __selectedColor = color;
    __config.setColorizedImage(parseInt(color.parentNode.id) - 1);
}

function setColorInterior(color) {
    // if color is already selected de-select it and return
    if (color == __selectedColorInterior) {
        color.className = "configColor";
        __selectedColorInterior = null;
        return;
    }
    // de-select previous selected
    if (__selectedColorInterior) __selectedColorInterior.className = "configColor";
    color.className = "configColorSelected";
    __selectedColorInterior = color;
}