$(document).ready(function()
{
	
	// -------------------------------------------------------------------------
	/*
		JSONSuccess()
		
		Checks a JSON response for success status.
		
		Should be used whenever expecting a JSON response to determine
		how to handle that response.
		
		Returns true if response is successful, false if otherwise.
	*/
	$.fn.JSONSuccess = function(json)
	{
		if (json.success == 0)
		{
			if (json.error)
			{
				msg = json.error;
			}
			return false;
		}
		
		return true;
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.parseXMLResponse = function(xml)
	{
		var xmlJSON = {
			status: "0",
			status_msg: "",
			successHTML: ""
		}
		
		var resultNode = $(xml).find('result');
		var statusNode = $(resultNode).find('status');
		
		xmlJSON.status = statusNode.attr('type');
		xmlJSON.statusMsg = statusNode.text();
		xmlJSON.successHTML = $(xml).find('successHTML').text();
		xmlJSON.moreHTML = $(xml).find('moreHTML').text();
		xmlJSON.html1 = $(xml).find('html1').text();
		xmlJSON.html2 = $(xml).find('html2').text();
		xmlJSON.html3 = $(xml).find('html3').text();
		xmlJSON.html4 = $(xml).find('html4').text();
		xmlJSON.html5 = $(xml).find('html5').text();
		
		return xmlJSON;
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.browserSupportsCanvas = function()
	{
		var canvas = document.createElement('canvas'), context;
		
		if (!canvas.getContext)
		{
			return false;
		}
		
		return true;
	}
	
	// -------------------------------------------------------------------------
	
	$.fn.testUrl = function() {
		var urlRegex = /usercp\/verify/;
		
		if (urlRegex.test(window.location))
		{
			window.location = '/';
		}
		else
		{
			window.location = '';
		}
	}
	
	// -------------------------------------------------------------------------
	
	/*
		checkboxToStr()
		
		Gets a string of all checked values in a checkbox selection.
	 
	*/
	$.fn.checkboxToStr = function(checkbox_id)
	{
		var str = '';
		
		$('#'+ checkbox_id + ':checked').each(function()
		{
			str += $(this).val() + ',';
		});
		
		str = str.slice(0, -1);
		
		return str;
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.arrayToPostdata = function(array)
	{
		var postdata = '';
		
		var count = array.length;
		
		for (var i = 0; i < count; i++)
		{
			if (array[i] != undefined)
			{
				postdata += array[i] + ',';
			}
		}
		
		return postdata.slice(0, -1);
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.alertArray = function(array)
	{
		var count 	= array.length;
		var str 	= '';
		
		for (var i = 0; i < count; i++)
		{
			str += array[i] + ', ';
		}
		
		alert(str);
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.assignPlaceholders = function()
	{
		// Handle placeholders for all browsers
		$('[placeholder]').each(function()
		{	
			var placeholder = $(this).attr('placeholder');
			
			// If element has user input, don't overwrite with placeholder
			
			if ($(this).val() == '' || $(this).val() == placeholder)
			{
				$(this).val(placeholder);
				$(this).addClass('placeholder');
			}

			
			// If click, blank the input
			$(this).click(function()
			{
				if ($(this).val() == placeholder)
				{
					$(this).val('');
					$(this).removeClass('placeholder');
				}
			});
			
			$(this).focus(function(){
				if ($(this).val() == placeholder)
				{
					$(this).val('');
					$(this).removeClass('placeholder');
				}
			});
			
			$(this).blur(function()
			{
				if ($(this).val() == '')
				{
					$(this).val(placeholder);
					$(this).addClass('placeholder');
				}
			});
			
		});
	};
	
	$(this).assignPlaceholders();
	
	// -------------------------------------------------------------------------
	
	$.fn.textfieldVal = function(useAlert)
	{
		if (useAlert)
		{
			alert($(this).attr('id') + " " + $(this).val() + " " + $(this).attr('placeholder'));
		}
		
		if ($(this).val() == $(this).attr('placeholder'))
		{
			return '';
		}
		
		return $(this).val();
	};

	// -------------------------------------------------------------------------

	/*
	 *
	 */
	$.fn.showDropDown = function(targetId, xOffset, yOffset)
	{
		var height 	= $(targetId).outerHeight();
		
		$(this).matchWidth(targetId);
		$(this).positionAbsoluteOfElement(targetId, 0, height, 'l', true);
		$(this).show();
		
		var dropdown = $(this);
		
		$(targetId).focusout(function(){
			dropdown.hide();
		});
	};
	
	// -------------------------------------------------------------------------

	/*
	 *
	 */
	$.fn.matchWidth = function(targetId)
	{
		var width = $(targetId).outerWidth();

		$(this).css({
			width: width + 'px'
		});
		
		return width;
	};
	
	// -------------------------------------------------------------------------

	/*
	 *
	 */
	$.fn.showBubble = function(msg, status, xOffset, yOffset)
	{
		if (status == null) {status = 1;}
		
		if (xOffset == null) {xOffset = 5;}
		if (yOffset == null) {yOffset = 0;}
		
		var elemId 		= '#' + $(this).attr('id');
		var bubbleId 	= elemId + '_error';
		
		if ($(bubbleId).hasClass('form_success_bubble'))
		{
			$(bubbleId).removeClass('form_success_bubble');
		}
		
		if (status == 1)
		{
			$(bubbleId).addClass('form_success_bubble');
		}
		
		$(bubbleId).show();
		
		$(bubbleId).html(msg);
		$(bubbleId).positionAbsoluteOfElement($(this), xOffset, yOffset, 'r', true);
		
		$(bubbleId).fadeIn(200);
	};
	
	$.fn.hideBubble = function()
	{
		bubbleId = '#' + $(this).attr('id') + '_error';
		
		$(bubbleId).fadeOut(500);
	};

	// -------------------------------------------------------------------------

	/*
	 *
	 */
	$.fn.positionAbsoluteOfElement = function(targetId, xOffset, yOffset, edge, usePos)
	{
		
		if (edge == 'r')
		{
			xOffset += $(targetId).width();
		}
		else if (edge == 'b')
		{
			yOffset += $(targetId).height();
		}
		
		var position = $(targetId).offset();
		
		if (usePos != null)
		{
			position = $(targetId).position();
		}

		xOffset += position.left;
		yOffset += position.top;

		$(this).css({
			position:'absolute',
			top: yOffset + 'px',
			left: xOffset + 'px'
		});
	};

	// -------------------------------------------------------------------------

	/*
	 *
	 */
	$.fn.positionFixedOfElement = function(targetId, xOffset, yOffset)
	{
		var position = $(targetId).position();

		xOffset += position.left;
		yOffset += position.top;

		$(this).css({
			position:'fixed',
			top: yOffset + 'px',
			left: xOffset + 'px'
		});
	};
	
	// -------------------------------------------------------------------------

	$.fn.showInlineBlock = function()
	{
		
		$(this).show();
		$(this).css({display: 'inline-block'});
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.showWidth = function()
	{
		
		$(this).show();
		$(this).css({width: '330px'});
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.hoverHelp = function(msg)
	{
		var elementId = $(this).attr('id');
		
		$('#help_hover_wrapper').html(msg);
		$('#help_hover_wrapper').positionAbsoluteOfElement('#'+ elementId, 16, 0);
		$('#help_hover_wrapper').fadeIn(200);
	};
	
	$.fn.hideHoverHelp = function()
	{
		$('#help_hover_wrapper').fadeOut(500);
	};
	
	$.fn.bindHoverHelp = function(msg)
	{
		$(this).mouseover(function(){
			$(this).hoverHelp(msg);
		});
		
		$(this).mouseout(function(){
			$(this).hideHoverHelp();
		});
	};
	
	// -------------------------------------------------------------------------
	
	/*
	 *
	 */
	$.fn.debug = function(str)
	{
		$('#debug').html(str);
		$('#debug').show();
	};
	
	// -------------------------------------------------------------------------
	
	$.fn.addDebug = function(str)
	{
		var cur = $('#debug').html();
		$('#debug').html(cur + "<br />" + str);
		$('#debug').show();
	};
	
	
		
});
