function ilDisplay(sTitle, sMessage, iW, iH, iT, iL, bFade)
{
	if (!iW) {
		iW = 300;
	}
	if (!iH) {
		iH = 200;
	}

	$('#ilInline, #ilInlineBG').remove();
	if (!$('#ilInline').length)
	{
		$('body').prepend('<div id="ilInline"><div class="ilTitle"><div class="ilClose"></div><div class="ilTitleText">'+sTitle+'</div></div><div class="ilContent">'+sMessage+'</div></div>');
		$('#ilInline .ilTitle .ilClose').click(function(){
			$('#ilInlineBG').fadeOut();
			$('#ilInline').slideUp();
			return false;
		});
	}
	if (!$('#ilInlineBG').length)
	{
		$('body').prepend('<div id="ilInlineBG"></div>');
		$('#ilInlineBG').css('margin-top', '-'+$('body').css('margin-top')).css('margin-left', '-'+$('body').css('margin-left'));
	}

	$('#ilInline').width(iW + (parseInt($('#ilInline .ilContent').css('padding').replace(/px$/, '')) * 2)).height(iH + (parseInt($('#ilInline .ilContent').css('padding').replace(/px$/, '')) * 2) + (parseInt($('#ilInline .ilTitle').css('height').replace(/px$/, ''))));
	$('#ilInline .ilContent').width(parseInt(iW)).height(parseInt(iH));
	if (!iT) {
		iT = ($(window).height() / 2) - (iH / 2) - ($('#ilInline .ilTitle').height() / 2);
	}
	if (!iL)
	{
		iL = ($(window).width() / 2) - (iW / 2);
	}
	iT = Math.round(iT);
	iL = Math.round(iL);
	if (bFade)
	{
		if ($.browser.msie) {
			$('#ilInlineBG').fadeIn(function() {
				$(this).css('filter','alpha(opacity='+$(this).css('alpha')+')');
			});
		}
		else {
			$('#ilInlineBG').fadeIn();
		}
	}
	$('#ilInline').css({
		'top':iT+'px',
		'left':iL+'px'
	}).slideDown();
	return false;
}

function _hookLists()
{
	$('*[list=verticle]').each(function(){
		widget = $(this);
		contents = $(widget).find('.contents');
		perpage = ($(contents).height() / $(contents).find('.list li').height());
		page = parseInt(($(widget).attr('page'))?$(widget).attr('page'):0);
		pages = Math.floor($(contents).find('.list li').length / perpage);
		if (pages <= 0)
		{
			$(widget).children('.arrowUp, .arrowDown').css({
				'background-image':'none',
				'cursor':'default'
			});
		}
	});
	$('*[list=verticle] .arrowUp').click(function(){
		widget = $(this).parents('*[list=verticle]');
		contents = $(widget).find('.contents');
		perpage = ($(contents).height() / $(contents).find('.list li').height());
		page = parseInt(($(widget).attr('page'))?$(widget).attr('page'):0);
		if (page > 0)
		{
			$(widget).attr('page', (page - 1));
			$(contents).find('.list').animate({
				'top':'+='+($(contents).find('.list li').height() * (perpage - 1))+'px'
			}, 1000);
		}
	});
	$('*[list=verticle] .arrowDown').click(function(){
		widget = $(this).parents('*[list=verticle]');
		contents = $(widget).find('.contents');
		perpage = ($(contents).height() / $(contents).find('.list li').height());
		page = parseInt(($(widget).attr('page'))?$(widget).attr('page'):0);
		pages = Math.floor($(contents).find('.list li').length / perpage);
		if (page < pages)
		{
			$(widget).attr('page', (page + 1));
			$(contents).find('.list').animate({
				'top':'-='+($(contents).find('.list li').height() * (perpage - 1))+'px'
			}, 1000);
		}
	});

	$('*[list=horizontal] .arrowLeft').click(function(){
		widget = $(this).parents('*[list=horizontal]');
		contents = $(widget).find('.contents');
		perpage = ($(contents).width() / $(contents).find('.list li').width());
		page = parseInt(($(widget).attr('page'))?$(widget).attr('page'):0);
		if (page > 0)
		{
			$(widget).attr('page', (page - 1));
			$(contents).find('.list').animate({
				'left':'+='+($(contents).find('.list li').width() * (perpage - 1))+'px'
			}, 1000);
		}
	});
	$('*[list=horizontal] .arrowRight').click(function(){
		widget = $(this).parents('*[list=horizontal]');
		contents = $(widget).find('.contents');
		perpage = ($(contents).width() / $(contents).find('.list li').width());
		page = parseInt(($(widget).attr('page'))?$(widget).attr('page'):0);
		pages = Math.floor($(contents).find('.list li').length / perpage);
		if (page < pages)
		{
			$(widget).attr('page', (page + 1));
			$(contents).find('.list').animate({
				'left':'-='+($(contents).find('.list li').width() * (perpage - 1))+'px'
			}, 1000);
		}
	});
}

function _hookPopUps()
{
	$('*[popup]').unbind('click.ilPopup').bind('click.ilPopup', function(){
		link = this;
		eval("popup = {"+$(link).attr('popup')+"};");
		if (!popup.widget)
		{
			alert('No widget specified for the popup.');
		}
		else
		{
			/*$.getJSON('ajax.php?c='+$(this).attr('popup'), function(data) {
				ilDisplay($(link).html(), data.content, data.width, data.height);
			});*/

			if (!popup.title)
			{
				popup.title = popup.widget.replace(/_/g, ' ');
			}
			if (popup.size)
			{
				switch(popup.size)
				{
					case 'player':
						popup.width = 480;
						popup.height = 290;
						break;
					case 'brochure':
						popup.width = 720;
						popup.height = 480;
						break;
					case 'page':
						popup.width = 1000;
						popup.height = 600;
						break;
					default:
						popup.width = 640;
						popup.height = 480;
				}
			}
			$.getJSON('ajax.php?w='+popup.widget, function(data) {
				//alert(data.content);
				ilDisplay(popup.title, data.content, popup.width, popup.height, popup.top, popup.left, popup.fade);
				$(document).ready(function() { _hookPopUps(); })
			});
		}
		return false;
	});
}

$(document).ready(function(){
	_hookLists();

	_hookPopUps();
})