;(function($) { 
$.fn.extend(
	{
		processLink: function(e)
		{
			var link = $(this);
			var url = link.attr("href");
			url = url + (url.indexOf("?") != -1 ? "&" : "?") + "jQuery=" + Math.random();
			if (this.attr("jqlink") == "delete" || this.attr("jqlink") == "link")
			{
				var title = this.attr("title");				
				if (title == "")
					title = "Do you want delete this item?";
				if(this.attr("jqlink") == "delete")
				{
					if (!window.confirm(title))
					{
						return false;
					}
				}
				$.ajax(
					{
						url: url
						,
						dataType: "json"
						,
						success: function(data, status)
						{
							link.processLinkResponse(data);
						}
						,
						error: function(data, status)
						{
							$.blockUI({message: "Link processign error: " + status});
							setTimeout($.unblockUI, 1000);
						}
					}
				);
			}
			else if (this.attr("jqlink") == "form" || this.attr("jqlink") == "detail")
			{
				var type = this.attr("jqlink");
				var title = this.text();
				$.ajax(
					{
						url: url
						,
						success: function(data, status)
						{
							var buttons = {
								Close: function() {
									$.modalDialog.dialog("destroy");
									$.modalDialog = null;
								}
							}
							if (type == "form")
							{
								buttons.Ok = function() {
									$.modalDialog.find("form").submit();
								}
							}
							height = $(window).height() / 2 - 100;
							width = $(window).width() / 2 - 100;
							$.modalDialog = $("<div class=\"jqLink\">" + data + "</div>").dialog({
								title: title,
								bgiframe: true,
								resizable: true,
								height: height,
								width: width,
								modal: true,
								overlay: {
									backgroundColor: "#000",
									opacity: 0.5
								},
								buttons: buttons
							});
							if ($().initForm)
							{
								$.modalDialog.find("form").initForm();
								$.modalDialog.bind("keydown.casper", function(ev){
									if (ev.keyCode == 13 && ev.srcElement.tagName != "TEXTAREA")
										$.modalDialog.find("form").submit();
								});
							}
						}
						,
						error: function(data, status)
						{
							$.blockUI({message: "Link processign error: " + status});
							setTimeout($.unblockUI, 1000);
						}
					}
				);
			}
			return false;
		}
		,
		processLinkResponse: function(data)
		{
			if (data["Message"] != "" && !(data["Message"] == undefined))
			{
				alert(data["Message"]);
				//TODO: show message
			}

			if (data["Status"] == "OK")
			{
				if (data["Type"] == 1)
				{
					if (data["URL"] == "")
					{
						window.location = "index.php";
					}
					else
					{
						window.location = data["URL"];
					}
				}
				if (data["Type"] == 2)
				{
					var obj = null;
					var filter = "";
					if (data["Control"] != null && data["Control"] != "")
					{
						obj = $(data["Control"]);
						filter = data["Control"];
					}
					else
					{
						obj = this.parents("#jqrefresh");
						if (obj.length == 0)
						{
							obj = $("#jqrefresh");
						}
						else
						{
							obj = $(obj.get(0));
						}
						if (obj.length == 0)
						{
							obj = null;
						}
					}
					var url = data["URL"];
					if (url == null || url == "")
					{
						url = window.location.href;
					}
					if (obj == null)
					{
						window.location = url;
						return;
					}
					url = url + (url.indexOf("?") != -1 ? "&" : "?") + "jQuery=" + Math.random();
					if (filter != "")
						url = url + " " + filter;
					obj.load(url, null, function(data, status, ajax){
						if ($().initForm)
						{
							obj.find("form").initForm();
						}
					});
				}
				if (data["Type"] == 3)
				{
					var obj = null;
					if (data["Control"] != null && data["Control"] != "")
					{
						obj = $(data["Control"]);
					}
					else
					{
						obj = this.parents("#jqrefresh");
						if (obj.length == 0)
						{
							obj = $("#jqrefresh");
						}
						else
						{
							obj = $(obj.get(0));
						}
						if (obj.length == 0)
						{
							obj = $(this.parent().get(0));
						}
					}
					var cont = $(data["Content"]);
					if ($().initForm)
					{
						cont.find("form").initForm();
					}
					obj = obj.replaceWith(cont);
				}
			}
			else if (data["Status"] == "Error")
			{
				$.blockUI({message: "Link processign error: " + data["Error"]["Global"]});
				setTimeout($.unblockUI, 1000);
			}
		}
	}
);

if ($.blockUI)
{
	$.blockUI.defaults.message = "<img src=\"/images/system/loading.gif\"/>";
	$.blockUI.defaults.css.border = "0px solid #000";
	$.blockUI.defaults.css.padding = "0px";
	$.blockUI.defaults.css.top = "0px";
	$.blockUI.defaults.css.left = "0px";
	$.blockUI.defaults.css.width = "auto";
	$.blockUI.defaults.css.height = "auto";
	$.blockUI.defaults.css.cursor = "auto";
	$.blockUI.defaults.css.textAlign = "";
	$.blockUI.defaults.fadeOut = 200;
	$.blockUI.defaults.overlayCSS.opacity = "0.5";
}

})(jQuery);

$().ready(function(){
	$("a[jqlink]").live("click.casper", function(ev){return $(this).processLink(ev)});
});

$.fn.modalDialog = null;


