/**
 * @author Cody Robert
 */

(function($){
	
	var glowZindex = 1;  //z-index counter

	$.fn.glow = function(options) {
	
		// Default options
		var opt = $.extend({
			type: "outer",
			size: 5,
			color: "black",
			opacity: 1
		}, options);
			
		var jGlows = $([]); //empty jQuery collection
			
			
		// Loop through original elements
		this.not(".glow").each(function() {
		
			var jthis = $(this);
			var glows = [];
			var type = (opt.type == "outer") ? -1 : 1;
			var zOriginal = (opt.type == "outer") ? glowZindex + 1 : glowZindex;
			var zGlow = (opt.type == "outer") ? glowZindex : glowZindex + 1;
			
			// Create ID for glow
			var glowId;
			if (this.id) {
				glowId = this.id + "_glow";
			}
			else {
				glowId = "g" + (1 + Math.floor(9999 * Math.random()));
			}
	
			// Modify original element
			$.data(this, "glowId", glowId); //store id in expando
			$.data(this, "glowOptions", options); //store options in expando
			jthis
				.attr("glowId", glowId)
				.css("zIndex", zOriginal);
			if (jthis.css("position") != "absolute") {
				jthis.css({
					position: "relative",
					zoom: 1 //for IE layout
				});
			}
			
			// Create first glow layer
			glows[0] = $("<div></div>");

			glows[0]
				.addClass("glow")
				.css({
					position: "absolute",
					zIndex: zGlow,
					opacity: opt.opacity / opt.size,
					top: 0,
					left: 0,
					width: jthis.outerWidth() - opt.size * type * 2,
					height: jthis.outerHeight() - opt.size * type * 2
				});
				
			if(type == 1) 
				glows[0].css({
					borderColor: opt.color,
					borderStyle: "solid",
					borderWidth: opt.size
				});
				
			else
				glows[0].css({
					background: "black",
					top: -opt.size,
					left: -opt.size
				});
				
			// Create other glow layers
			for (i = 1; i < opt.size; i++) {
				glows[i] = glows[0].clone();
				glows[i]
					.css({
						width: jthis.outerWidth() - (opt.size - i) * type * 2,
						height: jthis.outerHeight() - (opt.size - i) * type * 2
					});
					
				if(type == 1)
					glows[i].css({
						borderWidth: opt.size - i
					});
				
				
				else
					glows[i].css({
						top: -opt.size + i,
						left: -opt.size + i
						
					});

			}
			
	
			// Create container
			var divGlow = $("<div></div>")
				.attr("id", glowId) 
				.addClass("glow")
				.css({
					position: "absolute",
					top: jthis.position().top,
					left: jthis.position().left,
					zIndex: zGlow,
					marginTop: jthis.css("marginTop"),
					marginRight: jthis.css("marginRight"),
					marginBottom: jthis.css("marginBottom"),
					marginLeft: jthis.css("marginLeft")
				});
	
			// Add layers to container	
			for (i = 0; i < opt.size; i++) {
				divGlow.append(glows[i]);
			}
			
			// Add container to DOM
			jthis.after(divGlow);

			// Add glow to return set
			jGlows = jGlows.add(divGlow);
	
			// Re-align glow on window resize
			$(window).resize(function()
			{
				try {
					divGlow.css({
						left: jthis.position().left,
						top: jthis.position().top
					});
				}
				catch(e){}
			});
			
			// Increment z-index counter
			glowZindex += 2;
	
	
		});
	
	
		return this.pushStack(jGlows);
	
	
	};

})(jQuery);
