﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

(function($) {
var iframeId;
    $.preparePopup = function(p) {
        p = $.extend({
            url: '',
            frameId: '',
            title: 'SWSR Popup',
            width: 400,
            height: 400,
            okCallback: null,
            cancelCallback: null,
            showCloseButton: true,
            overlayColor: '#323232',
            overlayOpacity: .5,
            draggable: true,
            sourceHref: null,
            sourceName: null
        }, p);
        var g = {
            createPopup: function() {
			    iframeId=p.frameId;
                $(g.popupDiv).append("<DIV>" +
					/*"<DIV CLASS='TITLE-BAR'>" +
                    //                "<TABLE CELLSPACING='0' CELLPADDING='0' STYLE='WIDTH:100%; HEIGHT:20PX;'>" +
                    //                    "<TR><TD CLASS='LEFT'></TD>" +
                                        "<TD CLASS='CAPTION'>" +
                                            "<TABLE CELLSPACING='0' CELLPADDING='0' STYLE='WIDTH:100%; HEIGHT:20PX;'>" +
                                                "<TR><TD CLASS='CAPTION-TEXT'>" + P.TITLE + "</TD><TD CLASS='CLOSE-HANDLE'></TD></TR>" +
                                            "</TABLE>" +
                                        "</TD>" +
                                        "<TD CLASS='RIGHT'></TD></TR>" +
                                    "</TABLE>" +
                                "</DIV>" +*/
                                "<div class='popup-body'>" +
                                    "<div class='progress' style'display:none;'></div>" +
                                    "<iframe id='" + p.frameId + "' class='popup-frame' onload='javascript:$(this).prev().hide();'></iframe>" +
                                "</div></div>");
            },
            showPopup: function() {
                this.showPopupOverlay();
                g.popupDiv.css({
                    zIndex: this.getNextZindex(),
                    height: p.height,
                    width: p.width
                });
                g.documentBody.append(g.popupDiv).show();
                g.popupiFrame.css({ height: p.height - 25, width: p.width }).attr('src', p.url);
                g.popupProgress.css({
                    position: this.getPositionByBrowser(),
                    top: (g.popupDiv.height() - g.popupProgress.height()) / 2,
                    left: (g.popupDiv.width() - g.popupProgress.width()) / 2
                }).show();
            },
            setPopupPosition: function() {
                g.popupDiv.css({
                    position: this.getPositionByBrowser(),
                    top: ($(window).height() - p.height) / 2 + 'px',
                    left: ($(window).width() - p.width) / 2 + 'px'
                });
            },
            hidePopup: function() {
                if (p.cancelCallback != null) {
                    eval(p.cancelCallback);
                }
                //$(g.popupDiv).draggable("destroy")
                g.popupDiv.remove();
                //$("#popup_overlay").remove();
                this.hidePopupOverlay()
            },
            showPopupOverlay: function() {
                //g.documentBody.append('<div id="popup_overlay" class="popup-overlay"></div>');
                //var popupOverlay = g.documentBody.find("#popup_overlay");
                var popupOverlay = document.createElement("div");
                $(popupOverlay).attr("id", "popup_overlay");
                $(popupOverlay).addClass('popup-overlay');
                $(popupOverlay).css({
                    position: this.getPositionByBrowser(),
                    zIndex: this.getNextZindex(),
                    top: '0px',
                    left: '0px',
                    width: '100%',
                    height: $(document).height(),
                    background: p.overlayColor,
                    opacity: p.overlayOpacity
                });
                g.documentBody.append(popupOverlay);
            },
            hidePopupOverlay: function() {
                //g.documentBody.find("#popup_overlay").remove();
                var topOverlay = this.getTopMostOverlay();
                if (topOverlay != null) {
                    topOverlay.remove();
                }
            },
            getNextZindex: function() {
                var maxZindex = 99999;
                $("div.swsr-modal-popup").each(function() {
                    if ($(this).css("z-index") > maxZindex) {
                        maxZindex = $(this).css("z-index");
                    }
                });
                $(".popup-overlay").each(function() {
                    if ($(this).css("z-index") > maxZindex) {
                        maxZindex = $(this).css("z-index");
                    }
                });
                return parseInt(maxZindex, 10) + 1;
            },
            getTopMostOverlay: function() {
                var maxOverlayzIndex = 0;
                var topMostOverlay = null;
                $(".popup-overlay").each(function() {
                    if ($(this).css("z-index") > maxOverlayzIndex) {
                        maxOverlayzIndex = $(this).css("z-index");
                        topMostOverlay = $(this);
                    }
                });
                return topMostOverlay;
            },
            getPositionByBrowser: function() {
                //return ($.browser.msie && parseInt($.browser.version) <= 6) ? 'absolute' : 'fixed';
                //return ($.browser.msie) ? 'absolute' : 'fixed';
                return 'absolute';
            },
            setDraggable: function() {
                try {
                    $(g.popupDiv).draggable({ handle: g.popupHeaderDiv });
                    $(g.popupHeaderDiv).css({ cursor: 'move' });
                } catch (e) { /* requires jQuery UI draggables */ }
            }
        };
        g.documentBody = $("BODY");
        var pDiv = document.createElement("div");
        g.popupDiv = $(pDiv);
        $(g.popupDiv).addClass("swsr-modal-popup");
        $(g.popupDiv).attr("InitialHeight", p.height);

        if (p.sourceHref != null) {
            $(g.popupDiv).attr("sourceHref", p.sourceHref);
        }
        if (p.sourceName != null) {
            $(g.popupDiv).attr("sourceName", p.sourceName);
        }
        g.createPopup();

        g.popupHeaderDiv = $("div.title-bar", g.popupDiv);
        g.popupBody = $("div.popup-body", g.popupDiv);
        g.popupiFrame = $("iframe#" + p.frameId, g.popupDiv);
        g.popupProgress = $("div.progress", g.popupDiv);

        g.showPopup();
        g.setPopupPosition();

        $("td.close-handle", g.popupDiv).click(function() {
            g.hidePopup();
        });

        if (p.draggable) {
            //g.setDraggable();
        }
    };

    swsrPopup = function(p, event) {
        var target = null;
        if (event != null) {
            target = event.target || event.srcElement;
            if (target != null) {
                if (target.href != null && target.href != '') {
                    p.sourceHref = target.href;
                }
                else if (target.name != '') {
                    p.sourceName = target.name;
                }
                if ($(target).parents("span.swsr-button:first").length > 0) {
                    target = $(target).parents("a:first").get(0);
                    p.sourceHref = target.href;
                }
                cancelEvent(event);
            }
        }
        $.preparePopup(p);
    };

    closePopup = function(url, isPostback) {
        var g =
        {
            getTopMostOverlay: function() {
                var maxOverlayzIndex = 0;
                var topMostOverlay = null;
                $(".popup-overlay").each(function() {
                    if ($(this).css("z-index") > maxOverlayzIndex) {
                        maxOverlayzIndex = $(this).css("z-index");
                        topMostOverlay = $(this);
                    }
                });
                return topMostOverlay;
            }
        }
        url = url.substring(url.lastIndexOf("/"), (url.length))
        var popup = $(document).find("iframe[src*='" + url + "']");

        if (popup.length == 0) {
            $(document).find("iframe").each(function() {
                var source = $(this)[0].contentWindow.location.href;
                if (source.indexOf(url) >= 0) {
                    popup = $(this);
                    return false;
                }
            });
        }

        var popupParent = popup.parents("div.swsr-modal-popup:first").get(0);
        if (popupParent != null) {
            $(popupParent).draggable("destroy")
            $(popupParent).remove();
            //$("#popup_overlay").remove();
            var overlyToRemove = g.getTopMostOverlay()
            if (overlyToRemove != null) {
                overlyToRemove.remove();
            }
            if (isPostback == true) {
                var popupSourceName = $(popupParent).attr("sourceName");
                if (popupSourceName != null) {
                    eval("__doPostBack('" + popupSourceName + "', '');");
                }
                else {
                    var popupHref = $(popupParent).attr("sourceHref")
                    if (popupHref != null) {
                        eval(popupHref);
                    }
                }
            }
        }
    };
	closePopupFromInside=function(url, isPostback) {	
	 var g =
        {
            getTopMostOverlay: function(doc) {
                var maxOverlayzIndex = 0;
                var topMostOverlay = null;
                $(doc).find(".popup-overlay").each(function() {
                    if ($(this).css("z-index") > maxOverlayzIndex) {
                        maxOverlayzIndex = $(this).css("z-index");
                        topMostOverlay = $(this);
                    }
                });
                return topMostOverlay;
            }
        }
        url = url.substring(url.lastIndexOf("/"), (url.length))
			//var parentDocument=document.parentWindow.parent.document;
			/*Fix for signin pop up close*/
			var parentDocument=$('#'+iframeId,top.document).prevObject;
            var popup = $(parentDocument).find("iframe[src*='" + url + "']");

        if (popup.length == 0) {
            $(parentDocument).find("iframe").each(function() {
                var source = $(this)[0].contentWindow.location.href;
                if (source.indexOf(url) >= 0) {
                    popup = $(this);
                    return false;
                }
            });
        }

        var popupParent = popup.parents("div.swsr-modal-popup:first").get(0);
        if (popupParent != null) {
            //$(popupParent).draggable("destroy")
            $(popupParent).remove();
            //$("#popup_overlay").remove();
            var overlyToRemove = g.getTopMostOverlay(parentDocument)
            if (overlyToRemove != null) {
                overlyToRemove.remove();
            }
            if (isPostback == true) {
                var popupSourceName = $(popupParent).attr("sourceName");
                if (popupSourceName != null) {
                    eval("__doPostBack('" + popupSourceName + "', '');");
                }
                else {
                    var popupHref = $(popupParent).attr("sourceHref")
                    if (popupHref != null) {
                        eval(popupHref);
                    }
                }
            }
        }
	};
    resizePopupHeightforDui = function(url) {

        url = url.substring(url.lastIndexOf("/"), (url.length))
        var popup = $(document).find("iframe[src*='" + url + "']");
        var popupParent = popup.parents("div.swsr-modal-popup:first").get(0);

        if (popupParent != null) {
            var innerContent = $(popup.attr('contentWindow').document).find(".dummy_Main_Content");
            var heightToAdd = 0;
            $(popup.attr('contentWindow').document).find(".dynamic-ui-placeholder").each(function() {

                if ($(this).css("display") != "none")
                    heightToAdd = heightToAdd + $(this).height();
            });

            var diff = $(popupParent).height() - $(popup).height();
            var initialHeight = Number($(popupParent).attr("InitialHeight"));

            var popUpheightforInnerContent = $(popupParent).height();

            $(popup).css({ "height": initialHeight - diff });
            $(popupParent).css({ "height": initialHeight });
            if (innerContent.length > 0) {
                innerContent.css({ "height": (innerContent.attr("InitialHeight") ? Number(innerContent.attr("InitialHeight")) : (innerContent.height() + $(popupParent).height() - popUpheightforInnerContent)) });
            }

            if (heightToAdd > 0) { heightToAdd = heightToAdd + 20; }
            else {
                adjustPopupPosition(popupParent);
                return;
            }

            var heightToApply = $(popupParent).height() + Number(heightToAdd);
            var maxHeight = $(document).height() - $(popupParent).offset().top - 40;

            var applyTopLaterOn = false;
            if (maxHeight < heightToApply) {
                if (($(document).height() - heightToApply) > 0 && (($(document).height() - heightToApply) < $(popupParent).offset().top))
                    $(popupParent).css({ "top": ($(popupParent).offset().top - ($(document).height() - heightToApply)) });
                else
                    $(popupParent).css({ "top": 10 });
            }
            else
                applyTopLaterOn = true;

            maxHeight = $(document).height() - $(popupParent).offset().top - 40;
            if (Number(heightToApply) > Number(maxHeight)) {
                $(popupParent).css({ "height": maxHeight });
                $(popup).css({ "height": maxHeight - diff });
            }
            else {
                $(popupParent).css({ "height": (Number($(popupParent).height()) + Number(heightToAdd)) });
                $(popup).css({ "height": (Number($(popup).height()) + Number(heightToAdd)) });
            }

            if (innerContent.length > 0) {
                innerContent.css({ "height": innerContent.height() + (Number($(popupParent).height()) - initialHeight) });
            }

            if (applyTopLaterOn)
                adjustPopupPosition(popupParent);
        }
    };

    function adjustPopupPosition(popupParent) {
        $(popupParent).css({ "top": ($(document).height() - $(popupParent).height()) / 2 });
    }


})(jQuery);


