$.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()	{
	//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();
			}else	{
				throwRegister();
			}			
		}
	});
})

//zeg dat degene zich eerst moet registreren
function throwRegister()
{
	if ($("div.favoriteDetail").length > 0)	{
		
		var curPageFav = '';
		$("div[title^=favo_]").each(function()	{
			curPageFav = curPageFav + "/" + $(this).attr("title");
		})
		
		if (curPageFav != '')	{
			
			//check showing what kind of button
			if ($("div.favoriteDetail").hasClass('overviewFavos'))	{
				var overview = 'yes';
			}else	{
				var overview = 'no';
			}
			
			//handel de inhoud af
			//request voor status van veld
			$.ajax({
				url: "/modules/favoSystem/ajaxRequests/handleFavPage.php",
				type: "GET",
				data: "nlf=true&clRow="+curPageFav+"&sOver="+overview,
				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[title=favo_"+i.id+"]").html(i.innerHTML).show();

						});
						$holder.remove();
					}
				}			
			});
			
			
			$("div.favoriteDetail").each(function()	{		
		
				var divRel =  $(this).attr("title").split("_");
				if (divRel[1] != '')	{			
					
					$("div.favoriteDetail[title=favo_"+divRel[1]+"] a.noLoginFavo").live("click",function()	{
						var melding = $("div.favoriteDetail[title=favo_"+divRel[1]+"] a.noLoginFavo span").html();
						$.jGrowl(melding,
						 {header: "Informatie:"}
						 );				
					});
				}
			})
			
					
		}
	}
}

function useFavoSystem()
{
	if ($("div.favoriteDetail").length > 0)	{
		
		var curPageFav = '';
		$("div[title^=favo_]").each(function()	{
			curPageFav = curPageFav + "/" + $(this).attr("title");
		})
		
		if (curPageFav != '')	{
			
			//check showing what kind of button
			if ($("div.favoriteDetail").hasClass('overviewFavos'))	{
				var overview = 'yes';
			}else	{
				var overview = 'no';
			}
			
			//handel de inhoud af
			loadFavosPerPage(curPageFav,overview);
			
			$("div.favoriteDetail").each(function()	{		
				//bestaat
				var divRel = $(this).attr("title").split("_");
				if (divRel[1] != '')	{
					
					//handel de inhoud af
					//updateFavoOptions(divRel[1],overview);

					//toevoegen opvangen
					$("div.favoriteDetail[title=favo_"+divRel[1]+"] a.makeFavo").live("click",function()	{
						$("div.favoriteDetail[title=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[title=favo_"+divRel[1]+"]").append(data);
								}else	{
									updateFavoOptions(divRel[1],overview);
									$("div.favoriteDetail[title=favo_"+divRel[1]+"] img.favoLoad").hide();
								}
							}
						});

					})
				}
			
			})	
		}

	}
}

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

function loadFavosPerPage(classRow,overview)
{
	//request voor status van veld
	$.ajax({
		url: "/modules/favoSystem/ajaxRequests/handleFavPage.php",
		type: "GET",
		data: "clRow="+classRow+"&sOver="+overview,
		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[title=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);
	})
}


