/*
	iotServerRequest.js
	last revision: 2009-08-18 by Steven
*/

var iotServerRequest = {};
iotServerRequest.sendRequest = 
	function(uri, data)
	{
		$.post(uri, data,
			function(data, textStatus)
			{
				var xmlData = data.documentElement;
				var nameSpace = "";
				var nameSpaceC = "";
				
				if(xmlData.nodeName != "serverResponse")
				{
					var tmp = xmlData.nodeName.split(":");
					
					if(tmp[1] != "serverResponse")
					{
						iotServerRequest.handleMessage("iotServerRequest Error: Response was not valid");
						return;
					}
					
					nameSpace = tmp[0];
					nameSpaceC = nameSpace + ":";
				}
				
				
				//$(data).find(nameSpace + "\\:serverResponse").children().each(
				$(xmlData).children().each(
					function()
					{
						//if(!this instanceof Element)
							//return;
						
						//common attributes
						
						var selector = $(this).attr("selector");
						var fade = $(this).attr("fade");
						var newPart = $($(this).text());  // =(
						
						if(this.nodeName == nameSpaceC + "redirect")
						{
							var uri = $(this).attr("uri");
							
							if(uri)
							{
								window.location = uri;
								return;
							}
						}
						else if(this.nodeName == nameSpaceC + "message")
						{
							var message = $(this).text();
							var type = $(this).attr("type");
							
							iotServerRequest.handleMessage($.trim(message), type);
						}
						else if(this.nodeName == nameSpaceC + "setText" && selector)
						{
							$(selector).text($(this).text());
						}
						else if(this.nodeName == nameSpaceC + "setHtml" && selector)
						{
							if(fade)
							{
								$(selector).fadeOut(fade,
									function()
									{
										$(selector).html(newPart);
										$(selector).fadeIn(fade);
									}
								);
							}
							else
							{
								$(selector).empty();
								$(selector).append(newPart);
							}
						}
						else if(this.nodeName == nameSpaceC + "remove" && selector)
						{
							if(fade)
							{
								$(selector).fadeOut(fade,
									function()
									{
										$(this).remove();
									}
								);
							}
							else
							{
								$(selector).remove();
							}
						}
						else if(this.nodeName == nameSpaceC + "append" && selector)
						{
							if(fade)
								newPart.hide();
							$(selector).append(newPart);
							if(fade)
								newPart.fadeIn(fade);
						}
						else if(this.nodeName == nameSpaceC + "prepend" && selector)
						{
							if(fade)
								newPart.hide();
							$(selector).prepend(newPart);
							if(fade)
								newPart.fadeIn(fade);
						}
						else if(this.nodeName == nameSpaceC + "replace" && selector)
						{
							if(fade)
							{
								newPart.hide();
								
								$(selector).fadeOut(fade, 
									function()
									{
										$(this).replaceWith(newPart);
										newPart.fadeIn(fade);
									}
								);
							}
							else
							{
								$(selector).replaceWith(newPart);
							}
						}
						else if(this.nodeName == nameSpaceC + "focus" && selector)
						{
							$(selector).focus();
						}
						else if(this.nodeName == nameSpaceC + "popUp")
						{
							if(iotServerRequest.popUpOpen)
							{
								iotServerRequest.popUpClose(
									function()
									{
										iotServerRequest.popUp(newPart, fade);
									}
								);
							}
							else
							{
								iotServerRequest.popUp(newPart, fade);
							}
						}
						else if(this.nodeName == nameSpaceC + "closePopUp")
						{
							if(iotServerRequest.popUpOpen)
								iotServerRequest.popUpClose();
						}
						else if(this.nodeName == nameSpaceC + "show")
						{
							if(fade)
								$(selector).fadeIn(fade);
							else
								$(selector).show();
						}
						else if(this.nodeName == nameSpaceC + "hide")
						{
							if(fade)
								$(selector).fadeOut(fade);
							else
								$(selector).hide();
						}
						else if(this.nodeName == nameSpaceC + "callback")
						{
							var callFunction = $(this).attr("function");
							var parameters = new Array();
							var pcount = 0;
							if(window[callFunction])
							{
								$(this).children().each(
									function()
									{
										parameters[pcount++] = $(this).text();
									}
								);
								
								window[callFunction].apply(null, parameters);
							}
						}
					}
				);
			}, "xml"
		);
		return false;
	}

iotServerRequest.popUpOpen = false;
iotServerRequest.popUpFade = false;

iotServerRequest.popUp = function(node, fade)
{
	iotServerRequest.popUpOpen = true;
	
	node.hide();
	var innerCont = node.appendTo($("body"));
	var bg = node.wrap("<div id=\"iotServerRequestContainer\"></div>")
	
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	
	innerCont.css("position", "absolute");
	var popUpWidth = innerCont.width();
	var popUpHeight = innerCont.height();
	
	innerCont.css("left", Math.ceil((windowWidth - popUpWidth) / 2));
	innerCont.css("top", Math.ceil((windowHeight - popUpHeight) / 2) + $(window).scrollTop());
	
	$("#iotServerRequestContainer").prepend("<div id=\"iotServerRequestBackGround\"></div>");
	
	$("#iotServerRequestBackGround").width($(window).width());
	$("#iotServerRequestBackGround").height($(document).height());
	
	if(fade)
	{
		iotServerRequest.popUpFade = fade;
		bg.fadeIn(fade);
	}
	else
		bg.show();
}

iotServerRequest.popUpClose = function(additional_callback)
{
	iotServerRequest.popUpOpen = false;
	if(iotServerRequest.popUpFade)
	{
		//$("#iotServerRequestBackGround").fadeOut(iotServerRequest.popUpFade);
		//$("#iotServerRequestContainer").fadeOut(iotServerRequest.popUpFade, function() { $(this).remove(); });
		
		var firstCallback = true;
		
		$("#iotServerRequestContainer").children(":not(#iotServerRequestBackGround)").each(
			function()
			{
				if(firstCallback)
				{
					firstCallback = false;
					$(this).fadeOut(iotServerRequest.popUpFade,
						function()
						{
							$("#iotServerRequestContainer").remove();
							
							if(additional_callback)
								additional_callback();
						}
					);
				}
				else
					$(this).fadeOut(iotServerRequest.popUpFade);
			}
		);
	}
	else
	{
		$("#iotServerRequestContainer").remove();
		if(additional_callback)
			additional_callback();
	}
}

iotServerRequest.submitForm = function(url, selector)
{
	iotServerRequest.sendRequest(url, $(selector).serialize());
	return false;
}

//this method allows us to use a custome handler, or just use an unimpressive default alert()
iotServerRequest.handleMessage =
	function(message, type)
	{
		if(iotServerRequest['customMessageHandler'])
			iotServerRequest['customMessageHandler'](message, type);
		else
			alert(message);
	}

$(document).ajaxError(
	function(event, request, options, thrownError)
	{
		var message = "AJAX Error: ";
		switch(request.status)
		{
			case 400:
				message += "Bad Request";
				break;
			case 403:
				message += "Content Forbidden";
				break;
			case 404:
				message += "Content Not Found";
				break;
			case 408:
				message += "Request Timeout";
				break;
			default:
				message += "Unknown Error";
		}
		iotServerRequest.handleMessage(message, "error");
	}
);
