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()
{
	productInit();
	selfLabelledInit();
	$('form#search').submit(searchSubmit);
}

/* Search box */
function searchSubmit(e)
{
	e.preventDefault();

	var	query			= $('form#search input[name=query]').val();
	if (!query)
		return false;
		query			= query.split('/').join(' ');
		query			= escape(query);
	var	queryCurrent	= $('form#search').attr('class');

	if(!isNaN(query))
		query			+= '_';
	var url				= $('form#search').attr('action');
		url				= url.split('zoek:' + queryCurrent).join('zoek:' + query);

	document.location.href	= url;

	return false;
}

/* Product detail page */
function productInit()
{
	if ($('div#product').length)
		$.getScript(SCORE_URL + 'Includes/Javascripts/JQuery.Plugin.Fancybox.js', productFancyboxLoaded);
}
function productFancyboxLoaded()
{
	$('div#product a').has('img').fancybox();
}

/*	Self labeled fields	*/
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[id=' + $(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('id') + '" type="text" value="' + label + '" />').focus(selfLabelledFocus);
			$(this).blur(selfLabelledBlur);
			$(this).parent().prepend(passwordLabel);
			$(this).hide();
		}
	});
}
