/*
 *  jQueryPopup - jQuery plugin
 *  written by Bumbella
 *  Copyright (c) 2011
 *  Built for jQuery library
 *  http://jquery.com
/*
 *  example for $(".wrapper").jQueryPopup();
 *
 */

$(document).ready(function(){
    $(window).resize(function() {
        var custLightBoxTop = $(document).scrollTop()+(parseInt($(window).height()/2)-(parseInt($('.custLightBox').height()/2)));
        var custLightBoxLeft = (parseInt($(window).width()/2))-(parseInt($('.custLightBox').width()/2));

        $('.custLightBox').css({
            'left':custLightBoxLeft+'px',
            'top':custLightBoxTop+'px'
        })
        

    });
});

(function($){

    var docHeight = function() {
        var db = document.body;
        var dde = document.documentElement;
        var dcheight = Math.max(db.scrollHeight, dde.scrollHeight, db.offsetHeight, dde.offsetHeight, db.clientHeight, dde.clientHeight);
        return dcheight;
    }

    $.fn.jQueryPopup = function(opts){

        var closeHandle = function(popupContainer,width){
        $('.formSubmit',$(popupContainer)).css({'display':'block'});
        $(popupContainer)
            .css({
                'width':width,
                'display':'none'
            })
            .appendTo('body');

        $('.custLightBox').remove();

        $('.overlayBox').filter(':not(:animated)').animate(
            { 'opacity' : 0 },
            500,
            function(){
                $('.overlayBox').remove();
            });
        }

        var defaults = {
            background           : '#000000',
            backgroundOpacity    : '0.8',
            popupContainer       : null
        }

        var options = $.extend(defaults,opts);

        if (options.popupContainer !=null) {
            var popupContainer = $('.'+options.popupContainer);
            var dcHeight = docHeight();
            var popupContainerOffset = Array($(popupContainer).width(),$(popupContainer).height());
            var custLightBoxTop = $(document).scrollTop()+(parseInt($(window).height()/2)-(parseInt($(popupContainer).height()/2)));
            var custLightBoxLeft = (parseInt($(window).width()/2))-(parseInt(popupContainerOffset[0]/2));

            $('<div class="overlayBox">&nbsp;</div>').appendTo('body');

            $('.overlayBox')
                .live('click',function(){closeHandle(popupContainer,popupContainerOffset[0]);})
                .css({
                    'height':dcHeight+'px',
                    'background' : options.background
                })
                .filter(':not(:animated)')
                .animate({'opacity':options.backgroundOpacity},500);

            $('<div class="custLightBox"></div>')
                .appendTo('body')
                .append('<div class="custLightBoxClose"><a href="#_"></a></div>')
                .append('<div class="content"></div>')
                .css({
                    'top'     : custLightBoxTop+'px',
                    'left'    : custLightBoxLeft,
                    'width'   : popupContainerOffset[0],
                    'height'  : popupContainerOffset[1]
                });

            $(popupContainer)
                .appendTo($('.custLightBox .content'))
                .css({
                    'display' : 'block',
                    'opacity' : '1'
                });

            $('.custLightBoxClose a').live('click',function(){closeHandle(popupContainer,popupContainerOffset[0]);});

        }
    }

})(jQuery)

