// Global vars
var inhoudElm, voetElm, pollElm, player, videoUrl, layerWrapperElm;

$(document).ready(function()
{
	// Als het document is geladen, dit doen:
	inhoudElm = $("#inhoud");
	voetElm = $("#voettekst")
	pollElm = $("#extrainhoud form.poll");
	
	// Init
	positioneerVoettekst();
	pollScore();
	videoPreviewLayer();
	prepPaginaelement();
	kruimelpadTekst();
	presentatieLink();
	
	// Window event
	$(window).wresize(positioneerVoettekst); positioneerVoettekst();
});

$(window).load(function () {
	prepAfbeeldingen();
});

// Functions
function positioneerVoettekst()
{
 	if (inhoudElm.length == 0) return false;
	inhoudElm.css("height","auto").css("min-height","auto");
	var bodyTargetHeight = (voetElm.position().top)+174;
	
	if (bodyTargetHeight < $(window).height())
	{
		bodyTargetHeight = $(window).height();
		inhoudElm.css("height","auto").css("min-height",$(window).height()-386+"px");
	}
	
	$("body").css("min-height",(bodyTargetHeight)+"px");
	$("body").css("background-position","center bottom");
}

function pollScore()
{
	if (pollElm.length == 0) return false;
	
	var pElms = pollElm.find("p.resultaat");
	pElms.each(
		function()
		{
			var procent = parseInt($(this).find("img").attr("title"));
			$(this).find("br, img").remove();	// Verwijder onnodige html
			$(this).wrapInner('<div class="gauge"></div>');	// Voeg div waar vollopend balkje in komt
			var gaugeElm = $(this).find("div.gauge");
			var tekst = gaugeElm.text().split(":")[0];
			gaugeElm.width(procent+"%").wrapInner('<span></span>').find("span").text(tekst+":").append("<strong>"+procent+"%</strong");	// Tekst in span zodat die met css te positioneren is
		}
	);
}

function prepAfbeeldingen()
{
	$('.afbeelding img').each (function () {
		var div = $(this).parent();
		
		$(div).css('width', $(this).width());
		$(div).css('height', $(this).height());

		$(div).prepend('<div class="corners_top"><div class="right"></div><div class="left"></div></div');
		$(div).prepend('<div class="corners_bottom"><div class="right"></div><div class="left"></div></div');
		
		$(this).show();
	});
}

function videoPreviewLayer()
{
	var prevBtns = $("#inhoud a.showPreview");
	if (prevBtns.length == 0) return false;
	
	// Geef alle preview buttons events
	prevBtns.each(
		function()
		{
			$(this).click(
				function()
				{
					videoUrl = this.getAttribute("href");
					// showVideo(videoUrl, 320, 240);
					
					
					Shadowbox.open({
						player:     'html',
						title:      'Welcome',
						content:    '<div id="welcome-msg">Welcome to my website!</div>',
						height:     350,
						width:      350
					});
					
					return false;
				}
			);
		}
	);
}

function prepPaginaelement()
{
	// Verwijderd achtergrond van paginaelements die gevolgd worden door een h2 (siblings)
	$("#inhoud > h2").each(
		function()
		{
			$(this).prev(".paginaelement").addClass("paginaelementVoorH2");
		}
	)
}

function kruimelpadTekst()
{
	$("#kruimelpad ul").prepend("<li>U bent hier:&nbsp;</li>");
}

function playerReady(obj) {
	var id = obj['id'];
	var version = obj['version'];
	var client = obj['client'];
	player = document.getElementById(id);
	player.sendEvent('LOAD',videoUrl);
	player.sendEvent('PLAY');
};

function presentatieLink()
{
	$('#sitecontainer').prepend('<a id="btnPresentatie" href="?nr=page_volgendekeerbeter_presentatie&site=site_volgendekeerbeter"></a>');
}

