﻿Type.registerNamespace('RIV.Web');

RIV.Web.VehicleMod = function(oArgs) {
    _service = oArgs.service;
    _root = oArgs.root;
    _ctrlVehicle = oArgs.ctrlVehicle;
    _ctrlFirstBtn = oArgs.ctrlFirstBtn;
    _ctrlPrevBtn = oArgs.ctrlPrevBtn; ;
    _ctrlStatus = oArgs.ctrlStatus;
    _ctrlNextBtn = oArgs.ctrlNextBtn;
    _ctrlLastBtn = oArgs.ctrlLastBtn;
    _ctrlImage = oArgs.ctrlImage;
    _divId = oArgs.divId;
    _language = oArgs.language;
    _notFoundMsg = oArgs.notFoundMsg;
    _timeoutMsg = oArgs.timeoutMsg;
    _waitMsg = oArgs.waitMsg;
    _statusTemplate = '{0} of {1}';
    _statusTemplateFrench = '{0} de {1}';
    _ctrlVehicle.VehicleMod = this;
    _currentIndex = 0;

    _ctrlVehicle.onchange = function() {  
      
        _currentIndex = this.selectedIndex;
        _ctrlVehicle.VehicleMod.showVehicleInfo(_currentIndex);
    }

    _ctrlFirstBtn.onclick = function() {
        _currentIndex = 0;
        _ctrlVehicle.VehicleMod.showVehicleInfo(_currentIndex);
    }

    _ctrlPrevBtn.onclick = function() {
        _currentIndex -= 1;

        if (_currentIndex < 0)
            _currentIndex = _ctrlVehicle.length - 1;

        _ctrlVehicle.VehicleMod.showVehicleInfo(_currentIndex);
    }

    _ctrlNextBtn.onclick = function() {
        _currentIndex += 1;

        if (_currentIndex == _ctrlVehicle.length)
            _currentIndex = 0;

        _ctrlVehicle.VehicleMod.showVehicleInfo(_currentIndex);
    }

    _ctrlLastBtn.onclick = function() {
        _currentIndex = _ctrlVehicle.length - 1;
        _ctrlVehicle.VehicleMod.showVehicleInfo(_currentIndex);
    }
}

RIV.Web.VehicleMod.prototype = {
    init: function() {
        this.getVehicles();
    },

    getVehicles: function() {
        _ctrlVehicle.options[0] = new Option(_waitMsg, '-1');
        _ctrlVehicle.disabled = 'true';
        _service.GetVehicleTypes(
            _language,
            function(data) {
                RIV.Web.VehicleMod.locLoadVehicleMods(data);
            },
            function() { }
         );
    },

    showVehicleInfo: function(vehicleIndex) {
        var vtype = parseInt(_ctrlVehicle[vehicleIndex].value);
        _ctrlVehicle.selectedIndex = vehicleIndex;
        _service.GetVehicleById(
            vtype,
            function(data) {
                RIV.Web.VehicleMod.locLoadVehicleInfo(data);
            },
            function() { }
        );
    },

    openVModWindow: function(url) {
        //window.open(url, '', 'toolbar=yes,scrollbars=yes,width=800,height=600,left=75,top=75');
    }
}

RIV.Web.VehicleMod.locLoadVehicleMods = function(data) {
    _ctrlVehicle.disabled = false;
    _ctrlVehicle.remove(0);

    var selectedIndex = _currentIndex;

    for (var i = 0; i < data.length; ++i) {
        if (data[i].IsSelected)
            _currentIndex = selectedIndex = i;

        _ctrlVehicle.options[_ctrlVehicle.length] = new Option(data[i].Name, data[i].Value);
    }

    _ctrlVehicle.VehicleMod.showVehicleInfo(selectedIndex);
}

RIV.Web.VehicleMod.locLoadVehicleInfo = function(data) {
    _ctrlImage.src = _root + data.ImageURL;
    _ctrlImage.style.display = 'block';
    RIV.Web.VehicleMod.locShowContent(data.Description);
    RIV.Web.VehicleMod.locUpdateStatus();
}

RIV.Web.VehicleMod.locUpdateStatus = function() {
if (_language == "en-US")
    _ctrlStatus.value = _statusTemplate.replace('{0}',
            (_currentIndex + 1)).replace('{1}', _ctrlVehicle.length);
else if (_language == "fr-CA")
    _ctrlStatus.value = _statusTemplateFrench.replace('{0}',
            (_currentIndex + 1)).replace('{1}', _ctrlVehicle.length);    
}

RIV.Web.VehicleMod.locShowContent = function(html) {
    var div = $get(_divId);

    if (div != null) {
        div.innerHTML = html;
    }
}

RIV.Web.VehicleMod.registerClass('RIV.Web.VehicleMod');

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();