function PersonalLink(link, title, active,zlowId)
{
	if(link) this.link = link;
	if(title) this.title = title;	
	if(active || active == '') this.active = active;
	if(zlowId) this.zlowId = zlowId;
}

function PersonalLinks()
{
	this.editor_visible = false;
	this.links = new Array();
	this.page = 0;
	this.links_per_page = 5;


}

PersonalLinks.prototype.checkIfIDexist = function (id) {
		
		for (linkVar in this.links) {
			if (id == this.links[linkVar].zlowId) return true;
		}
		return false;
	}

PersonalLinks.prototype.show_editor_pl = function()
{
	$('#blok_pl2 .clinks a').each(function(i,obj){
		editor = "<span class='pl_editor'>";
		editor += "<img src='img/2.0/link_edit.gif' onclick='pl.edit("+i+");' />";		
		editor += "<img src='img/2.0/link_delete.gif' onclick='if(confirm(\"Weet u zeker dat u deze link wilt verwijderen?\"))pl.remove("+i+");' /></span>";
		$(this).before(editor);
	});
}

PersonalLinks.prototype.show_editor = function()
{
	$('#elinks').show();
	$('#morelinks2').hide();
	this.clean_editor();
	this.show_editor_pl();
	$('#pl_advanced_edit').show();
	$('.mlinks a.meer:not([href="#"]),.blokb .clinks a,.blokr .clinks a').each(function(i){
		if (this.id == '' || $(this).attr('href') == './kinderslot.htm') return;
		$(this).before("<span class='pl_adder'><img src='img/2.0/link_add.gif' onclick='pl.add(\"" +this.id +"\")' /></span>");
	});

	this.editor_visible = true;
	$('#balloontip').show('fast',function(){		
		//show the ballontip directly over the 6th link of the right klolumn
		var pos = $('#kolomrechts .blokb:visible .blokcontent .clinks a:eq(6)').offset();    
		var width = $('#kolomrechts .blokb:visible .blokcontent .clinks a:eq(6)').width();  
		$("#balloontip").css( { 
			"z-Index": 100, 
			"left": (pos.left - 182) + "px", 
			"top":(pos.top - 134 )+ "px" 
			} );  
	});
}

PersonalLinks.prototype.clean_editor = function()
{
    $('#plSubmit').attr({'value':'Toevoegen'});
	$('#plId').val(-1);
	$('#plLink').val('http://');
	$('#plDescription').val('');
}

PersonalLinks.prototype.hide_editor = function()
{
	$('#elinks').hide();
	$('#morelinks2').show();
	$('#blok_pl2 .clinks .pl_editor').remove();
	$('#balloontip').hide();
	$('.mlinks .pl_adder,.blokb .clinks .pl_adder,.blokr .clinks .pl_adder').remove();
	$('#pl_advanced_edit').hide();
	this.clean_editor();
	this.editor_visible = false;
}

PersonalLinks.prototype.toggle_editor = function()
{
	
	if(this.editor_visible)
	{
		this.hide_editor();
	}
	else
	{
		this.show_editor();
	}
}



PersonalLinks.prototype.edit = function(i)
{
	$('#plSubmit').attr({'value':'Aanpassen'});
	$('#plId').val(i+(this.page*pl.links_per_page));
	$('#plLink').val(this.links[i+(this.page*pl.links_per_page)].link);
	$('#plDescription').val(this.links[i+(this.page*pl.links_per_page)].title);
}

PersonalLinks.prototype.remove = function(i)
{
	this.links.splice(i+(this.page*pl.links_per_page),1);
	linkActive = this.getActivelinks();
	if (linkActive < (this.page*pl.links_per_page)+1) this.page--;
	
	this.save();
	this.show();
}


alert_balloon_timer = null;
PersonalLinks.prototype.positionBalloon = function(balloonId) {
	if ($('#'+balloonId).length == 0 || (jQuery.browser.version <= 6 && $.browser.msie)) return;
	alert_balloon_timer
	clearTimeout(alert_balloon_timer);
	$('#alert_balloon').hide();
	$('#alert_balloon').html("<span class='ui-icon ui-icon-close'></span>Uw link is toegevoegd aan het persoonlijke<br />links blok boven aan de pagina.");
	$('#alert_balloon').css({
		position: 'absolute',
		left: $('#'+balloonId).position().left,
		top: ($('#'+balloonId).position().top)
	});
	$('#alert_balloon').animate({ 'top': "-=50px", "opacity": "show"}, 1250);
	alert_balloon_timer = setTimeout("$('#alert_balloon').hide('slow');",5000);	
}

PersonalLinks.prototype.getActivelinks = function()
{
	var linkActive = 0;
	for (linkVar in this.links){
		if (this.links[linkVar].active=="") {
			linkActive++;
		}
	}
	return linkActive;
}

PersonalLinks.prototype.add_link = function(link, title, active,zlowId,balloonId)
{
	if (pl.links.length >= 25) {
		alert ("Maximum van 25 links bereikt.");
		return;
	}
	this.positionBalloon(balloonId);
	this.links.push(new PersonalLink(link, title, active,zlowId));
	linkActive = this.getActivelinks();
	this.page = Math.round(((linkActive-1)/this.links_per_page)+0.5)-1;
	if (this.page > pl.links_per_page) this.page = pl.links_per_page;
	if (this.page <0 ) this.page =0;
	this.save();
	this.load();
	this.show();
	//hide the ballon tip if visible.
	if($('#balloontip').length > 0) $('#balloontip').hide();
	return false;
}



PersonalLinks.prototype.update_link = function(id, link, title, active)
{
	
	if (this.links[id].link != link) this.links[id].zlowId = ""; //set to '' after changed.	
	this.links[id].link = link;
	this.links[id].title = title;
	this.links[id].active = active;	
	
	
	this.show();
	this.save();
	return false;
}



PersonalLinks.prototype.add = function(id)
{
	link = $("#" + id).attr('href');
	title = $("#" + id).html();
	zlowId = $("#" + id).attr('id');
	x=0;
	while ($('#'+zlowId+'_'+x).length > 0 || this.checkIfIDexist(zlowId+'_'+x)) x++;
	zlowId = zlowId+'_'+x;	
	return this.add_link(link, title, '',zlowId,id);
}

PersonalLinks.prototype.initFormValidation = function()
{
	$('#plForm').validate({
		rules: {
		
			plDescription : {
				required :true
			},
			plLink : {
				required : true
			}
		},
		submitHandler : function () {
			pl.save_form();
		},
		errorPlacement: function(error, element) { 
			if (element.attr('id')=='plDescription') {
				error.appendTo("#form_plDescription_error"); 		
				$("#form_plDescription_error").show();
			} 
			if (element.attr('id')=='plLink') {
				error.appendTo("#form_plLink_error"); 			
				$("#form_plLink_error").show();	
			} 
		}
	});
}

PersonalLinks.prototype.save_form = function()
{
	
	if (($('#plLink').val() == null) || ($('#plLink').val() == "") || ($('#plLink').val() == "http://")) return false;
	
	if($('#plId').val() == -1)
	{
		var zlowId = 'k4perslink';
		x=0;
		while ($('#'+zlowId+'_'+x).length > 0 || this.checkIfIDexist(zlowId+'_'+x)) x++;
		zlowId = zlowId+'_'+x;	
		this.add_link($('#plLink').val(),$('#plDescription').val(), '',zlowId);
	}
	else
	{
		this.update_link($('#plId').val(), $('#plLink').val(),$('#plDescription').val(), '');
	}
	
	//hide the ballon tip if visible.
	$('#balloontip').hide();
	
	return true;
}


PersonalLinks.prototype.save = function()
{
	var tmp_links = new Array();
	var tmp_linksInactive =  new Array();
	for(x=0;x<this.links.length;x++)
	{
		if (this.links[x].active == '') {
			tmp_links.push(this.links[x].link + linkPropertySeperator+""+linkPropertySeperator + this.links[x].title + linkPropertySeperator+""+linkPropertySeperator + this.links[x].zlowId);
		} else {
			tmp_linksInactive.push(this.links[x].link + linkPropertySeperator+""+linkPropertySeperator + this.links[x].title+ linkPropertySeperator+""+linkPropertySeperator + this.links[x].zlowId);
		}
	}
	this.clean_editor();
	setCookie("blok0",pl.links_per_page+linkSeperator+linkSeperator+(tmp_links.join(linkSeperator+linkSeperator)));
	setCookie("blok0Inactive",tmp_linksInactive.join(linkSeperator+linkSeperator));
	if (tmp_links.length == 0) setCookie("blok0",linkSeperator+linkSeperator);
	if (tmp_linksInactive.length == 0) setCookie("blok0Inactive",linkSeperator+linkSeperator);
	$.get("profiel/profiel.php", { 
			change: "toprofiel",
			stel_in:"persoonlijke links",
			ajax:"true"
		} );
}


PersonalLinks.prototype.show = function()
{
	var linksHtml = "";
	var myLinksContainer = document.getElementById('blok_pl2');	
	if(myLinksContainer)
	{
		if(this.links.length>0)
		{
			for(x=this.page*this.links_per_page;((x<this.links.length)&&(x<(this.page+1)*this.links_per_page));x++)
			{
				
				if ((this.links[x].link.indexOf(':') < 0) && (this.links[x].link.indexOf('\\\\') < 0)) {
					this.links[x].link = 'http://' + this.links[x].link;
				}
				if (this.links[x].active == '') {
					if ((this.links[x].zlowId == "undefined") || (this.links[x].zlowId == "")) {
						zlowStr = ' id="k4perslink' + x +'" ';
					} else {
						zlowStr = ' id="' + this.links[x].zlowId +'" ';
					}
					linksHtml += '<a href="' + this.links[x].link + '" ' + zlowStr + '" onclick="n(this.id, \'PERS-' + this.links[x].link + '\', \'PERS-' + this.links[x].link + '\');">' + $('<div/>').text(this.links[x].title).html() + '</a><br/>';
				}
			}
		} else  {
			linksHtml = '<span class="tekst">Er zijn geen persoonlijke links gevonden.</span>';
		}
		$('#blok_pl2 .clinks').html(linksHtml);		
	} 
	
	this.update_paginator(this.page);
	
	if(this.editor_visible)
	{
		this.show_editor_pl();
	}
}


PersonalLinks.prototype.page_prev = function()
{
	if(this.page > 0) this.page--;
	this.page_set();
}

PersonalLinks.prototype.page_next = function()
{
	if(this.page < (Math.round((this.links.length/this.links_per_page)+0.5)-1)) this.page++;
	this.page_set();
}


PersonalLinks.prototype.update_paginator = function(page)
{
	var paginator = '';
	linkActive = this.getActivelinks();
	for(x=0;x<(linkActive/this.links_per_page);x++)
	{
		if (x==0) {paginator += '&nbsp;';} 
		if((Math.round((linkActive/this.links_per_page)) > pl.links_per_page)&&((x < page - 2)||(x > page + 1)))
		{
			paginator += ".";
		}
		else
		{
			paginator += '<a href="#" class="meer" onclick="pl.page_set('+x+');return false">';
			if(x == this.page)
			{
				paginator += '<b>'+(x+1)+'</b>';
			}
			else
			{
				paginator += ''+(x+1)+'';
			}
			paginator += '</a>&nbsp;';
		} 
	}
	if (linkActive>pl.links_per_page) {
		$('#prevnext').show();
		if (linkActive <= pl.links_per_page || this.page==0){
			 $('#paginator').css({
				"margin-left":"60px"
			});
		} else { 
			$('#paginator').css({
				"margin-left":"20px"
			});
		}
		$('#paginator').html(paginator);
		(linkActive <= pl.links_per_page || this.page==0)?$('#prevpage').hide():$('#prevpage').show();
		(linkActive >= pl.links_per_page && ((linkActive / pl.links_per_page) > (this.page + 1.0) ))?$('#nextpage').show():$('#nextpage').hide();
	} else {
		$('#prevnext').hide();
	}
	// make previous and next links ective / inactive.


}

PersonalLinks.prototype.page_set = function(page)
{
	if(page!=null)this.page = page;
	this.show();

}

PersonalLinks.prototype.load = function(){
	var activeLinksCookie	= "blok0";
	var inActiveLinksCookie	= "blok0Inactive";
	this.links = new Array();
	pl.loadSub(activeLinksCookie);
	pl.loadSub(inActiveLinksCookie);
	if (pl.links.length == 0) {
		pl.links.push(new PersonalLink('http://www.omroep.nl', 'omroep.nl', '', 'k4perslink0'));
		pl.links.push(new PersonalLink('http://www.ing.nl', 'mijn ing', '', 'k4perslink1'));
		pl.links.push(new PersonalLink('http://maps.google.nl', 'google maps', '', 'k4perslink2'));
		//this.save();
	}
	
}


PersonalLinks.prototype.loadSub = function(PlCookiename)
{
		var linksHtml = "Er zijn nog geen persoonlijke links opgegeven.<br/>"
		var active = "";
		if (PlCookiename == 'blok0Inactive') {
			active = "no";
		}
		
		if(GetCookie(PlCookiename) != null)
		{			
			UrlGegevens = GetCookie(PlCookiename).split(linkSeperator+linkSeperator);
			if(UrlGegevens.length > 0)
			{
				setDefaultLinks = false;
	
				if(UrlGegevens[0] == "none")
				{
					// Sanity check.
				}
				else
				{
					for (i=0; i < UrlGegevens.length; i++)
					{
						if (parseFloat(UrlGegevens[i])==UrlGegevens[i] ) {
							pl.links_per_page = UrlGegevens[i];
						}
						else {
							var pr = UrlGegevens[i].split(linkPropertySeperator+""+linkPropertySeperator);
							if(pr[0])
							{
								pr[0] = pr[0].replace(/'/g, "?");
								pr[0] = pr[0].replace(/&apos;/g, " ");
								pr[0] = pr[0].replace(/"/g, " ");
								temppers = "";
								temppers = pr[1];
								temppers = temppers.replace(/'/g, "?");
								temppers = temppers.replace(/&apos;/g, " ");
								temppers = temppers.replace(/"/g, " ");
								temppers = temppers.replace(/\+/g, " ");
								pr[1]=temppers;
								pr[2] = pr[2].replace('http://','');
								pr[2] = pr[2].replace(/\+/g, " ");
								pr[2] = pr[2].replace(/\./g,'');
								if (pr[2] == "") pr[2] = "k4perslink";
								if ($('#' + pr[2]).lenght > 0 || this.checkIfIDexist(pr[2])) {
									x=0;
									while ($('#' + pr[2] + "_" + x).lenght > 0 || this.checkIfIDexist(pr[2] + "_" + x)) {
										x++;
									}
									pr[2] = pr[2] + "_" + x;
								}

								var tmp_pl = new PersonalLink(pr[0], pr[1], active, pr[2]);
							
								pl.links.push(tmp_pl);
							}
						}
					}
				}
			} 
		}
	
}

jQuery.extend(jQuery.validator.messages, {
	required: "Dit veld is verplicht.",
	maxlength: jQuery.format("U kunt niet meer dan {0} karakters invoeren."),
	minlength: jQuery.format("U dient minimaal {0} karakters in te voeren."),
	rangelength: jQuery.format("U dient minimaal {0} en maximaal {1} karakters in te voeren."),
	email: "Een geldig emailadres is verplicht.",
	url: "Een geldig URL is verplicht.",
	date: "Een geldige datum is verplicht.",
	number: "Een geldig getal is verplicht.",
	digits: "Gebruik alleen cijfers."
});

pl = new PersonalLinks();
$(document).ready(function(){	
	pl.initFormValidation();
	pl.load();
	pl.show();
	$('#balloontip').hide('fast',function(){		
		//show the ballontip directly over the 6th link of the right klolumn
		var pos = $('#kolomrechts .blokb:visible .blokcontent .clinks a:eq(6)').offset();    
		var width = $('#kolomrechts .blokb:visible .blokcontent .clinks a:eq(6)').width();  
		$("#balloontip").css( { 
			"left": (pos.left - 182) + "px", 
			"top":(pos.top - 134 )+ "px" 
			} );  
	});
});

