$.fn.editable = function(url, options) {
	// Options
	options = arrayMerge({
		"url": url,
		"paramName": "q",
		"callback": null,
		"saving": "saving ...",
		"type": "text",
		"submitButton": 0,
		"delayOnBlur": 0,
		"extraParams": {},
		"editClass": null,
		"favoId": 0
	}, options);
	// Set up
	this.click(function(e) {
			if (this.editing) return;
			if (!this.editable) this.editable = function() {
				var me = this;
				me.editing = true;
				me.orgHTML = $(me).html();
				me.innerHTML = "";
				var tempId = $(this).attr("id").split('|');
				options.favoId = tempId[1];
				if (options.editClass) $(me).addClass(options.editClass);
				var f = document.createElement("form");
				var i = createInputElement(me.orgHTML);
				var t = 0;
				f.appendChild(i);
				if (options.submitButton) {
					var b = document.createElement("input");
					b.type = "submit";
					b.value = "OK";

					f.appendChild(b);
				}
				me.appendChild(f);
				$("input[type=submit]").addClass("editButton");
				i.focus();
				$(i).blur(
						options.delayOnBlur ? function() { t = setTimeout(reset, options.delayOnBlur) } : reset
					)
					.keydown(function(e) {
						if (e.keyCode == 27) { // ESC
							e.preventDefault;
							reset
						}
					});
				$(f).submit(function(e) {
					if (t) clearTimeout(t);
					e.preventDefault();
					var p = {};
					p[i.name] = $(i).val();
					$(me).html(options.saving).load(options.url+'?favoId='+options.favoId, arrayMerge(options.extraParams, p), function() {
						// Remove script tags
						me.innerHTML = me.innerHTML.replace(/<\s*script\s*.*>.*<\/\s*script\s*.*>/gi, "");
						// Callback if necessary
						if (options.callback) options.callback(me);
						// Release
						me.editing = false;
					});
				});
				function reset() {
					me.innerHTML = me.orgHTML;
					if (options.editClass) $(me).removeClass(options.editClass);
					me.editing = false;
				}
			};
			this.editable();
		})
	;
	// Don't break the chain
	return this;
	// Helper functions
	function arrayMerge(a, b) {
		if (a) {
			if (b) for(var i in b) a[i] = b[i];
			return a;
		} else {
			return b;
		}
	};
	function createInputElement(v) {
		if (options.type == "textarea") {
			var i = document.createElement("textarea");
			options.submitButton = true;
			options.delayOnBlur = 100; // delay onBlur so we can click the button
		} else {
			var i = document.createElement("input");
			i.type = "text";
		}
		$(i).val(v);
		i.name = options.paramName;
		return i;
	}
};





$(document).ready(function()	{
	//controle op activatie van favosystem

	$.ajax({
		url: "/modules/favoSystem/ajaxRequests/checkActive.php",
		type: "GET",
		dataType: "html",
		//cache: false,
		success: function(data) {
			if (data == 'use')	{
				//start favoSystem
				useFavoSystem();
			}
		}
	});
})

function useFavoSystem()
{
	var checkUse = document.getElementById('favoriteDetail');
	if (checkUse != undefined)	{
		//bestaat
		var divRel = $("div#favoriteDetail").attr("class").split("_");
		if (divRel[1] != '')	{
			//handel de inhoud af
			updateFavoOptions(divRel[1]);

			//toevoegen opvangen
			$("div#favoriteDetail[class=favo_"+divRel[1]+"] span.makeFavo").live("click",function()	{
				$("div#favoriteDetail[class=favo_"+divRel[1]+"] img.favoLoad").show();
				//request voor het toevoegen
				$.ajax({
					url: "/modules/favoSystem/ajaxRequests/addFavorite.php",
					type: "GET",
					data: "strId="+divRel[1],
					dataType: "html",
					//cache: false,
					success: function(data) {
						if (data != 'add')	{
							//show error
							$("div#favoriteDetail[class=favo_"+divRel[1]+"]").append(data);
						}else	{
							updateFavoOptions(divRel[1]);
							$("div#favoriteDetail[class=favo_"+divRel[1]+"] img.favoLoad").hide();
						}
					}
				});

			})
		}

	}
}

function updateFavoOptions(workStrId)
{
	//request voor status van veld
	$.ajax({
		url: "/modules/favoSystem/ajaxRequests/handleField.php",
		type: "GET",
		data: "strId="+workStrId,
		dataType: "html",
		//cache: false,
		success: function(data) {
			//data vullen
			$("div#favoriteDetail[class=favo_"+workStrId+"]").html(data).show();
		}
	});
}

function showFavoOverview(page)	{
	//request voor vullen van de div
	//page addon
	if (page !== false)	{
		var pageAddOn = '?page='+page;
	}else	{
		var pageAddOn = '';
	}

	$(".favoOverview").load("/modules/favoSystem/ajaxRequests/showFavoOverview.php"+pageAddOn," ", function()	{
		handleOverviewActions();
	});



}

//verwijderen opvangen
function handleOverviewActions()
{
	$("img.delFavo").live("click",function()	{
		//verwijderen
		var favoIdAndPage = $(this).attr("id").split('|');
		var favoId = favoIdAndPage[1];
		if (favoIdAndPage[2] == 0)	{
			var goPage = false;
		}else	{
			var goPage = favoIdAndPage[2];
		}

		 $.ajax({
			url: "/modules/favoSystem/ajaxRequests/delFavorite.php",
			type: "GET",
			data: "favoId="+favoId,
			dataType: "html",
			//cache: false,
			complete: function(XMLHttpRequest, textStatus) {
				//data bijwerken
				showFavoOverview(goPage);
			}
		});
	}).mouseover(function()	{
		$(this).attr("src","/grafix/verwijder_hover.gif");
	}).mouseout(function()	{
		$(this).attr("src","/grafix/verwijder.gif");
	})

	//commentaar verwerken
	$(".editComment").editable("/modules/favoSystem/ajaxRequests/handleComment.php",
								{paramName : "comment",
								 saving: "<img src='/grafix/loader.gif'>",
								 submitButton: 1,
								 delayOnBlur: 100
								});

	$(".pagination a").click( function()	{
		var pageNum = $(this).attr("rel");

		if (pageNum == '' || pageNum == 'undefined')
			return

		//pagina inladen
		showFavoOverview(pageNum);
	})
}


