var	filesToLoadTemplate		= new Array();
var	currentFileTemplate		= 0;
function loadFilesTemplate()
{
	if (typeof filesToLoadTemplate[currentFileTemplate] == 'string')
	{
		$.getScript(SCORE_URL + 'Includes/Javascripts/' + filesToLoadTemplate[currentFileTemplate], loadFilesTemplate);
		currentFileTemplate++;
	}
	else
	{
		executeTemplate();
	}
}

$(document).ready(loadFilesTemplate);

function executeTemplate()
{
	filtersInit();
	formInit();
	$('div#header img').load(headerInit);
	selfLabelledInit();
	ratingInit();
	ratingFormInit();

	if($('p#player, p#trailer').not('[class=""]').length)
		$.getScript(SCORE_URL + 'Includes/Javascripts/swfobject.js', playerInit);
	if($('#chatbox').length)
		chatboxInit();

	$('form#search').submit(searchSubmit);
}

/* Chatbox */
function chatboxInit()
{
	paymentPhoneCheckInterval	= setInterval(dialedCheck, 1000);
}

/* Dialed check */
function dialedCheck()
{
	$.get(SCORE_URL + 'check.xml' + promo, dialedCheckCallback, 'xml');
}
function dialedCheckCallback(data)
{
	var	status				= $(data).find('status').text();
	var	location			= document.location + '';
	if (status != 'OPEN')
		document.location	= document.location + (location.split('?').length > 1 ? '&' : '?') + 'disconnected';
}

/* Filters */
function filtersInit()
{
	$('form#filters select').change(filtersChange);
}
function filtersChange()
{
	document.location = $(this).val();
}

/* Custom form submit button */
function formInit()
{
	$('form a[href=#submit]').click(formSubmit);
	$('form').keypress(function (e)
	{
		if (e.srcElement.tagName != 'TEXTAREA' && event.keyCode == 13)
			$(this).submit();
	});
}
function formSubmit()
{
	var	parent	= $(this).parent().get(0);
	while (parent.tagName != 'FORM')
		parent	= $(parent).parent().get(0);
	$(parent).submit();
}

/* Header init */
function headerInit()
{
	$('div#header h1, div#header h2').fadeTo(1, 0.7);
	$('div#header h1, div#header h2').css('marginTop', -$('div#header img').height()+h1Margin + 'px');
}

/* Player */
function playerInit()
{
	if ($('p#player, p#trailer').attr('class').split('.').length > 1)
	{
		var	flashvars = {
			flv				: $('p#player, p#trailer').attr('class'),
			ration			: 'showall',
			skin			: '/Includes/Flash/Skins/SuperSimpleSquare.swf',
			skinClrBG		: '666666'
		};
	}
	else
	{
		if ($('div#myMovie').length)
		{
			var	sessId		= $('p#player, p#trailer').attr('title');
			$('p#player, p#trailer').attr('title', '');
		}
		else
		{
			var	sessId		= userId;
			paymentPhoneCheckInterval	= setInterval(dialedCheck, 1000);
			$('a').click(playerUnloadd);
			$(window).unload(playerUnloadd);
		}

		var	flashvars = {
//			debug			: true,
			hash			: sessId,
			HQ				: $('p#player, p#trailer').attr('class') + '_HIGH.mp4',
			HQBitRate		: 1000,
			LQ				: $('p#player, p#trailer').attr('class') + '_LOW.mp4',
			LQBitRate		: 400,
			ration			: 'showall',
			serv			: 'rtmpt://streams.vodpartner.nl:80/' + ($('div#myMovie').length ? 'VOD' : 'PPM') + '/',
			skin			: '/Includes/Flash/Skins/SuperSimpleSquare.swf',
			skinClrBG		: '666666'
		};
	}
	var	params = {
		menu				: 'false',
		scale				: 'exactfit',
		allowFullscreen		: 'true',
		allowScriptAccess	: 'always',
		bgcolor				: '#000000'
	};
	var	attributes = {
		id					: '_' + $('p#player, p#trailer').attr('id')
	};
	swfobject.embedSWF('/Includes/Flash/FlashPlayer.swf', 'player', $('p#player, p#trailer').css('width'), $('p#player, p#trailer').css('height'), '10.0.0', '/Includes/Flash/ExpressInstall.swf', flashvars, params, attributes);
	swfobject.embedSWF('/Includes/Flash/FlashPlayer.swf', 'trailer', $('p#player, p#trailer').css('width'), $('p#player, p#trailer').css('height'), '10.0.0', '/Includes/Flash/ExpressInstall.swf', flashvars, params, attributes);
}
function playerUpdate(getAction,getVar)
{}
function playerUnloadd()
{
	$('object').remove();
}

/* Rating stuff */
function ratingFormInit()
{
	if ($('div.rate').length)
		$.getScript(SCORE_URL + 'Includes/Javascripts/JQuery.Plugin.Rating.js', ratingFormLoaded);
}
function ratingFormLoaded()
{
	$('div.rate input.rating').rating();
}
function ratingInit()
{
	$('.rating').each(ratingSet);
}
function ratingSet()
{
	$(this.tagName, this).animate({width: ($(this.tagName, this).attr('class')*16)}, 1000);
}

/*	Self labeled fields for login	*/
function selfLabelledBlur()
{
	var	parent	= $(this).parent().get(0);
	while (parent.tagName != 'FORM')
		parent	= $(parent).parent().get(0);

	$('label[for=' + $(this).attr('id') + ']', parent).hide();
	var	label	= $('label[for=' + $(this).attr('id') + ']', parent).html();

	if ($(this).attr('type') == 'password' && $(this).val() == '')
	{
		$(this).hide();
		$('input.passwordLabel', $(this).parent().get(0)).show();
	}
	else
	{
		if ($(this).val() == '')
			$(this).val(label);
	}
}
function selfLabelledFocus(event)
{
	var	parent	= $(this).parent().get(0);
	while (parent.tagName != 'FORM')
		parent	= $(parent).parent().get(0);

	var	label	= $('label[for=' + $(this).attr('id') + ']', parent).html();

	if ($(this).hasClass('passwordLabel'))
	{
		$(this).hide();
		$('input[name=' + $(this).attr('rel') + ']', $(this).parent().get(0)).show().focus();
	}
	else
	{
		if ($(this).val() == label)
			$(this).val('');
	}
}
function selfLabelledInit()
{
	$('input.self').each(function ()
	{
		var	parent	= $(this).parent().get(0);
		while (parent.tagName != 'FORM')
			parent	= $(parent).parent().get(0);

		$('label[for=' + $(this).attr('id') + ']', parent).hide();
		var	label	= $('label[for=' + $(this).attr('id') + ']', parent).html();

		if ($(this).attr('type') == 'text')
		{
			$(this).val(label).focus(selfLabelledFocus).blur(selfLabelledBlur);
		}
		else if ($(this).attr('type') == 'password')
		{
			var	passwordLabel	= $('<input class="' + $(this).attr('class') + ' passwordLabel" rel="' + $(this).attr('name') + '" type="text" value="' + label + '" />').focus(selfLabelledFocus);
			$(this).blur(selfLabelledBlur);
			$(this).after(passwordLabel);
			$(this).hide();
		}
	});
}

/* Zoekmachine */
function searchSubmit(e)
{
	var	query			= $('form#search input[name=query]').val();
		query			= escape(query);
	var	queryCurrent	= $('form#search').attr('class');

	var url				= $('form#search').attr('action');
		url				= url.split('zoek:' + queryCurrent).join('zoek:' + query);

	document.location	= url;

	e.preventDefault();
	return false;
}


