(function($){
	$.fn.famiganLightBox = function(data, options) {
		var defaults = {
			background: "#FFFFFF",
			contentClass: "lightbox",
			adjustX: 0,
			adjustY: 0,
			fadeOutTime: 200,
			width: {
				selector: "",
				minimum: 450
			},
			ok: function(){},
			cancel: function(){},
			okSelector: "#lightbox-ok",
			cancelSelector: "#lightbox-cancel"
		};
		options = $.extend(defaults, options);
		
		//famigo colors
		switch (options.background) {
			case "f-green": options.background = "#CCFFCC";
			break;
			case "f-gray": options.background = "#EFEFEF";
			break;
			case "f-pink": options.background = "#FFCCFF";
		}
		
		var thisArray = $(this);
		
		return this.each(function(){
			//first step is to bind the "click" event to the element
			$(this).bind("click", function(){
				thisArray.unbind("click"); //unbind so that the user can't open another lightbox
				
				//proceed
				openLightBox(data(this), options, function(){
					thisArray.famiganLightBox(data, options);
				});
			});
		});
	};
	
	//button generator to quickly generate html code for a button
	$.fn.famiganLightBox.buttonGen = function(options) {
		var defaults = {
			id: "lightbox-ok",
			color: "green",
			text: "Close",
			type: "regular"
		};
		options = $.extend(defaults, options);
		
		var wrapper = "";
		if (options.type == "regular") {
			wrapper = "<span class='georgia button-text'>" + options.text + "</span>";
		} else if (options.type == "submit") {
			wrapper = "<button type='submit' class='georgia button-text'>" + options.text + "</button>";
		}
		var button = [];
		button[0] = "<div id='";
		button[1] = options.id;
		button[2] = "' class='f-button ";
		button[3] = options.color;
		button[4] = "'>";
		button[5] = "<b><b></b><i></i></b>";
		button[6] = wrapper;
		button[7] = "<i><i></i><b></b></i></div>";
		
        return button.join("");
	};
	
	function openLightBox(content, options, callback) {
		var lb = "lightbox", lbContainer = "lightbox-container";
		
        var html = [];
		html[1] = "<div id='";
		html[2] = lb;
		html[3] = "' style='visibility: hidden; width: ";
		html[4] = options.width.minimum;
		html[5] = "px;'><div id='lightbox-horizontal' class='side horizontal ten lightbox'></div><div id='lightbox-vertical' class='side vertical ten lightbox'></div><div class='corner topleft ten lightbox'></div><div class='corner topright ten lightbox'></div><div class='corner bottomleft ten lightbox'></div><div class='corner bottomright ten lightbox'></div><div id='";
		html[6] = lbContainer;
		html[7] = "' style='background: ";
		html[8] = options.background;
		html[9] = "'><div id='lightbox-content' class='";
		html[10] = options.contentClass;
		html[11] = "'>";
		html[12] = content;
		html[13] = "</div></div></div>";

		$("body").prepend(html.join("")); //insert html into page
		
		//if the user wants the width to be dynamic
		if (options.width.selector != "") {
			//cache selectors
			var theBox = $("#" + lb);
			var theObject = $(options.width.selector);
			
			//set to nowrap so that we can see how high the element should be if the lightbox is wide enough
			theObject.css({"white-space":"nowrap"});
			var headerHeight = theObject.height();
			
			//shorten the number of calculations we have to do to get the box wide enough.
			theObject.css({"display":"inline"});
			if (theObject.width() > theBox.width()) {
				theBox.width(theObject.width());
			}
			theObject.css({"display":"block"});
			
			//set nowrap back to normal and begin increasing width
			theObject.css({"white-space":"normal"});
			while (theObject.height() > headerHeight) {
				theBox.width(theBox.width() + 1);
			}
		}
	
		//center lightbox on screen
		var top = (($(window).height() - $("#" + lb).height()) / 2) + $(window).scrollTop() + options.adjustY;
		var left = (($(window).width() - $("#" + lb).width()) / 2) + $(window).scrollLeft() + options.adjustX;
		$("#" + lb).css({"top":top + "px", "left": left + "px"});
		
		// once calculations are done, show lightbox
		$("#" + lb).css({"visibility":"visible"});

		// Make an ajax call to a helper action, informing it that a Games lightbox has been expanded.
        	// Note that this goes to production, regardless.  That's because production doesn't respond to calls on port 5001.
		$.ajax({ url: 'http://www.famigogames.com/main/addtouch?feature=game%20more%20info'});
		
		//bind "ok" callback to "ok" button
		$(options.okSelector).bind("click", function(){
			$(this).unbind("click");
			options.ok();
			if ($.support.opacity) {
				$("#" + lb).fadeOut(options.fadeOutTime, function(){
					$(this).remove();
					callback();
				});
			} else {
				$("#" + lb).remove();
				callback();
			}
		});
		
		//bind "cancel" callback to "cancel" button
		$(options.cancelSelector).bind("click", function(){
			$(this).unbind("click");
			options.cancel();
			if ($.support.opacity) {
				$("#" + lb).fadeOut(options.fadeOutTime, function(){
					$(this).remove();
					callback();
				});
			} else {
				$("#" + lb).remove();
				callback();
			}
		});
	}
})(jQuery);
