var popupImagePath;

function showMiniConfirm(message, callback) {
    $.popup.showMini(null, null, message + '<div class="popupMiniButtons"><a href="javascript:' + callback + '; void(0);"><img class="btnFirst" src="' + pirsApplicationPath + 'Content/Images/buttonAdminConfirm' + pirsApplicationLanguage + '.gif" /></a><a href="javascript: $.popup.hideMini(); void(0);"><img class="btn" src="' + pirsApplicationPath + 'Content/Images/buttonAdminCancel' + pirsApplicationLanguage + '.gif" /></a></div>', { addCloseButton: false });
}
function showMiniAlert(message) {
    $.popup.showMini(null, null, message);
}

$.popup = {
    miniPopupInitialized: false,
    miniPopupVisible: false,
    showMini: function(clickEvent, title, content, options) {
        var settings = jQuery.extend({
            autoSizeControlId: null,
            popupWidth: 400,
            addCloseButton: true
        }, options);

        if (!jQuery.popup.miniPopupInitialized) {
            jQuery('body').append('<div id="popupMiniBackground"></div>');
            jQuery('#popupMiniBackground')
                .css('opacity', '0.6')
                .click(jQuery.popup.hideMini)
                ;

            jQuery("body").append(
'<table border="0" cellpadding="0" cellspacing="0" id="miniPopup">' +
    '<tr>' +
        '<td id="miniPopupTitleContainer">' +
            '<table border="0" cellpadding="0" cellspacing="0" style="width: 100%;">' +
                '<tr>' +
                    '<td id="miniPopupTitle"></td>' +
                    '<td id="miniPopupCloseContainer"><a id="miniPopupClose"><img src="' + pirsApplicationPath + 'Content/Images/popupMiniClose.gif" /></a></td>' +
                '</tr>' +
            '</table>' +
        '</td>' +
    '</tr>' +
    '<tr><td><div id="miniPopupContent"></div></td></tr>' +
'</table>'
);
            jQuery("#miniPopupClose").click(jQuery.popup.hideMini);
            jQuery.popup.miniPopupInitialized = true;
        }

        if (!title || title.length == 0) {
            title = '&nbsp;'
        }
        jQuery('#miniPopupTitle').html(title);
        if (settings.addCloseButton) {
            content += '<div class="popupMiniButtons"><a href="javascript: $.popup.hideMini(); void(0);"><img class="btnFirst" src="' + pirsApplicationPath + 'Content/Images/buttonAdminClose' + pirsApplicationLanguage + '.gif" /></a></div>';
        }
        jQuery('#miniPopupContent').html(content);

        jQuery('#popupMiniBackground').show();

        //        jQuery.popup.repositionBackgrounds();

        jQuery.popup.resizeMini(settings.popupWidth, clickEvent);
        jQuery("#miniPopup").width(settings.popupWidth + 'px').show();
        jQuery.popup.miniPopupVisible = true;

        jQuery.popup.preventMiniBleedThrough();

        if (settings.autoSizeControlId && (settings.autoSizeControlId.length > 0)) {
            jQuery('#' + settings.autoSizeControlId).bind("load", function() {
                jQuery.popup.resizeMini($(this).width(), clickEvent);
            });
        }
    },
    hideMini: function() {
        if (jQuery.popup.miniPopupVisible) {
            jQuery("#miniPopup").hide();
            jQuery('#popupMiniBackground').hide();

            jQuery.popup.allowMiniBleedThrough();

            jQuery.popup.miniPopupVisible = false;
        }
    },
    resizeMini: function(width, clickEvent) {
        if (!jQuery.popup.miniPopupInitialized) return;

        var $miniPopup = jQuery("#miniPopup");
        $miniPopup.width(width + 'px');

        var miniPopupHeight = $miniPopup.height();
        var miniPopupWidth = $miniPopup.width();

        var $window = jQuery(window);
        var windowHeight = $window.height();
        var windowWidth = $window.width();
        var windowScrollTop = $window.scrollTop();
        var windowScrollLeft = $window.scrollLeft();

        if (clickEvent) {
            // HACK Assuming chrome is accounted for in jQuery values

            // Set miniPopupTop
            if (miniPopupHeight < windowHeight) {
                if (clickEvent.pageY - windowScrollTop + miniPopupHeight < windowHeight)
                    $miniPopup.css('top', clickEvent.pageY + 'px');
                else if (clickEvent.pageY - miniPopupHeight > windowScrollTop)
                    $miniPopup.css('top', clickEvent.pageY - miniPopupHeight + 'px');
                else
                    $miniPopup.css('top', windowHeight + windowScrollTop - miniPopupHeight + 'px');
            }
            else $miniPopup.css('top', clickEvent.pageY + 'px');

            // Set miniPopupLeft
            if (miniPopupWidth < windowWidth) {
                if (clickEvent.pageX - windowScrollLeft + miniPopupWidth < windowWidth)
                    $miniPopup.css('left', clickEvent.pageX + 'px');
                else if (clickEvent.pageX - miniPopupWidth > windowScrollLeft)
                    $miniPopup.css('left', clickEvent.pageX - miniPopupWidth + 'px');
                else
                    $miniPopup.css('left', windowWidth + windowScrollLeft - miniPopupWidth + 'px');
            }
            else $miniPopup.css('left', clickEvent.pageX + 'px');
        }
        else $miniPopup.css({
            'top': Math.max(windowScrollTop + ((windowHeight - miniPopupHeight) / 2), 0) + 'px',
            'left': Math.max(windowScrollLeft + ((windowWidth - miniPopupWidth) / 2)) + 'px'
        });
    },

    advancedPopupInitialized: false,
    advancedPopupVisible: false,
    showAdvanced: function(imagePath, parentElementId, content) {
        if (!jQuery.popup.advancedPopupInitialized) {
            jQuery('body').append('<div id="popupAdvancedBackground"></div>');
            jQuery('#popupAdvancedBackground')
                .css('opacity', '0.6')
                .click(jQuery.popup.hideAdvanced)
                ;

            jQuery("body").append(
'<table border="0" cellspacing="0" cellpadding="0" id="popupAdvanced">' +
	'<tr>' +
		'<td width="20"><img src="' + imagePath + '/popupAdvancedLT.png" /></td>' +
		'<td id="popupAdvancedTopLeft">' +
		'<img id="popupAdvancedArrowImage" src="' + imagePath + '/popupAdvancedArrow.png" width="21" height="34" />' +
		'</td>' +
		'<td width="20"><img src="' + imagePath + '/popupAdvancedRT.png" /></td>' +
	'</tr>' +
	'<tr>' +
		'<td rowspan="2" style="background-image:url(' + imagePath + '/popupAdvancedBackgroundLeft.png);background-position:left;">&nbsp;</td>' +
		'<td id="popupAdvancedCloseContainer"><img id="popupAdvancedClose" src="' + imagePath + '/popupAdvancedClose.gif" /></td>' +
		'<td rowspan="2" style="background-image:url(' + imagePath + '/popupAdvancedBackgroundRight.png);background-position:right;">&nbsp;</td>' +
	'</tr>' +
	'<tr>' +
		'<td id="popupAdvancedContent" colspan="0"></td>' +
	'</tr>' +
	'<tr>' +
		'<td width="20"><img src="' + imagePath + '/popupAdvancedLB.png" /></td>' +
		'<td colspan="0" style="background-image:url(' + imagePath + '/popupAdvancedBackgroundBottom.png);background-position:bottom;">&nbsp;</td>' +
		'<td width="20"><img src="' + imagePath + '/popupAdvancedRB.png" /></td>' +
	'</tr>' +
'</table>');
            jQuery.popup.advancedPopupInitialized = true;
        }

        jQuery('#popupAdvancedContent').html(content);

        jQuery('#popupAdvancedBackground').show();

        //        jQuery.popup.repositionBackgrounds();

        var advancedPopup = jQuery('#popupAdvanced');
        var advancedPopupTopLeft = jQuery('#popupAdvancedTopLeft');
        var advancedPopupTopRight = jQuery('#popupAdvancedTopRight');

        jQuery('#popupAdvancedClose')
            .click(function() {
                jQuery.popup.hideAdvanced();
            })
            .css('cursor', 'pointer')
        ;

        var parentElement = jQuery('#' + parentElementId);
        var parentElementPosition = parentElement.position();
        if (parentElement.css('position') == 'absolute') {
            parentElementPosition = parentElement.offset();
        }
        var mainContentElement = jQuery('#MainContentDiv');
        var mainContentElementPosition = mainContentElement.position();

        var advancedPopupLeft = Math.floor(((mainContentElement.width() - advancedPopup.width()) / 2)) + mainContentElementPosition.left;
        var parentElementCenterLeft = Math.floor(parentElementPosition.left + (parentElement.width() / 2));
        var advancedPopupLeftWidth = parentElementCenterLeft - advancedPopupLeft;

        var image = jQuery("#popupAdvancedArrowImage");
        image.css({ 'margin-left': parentElementPosition.left + parentElement.width() / 2 - advancedPopupLeft - 28 });

        //        advancedPopupTopLeft.css('width', advancedPopupLeftWidth + 'px');
        //        advancedPopupTopRight.css('width', (600 - advancedPopupLeftWidth) + 'px');

        advancedPopup.css({
            'left': advancedPopupLeft + 'px',
            'top': (parentElementPosition.top + parentElement.height()) + 'px'
        });
        advancedPopup.show();
        jQuery.popup.advancedPopupVisible = true;

        jQuery.popup.preventAdvancedBleedThrough();
    },
    hideAdvanced: function() {
        if (jQuery.popup.advancedPopupVisible) {
            jQuery("#popupAdvanced").hide();
            jQuery('#popupAdvancedBackground').hide();

            jQuery.popup.allowAdvancedBleedThrough();

            jQuery.popup.advancedPopupVisible = false;
        }
    },


    mainBleedPrevented: false,
    advancedBleedPrevented: false,
    preventMiniBleedThrough: function() {
        if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7) && !jQuery.popup.advancedBleedPrevented) {
            if (jQuery.popup.advancedPopupVisible) {
                jQuery('#popupAdvancedContent select').each(function() {
                    var $this = jQuery(this);
                    $this
                        .hide()
                        .after('<input disabled="disabled" type="text" class="bleedThroughDummy" style="width:' + $this.width() + 'px;" />')
                        ;
                });
                jQuery.popup.advancedBleedPrevented = true;
            }
            else if (!jQuery.popup.mainBleedPrevented) {
                jQuery('select').each(function() {
                    var $this = jQuery(this);
                    $this
                        .hide()
                        .after('<input disabled="disabled" type="text" class="bleedThroughDummy" style="width:' + $this.width() + 'px;" />')
                        ;
                });
                jQuery.popup.mainBleedPrevented = true;
            }
        }
    },
    allowMiniBleedThrough: function() {
        if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7)) {
            if (jQuery.popup.advancedPopupVisible && jQuery.popup.advancedBleedPrevented) {
                selects = jQuery('#popupAdvancedContent select').show();
                jQuery('#popupAdvancedContent .bleedThroughDummy').remove();
                jQuery.popup.advancedBleedPrevented = false;
            }
            else if (jQuery.popup.mainBleedPrevented) {
                selects = jQuery('select').show();
                jQuery('.bleedThroughDummy').remove();
                jQuery.popup.mainBleedPrevented = false;
            }
        }
    },
    preventAdvancedBleedThrough: function() {
        if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7) && !jQuery.popup.mainBleedPrevented) {
            jQuery('select:not(#popupAdvancedContent select)').each(function() {
                var $this = jQuery(this);
                $this
                    .hide()
                    .after('<input disabled="disabled" type="text" class="bleedThroughDummy" style="width:' + $this.width() + 'px;" />')
                    ;
            });
            jQuery.popup.mainBleedPrevented = true;
        }
    },
    allowAdvancedBleedThrough: function() {
        if (jQuery.popup.mainBleedPrevented && jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7)) {
            jQuery('select').show();
            jQuery('.bleedThroughDummy').remove();
            jQuery.popup.mainBleedPrevented = false;
        }
    }
    //    ,
    //    repositionBackgrounds: function() {
    ////        if (jQuery.browser.msie && (parseInt(jQuery.browser.version) < 7)) {
    //            if (jQuery.popup.advancedPopupVisible) {
    //                jQuery('#popupAdvancedBackground').css({ 'background-color': '#FFF', 'position': 'absolute', 'height': jQuery(document).height() + 'px' });
    //            }
    //            if (jQuery.popup.miniPopupVisible) {
    //                jQuery('#popupMiniBackground').css({ 'background-color': '#FFF', 'position': 'absolute', 'height': jQuery(document).height() + 'px' });
    //            }
    //        }
    //    }
};