var ajaxTimeout=20;

$(document).ready(function(){
	$("#NavigatieKnoppen a, #SubNavigatie a").each(function (i, elem) {
		var tip=$(elem).attr("title");
		$(elem).attr("title","");
		$(this).bind("mousemove", function(){
			var pagina_id=$(this).attr("rel");
			if(pagina_id!==""){
				$(".SubGroep").hide();
				$("#SubGroep" + pagina_id).show();
			}
		});
		if(tip!==''){
			$(elem).simpletip({content: "<img src='http://www.groenlinksweststellingwerf.nl/afbeeldingen/iconset/information.png'/> " + tip, offset: [0, -2], position: 'top' });
		}
	});
	$(".TextSizeBox a#kleiner").simpletip({content: "<img src='http://www.groenlinksweststellingwerf.nl/afbeeldingen/iconset/information.png'/>Kleinere tekst", position: 'top', offset: [0, -2]});
	$(".TextSizeBox a#groter").simpletip({content: "<img src='http://www.groenlinksweststellingwerf.nl/afbeeldingen/iconset/information.png'/> Grotere tekst", position: 'top', offset: [0, -2]});
});

function TextKleiner(){
	$("#inhoud, #inhoud2, #inhoud div, #inhoud2 div, #inhoud span, #inhoud2 span").each(function(i){
		var size=$(this).css("font-size");
		size=size.substr(0,size.length-2);
		size=(parseInt(size)-1) + "px";
		$(this).css("font-size",size);
	});
}

function TextGroter(){
	$("#inhoud, #inhoud2, #inhoud div, #inhoud2 div, #inhoud span, #inhoud2 span").each(function(i){
		var size=$(this).css("font-size");
		size=size.substr(0,size.length-2);
		size=(parseInt(size)+1) + "px";
		$(this).css("font-size",size);
	});
}

function showMyVideos(data) {
	var entries = data.feed.entry || [];
	for (var i = 0; i < entries.length; i++) {
		var entry = entries[i];
		loadVideo(entry.media$group.media$content[0].url, false,'player' + i);
	}
}

function loadVideo(playerUrl, autoplay, id) {
	swfobject.embedSWF(playerUrl + '&rel=1&border=0&fs=1&autoplay=' + (autoplay?1:0), id, '200', '150', '9.0.0', false, false, {allowfullscreen: 'true'});
}

function CreateCommentBox(parent_id, comment_type, comment_destination_id){
	$("#AddCommentLink").hide("fast");
	$(".CommentForm").remove();
	if(parent_id==0){
		var title="Plaats een nieuwe reactie";
		var button="Plaats Reactie";
	} else {
		var title="Reageer op dit bericht";
		var button="Plaats Reactie";
	}
	var box = '<form class="CommentForm"><a onclick="CloseCommentBox();" class="CommentboxCloseLink"></a><h3>' + title + '</h3><input type="hidden" id="CommentParent" value="' + parent_id + '"/><input type="hidden" id="CommentType" value="' + comment_type + '"/><input type="hidden" id="CommentDestinationId" value="' + comment_destination_id + '"/><span class="label">Naam:</span><input type="text" class="textbox" id="CommentNaam"/><br/><br/><span class="label">E-mail:</span><input type="text" class="textbox" id="CommentEmail"/><br/><br/><span class="label">Bericht:</span><textarea id="CommentMessage"></textarea><br/><br/><span class="label">Anti-spam test:</span><br/><br/><div id="captcha"></div><br/><input type="button" class="button" value="' + button + '" onclick="PostComment();"/></form>';
	if(parent_id==0)
		$("#CommentReplyBox").html(box)
	else
		$("#comment" + parent_id).append(box);
	Recaptcha.create("6LcYHAoAAAAAAC8iZdTJ6woaxACTIG-5b8dotc1o", "captcha", {theme: "white", lang: "nl"});
	$(".CommentForm").slideDown("fast");
}

function CloseCommentBox(){
	$("#AddCommentLink").show("fast");
	$(".CommentForm").slideUp("fast", function(){
		$(this).remove();
	});
}

function PostComment(){
	$(".CommentForm h3").addClass("Busy");
	var v_parent_id=$("#CommentParent").val();
	var v_message=$("#CommentMessage").val();
	var v_comment_type=$("#CommentType").val();
	var v_destination_id=$("#CommentDestinationId").val();
	var v_email=$("#CommentEmail").val();
	var v_naam=$("#CommentNaam").val();
	var challenge=Recaptcha.get_challenge(); 
	var response=Recaptcha.get_response(); 
	$.ajax({
		type: "POST",
		url: "http://www.groenlinksweststellingwerf.nl/ajax/plaats_bericht.php",
		dataType: "json",
		timeout: (ajaxTimeout * 1000),
		data: ({recaptcha_challenge_field: challenge, recaptcha_response_field: response, parent_id : v_parent_id, message : v_message, comment_type: v_comment_type, destination_id: v_destination_id, email: v_email, naam: v_naam}),
		success: function(data){
			if(data.error==""){
				$(".CommentForm h3").removeClass("Busy");
				var comment='<div class="comment HiddenComment" id="comment' + data.comment_id + '"><img src="http://1.gravatar.com/avatar/' + data.emailhash + '?s=50&d=identicon" class="Avatar"/><div><span class="commentinfo">' + data.author + ', ' + data.datetime_posted + '</span>' + data.message + '</div><a class="reply_link" onclick="CreateCommentBox(\'' + data.comment_id + '\', \'' + data.comment_type + '\', \'' + data.destination_id + '\');">Reageren op ' + data.author + '</a></div>';
				$("#CommentsListing .DisplayedMessage").hide("fast");
				if(data.parent_id==0)
					$("#CommentsListing").append(comment);
				else
					$("#comment" + data.parent_id).after(comment);
				$("#comment" + data.comment_id).css("margin-left",(data.level * 20) + "px");
				$(".CommentForm").slideUp("fast", function(){
					$("#comment" + data.comment_id).slideDown("fast");
					$(this).remove();
				});
			} else {
				Recaptcha.destroy();
				Recaptcha.create("6LcYHAoAAAAAAC8iZdTJ6woaxACTIG-5b8dotc1o", "captcha", {theme: "white", lang: "nl"});
				if(data.error=="invalid recaptcha"){
					alert("Foute captcha code, probeer het nog eens.");
				}
				if(data.error=="missing details"){
					alert("Niet alle benodigde velden waren ingevuld.");
				}
			}
		},
		error: function( objAJAXRequest, strError, errorThrown ){
			alert("Bericht plaatsen is mislukt. Probeer het nog eens? Error:" + strError + "-" + errorThrown);
			$(".CommentForm h3").removeClass("Busy");
		}
	});
}
