
$(document).ready(function()
{
	$('div#search input#search-text').keydown( checkSearchEnter );
	$('div#search a#search-button').click( startSearch );
	
	$('div.commandbuttons a.afdrukken').click( printInhoud );
	$('div.commandbuttons a.doorsturen').click( paginaDoorsturen );
	$('div.commandbuttons a.mailredactie').click( mailRedactie );
	
	setFontSize();
	$('div.commandbuttons a.groter').click( increaseFontSize );
	$('div.commandbuttons a.kleiner').click( decreaseFontSize );
});


function startSearch($evt)
{
	$evt.preventDefault();
	
	document.location = "/Zoeken/tabid/1268/Default.aspx?" + $('div#search input').serialize();
}


function checkSearchEnter($evt)
{
	if ($evt.keyCode == 13)
	{
		$evt.stopPropagation();
		startSearch($evt);
	}
}


function menuClick($evt)
{
	var $link = $($evt.target).children('a:first');
	if ($link.length > 0) window.location = $link.attr('href');
}



// Afdrukken van de pagina-inhoud m.b.v. een nieuw window waar het inhoud-deel van de huidige
// pagina naar toe wordt gekopieerd.
function printInhoud($evt)
{
	$evt.preventDefault();
	
	// verzamel inhoud uit breadcrumb, contentpane en rightpane
	
	var breadcrumb = $('div#breadcrumb').html();
	var contentpane = $('div#contentpane').html();
	var rightpane = "";
	var $rightpane = $('div#rightpane > div:not(:first)');
	for (i = 0; i < $rightpane.length; i++)
	{
		rightpane += $rightpane.eq(i).html();
	}

	// plaats inhoud in html context met stylesheet
	html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
		+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
		+ "<title>" + document.title + "</title>"
		+ "<style type='text/css'>"
		+ " @import '/Portals/veiligeopvang/skins/veiligeopvang/print.css'; "
		+ "</style>"
		+ "</head>"
		+ "<body>"
		+ "<img id='logo' src='/portals/veiligeopvang/skins/veiligeopvang/images/logo.gif' />"
		+ "<div id='printbar'>"
		+ "<a href='javascript:window.print();'>Afdrukken</a> <a href='javascript:window.close();'>Sluiten</a>"
		+ "</div>"
		+ "<div id='printcontent'>"
		+ "<div id='breadcrumb'>" + breadcrumb + "</div>"
		+ "<div id='contentpane'>" + contentpane + "</div>"
		+ "<div id='rightpane'>" + rightpane + "</div>"
		+ "</div>"
		+ "</body>"
		+ "</html>";

	if (html.length)
	{
		// open popup window, schrijf print-inhoud erin en open print dialog
		var args = "height=450, width=700, resizable=yes, scrollbars=yes, directories=no, location=no, menubar=no, toolbar=no";
		var printwindow = window.open("", "printwindow", args);
		printwindow.document.write(html);
		printwindow.document.close(); // (flush buffer met in window geschreven html)
	}
}



function paginaDoorsturen(evt)
{
	evt.preventDefault();
	
	if ($('div#paginadoorsturen').length == 0)
	{
		// popup laden, tonen, en koppelen aan eventhandlers
		var tabid = $('input#_hx_tabid').val();
		$.get
		(
			'/Portals/veiligeopvang/controls/PaginaDoorsturen.aspx',
			{ tabid: tabid, title: document.title },
			function(html)
			{
				// popup html ontvangen via jquery ajax callback, toon de popup
				$('div#contentcontainer').append(html);
				
				// voor ie6 is absolute position i.p.v. fixed position ingesteld in fix-ie6.css
				// zorg dat de popup in beeld komt
				if (navigator.appVersion.indexOf('MSIE 6') > 0)
				{
					$('div#paginadoorsturen')[0].scrollIntoView(false);
				}

				// eventhandler voor versturen				
				$('div#paginadoorsturen a.verzendknop').click(function($evt)
				{
					$evt.preventDefault();
					$('div#paginadoorsturen div.invoer div.knoppen img.loader').show();
					var input = $('div#paginadoorsturen div.invoer input, div#paginadoorsturen div.invoer textarea').serialize();
					var url = '/Portals/veiligeopvang/controls/PaginaDoorsturen.aspx';

					$.getJSON(url, input, function(result)
					{
						if (result.success)
						{
							$('div#paginadoorsturen div.invoer').hide();
							$('div#paginadoorsturen div.invoer div.knoppen img.loader').hide();
							$('div#paginadoorsturen div.verstuurd span.ontvanger').html($('div.invoer input#ontvanger').val());
							$('div#paginadoorsturen div.verstuurd').show();
						}
						else if (result.errors)
						{
							$('div#paginadoorsturen div.invoer .validatie').hide();
							$('div#paginadoorsturen div.invoer div.knoppen img.loader').hide();
							for (var i = 0; i < result.errors.length; i++)
							{
								$('div#paginadoorsturen div.invoer .err-' + result.errors[i]).show();
							}
						}
						else
						{
							$('div#paginadoorsturen div.invoer').hide();
							$('div#paginadoorsturen div.foutmelding').show();
						}
					});
				});
				
				// eventhandler voor sluiten / annuleren
				$('div#paginadoorsturen a.sluitknop').click(function($evt)
				{
					$evt.preventDefault();
					$('div#paginadoorsturen').hide();
				});
			}
		);
	}
	else
	{
		// reeds geladen popup in begintoestand brengen en opnieuw tonen
		$('div#paginadoorsturen div.invoer .validatie').hide();
		$('div#paginadoorsturen div.verstuurd').hide();
		$('div#paginadoorsturen div.foutmelding').hide();
		$('div#paginadoorsturen div.invoer div.knoppen img.loader').hide();
		$('div#paginadoorsturen div.invoer').show();
		$('div#paginadoorsturen').show();
	}
}


function mailRedactie(evt)
{
	evt.preventDefault();
	
	if ($('div#mailredactie').length == 0)
	{
		// popup laden, tonen, en koppelen aan eventhandlers
		var tabid = $('input#_hx_tabid').val();
		$.get
		(
			'/Portals/veiligeopvang/controls/MailRedactie.aspx',
			{ tabid: tabid, title: document.title },
			function(html)
			{
				// popup html ontvangen via jquery ajax callback, toon de popup
				$('div#contentcontainer').append(html);
				
				// voor ie6 is absolute position i.p.v. fixed position ingesteld in fix-ie6.css
				// zorg dat de popup in beeld komt
				if (navigator.appVersion.indexOf('MSIE 6') > 0)
				{
					$('div#mailredactie')[0].scrollIntoView(false);
				}
				
				// eventhandler voor versturen				
				$('div#mailredactie a.verzendknop').click(function($evt)
				{
					$evt.preventDefault();
					$('div#mailredactie div.invoer div.knoppen img.loader').show();
					var input = $('div#mailredactie div.invoer input, div#mailredactie div.invoer textarea').serialize();
					var url = '/Portals/veiligeopvang/controls/MailRedactie.aspx';

					$.getJSON(url, input, function(result)
					{
						if (result.success)
						{
							$('div#mailredactie div.invoer').hide();
							$('div#mailredactie div.invoer div.knoppen img.loader').hide();
							$('div#mailredactie div.verstuurd span.ontvanger').html($('div.invoer input#ontvanger').val());
							$('div#mailredactie div.verstuurd').show();
						}
						else if (result.errors)
						{
							$('div#mailredactie div.invoer .validatie').hide();
							$('div#mailredactie div.invoer div.knoppen img.loader').hide();
							for (var i = 0; i < result.errors.length; i++)
							{
								$('div#mailredactie div.invoer .err-' + result.errors[i]).show();
							}
						}
						else
						{
							$('div#mailredactie div.invoer').hide();
							$('div#mailredactie div.foutmelding').show();
						}
					});
				});
				
				// eventhandler voor sluiten / annuleren
				$('div#mailredactie a.sluitknop').click(function($evt)
				{
					$evt.preventDefault();
					$('div#mailredactie').hide();
				});
			}
		);
	}
	else
	{
		// reeds geladen popup in begintoestand brengen en opnieuw tonen
		$('div#mailredactie div.invoer .validatie').hide();
		$('div#mailredactie div.verstuurd').hide();
		$('div#mailredactie div.foutmelding').hide();
		$('div#mailredactie div.invoer div.knoppen img.loader').hide();
		$('div#mailredactie div.invoer').show();
		$('div#mailredactie').show();
	}
}





var fontSizeIndex = 1;
var fontSizeList = ['x-small', 'small', 'medium', 'large'];

function setFontSize()
{
	var cookie = $.cookie('fontSizeIndex');
	
	if (cookie)
	{
		fontSizeIndex = new Number(cookie);
		if (isNaN(fontSizeIndex)) fontSizeIndex = 1;
		$('body').css('font-size', fontSizeList[fontSizeIndex]);
	}
}

function increaseFontSize(evt)
{
	evt.preventDefault();
	if (fontSizeIndex >= fontSizeList.length) return;
	fontSizeIndex += 1;
	$('body').css('font-size', fontSizeList[fontSizeIndex]);
	$.cookie('fontSizeIndex', fontSizeIndex, { expires: 7, path: '/' });
}

function decreaseFontSize(evt)
{
	evt.preventDefault();
	if (fontSizeIndex == 0) return;
	fontSizeIndex -= 1;
	$('body').css('font-size', fontSizeList[fontSizeIndex]);
	$.cookie('fontSizeIndex', fontSizeIndex, { expires: 7, path: '/' });
}
