﻿trs =
{
    addShowHide: function(el) {
        if (el) {
            el.show = function() {
                el.removeClass('Hide');
            };
            el.hide = function() {
                el.addClass('Hide');
            };
        }
    },
    modalShowHide: function(el) {
        if (el) {
            el.jqm({left:'300px'});
            
            el.modalshow = function() {
                el.show();
                el.jqmShow();
            };
            el.modalhide = function() {
                el.hide();
                el.jqmHide();
            }
        }
    },
    dropDownLists:
    {
        yearList: null,
        manufacturerList: null,
        makeList: null,
        slideList: null,
        sleepsList: null,
        lengthList: null
    },
    landingPage: null,
    webservice: null,
    includeCargoWeightCheckBox: null,
    radiobuttons:
    {
        floorPlanName: null,
        typeName: null
    },
    vehicleSearch: function() {
        var query = '?Year=' + trs.dropDownLists.yearList.val();
        query += '&Manufacturer=' + trs.dropDownLists.manufacturerList.val();
        query += '&Make=' + trs.dropDownLists.makeList.val();
        window.location = query;
    },
    modelSearch: function() {
        var query = '?Year=' + trs.dropDownLists.yearList.val();
        query += '&Manufacturer=' + trs.dropDownLists.manufacturerList.val();
        query += '&Make=' + trs.dropDownLists.makeList.val();
        query += '&PageState=1';
        window.location = query;
    },
    box:
    {
        main: null,
        loading: null,
        error: null,
        response: null,
        cmdbar: null,
        closebtn: null,
        showError: function() {
            trs.show();
            trs.box.error.show();
            trs.box.cmdbar.show();
            trs.box.loading.hide();
            trs.box.response.hide();
        },
        showResponse: function() {
            trs.show();
            trs.box.response.show();
            trs.box.cmdbar.show();
            trs.box.loading.hide();
            trs.box.error.hide();
        },
        showLoading: function() {
            trs.show();
            trs.box.loading.show();
            trs.box.cmdbar.show();
            trs.box.error.hide();
            trs.box.response.hide();
        }
    },
    show: function() {
        trs.box.main.modalshow();
        return false;
    },
    hide: function() {
        trs.box.main.modalhide();
        return false;
    },
    referenceControls: function() {
        if (trs.box.main == null) {
            trs.box.main = $('#TowRatingSearch');
            trs.addShowHide(trs.box.main);
            trs.modalShowHide(trs.box.main);
        }

        if (trs.box.loading == null) {
            trs.box.loading = $('#trsLoading');
            trs.addShowHide(trs.box.loading);
        }

        if (trs.box.error == null) {
            trs.box.error = $('#trsError');
            trs.addShowHide(trs.box.error);
        }

        if (trs.box.response == null) {
            trs.box.response = $('#trsResult');
            trs.addShowHide(trs.box.response);
        }

        if (trs.box.cmdbar == null) {
            trs.box.cmdbar = $('#trsCommandBar');
            trs.addShowHide(trs.box.cmdbar);
        }

        if (trs.box.closebtn == null) {
            trs.box.closebtn = $('#trsCloseButton');
            trs.box.closebtn.click(function() { trs.hide(); });
        }

    },
    setError: function(message) {
        trs.box.error.html(message);
        trs.box.showError();
    },
    setResponse: function(message) {
        trs.box.response.html(message);
        trs.box.showResponse();
    },
    getJsonObject: function(data) {
        var jsonObject = {};
        try {
            var result;
            if (data.xml == null) {
                var serializer = new XMLSerializer();
                result = serializer.serializeToString(data);
            } else {
                result = data.xml;
            }
            var regexp = /<[/]?string[^<>]*>|<\?xml[^<>]+>/gi;
            result = result.replace(regexp, "");
            regexp = /\&lt\;/gi;
            result = result.replace(regexp, "<");
            regexp = /\&gt\;/gi;
            result = result.replace(regexp, ">");
            jsonObject = eval('(' + result + ')');
        } catch(e) {
            jsonObject = {errorNum:1,errorText:"Your request could not be handled at this time"};
        }
        return jsonObject;
    },
    getPayload: function(year, manufacturer, make, model) {
        var floorPlan = $("input[name='" + trs.radiobuttons.floorPlanName + "']:checked").val();
        var towableType = $("input[name='" + trs.radiobuttons.typeName + "']:checked").val();
        var payload =
        {
            Year: year,
            Manufacturer: manufacturer,
            Make: make,
            Model: model,
            Type: towableType == null ? '' : towableType,
            FloorPlan: floorPlan == null ? '' : floorPlan,
            Sleeps: trs.dropDownLists.sleepsList.val(),
            Slides: trs.dropDownLists.slideList.val(),
            Length: trs.dropDownLists.lengthList.val(),
            LandingPage: trs.landingPage == null ? '' : trs.landingPage,
            IncludeCargoWeight: trs.includeCargoWeightCheckBox.attr('checked')
            
        };
        return payload;
    },
    findRV: function(element, year, manufacturer, make, model) {
        trs.referenceControls();
        window.setTimeout(function() { trs.show(); trs.box.showLoading(); },0);
        $.post(trs.webservice,trs.getPayload(year,manufacturer,make,model),
            function(data) {
                var responseJson = trs.getJsonObject(data);
                if (responseJson.errorNum == 0) {
                    trs.setResponse(responseJson.responseText);
                } else {
                    trs.setError(responseJson.errorText);
                }
            },'xml');
        

    },
    findTowableRV: function(year, manufacturer, make, model) {
        var query = '?Year=' + year;
        query += '&Manufacturer=' + manufacturer;
        query += '&Make=' + make;
        query += '&VehicleModelID=' + model;
        query += '&PageState=3';
        window.location = query;
    },
    showPage: function(pageIndex, maxIndex) {
        for (var i = 1; i <= maxIndex; i++) {
            var id = '#model-page' + i;
            var link = '#model-pager' + i;
            $(id).css('display', i == pageIndex ? '' : 'none');
            if (i == pageIndex) {
                $(link).removeClass('model-pager');
                $(link).addClass('model-pager-selected');
            } else {
                $(link).removeClass('model-pager-selected');
                $(link).addClass('model-pager');
            }

        }
    },
    init: function(yearList, manufacturerList, makeList, slideList, sleepsList, lengthList,
        floorPlanName, typeName, webservice, landingPage, includeCargoWeightCheckBox) {

        trs.webservice = webservice;
        trs.landingPage = landingPage;
        trs.includeCargoWeightCheckBox = $('#' + includeCargoWeightCheckBox);
        trs.dropDownLists.yearList = $('#' + yearList);
        trs.dropDownLists.manufacturerList = $('#' + manufacturerList);
        trs.dropDownLists.makeList = $('#' + makeList);
        trs.dropDownLists.slideList = $('#' + slideList);
        trs.dropDownLists.sleepsList = $('#' + sleepsList);
        trs.dropDownLists.lengthList = $('#' + lengthList);

        trs.radiobuttons.floorPlanName = floorPlanName;
        trs.radiobuttons.typeName = typeName;

        trs.dropDownLists.yearList.change(function() {
            trs.vehicleSearch();
        });

        trs.dropDownLists.manufacturerList.change(function() {
            trs.vehicleSearch();
        });



        $('#FindModelButton').click(function() {
            trs.modelSearch();
            return false;
        });
    }
    


};

