﻿TextKing.InlineListing = (function (TextKing, $) {

    var isDisabled = false;

    var refreshAlert = "";

    function getScrollXY2() {
        var scrOfX = 0, scrOfY = 0;
        if (typeof (window.pageYOffset) == 'number') {
            //Netscape compliant
            scrOfY = window.pageYOffset;
            scrOfX = window.pageXOffset;
        } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
            //DOM compliant
            scrOfY = document.body.scrollTop;
            scrOfX = document.body.scrollLeft;
        } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
            //IE6 standards compliant mode
            scrOfY = document.documentElement.scrollTop;
            scrOfX = document.documentElement.scrollLeft;
        }
        return [scrOfX, scrOfY];
    };

    TextKing.setRefreshAlert = function (message) {
        refreshAlert = message;
    };

    TextKing.setTranslator = function () {
        if (!isDisabled) {
            if (_gaq) {
                _gaq.push(['_trackEvent', 'Inline_Profile', 'Uebersetzer_Waehlen', 'Klick_Uebersetzer_Waehlen_Gueltig']);
            }
            document.forms["set-translator-form"].submit();
        }
        else {
            if (_gaq) {
                _gaq.push(['_trackEvent', 'Inline_Profile', 'Uebersetzer_Waehlen', 'Klick_Uebersetzer_Waehlen_Ungueltig']);
            }
            alert(refreshAlert);
        }
    };

    TextKing.changeTranslator = function () {
        if (!isDisabled) {
            document.forms["change-translator-form"].submit();
        }
        else {
            alert(refreshAlert);
        }
    };

    TextKing.enableControls = function () {
        isDisabled = false;
    };

    TextKing.disableControls = function () {
        isDisabled = true;
    };

    TextKing.viewTranslatorProfileInline = function (translatorPK) {
        $.ajax(
        {
            type: "GET",
            url: '/ViewProfile/LoadProfileOverviewLightBox',
            data: { translatorPK: translatorPK },
            success: function (response) {
                var windowWrapper = $('#ProfileViewLightbox');
                windowWrapper.bind('open', function (e) {
                    windowWrapper.css('top', 50 + getScrollXY2()[1] + 'px');
                    if (windowWrapper.height() > $(window).height()) {
                        desiredHeight = $(window).height() - 70;
                        windowWrapper.height(desiredHeight);
                        $('#ProfileViewLightbox > .t-window-content.t-content').height(
                            (desiredHeight < 400 ? 400 : (desiredHeight - 50))
                        );
                    }
                });
                var winHeader = "#ProfileViewLightbox" + " .t-header";
                $(winHeader).show();
                var winContent = "#ProfileViewLightbox" + " .t-window-content";
                $(winContent).css({ 'overflow-x': 'hidden' });
                var lightbox = windowWrapper.data('tWindow');
                lightbox.content(response);
                lightbox.title(" ");
                lightbox.center().open();
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert("ERROR: " + xhr.responseText);
            }
        }
        );
    };


    return TextKing;
})(TextKing, jQuery);
