razsor.definePackage("razsor.insurancequote");

var MyBrowser = function() {
};
MyBrowser.openWindow = function(url, title, params) {
    window.open(url, title, params);
};

razsor.defineClass({
    "_className" : "InsuranceQuote",

    "_package" : razsor.insurancequote,

    "_static" : {
        POPUP_WIDTH : 800,
        POPUP_HEIGHT : 600
    },

    "_class" : {
        "_model" : null,
        "_advertId" : "",

        "init" : function(element) {
            this._model = new this.__package.InsuranceQuoteModel(element);
            this._advertId = this._model.getAdvertId();
        },

        "ready" : function() {
            Razsor.Network.post({
                url : "/xhr/insurancequote",
                data : this._model.getQuoteData(),
                successHandler : razsor.bind("_successHandler", this),
                failHandler : razsor.bind("_failHandler", this)
            });
        },

        "_successHandler" : function(data, textStatus, request) {
            var jsonData = eval('(' + data + ')');
            if (jsonData.status === "ok") {
                this._responseReceived(jsonData);
            } else {
                this._ajaxError();
            }
            this._model.makeVisible();
        },
        "_failHandler" : function() {
            this._ajaxError();
            this._model.makeVisible();
        },
        "_responseReceived" : function(jsonData) {
            if (jsonData.hasPrices) {
                var advertQuote = jsonData["AD_" + this._advertId];
                this._model.setLinkAs("Insure from " + advertQuote.annualPremium + " per year", advertQuote.reviewAndBuyUrl);
                this._openInNewWindow(advertQuote.reviewAndBuyUrl);
            } else {
                this._model.setLinkAs("Get an insurance quote", jsonData.customerDetailsUrl);
                this._openInNewPopUpWindow(jsonData.customerDetailsUrl);
            }
        },
        "_ajaxError" : function() {
            this._model.replaceLinkWith("Insurance quotes not available");
        },
        "_openInNewPopUpWindow" : function(windowUrl) {
            var $linkElement = this._model.getLinkElement();
            var self = this;
            $linkElement.click(function(e) {
                e.preventDefault();
                self._openWindow(windowUrl);
                return false;
            });
        },
        "_openInNewWindow" : function(windowUrl) {
            var $linkElement = this._model.getLinkElement();
            $linkElement.click(function(e) {
                e.preventDefault();
                MyBrowser.openWindow(windowUrl, "_blank", "");
                return false;
            });
        },

        "_openWindow" : function(popupLink) {
            var popupWidth = this.__class.POPUP_WIDTH;
            var popupHeight = this.__class.POPUP_HEIGHT;

            var popupLeft = (screen.width - popupWidth) / 2;
            var popupTop = (screen.height - popupHeight) / 2;

            MyBrowser.openWindow(popupLink, "_blank", "width=" + popupWidth + ",height=" + popupHeight + ",left=" + popupLeft + ",top=" + popupTop + ",scrollbars=yes,menubar=no,location=no");
        }
    }
});

razsor.defineClass({
    "_className" : "InsuranceQuoteModel",

    "_package" : razsor.insurancequote,

    "_class" : {
        "_dataSection" : null,

        "init" : function(element) {
            this._dataSection = $(element);
        },

        "getAdvertId" : function() {
            return $.trim(this._dataSection.parent().find('.advert-id').text());
        },
        "setLinkAs" : function(text, href) {
            var link = this._dataSection.find('a');
            link.attr('href', href);
            link.text(text);
        },
        "getQuoteData" : function() {
            var year = this._dataSection.parent().find('.year').text();
            var cid = this._dataSection.parent().find('.cid').text();
            var price = this._dataSection.parent().find('.vehicle-data .price').text();
            return {
                'insuranceQuoteYear' : year,
                'insuranceQuoteCid' : cid,
                'insuranceQuoteValue' : price,
                'insuranceQuoteAdvertId' : this.getAdvertId()
            };
        },
        "replaceLinkWith" : function(message) {
            var insuranceElement = this._dataSection.find('li');
            insuranceElement.children().remove();
            insuranceElement.append('<span class="insurance-error">' + message + '</span>');
        },
        "makeVisible" : function() {
            var insuranceLinkElement = this._dataSection.find('li a');
            insuranceLinkElement.show();
        },
        "getLinkElement" : function() {
            return this._dataSection.find("a");
        }
    }
});

$('document').ready(function() {
    if ($('.requestquote').length > 0) {
        $('.requestquote').each(function(index, element) {
            var quote = new razsor.insurancequote.InsuranceQuote(element);
            quote.ready();
        });
    }
});
