$.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("submit");
				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()	{

	checkUseFavoSystem();
})

function checkUseFavoSystem() {
	//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()
{
	if ($("div.favoriteDetail").length > 0) var checkUse = true;

	if (checkUse != undefined)	{
		//first get the class(id) for showing
		var curPageFav = '';
		$("div.favoriteDetail").each(function()	{
			curPageFav = curPageFav + "/" + $(this).attr("id");
		})

		if (curPageFav != '')	{
			//handel de inhoud af
			loadFavosPerPage(curPageFav);

			$("div.favoriteDetail").each(function()	{
				//bestaat
				var divRel = $(this).attr("id").split("_");
				if (divRel[1] != '')	{
					//handel de inhoud af
					//alert(divRel[1]);

					updateFavoOptions(divRel[1]);

					//toevoegen opvangen
					//$("div.favoriteDetail[class=favo_"+divRel[1]+"] span.makeFavo").live("click",function()	{
					$("div#favo_"+divRel[1]+" a").live("click",function()	{
						$("div#favo_"+divRel[1]+" span.makeFavo img").attr('src','/grafix/loader.gif');
						//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#favo_"+divRel[1]+"").append(data);
								}else	{
									updateFavoOptions(divRel[1]);
								}
							}
						});

					})
				}
			});
		}

	}
}




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#favo_"+workStrId+"").html(data).show();
			$("div#favo_"+workStrId+" img.favoLoad").hide();
		}
	});
}

function loadFavosPerPage(classRow)
{
	//request voor status van veld
	$.ajax({
		url: "/modules/favoSystem/ajaxRequests/handleFavPage.php",
		type: "GET",
		data: "clRow="+classRow,
		dataType: "html",
		//cache: false,
		success: function(data) {
			//data vullen
			if (data != '0')	{
				var $holder = $('<div/>').html(data);
				var $children = $holder.children('div');
				$.each($children, function(val, i) {

					$("div#favo_"+i.id+"").html(i.innerHTML).show();

				});
				$holder.remove();
			}
		}
	});
}



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/favorite/remove_hover.gif");
	}).mouseout(function()	{
		$(this).attr("src","/grafix/favorite/remove.gif");
	})

	$(".shop_button").mouseover(function()	{
		$(this).attr("src","/grafix/favorite/inSC_hover.gif");
	}).mouseout(function()	{
		$(this).attr("src","/grafix/favorite/inSC.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);
	})
}


