var modalGlobals = {};

function openModal(url, data, modalArgs)
{
	if (data == null)
		data = {};

	if (typeof(data) == "object")
	{
		data["_s"] = randStr(10);
		data = objectToString(data);
	}
	$.get(url, data, function(data){ modalBox(data, modalArgs); }, "html");
}
function objectToString(data, kwargs)
{
	kwargs = kwargs || {};
	var itemDelimiter = kwargs.itemDelimiter || "&";
	var valueDelimiter = kwargs.valueDelimiter || "=";
	var includeEmpty = kwargs.includeEmpty === true; // default to false

	var output = [];
	for (key in data)
		if (includeEmpty || testNullString(data[key]))
			output.push(key + valueDelimiter + testNullString(data[key], ""));

	return output.join(itemDelimiter);
}

function closeModal(modalArgs){ return modalBox(null, modalArgs) }

function isModalOpen() { return $("#nimble_modal").length > 0; }

/**
 * Opens up a modal window with provided content
 * modalArgs can define the following optional params:
 *
 * openCallback: function -- if provided, this function will be executed after the overlay is first brought up
 * closeCallback: function -- if provided, this function will be executed when the overlay is closed
 * overlayOnclick: function -- if provided, this function will be executed when a user clicks on the grayed-out portion of the screen when an overlay is up
 * width: int width -- can manually specify here (can also specify in modal.css)
 * pin: bool -- if true, the modal will not scroll with the window
 **/
function modalBox(content, modalArgs) {
	var modalArgs = modalArgs || {};
	var createModal = $("#nimble_overlay").length == 0;

	var width = modalArgs.width || 350;
	var openCallback = modalArgs.openCallback;
	var closeCallback = modalArgs.closeCallback;
	var overlayOnclick = modalArgs.overlayOnclick || function(){ modalBox(); }
	var pin = modalArgs.pin !== false; // default to pinning the modal

	if (!content && modalGlobals.restoreContent)
	{
		restoreModalState();
		return;
	}

	if(!content) {
		$('#nimble_modal').remove();
		$('#nimble_overlay').fadeOut(function(){$(this).remove(); if (typeof(closeCallback) == "function"){closeCallback();} });

		return false;
	}

	if (createModal) {
		//try{if(IE6){$('body').find('select:visible').addClass('unhideThis').visibility('hidden');}}catch(err){};
		//$('embed:visible, object:visible').addClass('unhideThis').css('visibility', 'hidden');
		$('body').append('<div id="nimble_overlay"></div>');
		$('#nimble_overlay').css({
			height : $(document).height()
		}).fadeTo(500, 0.75).click(overlayOnclick);
		$('body').append('<div id="nimble_modal"></div>');
		$('#nimble_modal').css({
			width : width,
			top : $(document).scrollTop() + 50,
			marginLeft : -(Math.ceil((width)/2))
		}).append(content);
	}
	else // A modal is already open, reset its bound functions
	{
		$(document).unbind('scroll');
		$('#nimble_overlay').unbind("click").click(overlayOnclick);
		$("#nimble_modal").html(content);
	}

	width = modalArgs.width || $("#nimble_modal :first").width();
	$('#nimble_modal').css({width: width, marginLeft: -(Math.ceil(width / 2))});
	$('#nimble_modal input:eq(0)').focus();

	if (typeof(openCallback) == "function")
		openCallback();

	if (pin)
	{
		$(document).unbind('scroll');

		scrollTo(0, 0);
		$('#nimble_modal').animate({
			top: 50
		}, 0);
	}
	else
	{
		$(document).bind('scroll', function(){
			$('#nimble_modal').queue([]); // scroll fires many times on a scroll, we don't want the animation queue to back up, clear it.
			$('#nimble_modal').animate({
				top: $(document).scrollTop() + 50
			}, 300);
		});
	}
};

function saveModalState(saveFormState)
{
	saveFormState = (saveFormState === true);

	var isFromOverlay = isModalOpen();
	if (!isFromOverlay)
		return;

	modalGlobals.restoreContent = $("#nimble_modal").html();

	if (saveFormState)
	{
		modalGlobals.restoreForm = {};
		var tmpInputs = $("#nimble_modal input, "+ MODAL_ID +" select");

		for (var i = 0; i < tmpInputs.length; i ++)
		{
			var curInput = tmpInputs[i];

			if (curInput.type == "checkbox")
				modalGlobals.restoreForm[curInput.id] = curInput.checked;
			else
				modalGlobals.restoreForm[curInput.id] = curInput.value;
		}
	}
	else
		modalGlobals.restoreForm = {};
}

function restoreModalState()
{
	if (modalGlobals.restoreContent)
		modalBox(modalGlobals.restoreContent)
	else // No modal to restore. Close the overlay (there might be a form underneath that instigated the overlay)
		closeModal();

	if (modalGlobals.restoreForm)
	{
		for (var key in modalGlobals.restoreForm)
		{
			try {
				if (/password/i.test(key)) // don't restore password fields
					continue;

				if (key == "credit_package")
					$("#nimble_modal #credit_"+ modalGlobals.restoreForm[key]).trigger("click");
				else
				{
					var curInput = $("#nimble_modal #"+ key)[0];

					if (curInput.type == "checkbox")
						curInput.checked = modalGlobals.restoreForm[key];
					else
						curInput.value = modalGlobals.restoreForm[key];
				}
			} catch(e) {}
		}
	}

	modalGlobals.restoreContent = null;
}
function randStr(length)
{

	var src = "abcdefghijklmnopqrstuvwxyz0123456789";
	src = src.split("");
	
	var randomized = [];
	for (var i = 0; i < length; i ++)
		randomized.push(src[Math.floor(Math.random() * src.length)]);
	return randomized.join("");
}
function testNullString(value, returnIfNull)
{
	value = String(((value || value == 0) ? value : ""));
	returnIfNull = ((typeof(returnIfNull) != "undefined") ? returnIfNull : null);
	return ((value && value != "undefined") ? value : returnIfNull);
}

/*

var sections = [];
var open = [];

function open_toggle(name) {
	
	// build a new open array which doesn't contain 'name',
	// if 'open' doesn't contain 'name', add it at the end
	var newOpen = [];
	var hasName = false;
	for (i = 0; i < open.length; i ++) {
		if (open[i] == name) {
			hasName = true;
		}
		else {
			newOpen.push(open[i]);
		}
	}
	if (!hasName) {
		newOpen.push(name);
	}
	open = newOpen;
}

function in_array(n, h) {
	for (i = 0; i < h.length; i ++) {
		if (h[i] == n) {
			return true;
		}
	}
	return false;
}

function toggle_tbody(t) {
	var c = $('th:eq(0)', t).attr('class');
	if (in_array(c, open)) {
		$('tr:gt(0)', t).show();
		$('tr:eq(0)', t).removeClass('contracted');
	}
	else {
		$('tr:gt(0)', t).hide();
		$('tr:eq(0)', t).addClass('contracted');
	}
}

function _hideAll() {
	var i = 0;
	$('tbody').each(function () {
		$('tr:gt(0)', this).hide();
		$('tr:eq(0)', this).addClass('contracted');
		i++;
	});
}

function _showAll() {
	var i = 0;
	$('tbody').each(function () {
		$('tr:gt(0)', this).show();
		$('tr:eq(0)', this).removeClass('contracted');
		i++;
	});
}

$(function () {
	var i = 0;
	$('tbody').each(function () {
		if (i > 0) {
			$('tr:gt(0)', this).hide();
			$('tr:eq(0)', this).addClass('contracted');
		}
		i ++;
	});

	// determine which is the first row in the concertina
	open.push($('tbody:eq(0) th:eq(0)').attr('class'));

	// handle concertina
	$('tbody tr.section').click(function () {
		var p = this.parentNode;
		$(this).toggleClass('contracted');
		$('tr:gt(0)', p).toggle();
		open_toggle($('th', this).eq(0).attr('class'));
	});

	// page selector
	$('#specs_pagination a').click(function (e) {
		e.preventDefault();

		// handle 'on' class
		$('#specs_pagination a').removeClass('on');
		$(this).addClass('on');

		// show/hide the correct tables
		var parts = this.href.split('#');
		var href = parts[parts.length - 1];
		$('table:visible').hide();
		$('#' + href).show();

		// we now need to look at which items are open, and expand/contract
		// the rows that are/aren't
		//$('#' + href + ' tbody tr:gt(0)').hide();
		$('tbody').each(function () { toggle_tbody(this) });
	});
});


var secNav = false;

$(function () {

	$('h3#more-about-this-range').css('cursor', 'pointer');
	$('#nav-secondary').css('background-color', '#f1f1f4');

	$('h3#more-about-this-range').click(function () {

		secNav = ! secNav;

		$('#nav-secondary').toggleClass('expanded');
	
		if (secNav) {
			$('#nav-secondary ul').slideDown('fast');
			$('#nav-secondary #next-steps').slideDown('fast');
			$('#nav-secondary').css('background-color', '#f1f1f4');
		}
		else {
			$('#nav-secondary #next-steps').slideUp('fast');
			$('#nav-secondary ul').slideUp('fast');
			//$('#nav-secondary').css('background-color', '#fff');
		}
	});
});
*/

