(function($){ 
	$.fn.forumEditor = function(containerId, previewButtonId, defaultText, numMessages) {
		var dialogContent = {
			'image':'<p>Insert an Image below:</p><p><input type="text" id="imgSelect"></p>',
			'link':'<p>Insert a Link below:</p><p><input type="text" id="urlSelect"></p>',
			'size':'<p>Select Font Size:</p><p><select id="fontSizeSelect"><option value="-1">-1</option><option value="1">1</option><option value="2">2</option><option value="3">3</option></select></p>',
			'quote':'<p>Person\'s Name:</p><p><input type="text" id="quotePerson"></p><p>Quoted Text:</p><p><textarea id="quoteText"></textarea></p>'
		};
		
		if (!$("#forumEditorDialog").length)
			$('body').append('<div id="forumEditorDialog"></div>');
			
		$("#forumEditorDialog").dialog({
			autoOpen: false,
			bgiframe: true,
			modal: true,
			resizable: false,
			width: 320,
			zIndex: 3000
		});
		
	  $(this).each(function() {
	  	var textarea = document.getElementById($(this).attr('id'));
	  	
	  	if (numMessages == 0 && defaultText.length > 0 && $(this).val().length == 0) {
	  		$(this).val(defaultText);
	  		$(this).focus(function() {
					clearComment(textarea);
					return false;
				});
	  	}
	  		
			if ($('#'+containerId).length) {
				$('#'+containerId).before(toolbar(textarea));
			}
			
			if ($('#'+previewButtonId).length) {
				$('#'+previewButtonId).click(function() {
					preview(textarea);
				});
			}
		});
		
		function bold(textarea) {
			textarea.value += '[b][/b]';
		}
		
		function italic(textarea) {
			textarea.value += '[i][/i]';
		}
		
		function underline(textarea) {
			textarea.value += '[u][/u]';
		}
		
		function list(textarea) {
			textarea.value += '[list]\n[*][/*]\n[*][/*]\n[*][/*]\n[/list]';
		}
		
		function quote(textarea) {
			$("#forumEditorDialog").dialog('option', 'title', 'Insert a Quote');
			$("#forumEditorDialog").dialog('option', 'buttons', {
				'Cancel':	function() { 
					$(this).dialog("close");
				},
				'Insert':	function() {
					$(this).dialog("close");
					var quotePersonName = '';
					
					if ($("#quotePerson").val().length > 0)
						quotePersonName = '='+$("#quotePerson").val();
					
					textarea.value += '[quote' + quotePersonName + ']' + $("#quoteText").val() +'[/quote]';
				}
			});
			
			openDialog('quote');
		}
		
		function image(textarea) {
			$("#forumEditorDialog").dialog('option', 'title', 'Insert an Image');
			$("#forumEditorDialog").dialog('option', 'buttons', {
				'Cancel':	function() { 
					$(this).dialog("close");
				},
				'Insert':	function() {
					$(this).dialog("close");
					textarea.value += '[img]'+ $("#imgSelect").val() +'[/img]';
				}
			});
			
			openDialog('image');
		}
		
		function link(textarea) {
			$("#forumEditorDialog").dialog('option', 'title', 'Insert a Link');
			$("#forumEditorDialog").dialog('option', 'buttons', {
				'Cancel':	function() { 
					$(this).dialog("close");
				},
				'Insert':	function() {
					$(this).dialog("close");
					textarea.value += '[url]'+ $("#urlSelect").val() +'[/url]';
				}
			});
			
			openDialog('link');
		}
		
		function size(textarea) {
			$("#forumEditorDialog").dialog('option', 'title', 'Choose Font Size');
			$("#forumEditorDialog").dialog('option', 'buttons', {
				'Cancel':	function() { 
					$(this).dialog("close");
				},
				'Insert':	function() {
					$(this).dialog("close");
					textarea.value += '[size='+ $("#fontSizeSelect").val() +'][/size]';
				}
			});
			
			openDialog('size');
		}
		
		function openDialog(type) {
			$("#forumEditorDialog").html(dialogContent[type]);
			
			$("#forumEditorDialog").dialog('open');
	
			window.onscroll = function() {
		  	$("#forumEditorDialog").dialog('option', 'position', 'center');
		  };
		}
		
		function clearComment(textarea) {
			if (textarea.value == defaultText)
				textarea.value = "";
		}
		
		function toolbar(textarea) {
			var tb = $('<div class="ft_forumPostFormatting">\
					<a href="javascript:void(0);" id="bbBold" class="ft_icon16Bold" title="Add bold tags eg. [b]bold[/b]"><!-- BOLD --></a>\
					<a href="javascript:void(0);" id="bbItalic" class="ft_icon16Italic" title="Add italic tags eg. [i]italic[/i]"><!-- ITALIC --></a>\
					<a href="javascript:void(0);" id="bbUnderline" class="ft_icon16Underline" title="Add underline tags eg. [u]underline[/u]"><!-- UNDERLINE --></a>\
					<a href="javascript:void(0);" id="bbSize" class="ft_icon16FontSize" title="Add font size tags eg. [size=1]size[/size] (sizes= -1, 1, 2, & 3)"><!-- FONT SIZE --></a>\
					<a href="javascript:void(0);" id="bbLink" class="ft_icon16Url" title="Add url tags eg. [url]http://www.footytips.com.au[/url]"><!-- URL --></a>\
					<a href="javascript:void(0);" id="bbImage" class="ft_icon16Image" title="Add image tags eg. [img]http://www.footytips.com.au/images/favicon.ico[/img]"><!-- IMAGE --></a>\
					<a href="javascript:void(0);" id="bbQuote" class="ft_icon16Quote" title="Add quote tags eg. [quote=footytips.com.au]footytips[/quote] (quote=username to include a username)"><!-- QUOTE --></a></div>');
	
			$('#bbBold', tb).click(function() {
				bold(textarea);
				return false;
			});
			
			$('#bbItalic', tb).click(function() {
				italic(textarea);
				return false;
			});
			
			$('#bbUnderline', tb).click(function() {
				underline(textarea);
				return false;
			});
			
			$('#bbSize', tb).click(function() {
				size(textarea);
				return false;
			});
			
			$('#bbLink', tb).click(function() {
				link(textarea);
				return false;
			});
			
			$('#bbImage', tb).click(function() {
				image(textarea);
				return false;
			});
			
			$('#bbQuote', tb).click(function() {
				quote(textarea);
				return false;
			});
			
			return tb;
		}
		
		function preview(textarea) {
			var messageText = textarea.value.replace(/\n/g, '\r\n');
			var encMessage = escape(messageText);
			var postStr = '&message='+encMessage;
			
			if ($("#threadTitle").length)
				postStr += '&title='+escape($("#threadTitle").val());
				
			if ($("#editReason").length)
				postStr += '&editReason='+escape($("#editReason").val());
	
			if ($("#bbPostPreview").length)
				getcontent('POST','/cfm/ft/sub/retrieve.cfm','&fg=forums&ff=previewPost&loadType=ajax', postStr, 'bbPostPreview', '');
		}
	};
	
	$.fn.commentEditor = function(defaultText, numMessages) {
	  $(this).each(function() {
	  	var textarea = document.getElementById($(this).attr('id'));
	  	
	  	if (numMessages == 0 && defaultText.length > 0) {
	  		$(this).val(defaultText);
	  		$(this).focus(function() {
					clearComment(textarea);
					//$(".ft_profileRightWallCommentButton").show();
					return false;
				});
	  	}
	  	
	  	
	  	/*
			$(this).blur(function() {
				$(".ft_profileRightWallCommentButton").hide();
			})
			*/
		});
		
		function clearComment(textarea) {
			if (textarea.value == defaultText)
				textarea.value = "";
		}
	};
	
	$.fn.autofill = function(options){
		var defaults = {
			defaultText : 'Search...',
			inactiveClass : 'ft_compsQuickFindText',
			activeClass : 'ft_compsQuickFindTextActive',
			inactiveColour: '#808080',
			activeColour: '#000000',
			pref: 'class',
			showButton: false,
			buttonText: '',
			buttonClass: 'ft_compsQuickFindButtonSearch',
			label: ''
		};
		
		var options = $.extend(defaults, options);
		
		return this.each(function(){
			var hasContent = $(this).val() != '' && $(this).val() != options.defaultText;
			
			if (options.pref == 'class') {
				$(this).addClass(options.inactiveClass);
				if (hasContent)
					$(this).addClass(options.activeClass);
					
				if (options.showButton) {
					$(this).after('<input class="' + options.buttonClass + '" value="' + options.buttonText + '" type="submit">')
				}
			}
			else {
				if (hasContent)
					$(this).css({color: options.activeColour});
				else
					$(this).css({color: options.inactiveColour});
					
				if (options.showButton) {
					$(this).after('<input value="' + options.buttonText + '" type="submit">')
				}
			}
			
			if (options.label.length > 0) {
				$(this).before('<p>' + options.label + '</p>')
			}
			
			
			if (!hasContent)
				$(this).val(options.defaultText);
			
			$(this).focus(function(){
				if ($(this).val() == options.defaultText) {
					if (options.pref == 'class')
						$(this).addClass(options.activeClass);
					else
						$(this).css({color: options.activeColour});
						
					$(this).val('');
				}
			});
			
			$(this).blur(function() {
				if ($(this).val() == '') {
					if (options.pref == 'class')
						$(this).removeClass(options.activeClass);
					else
						$(this).css({color: options.inactiveColour});
					
					$(this).val(options.defaultText);
				}
			});
		});
	};
	
})(jQuery);

$.extend({
	Footytips: {
		initialised: false,
		userId: 0,
		displayName: '',
		afid: 0,
		menuLevel1: '',
		
		init: function(userId, displayName) {
			$.Footytips.userId = userId;
			$.Footytips.displayName = displayName;
			$.Footytips.initialised = true;
		},
		
		// Google Analytics Event Tracking
		gaPost: function(gaCategory, gaAction, gaLabel) {
			
			if ($.Footytips.afid == 1 && typeof(gaCategory) != 'undefined' && gaCategory.length > 0 && typeof(gaAction) != 'undefined' && gaAction.length > 0 && typeof(gaLabel) != 'undefined') {
				_gaq.push(['_trackEvent', gaCategory, gaAction, gaLabel]);
			}
		},
			
		Navigation: {
			show: function(elmnt) {
				if (typeof(elmnt) == 'object')
					$('div[id*=DropDown]' , elmnt).css('visibility', 'visible');
				else
					$('#' + elmnt + 'DropDown').css('visibility', 'visible');
			},
			
			hide: function(elmnt) {
				if (typeof(elmnt) == 'object')
					$('div[id*=DropDown]' , elmnt).css('visibility', 'hidden');
				else
					$('#' + elmnt + 'DropDown').css('visibility', 'hidden');
			},
			
			changeHeading: function(elmnt, heading) {
				if (typeof(elmnt) == 'object')
					$('div[id*=DropDownTitle]' , elmnt).html(heading);
				else
					$('#' + elmnt + 'DropDownTitle').html(heading);
				$.Footytips.Navigation.hide(elmnt);
			}
		},
		
		General: {
			focusLogin: function() {
				$("#userLogin").focus();
			},
			
			addDaysToDate: function(myDate, days) {
				var newDate = new Date(myDate.getTime() + days*24*60*60*1000);
				return newDate.getFullYear() + '-' + (newDate.getMonth()+1) + '-' + newDate.getDate() + ' ' + newDate.getHours() + ':' + newDate.getMinutes() + ':' + newDate.getSeconds();
			},
			
			fbShare: function(u, t) {
				var rand = parseInt(Math.random()*99999999);
				u += '&rand='+rand;
				window.open('http://www.facebook.com/sharer.php?&u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
				return false;
			},
			
			sortSports: function(sportView, width, height) {
			if (typeof(width) == 'undefined')
				var width = 715;
				
			if (typeof(height) == 'undefined')
				var height = 550;
				
				var isProfile = typeof(sportView) != 'undefined' && !isNaN(sportView);
				if (!isProfile)
					$.Footytips.Navigation.hide('ftNav1Tipping');
					
				$('#ft_teamSorter').remove();
				$('#ft_compSorter').remove();
				$('#ft_sportSorter').remove();

				if (!$('#ft_sportSorter').length)
					$('body').append('<div id="ft_sportSorter"></div>');
					
				$('#ft_sportSorter').dialog({
					autoOpen: false,
					bgiframe: true,
					position: 'center',
					modal: true,
					title: 'Re-Order Sports',
					resizeable: false,
					width: width,
					height: height,
					zIndex: 3000,
					buttons: {
						'Cancel': function() {
							$(this).dialog('close');
						},
						'Update Settings': function() {
							var mySportsList = '';
							var formStr = '';
							if (isProfile)
								formStr += '&sportView=' + sportView;
								
							if ($('#ft_sportSorter #displayOrder').length && $('#ft_sportSorter #displayOrder').val().length)
								formStr += '&displayOrder=' + $('#ft_sportSorter #displayOrder').val();
								
							$('#ft_sportSorter input[name=mySport]').each(function() {
								if ($(this).is(':checked')) {
									if (mySportsList.length > 0)
										mySportsList += ',';
										
									mySportsList += $(this).val();
								}
							});
							
							if (mySportsList.length > 0)
								formStr += '&mySport=' + mySportsList;
								
							if ($('#ft_sportSorter input[name=defaultView]:checked').length) {
								if ($('#ft_sportSorter input[name=defaultView]:checked').val() != 0)
									formStr += '&defaultView=' + $('#ft_sportSorter ul#itemSorter li.ui-state-default:first').attr('id');
								else
									formStr += '&defaultView=0';
							}

							$("#ft_sportSorter").dialog('close');

							getcontent('POST', '/cfm/ft/sub/retrieve.cfm', '&fg=settings&ff=account&content=sports&action=update', formStr, 'ft_sportSorter', isProfile ? '$.Footytips.Profiles.refreshMySportsNav' : '$.Footytips.General.refreshTippingNav');
						}
					}
				});
				
				getcontent('POST', '/cfm/ft/sub/retrieve.cfm', '&fg=settings&ff=account&content=sports', isProfile ? '&sportView=' + sportView : '', 'ft_sportSorter', '$.Footytips.General.initSorter');
				
				$('#ft_sportSorter').dialog('open');
			
				if ($.Footytips.menuLevel1.length > 0)
					$.Footytips.gaPost('sorting', 'sports', $.Footytips.menuLevel1);
					
				window.onscroll = function() {
					$('#ft_sportSorter').dialog('option', 'position', 'center');
				};
			},
			
			refreshTippingNav: function(httpObj) {
				if (typeof(httpObj.menu) != 'undefined') {
					if (typeof(httpObj.menu.tipping) != 'undefined') {
						if ($('#ftnav1noncell_Tipping').length)
							$('#ftnav1noncell_Tipping').html(unescape(httpObj.menu.tipping));
						else if ($('#ftnav1actcell_Tipping').length)
							$('#ftnav1actcell_Tipping').html(unescape(httpObj.menu.tipping));
					}
					
					if (typeof(httpObj.menu.tipGuide) != 'undefined') {	
						if ($('#ftnav1noncell_TipGuide').length)
							$('#ftnav1noncell_TipGuide').html(unescape(httpObj.menu.tipGuide));
						else if ($('#ftnav1actcell_TipGuide').length)
							$('#ftnav1actcell_TipGuide').html(unescape(httpObj.menu.tipGuide));
					}
				}
				
				$('#ft_sortConfirmMsg').show();
			},
			
			initSorter: function(httpObj) {
				httpObj.callback();
			},
			
			isEmail: function(email) {
				var pattern = /^([\w-'\*&]+(?:\.[\w-'\*&]+)*)@((?:[\w-']+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
				return pattern.test(email);
			}
		},
		
		AdServing: {
			viewTrailer: function(heading) {
				if (typeof(heading) == 'undefined')
					heading = 'View Trailer';
					
				if (!$("#adSpaceViewTrailer").length)
					$('body').append('<div id="adSpaceViewTrailer" style="padding: 2px 0px; text-align: center; width: 640px"><iframe id="adSpaceViewTrailerIframe" src="" scrolling="no" hspace="0" vspace="0" frameborder="0" marginheight="0" marginwidth="0" width="640" height="360" allowTransparency="true"></iframe></div>');
					
				$("#adSpaceViewTrailer").dialog({
					autoOpen: false,
					bgiframe: true,
					postition: 'center',
					modal: true,
					resizable: false,
					zIndex: 3000,
					title: heading,
					close: function(event, ui) {
						$(this).remove();
					}
				});
				
				if (navigator.userAgent.indexOf("MSIE")!= -1)
					$("#adSpaceViewTrailer").dialog('option', 'width', 665);
				else
					$("#adSpaceViewTrailer").dialog('option', 'width', 640);
					
				$("#adSpaceViewTrailer").dialog('open');
				
				$("#adSpaceViewTrailerIframe").attr('src', adhserver + allAdTags + '/AAMSZ=640X360/RESKIN=TRUE' + target + '?');
				
				window.onscroll = function() {
			  	$("#adSpaceViewTrailer").dialog('option', 'position', 'center');
			  };
			}
		},
		
		Mates: {
			addMate: function(userId) {
				if (!$("#addMateDialog").length)
					$('body').append('<div id="addMateDialog"></div>');
				
				$("#addMateDialog").dialog({
					bgiframe: true,
					modal: true,
					title: 'Add as Mate',
					resizable: false,
					width: 420,
					zIndex: 3000,
					buttons: {
						'Close': function() {
							$(this).dialog("close");
						}
					}
				});
				
				$("#addMateDialog").dialog('open');
				
				window.onscroll = function() {
			  	$("#addMateDialog").dialog('option', 'position', 'center');
			  };
			}
		}
	}
});
