﻿/* ------------------------------------------------------------------------
	Class: magipopup
	Use: To popup a simple div in the centre of the screen
	Author: Martin Poucher
	Company: Magico.ie
	Version: 1.0
	Created: 11th March 2009
------------------------------------------------------------------------- */

(function($){
    $.fn.magipopup = function(options) {
        var settings = $.extend({
            elementToDisplay: null,
            popupWidth: "50%",
            popupBackground: "#fff",
            popupBorder: "4px solid #ccc"
        },options);
        
        return $.each($(this), function() {
            $(settings.elementToDisplay).hide();
            $(this).click(function(e){
                var popupDiv = "simple_popup_div";
                $(popupDiv).remove();
                var windowHeight = $(window).height();
                var popupHeight = windowHeight - (windowHeight / 5);
                var strSimple = "<div class='" + popupDiv + "' style='width:" + settings.popupWidth + ";height:" + popupHeight + "px;z-index:9999;'>\
                                    <div class='simple_popup_inner'>";
                strSimple += "<p><a class='simple_close' href='#'>[ x ] Close</a></p>";
                strSimple += $(settings.elementToDisplay).html();
                strSimple += "<p><a class='simple_close' href='#'>[ x ] Close</a></p></div></div>";
                $("body").append(strSimple);
                var getViewportScrollY = getYPosition();
                
                $("." + popupDiv).css({
                    top: getViewportScrollY + (windowHeight / 10) + "px",
                    left: ($(window).width() / 2) - ($("." + popupDiv).width() / 2) + "px",
                    position: "absolute",
                    background: settings.popupBackground,
                    border: settings.popupBorder,
                    overflow: "auto"
                });
                $(".simple_close").click(function(){
                    $("." + popupDiv).remove();
                    return false;
                });
                return false;
            });
        });
    };
})(jQuery);

function getYPosition() {
    var scrollY = 0;
    if( document.documentElement && document.documentElement.scrollTop ) {
        scrollY = document.documentElement.scrollTop;
    }
    else if( document.body && document.body.scrollTop ) {
        scrollY = document.body.scrollTop;
    }
    else if( window.pageYOffset ) {
        scrollY = window.pageYOffset;
    }
    else if( window.scrollY ) {
        scrollY = window.scrollY;
    }
    return scrollY;
}


