/*global jQuery:false, window:false, document:false */

function GuestBook(cpp) {
	this.comments_per_page = (cpp)?cpp:0;
	this.currentpage = 0;
	
	this.loadComments();
}

GuestBook.prototype.cjson = function (url, data, async, asynchandler, mode) {
	async = (async)?true:false;
	mode = (mode)?mode:'GET';

	if(async) {
		jQuery.ajax({
			url:url,
			data:data,
			async:true,
			global:false,
			type:mode,
			complete:(asynchandler)?asynchandler:false
		});
		return true;
	} else {
		var json_text = (jQuery.ajax({
				url:url,
				data:data,
				async:false,
				global:false,
				type:mode
			}).responseText);
		var json = eval('(' + json_text + ')');
		
		if(json.error!==false) {
			window.alert(json.error);
			return false;
		} else {
			return json.result;
		}
	}
};
GuestBook.prototype.loadComments = function () {
	this.cjson('/_modules/guestbook/json.php',{t:'comments'},true,this.showComments);
};
GuestBook.prototype.showComments = function (xhr, stat) {
	var data = eval('(' + xhr.responseText + ')');
	if(data.result && data.result.length > 0) {	
		for(var i = 0; i < data.result.length; i += 1) {
			jQuery('#guestbook_comments #comments').append(GuestBook.commenthtml(data.result[i]));
		}
	} else {
		//alert("I haz no comments *cry*");
	}
	
	window.gb.paginate();
};
GuestBook.prototype.paginate = function() {
	if(this.comments_per_page === 0) { return; }
	jQuery('#guestbook_comments')
		.find('.comment').show()
		.filter(':lt(' + (this.currentpage * this.comments_per_page) + ')').hide().end()
		.filter(':gt(' + ((this.currentpage+1) * (this.comments_per_page-1)) + ')').hide().end();
	
	var rows = jQuery('#guestbook_comments').find('.comment').size();
	var pages = Math.ceil(rows / this.comments_per_page);
	
	if(pages > 1) {
		jQuery('#guestbook_comments .pager').show();
		
		var pager = jQuery('#guestbook_comments .pager');
			pager.html('<div class="page-name">Pagina\'s</div>');
		
		for(var page = 0; page < pages; page += 1) {
			pager.append('<a class="page-number ui-corner-all ui-state-default" href="#"><span>' + (page+1) + '</span></a>');
		}
		
		pager.find('.page-number').bind('click',function() {
			var page = jQuery(this).text();
				page = parseInt(page, 10)-1;
				
				window.gb.currentpage = page;
				window.gb.paginate();
				return false;
		});
		pager.find('.page-number:eq(' + this.currentpage + ')').removeClass('ui-state-default').addClass('ui-state-hover');
	} else {
		jQuery('#guestbook_comments .pager').hide();	
	}
};
GuestBook.commenthtml = function(data) {
	var html = '';
	html += '<div class="comment ui-corner-all ui-widget-content">';
	
	html += '<div class="ui-corner-all ui-widget-header">';
	html += '<div class="tijd">' + GuestBook.makeDate(data.tijd) + '</div>';
	if(data.email) {
		html += '<div class="naam"><a href="mailto:' + data.email + ';">' + data.naam + '</a></div>';
	} else {
		html += '<div class="naam">' + data.naam + '</div>';
	}
	html += '</div>';
	
	var comment = GuestBook.parseOpmerking(data.opmerking);
	
	html += comment;
	
	html += '</div>';
	return html;
};
GuestBook.parseOpmerking = function(txt) {
	var reto = {img:'',opmerking:''};
	
	// find links,
	// add http:// to www-links
	reto.opmerking = txt.replace(/\b(www\.[^\s]+)\b/g,'http://$1');
	// and remove double http://'s
	reto.opmerking = reto.opmerking.replace(/(http:\/\/){2}/g,'http://');
	// transform links into hyperlinks
	reto.opmerking = reto.opmerking.replace(/\b(http:\/\/([^\s]+))\b/g,'<a href="$1" target="_blank">$2</a>');

	var reth = jQuery('<div class="opmerkingen">' + reto.opmerking + '</div>');
	
	reth.find('a').each(function() {
		var href = jQuery(this).attr('href').toString();
		if(href.indexOf('http://')===-1) { href = 'http://' + href; }
		
		if(/\.(gif|jpe?g|png)/.test(href)) {
			reto.img = '<a href="' + href + '" target="_blank"><img src="/_modules/guestbook/thumb.php?f=' + encodeURIComponent(href) + '" width="50" height="50" /></a>';
		} else {
			jQuery(this).attr('href',href);
		}
		jQuery(this).text(jQuery(this).text().toString().replace('http://',''));
	});

	var ret = reto.img + '<div class="opmerkingen">' + reth.html() + '</div>';
	return ret;
};
GuestBook.makeDate = function(tijd_text) {
	var date = new Date(tijd_text);
	
	return 	date.getDate() + '-' + 
			(date.getMonth()+1<10?'0'+(date.getMonth()+1):(date.getMonth()+1)) + '-' + 
			date.getFullYear() + ' ' + 
			(date.getHours()<10?'0'+date.getHours():date.getHours()) + ':' + 
			(date.getMinutes()<10?'0'+date.getMinutes():date.getMinutes());
};
GuestBook.addcomment = function(form) {
	if(jQuery('#guestbook_form')[0].captcha) {
		
		if(form.naam.value === form.naam.defaultValue) { window.alert("Een naam is verplicht"); return false; }
		if(form.opmerking.value === form.opmerking.defaultValue) { window.alert("Een opmerking is verplicht"); return false; }
		
		
		var data = {
			naam:form.naam.value,
			email:form.email.value,
			opmerking:form.opmerking.value
		};
		window.gb.cjson('/_modules/guestbook/json.php?t=addcomment', data,true,false,'POST');
		data.tijd = new Date().toString();
		
		jQuery('#guestbook_comments #comments').prepend(GuestBook.commenthtml(data));
		form.reset();
		
		window.gb.currentpage = 0;
		window.gb.paginate();
	} else {
		window.alert('Ongeldige CAPTCHA invoer');
		form.reset();
	}
	return false;
};