(function() 
	{
		jQuery.fn.heightmatch.defaults = {
			matchList: {}
		};
	}
);

jQuery.fn.del = function (what, record)
{
	$(this).click
	(
		function ()
		{
			// Declare the node, and check if it has an ID
			var object = this.parentNode;
			
			// Keep checking until you find one with an ID
			while(object.id.toString() == '')
			{
				object = object.parentNode;
			}								
								
			if (confirm(lang['del']))
			{
				// Create the values string
				var values = 'call=delete_' + what +
							 '&record=' + record +
							 '&id=' + $.url.param("id");
					
				// Send the request
				$.post(
					'../ajax/',
					values,
					function (data, status)
					{
						var value = $(data).find("error").text();
						if (value != '')
						{
							// Print an error if there is one
							alert(value);
						}
						else
						{
							if ($(data).find("value").text() == "1")
							{
								// Remove the node
								$(object).remove();
							}
						}
					}
				);
			}
		}
	)		
}

jQuery.fn.delrow = function (what, record)
{
	$(this).click
	(
		function ()
		{
			// Declare the node
			var object = this;
			var max = 50;
			var count = 0;
			
			while ((count < max) && !$(object).is('tr'))
			{
				object = object.parentNode;
				count++;
			}
			
								
			if (confirm(lang['del']))
			{
				// Create the values string
				var values = 'call=delete_' + what +
							 '&record=' + record +
							 '&id=' + $.url.param("id");
					
				// Send the request
				$.post(
					'../ajax/',
					values,
					function (data, status)
					{
						var value = $(data).find("error").text();
						if (value != '')
						{
							// Print an error if there is one
							alert(value);
						}
						else
						{
							if ($(data).find("value").text() == "1")
							{
								// Remove the node
								$(object).remove();
							}
						}
					}
				);
			}
		}
	)		
}

jQuery.fn.heightmatch = function(properties)
{
	var height = 0;
	var ext = jQuery.extend(false,jQuery.fn.heightmatch.defaults,properties);
	
	for (col in ext.matchList)
	{
		var h = $(ext.matchList[col]).height();
		
		if (h > height)
		{
			height = h;
		}
	}
	
	for (col in ext.matchList)
	{
		$(ext.matchList[col]).height(height);
	}
}

jQuery.fn.redir = function(target)
{
	$(this).click
	(
		function () 
		{
			target = target || './';
			location.href = target;
		}
	)	
}

jQuery.fn.replicate = function(object)
{
	$(this).click
	(
		function () 
		{
			// Set up a new ID for the element
			var id = $(object).attr("id").substr(0,$(object).attr("id").lastIndexOf('_')) + '_new_';
			var i = 0;
			
			while ($("#" + id + i.toString()).length > 0)
			{
				i++;
			}
			
			id += i.toString();
			
			// Create the duplicate
			var duplicate = object.clone(true);
			duplicate.attr("id",id);
			
			// Update the element names
			var new_id = '';
			var element = new Array();
			element[0] = "input";
			element[1] = "select";
			element[2] = "textarea";
			element[3] = "label";
			
			var attrib = new Array();
			attrib[0] = "id";
			attrib[1] = "name";
			attrib[2] = "for";
			
			for (var j in element)
			{
				duplicate.find(element[j]).each
				(
					function()
					{
						for (var k in attrib)
						{
							if (($(this).attr(attrib[k]) != undefined) && ($(this).attr(attrib[k]).indexOf('[',0) != -1))
							{
								$(this).attr(attrib[k],$(this).attr(attrib[k]).substr(0,$(this).attr(attrib[k]).lastIndexOf('[')) + '[new_' + i.toString() + ']');
							}
						}
					}
				)
			}
			
			var clear_fields = false;
			if ($(this).hasClass('empty-fields'))
			{
				clear_fields = true;
			}
			
			if (clear_fields)
			{
				$(duplicate).find("input, select, textarea").each
				(
					function()
					{
						$(this).val('');
					}
				);
			}

			$(duplicate).insertAfter(object);			
		}
	)
}

jQuery.fn.tooltip = function()
{
	var text = "";
	$("#tooltip").fadeOut('fast');
	
	$(this).mouseover
	(
		function()
		{
			if ($(this).attr("title") != "")
			{			
				// Stop animations (prevents a fadein / fadout
				// queue from forming)
				$("#tooltip").stop(true,true);
				
				// Set the tooltip text
				text = $(this).attr("title");
				$(this).removeAttr("title");
				$("#tooltip").html(text);
				
				// ... and show
				$("#tooltip").show("fast").fadeTo('fast',0.9);
			}				
		}
	)
	
	$(this).mouseout
	(
		function()
		{
			// Hide the tooltip
			$("#tooltip").hide("fast").fadeOut('fast');
			
			// Reset the tooltip text
			$(this).attr("title",text);
			text = "";
		}
	)
}

jQuery.fn.update = function(what, target_id)
{
	$(this).change
	(
		function()
		{
			// Clear the target element and add a loading prompt
			$("#" + target_id).children().remove();
			switch ($("#" + target_id).attr("type"))
			{
				case "select-one":
				case "select-multiple":
					$("#" + target_id).append('<option>' + lang['loading'] + '...</option>');
					break;
				default:
					$("#" + target_id).html(lang['loading'] + '...');
					break;
			}
			
			// Create the values string
			var values = 'call=update_' + what +
						 '&record=' + $(this).attr("value") +
						 '&id=' + $.url.param("id");
						 
			$.post(
				'../ajax/',
				values,
				function (data, status)
				{
					var value = $(data).find("error").text();
					if (value != '')
					{
						// Print an error if there is one
						alert(value);
						
						$("#" + target_id).children().remove();
						switch ($("#" + target_id).attr("type"))
						{
							case "select-one":
							case "select-multiple":
								$("#" + target_id).append('<option>' + lang['no_load'] + '</option>');
								break;
							
							default:
								$("#" + target_id).html(lang['no_load']);
								break;
						}
						
					}
					else
					{
						// Clear the target again
						$("#" + target_id).children().remove();
						
						// Set up a counter
						var count = 0;
						
						switch ($(data).find("type").text())
						{
							// Ungrouped SELECT / OPTION list
							case "ungrouped":							
								$(data).find("value").each
								(
									function ()
									{
										var option = $('<option></option>');
										$(option).attr("value",$(this).attr("id"));
										$(option).attr("text",$(this).text());
										$("#" + target_id).append($(option));
										count++;
									}
								)
								
								if (count == 0)
								{
									$("#" + target_id).append('<option>' + lang['no_record'] + '</option>');
								}
								break;
								
							// Grouped SELECT / OPTION list
							case "grouped":	
								$(data).find("group").each
								(
									function()
									{
										var optgroup = $('<optgroup></optgroup>');
										$(optgroup).attr('label',$(this).find("label:first").text());
										
										$(this).find("value").each
										(
											function ()
											{
												var option = $('<option></option>');
												$(option).attr("value",$(this).attr("id"));
												$(option).attr("text",$(this).text());
												$(optgroup).append($(option));
												count++;
											}
										);
										
										$(obj).append($(optgroup));
									}
								);
								
								if (count == 0)
								{
									$(obj).append('<option>' + lang['no_record'] + '</option>');
								}
								else
								{
									var option = $('<option></option>');
									$(option).attr("value",'');
									$(option).attr("text",'Please select...');
									$(option).insertBefore($(obj).find("optgroup:first"));
									$(obj).val('');
								}
								break;
									
							default:
								$("#" + target_id).html($(data).find("value").text());
								break;
						}
					}
				}
			)
		}
	)
}

jQuery.fn.menu_click = function ()
{
	$(this).click 
	(
		function()
		{
			if ($(this).parent().parent().get(0).tagName != 'LI')
			{
				// First tier
				if (!$(this).hasClass('active'))
				{
					$("ul", $(this).parent()).stop(true);
					$("ul", $(this).parent()).slideUp('fast');
					$("li", $(this).parent()).removeClass('active');
					$("ul", $(this)).stop(true);
					$("ul:first", $(this)).slideDown('fast');
					$(this).addClass('active');
				}
				else
				{
					$("ul", $(this)).slideUp('fast');
					$(this).removeClass('active');
				}
			}
			else if ($("ul:first", $(this)).length)
			{
				// Second tier
				$("ul", $(this).parent()).stop(true);
				$("ul", $(this).parent()).hide('fast');
				$("ul", $(this)).stop(true);
				if ($("ul:first", $(this)).is(":visible"))
				{
					$("ul:first", $(this)).hide('fast');
				}
				else
				{
					$("ul:first", $(this)).show('fast');
				}
				return false;
			}
			else if (($("a", $(this)).length))
			{
				window.location.href = $("a", $(this)).attr('href');
			}		
		}
	);
}

jQuery.fn.nav_click = function()
{
	$(this).click
	(
		function ()
		{
			$("#footer-display-block").toggle('fast','linear');
			$(this).blur();
			return false;
		}		
	);
}

jQuery.fn.no_hide = function()
{
	$(this).click
	(
		function()
		{
			return false;
		}
	);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
