/**
*@author Justin Kimbrell
*@version 0.1.4
*
*@created 4/9/2010
*@modified 4/29/2010
*
*@overview 
*This plugin will easily dim the entire screen while allowing a prompt 
*(optional) to display in the center of browser window no matter the 
*size with either fixed or absolute positioning.
*
*@contact
*justin@inthesaucepan.com.
*
*@comments
*All comments, suggestions, bugs, or questions should be directed to
*justin@inthesaucepan.com
* 
*/

(function($) {

	$.fn.extend({
		
		center: function(json, callback) {
			
			var obj = $(this);
	        var height = obj.height();
	        var width = obj.width();
	        var zIndex = "1000";
	    	var offsetX = 0;
	    	var offsetY = 0;
	    	var position = "fixed";
	    	var parent = $(obj).parent();
	    	
	    	if(json && json != "function") {
	    	
		    	if(json.position)
			    	position = json.position;
			    	
				if(json.offsetX)
		    		offsetX = json.offsetX;
		    	
		    	if(json.offsetY)
		    		offsetY = json.offsetY;
				
				if(json.parent)
		       		var parent = $(json.parent);
		        	
	        } else if(json == "function") {
        		callback = json;
        		json = null;
        	}
        	
	        if(position == "fixed") {
		        var y = (($(window).height() - obj.height()) / 2);
		        var x = (($(window).width() - obj.width()) / 2);
		        		        
	        } else if(position == "absolute") {	     
	        	
	        	var objHeight = parseInt(obj.height());
	        	var parHeight = parseInt(parent.height());
	        	if(parHeight <= objHeight) {
	        		parent.css("min-height", parent.height()+obj.height()+offsetY);
		       		obj.attr("margin-left", "auto");
		       		obj.attr("margin-right", "auto");
	        	}
		       		
		       		parent.attr("min-height", parHeight);
	        	
	        		        	
		        var x = (parent.width() - obj.width()) / 2;
		       	var y = (parent.height() - obj.height()) / 2;
		    }
		    
	    	x += offsetX;
	    	y += offsetY;
	   		
	   		
			return this.each(function() {
		        obj.css(
		        	{
		        		"position": position,
		        		"left" : x,
		        		"top" : y,
		        		"z-index" : zIndex
		        	});
		        
		    	$(obj).show(0, callback);						
			});
		},
		
	    dim: function(json, callback) {
	    	
	    	var obj = $(this);
	    	
	    	if(typeof json == "function") {
	    		callback = json;
	    		json = null;
	    	}
	    	
	    	return this.each(function() {
	    		$.dim(json, function() {
					 $(obj).center(json, callback);
				});
			});
		},
		
	    
	    updateHTML: function(url, callback) {
	    
	    	if(!url || typeof url == "function")
	    		return "The script url is not defined.";
	    		
	    	var obj = $(this);
	    	
	    	return this.each(function() {	 
	    	   	
	    		$.get(url, function(data) {
	    			obj.html(data);
	    			
		    		if(typeof callback == "function")
		    			callback();
	    			
	    		});
	    	});
	    },
	    
		//Stops current dimming of the screen
	    showScreen: function(id, callback) {
	    
	    	var obj = $(this);
	    	
	        if(typeof obj == "object")
	        	obj.hide();
	       		  
	       	$.showScreen(id, callback);
	    }
		
	});
	
	$.extend({
		//Gets the document height
	     browserHeight: function(){
		    return Math.max(
		        $(document).height(),
		        $(window).height(),
		        /* For opera: */
		        document.documentElement.clientHeight
		    );
		},
		
		
		initDimScreen: function(json) {
			
			var speed = 500;
			var opacity = .75;
			var position = "fixed";
			var color = "#000";
			var height = "100%";
			var width = "100%"
			var left = "0px";
			var top = "0px";
			var zIndex = "999";
			var id = "__dim";
			var background = "fixed";
	    	var toScroll = false;
			
			if(json) {
				if(json.speed)
					speed = json.speed;
				
				if(json.opacity)
					opacity = json.opacity;
				
				if(json.position)
					position = json.position;
					
				if(json.color)
					color = json.color;
				
				if(json.background)
					background = json.background;
				
				if(json.height)
					height = json.height;
				
				if(json.width)
					width = json.width;
				
				if(json.left)
					left = json.left;
				
				if(json.top)
					top = json.top;
				
				if(json.id)
					id = json.id;
		        
		        if(json.scrollBars)
		        	toScroll = json.scrollBars
			}
			
			
			if($('#'+id).size() > 0) return;
						
			var html = $('<div></div>').attr({
			    id: id,
			    fade_opacity: opacity,
			    speed: speed
			}).css({
			    background: color,
			    height: height,
			    left: left,
			    opacity: 0,
			    position: background,
			    top: top,
			    width: width,
			    zIndex: zIndex
			});
			
		    if(background == "fixed" && toScroll == false)
		    	$(document.body).css("overflow", "hidden");
		    
		    
		    return html;
		
		},
		
		dim: function(json, callback) {
		
			var speed = 500;
	    	var opacity = .75;
	    	var parent = document.body;
	    	var id = "__dim";
	    	
	    	if(json) {
		    	if(json.speed)
		    		speed = json.speed;
		    	
		    	if(json.opacity)
		    		opacity = json.opacity;
		    	
		    	if(json.parent)
		    		parent = $(json.parent);
		    	
		    	if(json.id)
		    		id = json.id;
		    		
	    	   	if(typeof json == "function")
		    		callback = json;
		    
		    	
	    		if($('#'+json.id).size() > 0) return;	
		    }
	    		    	
			var html = $.initDimScreen(json).appendTo(parent).fadeTo(speed, opacity, callback);
	                
	        return html;

		},
			    
	    //Stops current dimming of the screen
	    showScreen: function(id, callback) {	
	 
	    	if (typeof id == "function")
	    		callback = id;
	    	
	    	var id = "__dimmer";	
	        var dim = $("#"+id);        
	        var speed = parseInt(dim.attr('speed'));
	        var position = dim.css("position");
	       	
	       	if(position == "fixed")
	       		$(document.body).css("overflow", "visible");
	       		
	        dim.fadeOut(speed, function() {
	            dim.remove();
	            
	            if(typeof callback == 'function') callback();
	        });
	    }
	    		
	});
	
})(jQuery);

