﻿OverlayView = function() {

    var _obj;
    return {
        init: function(obj) {
            _obj = obj;

            $("body").append("<div id=\"popUpView\"><span /></div>");
            $("#popUpView").hide();

            $("a.view-details-link").each(function() {
                var href = $(this).attr("href");
                $(this).click(function() {
                    OverlayView.launch({ url: href });
                    return false;
                });
            });

        },
        launch: function(obj) {

            var url = document.location.href
            url = url.substr(0, url.indexOf("/", 8));

            if (typeof addthis_share != "undefined") {
                // set the add this URL to be correct for this item, and not the host page
                addthis_share.url = url + obj.url;
            }

            _obj.url = obj.url;

            // track here
            trackPopUnder("view", obj.url);

            blockForms(false);

            $.ajax({
                url: obj.url,
                beforeSend: function(xhrObj) {
                    xhrObj.setRequestHeader("X-Request-Type", "application/remote-form");
                    waitMessage($so("TextRemoteWaitMessage"));
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    generalError($so("ErrorGeneralNetworkIssue"), true);
                },
                success: function(data) {
                    $("#popUpView").html(data);
                    OverlayView.render();
                },
                complete: function() {
                    killWaitMessage();
                }
            });
        },
        render: function() {

            DropShadows.addShadow($el("popUpView"), "drop-shadow-entry");
            //RemoteVote.init(_obj, null); //No longer voting instead having fb like

            addCloseToPopup("#popUpView", OverlayView.close);
            styleUpSelects($el("popUpView"));
            //replaceSubmitElement($el("likeSubmit"), "vote-style"); //No longer have submit button
            replaceSubmits($el("popUpView"));
            replaceLinks($el("popUpView"));

            addExternalTargets();

            addLightbox("#popupItem .image a");

            if (typeof addthis != "undefined") {
                addthis.toolbox(".addthis_toolbox");
            }

            this.center();

            videoEmbed();
            setupShareLinks(_obj.url);

            $("#popUpView").show();
        },
        center: function() {
            $("#popUpView div.image img").load(function() {
                $("#popUpView").center();
            });

            $("#popUpView").center();

            if ($(window).height() < $('#root').height())
                $('#popUpView').css({ 'margin-top': 0, 'position': 'absolute', 'top': 0 });

        },
        close: function() {
            $("#popUpView").empty();
            $("#popUpView").hide();
            hideGeneralError();
            unBlockForms(false);
        }
    }
} ();

