/*!
	Slimbox v2.02 - The ultimate lightweight Lightbox clone for jQuery
	(c) 2007-2009 Christophe Beyls <http://www.digitalia.be>
	MIT-style license.
*/


var FORM_TYPE 		= "formType"


var ADD_GAME 		= ".fnAddGame";
var ADD_CHAR 		= ".fnAddChar";
var CREATE_USER		= ".fnCreateUser";

(function($) {

	// Global variables, accessible to Slimbox only
	var win = $(window), options, compatibleOverlay, middle, centerWidth, centerHeight, ie6 = !window.XMLHttpRequest,
		operaFix = window.opera && (document.compatMode == "CSS1Compat") && ($.browser.version >= 9.3), documentElement = document.documentElement,

	// DOM elements
	overlay, center, image, sizer, bottom;

	/*
		Initialization
	*/

	$(function() {
		
		
		// Append the Slimbox HTML code at the bottom of the document
		$("body").append(
			$([
				overlay = $('<div id="lbOverlay" />')[0],
				center = $('<div id="lbCenter" />')[0]
			]).css("display", "none")
		);
		
		rForm = $('<div id="rForm"></div>').appendTo(center).append(
			sizer = $('<div style="position: relative; border:1px solid red;" />')[0]
		)[0];
		
		bottom = $('<div id="lbBottom" />').append([
			$('<a />').add(overlay).click(close)[0],
			$('<div style="clear: both;" />')[0]
		])[0];
	});


	/*
		API
	*/

	// Open Slimbox with the specified parameters
	$.slimbox = function(el, _options) {
		options = $.extend({
			overlayOpacity: 0.6,			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			overlayFadeDuration: 1,		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
			resizeDuration: 1,			// Duration of each of the box resize animations (in milliseconds)
			resizeEasing: "swing",			// "swing" is jQuery's default easing
			initialWidth: 450,			// Initial width of the box (in pixels)
			initialHeight: 250,			// Initial height of the box (in pixels)
			imageFadeDuration: 1			// Duration of the image fade-in animation (in milliseconds)
		}, _options);

		middle = win.scrollTop() + ((operaFix ? documentElement.clientHeight : win.height()) / 2);
		centerWidth = options.initialWidth;
		centerHeight = options.initialHeight;
		$(center).css({top: Math.max(0, middle - (centerHeight / 2)), width: centerWidth, height: centerHeight, marginLeft: -centerWidth/2}).show();
		compatibleOverlay = ie6 || (overlay.currentStyle && (overlay.currentStyle.position != "fixed"));
		if (compatibleOverlay) overlay.style.position = "absolute";
		$(overlay).css("opacity", options.overlayOpacity).fadeIn(options.overlayFadeDuration);
		position();
		setup(1);
		return changeForm(el);
	};

	/*
		options:	Optional options object, see jQuery.slimbox()
		linkMapper:	Optional function taking a link DOM element and an index as arguments and returning an array containing 2 elements:
				the image URL and the image caption (may contain HTML)
		linksFilter:	Optional function taking a link DOM element and an index as arguments and returning true if the element is part of
				the image collection that will be shown on click, false if not. "this" refers to the element that was clicked.
				This function must always return true when the DOM element argument is "this".
	*/
	$.fn.slimbox = function(_options, linkMapper, linksFilter) {
		var links = this;
		return links.unbind("click").click(function() {
			return $.slimbox(this, _options);
		});
	};


	/*
		Internal functions
	*/

	function position() {
		var l = win.scrollLeft(), w = operaFix ? documentElement.clientWidth : win.width();
		$([center]).css("left", l + (w / 2));
		if (compatibleOverlay) $(overlay).css({left: l, top: win.scrollTop(), width: w, height: win.height()});
	}

	function setup(open) {
		$("object").add(ie6 ? "select" : "embed").each(function(index, el) {
			if (open) $.data(el, "slimbox", el.style.visibility);
			el.style.visibility = open ? "hidden" : $.data(el, "slimbox");
		});
		var fn = open ? "bind" : "unbind";
		win[fn]("scroll resize", position);
	}
	
	function changeForm(el) {		
		stop();
		center.className = "lbLoading";
		animateBox();
		getForm(el);
		return false;
	}
	
	function getForm(el) {
		var form_type = $(el).data("form_type");
		if (form_type == "REMOTE") {
			$.get(get_form_url(el), function(data){
				$(rForm).html(data);
			});
		} else if (form_type == "LOCAL") {
			$(rForm).html(get_form_html(el));
		} else {
			throw "unknown form type:" + form_type;
		}
	}

	

	function animateBox() {
		
		// removes loading image
		center.className = "";
		
		$(sizer).width("0");
		$([sizer]).height("0");
		
		$([rForm]).height("auto");
		
		// use this if you want to resize the box after loading (form is bigger, for example)
		centerWidth = "450";
		//centerHeight = "400";
		
		$(center).height("auto");
		
		var top = Math.max(0, middle - (centerHeight / 2));
		if (center.offsetHeight != centerHeight) {
			//$(center).animate({height: centerHeight, top: top}, options.resizeDuration, options.resizeEasing);
		}
		if (center.offsetWidth != centerWidth) {
			$(center).animate({width: centerWidth, marginLeft: -centerWidth/2}, options.resizeDuration, options.resizeEasing);
		}
		$(center).queue(function() {
			$(rForm).css({display: "none", visibility: "", opacity: ""}).fadeIn(options.imageFadeDuration);
		});
	}

	function stop() {
		$([center]).stop(true);
		$([rForm]).hide();
	}

	function close() {
		stop();
		$(center).hide();
		$(overlay).stop().fadeOut(options.overlayFadeDuration, setup);
		return false;
	}

})(jQuery);