$(document).ready(
	function()
	{
		initTabs();
		initScroll();

		$('#gm-list li a.movie').click(onThumbClicked);
		$('#gm-list li a.screen').click(onThumbClicked);

		$('#gm-list li:first a').click();

		$('a.block-ui-iframe').click(onBlockUIIframe);
	}
);

function initTabs()
{
	$('#gd-tabs li a').click(onTabClicked);
}

function onTabClicked()
{
	var active_tab_id = $('#gd-tabs li a.active').attr('id');
	var tab_id = $(this).attr('id');
	var active_box_id = active_tab_id.replace('gdt', 'gdb');
	var box_id = tab_id.replace('gdt', 'gdb');

	$('#'+active_tab_id).removeAttr('class');
	$('#'+tab_id).attr('class', 'active');
	$('#'+active_box_id).hide();
	$('#'+box_id).show();

	return false;
}

function initScroll()
{
	$('#frame-main div.btn-scroll a').click(onScrollClicked);
	curr_row = 1;
}

function onScrollClicked()
{
	var sign, next_pos;
	var v_jump = 123;
	var curr_pos = parseInt($('#gm-list').css('top').match(/[+-]?\d+/));
	var rows = Math.ceil($('#gm-list li').length / 2);
	var min_row = 1;
	var max_row = rows - 1;
	var dir = $(this).attr('id').substr(3);

	if (dir == 'up' && curr_row > 1)
	{
		sign = '+';
		next_pos = curr_pos + v_jump;
		curr_row--;
	}
	else if (dir == 'down' && curr_row < max_row)
	{
		sign = '-';
		next_pos = curr_pos - v_jump;
		curr_row++;
	}

	if (curr_row == 1)
	{
		$('#bs-up').hide();
	}
	else
	{
		$('#bs-up').show();
	}

	if (curr_row == max_row)
	{
		$('#bs-down').hide();
	}
	else
	{
		$('#bs-down').show();
	}

//	$('#debug').prepend('curr_row: ' + curr_row + ', max_row: ' + max_row + ', min_row: ' + min_row + ', next_pos: ' + next_pos + '<br>\n');

	$('#gm-list').animate({top: sign+'='+v_jump+'px'}, 222);

	return false;
}

function onThumbClicked()
{
//	alert('onThumb');
	var media_type = $(this).attr('class');
	var src = $(this).attr('href');
	var media_id = $(this).attr('id');

	if (media_type == 'movie')
	{
		if ($.hasFlashPlayer)
		{
			var d = new Date();

			$('#gm-medium').flash(
				{
					id: 'media-player',
					swf: '/swf/media-player.swf?ck='+d.getTime(),
					width: 425,
					height: 241,
					params: {
						bgcolor: '#000000',
						wmode: 'transparent',
						flashvars: {
								id: media_id,
								e: 0
						}
					}
				}
			);
		}
		else
		{
			$('#gm-medium').html('<a href="http://get.adobe.com/flashplayer/" title="Get Flash Player"><img src="/gfx/game/no-flash.jpg" alt="No flash player installed"></a>');
		}
	}
	else if (media_type == 'screen')
	{
		$('#gm-medium').html('<img src="'+src+'" alt="screenshot">');
	}

	$('div.active-item').remove();
	$(this).append('<div class="active-item"></div>');

	return false;
}

function onBlockUIIframe()
{
	var href = $(this).attr('href').toString();

	$.blockUI({
		message: '<iframe src="' + href + '" style="border: none; height: 460px; width: 990px;" frameBorder="0"><p>Your browser does not support iframes.</p></iframe><p class="center large b" style="margin-top: 10px"><a class="js-link unblock-ui white">Close</a></p>',
		css: {
			background: '#181818',
			border: 0,
			cursor: 'normal',
			height: '500px',
			left: ($(window).width() - 990) /2 + 'px',
			overflow: 'hidden',
			padding: '20px 20px 7px 20px',
			top:  ($(window).height() - 500) /2 + 'px',
			width: '990px'
		},
		overlayCSS: {
			cursor: 'pointer'
		}
	});
	$('.blockOverlay').attr('title','Click to close').click($.unblockUI);
	$('.unblock-ui').attr('title','Click to close').click($.unblockUI);

	return false;
}