﻿﻿
;(function($) {
    var tempID = 0;
    var version = '1.0';
    
    $.fn.jBox = function(options){   
        var o = { s: this.selector, c: this.context };
        var myBox;
        var overlay;
        //var mObj = $(o.s);    
        var settings = { 'backgroundcolor':'white', 'border':'2px solid black'};
        
        $.extend(settings, options);              
                        
        this.close = function(){
            $("#overlay").fadeOut(200, function(){ $(this).remove(); });            
            $("#myBox").fadeOut(200, function(){ $(this).remove(); });  
            return this;
        };
        
        function rePosition(obj){        
            var x = (($(window).width() / 2) - ($("#myBox").outerWidth() / 2)) + jQuery(window).scrollLeft();
            var y = (($(window).height() / 2) - ($("#myBox").outerHeight() / 2) + jQuery(window).scrollTop());           
            $("#myBox").css("left", x + "px").css("top", y + "px");
        }
         
        this.showContainer = function(id){
            var mContainer = $("#" + id).html();
            this.showWindow(mContainer, true);                  
        };
            
        this.showWindow = function(html, sClose){
            var showClose = true;
            
            if (sClose != null) showClose = sClose;
                     
            $("#jBoxContainer").remove();
            
            if (showClose) {
                $("body").append('<div id="jBoxContainer"><div id="overlay"></div><div id="myBox"><div class="closeButton"></div><div class="boxContent"></div></div></div>');
            }
            else {
                $("body").append('<div id="jBoxContainer"><div id="overlay"></div><div id="myBox"><div class="boxContent"></div></div></div>');
            }
            
            $("#myBox").css('background-color', settings.backgroundcolor);        
            $(".boxContent").css('background-color', settings.backgroundcolor);
            $("#myBox").css('border', settings.border);
            
            $(".boxContent").append(html);
            
            
            rePosition();                                                   
            $("#overlay").fadeIn(200);
            $("#myBox").fadeIn(200);
           
            $(document).scroll(function(){ rePosition(); });
            
            if (showClose){
                $(".closeButton").click(function(){
                    $("#overlay").fadeOut(200, function(){ $(this).remove(); });
                    $("#myBox").fadeOut(200, function(){ $(this).remove(); });
                });
                
                $(".closeButton").mouseover(function(){$(this).css("top", (parseInt($(this).css("top")) + 1) + 'px');});                
                $(".closeButton").mousedown(function(){$(this).css("top", (parseInt($(this).css("top")) + 2) + 'px');});                
                $(".closeButton").mouseout(function(){$(this).css("top", (parseInt($(this).css("top")) - 1) + 'px');});                
                $(".closeButton").mouseup(function(){$(this).css("top", (parseInt($(this).css("top")) - 2) + 'px');});
            }
            return this;                        
        };     
        
        this.showFrame = function(url, sClose, height, width){
            var showClose = true;
            
            if (sClose != null) showClose = sClose;
                     
            $("#jBoxContainer").remove();
            
            if (showClose) {
                $("body").append('<div id="jBoxContainer"><div id="overlay"></div><div id="myBox"><div class="closeButton"></div><div class="boxContent"></div></div></div>');
            }
            else {
                $("body").append('<div id="jBoxContainer"><div id="overlay"></div><div id="myBox"><div class="boxContent"></div></div></div>');
            }
            
            $("#myBox").css('background-color', settings.backgroundcolor);        
            $(".boxContent").css('background-color', settings.backgroundcolor);
            $("#myBox").css('border', settings.border);
            
            
            
            $(".boxContent").append('<iframe src="' + url + '" width="' + width + '" height="' + height + '" frameborder="0" scrolling="no"><p>Your browser does not support iframes.</p></iframe>');
            
            
            rePosition();                                                   
            $("#overlay").fadeIn(200);
            $("#myBox").fadeIn(200);
           
            $(document).scroll(function(){ rePosition(); });
            
            if (showClose){
                $(".closeButton").click(function(){
                    $("#overlay").fadeOut(200, function(){ $(this).remove(); });
                    $("#myBox").fadeOut(200, function(){ $(this).remove(); });
                });
                
                $(".closeButton").mouseover(function(){$(this).css("top", (parseInt($(this).css("top")) + 1) + 'px');});                
                $(".closeButton").mousedown(function(){$(this).css("top", (parseInt($(this).css("top")) + 2) + 'px');});                
                $(".closeButton").mouseout(function(){$(this).css("top", (parseInt($(this).css("top")) - 1) + 'px');});                
                $(".closeButton").mouseup(function(){$(this).css("top", (parseInt($(this).css("top")) - 2) + 'px');});
            }
            return this;                        
        };
        
        
        return this;
    };
                      
})(jQuery);
