(function($){
 $.fn.jNickPopup = function(options) {
    

    var defaults = {
        action: "ajax",
        length: 300,
        minTrail: 20,
        moreText: "more",
        lessText: "less",
        ellipsisText: "..."
    };
    
    var options = $.extend(defaults, options);
    
    var close = function(){
        $("#mask",top.document).hide();
  		$('.window',top.document).hide();
        $('#jNickboxes',top.document).remove();
    };
    
    return this.each(function() {
        $(this).click(function(){
            var popup_body = '<div id="jNickboxes"><div class="pop_container_advanced window"><div id="dialog"><iframe id="dialog_frm" scrolling="no"></iframe></div></div><div id="mask"></div></div>';
            var contain = $("body",top.document);                               
            contain.append(popup_body);
                  
            //Get the screen height and width
    		var maskHeight = $(document,top.document).height();
    		var maskWidth = $(document,top.document).width();
            
            //Set height and width to mask to fill up the whole screen
            
            //$('#mask', top.document).css('width', maskWidth+"px");
            $('#mask', top.document).css('width', "100%");
            $('#mask', top.document).css('height', "100%");
            
            //transition effect		
    		$('#mask', top.document).fadeIn(300);    		
            
            //Get the window height and width
    		var winH = $(window,top.document).height();
    		var winW = $(window,top.document).width();
                  
            var id = ".window";     
    		//Set the popup window to center
    		$(id,top.document).css('top',  winH/2-$(id).height()/2);
    		$(id,top.document).css('left', winW/2-$(id).width()/2);
    	
    		//transition effect
    		$(id,top.document).fadeIn(300);
            
            if(options.action == "ajax")
            {
              var link = $(this).attr("href");
              $("#dialog_frm",top.document).attr("src", link);
              $(".jNickPopupClose",top.document).click(function(){close();});
              /*
              jQuery.ajax({
                url:link,
                type: "POST",
                success: function(data)
                {
                    $("#dialog").html(data);
                    $(".jNickPopupClose").click(function(){close();});
                },
                beforeSend: function()
                {   
                    $("#dialog").html("<h1>Loading...</h1>");
                },
                complete: function()
                {
                }        
                });    
               */            
            }
            
            //if close button is clicked
        	$('.window .close',top.document).click(function (e) {
        		//Cancel the link behavior
        		e.preventDefault();
        		$('#mask, .window',top.document).hide();
                $('#jNickboxes',top.document).remove();
        	});		
        	
        	//if mask is clicked
        	$('#mask',top.document).click(function () {
        	   /*
        		$(this).hide();
        		$('.window').hide();
                $('#jNickboxes').remove();
                */
                close();
        	});
            
            return false;
        });      
        
    });
 };
})(jQuery);
