﻿Type.registerNamespace( "CarQuotes" );

CarQuotes.CarSelector = function (element)
{
    CarQuotes.CarSelector.initializeBase(this, [element]);

    this._app_onload$delegate = Function.createDelegate( this, this._app_onload );
    this._app_onunload$delegate = Function.createDelegate( this, this._app_onunload );
    this._styleCascading_selectionChanged$delegate = Function.createDelegate( this, this._styleCascading_selectionChanged );
    
    this._onRequestComplete$delegate = Function.createDelegate( this, this._onRequestComplete );
    this._onRequestError$delegate = Function.createDelegate( this, this._onRequestError );

    this._styleDropdownID = null;
    this._styleCascadingID = null;
    
    this._carNameID = null;
    this._carImgID = null;
    this._carMsrpID = null;
    
    this._noCarImg = null;
    this._noCarName = null;
    this._servicePath = null;
    this._imageTemplateUrl = null;
    
    this._selectedBsID = null;
}

CarQuotes.CarSelector.prototype = {

    initialize: function() {
        CarQuotes.CarSelector.callBaseMethod(this, 'initialize');

        Sys.Application.add_load(this._app_onload$delegate);
        Sys.Application.add_unload(this._app_onunload$delegate);
    },

    dispose: function() {

        if (this._app_onload$delegate != null) {
            Sys.Application.remove_load(this._app_onload$delegate);
            delete this._app_onload$delegate; this._app_onload$delegate = null;
        }
        if (this._app_onunload$delegate != null) {
            Sys.Application.remove_unload(this._app_onunload$delegate);
            delete this._app_onunload$delegate; this._app_onunload$delegate = null;
        }

        CarQuotes.CarSelector.callBaseMethod(this, 'dispose');
    },

    _app_onload: function(sender, e) {
        $find(this._styleCascadingID).add_selectionChanged(this._styleCascading_selectionChanged$delegate);
    },

    _app_onunload: function(sender, e) {
        if (this._styleCascading_selectionChanged$delegate != null) {
            $find(this._styleCascadingID).remove_selectionChanged(this._styleCascading_selectionChanged$delegate);
            delete this._styleCascading_selectionChanged$delegate; this._styleCascading_selectionChanged$delegate = null;
        }
    },

    _styleCascading_selectionChanged: function(sender, args) {
        var bodyStyleID = $get(this._styleDropdownID).value;
        $get(this._selectedBsID).value = bodyStyleID;

        if (bodyStyleID == "") {
            this._showCar(this._noCarName, this._noCarImg, "");
        }
        else {
            this._loadCar(bodyStyleID);
        }
    },
    
    _loadCar: function (bodyStyleID)
    {
        var params = { bodyStyleID: bodyStyleID };
        Sys.Net.WebServiceProxy.invoke(this._servicePath, "GetCarInfo", false, params, this._onRequestComplete$delegate, this._onRequestError$delegate);
    },

    _showCar: function(carName, imgUrl, msrp) {
        var carNameCtl = $get(this._carNameID);
        var carImgCtl = $get(this._carImgID);
        var carMSRPCtl = $get(this._carMsrpID);

        this._setInnerText(carNameCtl, carName);
        carImgCtl.src = imgUrl;
        this._setInnerText(carMSRPCtl, msrp);
    },

    _setInnerText: function(ctl, text) {
        if (document.all)
            ctl.innerText = text;
        else
            ctl.textContent = text;
    },

    _onRequestComplete: function(result, userContext, methodName) {
        var carImgCtl = $get(this._carImgID);
        var url = String.format(this._imageTemplateUrl, carImgCtl.width, carImgCtl.height, result.BodyStyleID);
        this._showCar(result.FullName, url, result.Msrp);
    },
    _onRequestError: function(webServiceError, userContext, methodName) {
        var carNameCtl = $get(this._carNameID);
        if (webServiceError.get_timedOut())
            carNameCtl.value = "Web service call timeout";
        else
            carNameCtl.value = "Web service call error: " + webServiceError.get_statusCode();
    },

    hasSelectedCar: function() 
    {
        var bodyStyleID = $get(this._selectedBsID).value;
        return bodyStyleID != null && bodyStyleID.length > 0 && bodyStyleID != "-1";
    },

    selectCar: function (id)
    {
        $get(this._selectedBsID).value = id;
        this._loadCar(id);
    }
};

CarQuotes.CarSelector.registerClass('CarQuotes.CarSelector', Sys.UI.Control);
if( typeof(Sys) !== 'undefined' ) Sys.Application.notifyScriptLoaded();

