/**
 * Creates a popup, and puts content into the div loaded via an iframe
 * @param {Object} options
 */

jQuery.fn.ajaxIframePopup = function(options){
    var
    defaults = {
        link:'',
        bgContainer: 'AjaxBackground',
        wrapper: 'AjaxDefBox'
    },
    settings = jQuery.extend({}, defaults, options);

    // allow chaining
    return this.each(function() {
        jQuery(this).click(function() {
            if (jQuery('#AjaxContainer').size() == 0) {
                // launch the ajax request
                jQuery('body').prepend('<div id="'+settings.wrapper+'"><div id="popupTop"><div id="popupBot"><a href="#" id="PopupClose">&nbsp;</a><iframe src="'+ jQuery(this).attr('href')+settings.link+'" class="ajaxFrameContainerBox" border="0" frameborder="0" scrolling="no"></iframe></div></div></div>');
                jQuery('body').append('<div id="'+settings.bgContainer+'"></div>');
                // ie 6 opacity
                jQuery('#'+settings.bgContainer).css('opacity', .2);
                jQuery('#'+settings.bgContainer).hide().fadeIn('slow').css('height', jQuery(document).height() + 'px');

                // remove box when click outside container
                var canExit = true;
                jQuery('#'+settings.wrapper).hover(function(){
                    canExit = false;
                }, function(){
                    canExit = true;
                });
				
                jQuery('#'+settings.bgContainer).click(function(){
                    if (canExit) {
                        jQuery('#'+settings.wrapper).fadeOut('slow', function(){
                            jQuery('#'+settings.wrapper).remove();
                        });
                        jQuery('#'+settings.bgContainer).fadeOut('slow', function(){
                            jQuery('#'+settings.bgContainer).remove();
                        });
                    }
                });
                
                jQuery("#PopupClose").css({
                    'position':'absolute',
                    'top':'18px',
                    'right':'19px',
                    'width':'31px',
                    'height':'30px',                    
                    'display':'block',
                    'text-decoration':'none',
                    'z-index':'9999',
                    'background':'transparent url(/mysite/themes/gib/images/bttn-close.png) left top no-repeat'
                });

                jQuery("#PopupClose").click(function(event){
                    event.preventDefault();
                    jQuery('#'+settings.wrapper).fadeOut('slow', function(){
                        jQuery('#'+settings.wrapper).remove();
                    });
                    jQuery('#'+settings.bgContainer).fadeOut('slow', function(){
                            jQuery('#'+settings.bgContainer).remove();
                        });
                });
                
            }

            

            return false;
        });
    });
}
