/**
  * nnbox v0.2.1 @2009-05-06
  * By Carl Nordenfelt (http://blog.nordenfelt.com)
  * Copyright (c) 2009 Carl Nordenfelt
  * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

/**
  * Usage:
  * Dom: <a href="http://www.url-to-show.com" rel="nnbox" title="My Custom Title">Link</a>
  * Dom with params: <a href="http://www.url-to-show.com" rel="nnbox[iframe modal classname]" title="My Custom Title">Link</a>
  * Automatic: nnboxShow('http://www.url-to-show.com', 'My Custom Title', {width:200, mode: 'iframe'});
*/

$(document).ready(function()
{
	imgLoader = new Image();
	imgLoader.src = "/static/common/images/ajax/loading-animation.gif";
	$('a[rel*=nnbox]').bind('click', nnboxInit);
});

function nnboxInit()
{
	if($(this).data('classes') === undefined)
	{
		var classes = $(this).attr('rel').match(/nnbox\[([a-zA-Z0-9_\- ]+)\]?/);
		$(this).data('classes', (classes) ? classes[1] : '');
	}
	nnboxShow($(this).attr('href'), $(this).attr('title'), $(this).data('classes'));
	return false;
}

// This method can be called upon directly from your own scripts to show an nnbox
function nnboxShow(url, title, classes)
{
	nnboxHide();

	// Create the overlay if it doesn't exist
	if(document.getElementById("nnbox-overlay") === null)
	{
		$('body').append('<div id="nnbox-overlay"></div>');
	}
	$('#nnbox-overlay').show();

	// Prepare content
	$('body').append('<div id="nnbox-container"><div id="nnbox-caption"><div id="nnbox-title"><span id="nnbox-loader"><img src="' + imgLoader.src+ '" alt="Loading"/></span></div></div><div id="nnbox-content"></div></div>');
	$('#nnbox-container').addClass(classes);

	// Modal?
	if(!$('#nnbox-container').hasClass('modal'))
	{
		$('#nnbox-overlay').bind('click', nnboxHide);
		document.onkeyup = function(e)
		{
			keycode = (e == null) ? event.keyCode : e.which;
			if(keycode == 27)
			{
				nnboxHide();
			}
		};
		$('#nnbox-caption').append('<div id="nnbox-close"><a href="/close" onclick="javascript:nnboxHide(); return false;">Close</a></div>');
	}
	else
	{
		$('#nnbox-overlay').unbind('click');
	}

	$('#nnbox-container').css({"margin-left": 0-$('#nnbox-container').width()/2}); // Center the box
	$('#nnbox-container').css('top', $(window).scrollTop() + 50); // Place it 50px from top
	$('#nnbox-content').hide();
	$('#nnbox-container').show();
	$('#nnbox-loader').show();

	if($('#nnbox-container').hasClass('iframe'))
	{
		$('#nnbox-content').append('<iframe src="' + url + '" id="nnbox-iframe" onload="javascript:nnboxLoaded(\''+title+'\');"></iframe>');
	}
	else // ajax post
	{
		$('#nnbox-content').load(url, function()
		{
			nnboxLoaded(title);
			$('.nnbox').bind('click', nnboxInit);
		});
	}

	// Load jqueryui dependencies (optional)
	if($('#nnbox-container').hasClass('draggable') || $('#nnbox-container').hasClass('resizable'))
	{
		if($('body').data('nnboxLoadedUI') === undefined)
		{
			$('body').append('<script type="text/javascript" src="/static/common/js/jquery-ui/jquery.ui.core.js"></script>');

			$('body').data('nnboxLoadedJqueryuiCore', true);
		}
		if($('#nnbox-container').hasClass('draggable'))
		{
			if($('body').data('nnboxLoadedJqueryuiDraggable') === undefined)
			{
				$('body').append('<script type="text/javascript" src="/static/common/js/jquery-ui/jquery.ui.draggable.js"></script>');
				$('body').data('nnboxLoadedJqueryuiDraggable', true);
			}

			$("#nnbox-container").draggable({ handle: '#nnbox-caption' });
			$('#nnbox-caption').css({cursor: 'move'});
		}
		// We don't allow iframes to be resizable for now. Might be fixd in future versions.
		if($('#nnbox-container').hasClass('resizable') && !$('#nnbox-container').hasClass('iframe'))
		{
			if($('body').data('nnboxLoadedJqueryuiResizable') === undefined)
			{
				$('body').append('<script type="text/javascript" src="/static/common/js/jquery-ui/jquery.ui.resizable.js"></script>');
				$('body').data('nnboxLoadedJqueryuiResizable', true);
			}
			$("#nnbox-container").resizable();
		}
	}

	return false;
}

function nnboxLoaded(title)
{
	$('#nnbox-loader').hide();
	$('#nnbox-title').text(title);
	$('#nnbox-content').show();
	if($('#nnbox-container').height() > $(window).height()-50)
	{
		$('#nnbox-container').height($(window).height()-100);
	}
	$('#nnbox-content').height($('#nnbox-container').height() - 35);
	if($('#nnbox-container').hasClass('iframe'))
	{
		$('#nnbox-iframe').height($('#nnbox-container').height()-35);
	}
}

// This method can be called upon directly from your own scripts to hide an nnbox
function nnboxHide()
{
	$('#nnbox-container').remove();
	$('#nnbox-overlay').hide();
}

