var paginanummer;

$(document).ready(function(){
	if ($('#paginanummer').html())
	{
		paginanummer = $('#paginanummer').val;
	}
	
	// flash inladen
	// als een div aangemaakt wordt met als class "flash",
	// en als inhoud het absolute pad naar de flash en de variable
	// wordt deze automatisch ingeladen
	$('.flash').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			wmode: 'transparent'
		});
	});
	
	
	$('.videoplayer').each(function(){
		slideshowvars = $(this).find('.flashlink').html();
		slideshowheight = $(this).css('height');
		slideshowwidth = $(this).css('width');
		$(this).html("");
		$(this).flash({
	    	src: slideshowvars,
	    	width: slideshowwidth,
	    	height: slideshowheight,
			allowFullScreen: 'true'
		});
	});
	
	
	// subs in/uitklappen
	$('.uitklaplink').click(function () {
		var obj = $(this).parents('.uitklapblok').find('.uitklapdetail');
	
		$(this).parents('.uitklapblokken').find('.uitklapdetail').slideUp();
		$(this).parents('.uitklapblokken').find('a').removeClass('active');
	
		if (obj.css('display')=='none')
		{
			obj.slideDown();
			$(this).addClass('active');
		}		
	});

	// links in "_blank" i.p.v. via HTML i.v.m. valid XHTML
	$('.blankwindow').attr('target','_blank');
	
	//logo hover
	$('.grijs').each(function(){
		var kleur = $(this).parents('.logo').find('.kleur');
		$(this).hover(
			function(){ //hover grijs
				$(this).hide();
				kleur.show();
			}
		);
		kleur.hover(null,
			function(){ //out (van kleur)
				$(this).hide();
				$(this).parents('.logo').find('.grijs').show();
			}
		);
	});
	
	
	//##### OVERZICHTPAGINA'S #####
	$('#paginanummer').blur(openPagina);
	
	$('#paginanummer').bind('keypress', function(e) {
        if(e.keyCode==13){
                openPagina();
        }
	});
	$('#categorieselect').change(function(){
		if ($('#categorieselect').val() != 0)
		{
			document.location = overzichtURL + $('#categorieselect').val();
		}
		else
		{
			document.location = overzichtURL;
		}
	});
	
	$('#subcategorieselect').change(function(){
		if ($('#subcategorieselect').val() != 0)
		{
			document.location = overzichtURL + $('#categorieselect').val() + '/' + $('#subcategorieselect').val();
		}
		else
		{
			document.location = overzichtURL + $('#categorieselect').val();
		}
	});
	
	
	//##### MIJN MENU KNOPPEN (VIDEO OVERZICHT) #####
	$('#menuAdd').click(function(){
		
		videoId; //global var, set in template
		
		$.ajax({
			type: "POST",
			url: "/mijn_menu",
			data: "add=" + videoId,
			dataType: "json",
			success: function(response){ //START VULLEN
				if (typeof(response.success) != "undefined" && response.success)
				{
					$('#menuAdd').hide();
					$('#menuDel').show();
				}
			},
			error: function(reqObject, error, ex){
				alert("De video kon niet aan uw menu worden toegevoegd.");
			}
		});
		return false; //link niet openen
	});
	
	$('#menuDel').click(function(){
		
		videoId; //global var, set in template
		
		$.ajax({
			type: "POST",
			url: "/mijn_menu",
			data: "del=" + videoId,
			dataType: "json",
			success: function(response){ //START VULLEN
				if (typeof(response.success) != "undefined" && response.success)
				{
					$('#menuDel').hide();
					$('#menuAdd').show();
				}
			},
			error: function(reqObject, error, ex){
				alert("De video kon niet uit uw menu worden verwijderd.");
			}
		});
		return false; //link niet openen
	});
	
	//##### REACTIE PLAATSEN #####
	//field hints
	$(".hintfield").each(function(i) {
        var beginStyle = $(this).attr("style");
		$(this).addClass("hint");
		$(this).val($(this).attr("title"));

        $(this).bind("focus", function(e) {
            if ($(this).val() == $(this).attr("title")) {
                $(this).val("");
				$(this).removeClass("hint");
            }
        });
        $(this).bind("blur", function(e) {
            if ($(this).val() == "") {
				$(this).addClass("hint");
				$(this).val($(this).attr("title"));
            } else {
				$(this).css("border-color", "");
				$(this).removeClass("hint");
			}
        });
    });
	
	$('#reactie_send').click(function(){
		videoId; //global, set in view
		var vereist = new Array("naam", "email", "bericht");

		var regex = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		var dataQuery;
		var form = document.getElementById('reactie');
		var fout = new Array(); //array met fout ingevulde velden

		for (var i = 0; i < vereist.length; i++)
		{
			if (getHintfield(eval("form." + vereist[i])) == "")
			{ //controleer op lege of standaard waarde
				eval("form." + vereist[i] + ".blur()");
				fout.push(vereist[i]);
			}
		}
		if (fout.length) alert("Vul aub alle velden in.");

		if (!regex.test(getHintfield(form.email)) && fout.length == 0)
		{
			alert("Het ingevoerde emailadres is geen geldig adres.");
			form.email.focus();
			fout.push("email");
		}

		if (fout.length)
		{
//			for (var i = 0; i < fout.length; i++)
//			{
//				var veld = eval("form." + fout[i]);
//				veld.style.borderColor = "#FF6C00";
//				//$(veld).addClass("fielderror");
//			}
			return false;
		}
		
		dataQuery = "&video=" + videoId + "&naam=" + getHintfield(form.naam) + "&email=" + getHintfield(form.email) + "&bericht=" + getHintfield(form.bericht);
		
		$.ajax({
			type: "POST",
			url: "/public/ajax/reactie.php",
			data: dataQuery,
			dataType: "json",
			success: function(response){ //START VULLEN
				if (typeof(response.success) != "undefined")
				{
					if (response.success)
					{
						$('#reactie').hide();
						$('#reactiebedankt').show();
					}
					else {
						if (response.error == 'requiredfields') 
							alert("Vul aub alle velden in.");
						else if (response.error == 'email') 
							alert("Het ingevoerde emailadres is geen geldig adres.");
						else alert("Uw reactie kon niet worden verwerkt, probeer het aub later nogmaals.")
					}
				}	
			},
			error: function(reqObject, error, ex){
				alert("Uw reactie kon niet worden geplaatst, probeer het aub later nogmaals.");
			}
		});
		return false; //form niet submitten
	})
	
	/**
	 * Download pdf
	 */
	$('#downloadpdf').click(function ()	{
		$('#pdfform').attr('target', '_blank');
		$('#pdfform').submit(); 
	});
	
	/**
	 * Herbereken ingredienten
	 */
	$('.aantalpersonen').change(function(){
		var aantal = $(this).val();
		$('.aantalpersonen').each(function(){
			$(this).val(aantal);
		});
		videoId; //set in de view
		
		$.ajax({
			type: "POST",
			url: "/public/ajax/ingredienten.php",
			data: '&video_id=' + videoId + '&aantal=' + aantal,
			dataType: "html",
			success: function(response){ //START VULLEN
				$('#ingredientenkolommen').html(response);
			},
			error: function(reqObject, error, ex){
				
			}
		});
	})
	
});

function openPagina()
{
	var nieuw = $('#paginanummer').val();
	if (typeof(baseURL) == 'undefined' || typeof(pages) == 'undefined' || isNaN(parseInt(pages))) return false;
	if (!isNaN(nieuw) && 0 < nieuw && nieuw <= parseInt(pages) )
	{			
		document.location = baseURL + '/' + nieuw;
		return true;
	}
	//alert('De ingevoerde pagina kon niet worden gevonden');
	$('#paginanummer').val(paginanummer);
}

/**
 * Haal de waarde op van een hintfield (een veld met klasse fieldhint, waar het title attribuut als hint wordt gebruikt)
 * @param {Object} veld
 */
function getHintfield(veld)
{
	if (veld.value == "" || veld.value == veld.title) return "";
	else return veld.value
}

