﻿

/* This is the Global Application Namespace
===============================================*/
var YouFound = {};

YouFound.Controls = {};
YouFound.Forms = {};
YouFound.Forms.Admin = {};


//===============================================================================
// * Namespace:   YouFound.Global
// * Description: Provides general startup functionality required by each page.
//===============================================================================
YouFound.Global = (function () {

	return {
		/* Performs common initialisation required for each page
		==============================================================*/
		Initialise: function () {

			// Setup any TinyMCE editors
			$('textarea.tinymce').tinymce({
				// Location of TinyMCE script
				script_url: '/Content/Controls/tiny_mce/tiny_mce.js',

				// General options
				theme: "advanced",
				plugins: "pagebreak,style,layer,advhr,advimage,advlink,iespell,inlinepopups,preview,contextmenu,paste,directionalityoneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",

				// Theme options
				theme_advanced_buttons1: "bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,anchor,image",
				theme_advanced_buttons2: "",
				theme_advanced_toolbar_location: "top",
				theme_advanced_toolbar_align: "left",
				theme_advanced_statusbar_location: "bottom",
				theme_advanced_resizing: true,

				// Example content CSS (should be your site CSS)
				content_css: "/Content/Controls/tiny_mce/css/content.css",

				// Drop lists for link/image/media/template dialogs
				template_external_list_url: "/Content/Controls/tiny_mce/lists/template_list.js",
				external_link_list_url: "/Content/Controls/tiny_mce/lists/link_list.js",
				external_image_list_url: "/Content/Controls/tiny_mce/lists/image_list.js",
				media_external_list_url: "/Content/Controls/tiny_mce/lists/media_list.js"
			});

			// Setup text counting on any field requiring it
			YouFound.TextCounter.Initialise();
		}
	};
})();


//===============================================================================
// * Namespace:   YouFound.TextCounter
// * Description: Implements text counter functionality on all controls marked
// *              as requiring support.
//===============================================================================
YouFound.TextCounter = (function () {

	//
	// This is the general purpose changed event handler
	// for both INPUT and TEXTAREA elements. It is assumed
	// that the TEXTAREA elements will have a manually added
	// attribute for MaxLength (this is done on init)
	var changed = function (maxLength) {

		// Get the max length and the ui display field
		var maxLength = $(this).attr('maxLength');
		var span = $('span.countTextSpan', $(this).parent());
		var length = $(this).val().length;

		// Make sure there is a field to update
		if (span === undefined)
			return;

		// Make sure the maxlength is valid
		if (maxLength === undefined || maxLength === null) {
			span.html('[maxlength not defined]');
			return;
		}

		// Update the UI SPAN element		
		$(this).parent()
			   .find("span.countTextSpan")
			   .html(length + ' of ' + maxLength + ' characters used');
	};

	function limitTextArea(e) {
		var maxLength = $(this).attr('maxLength');
		var length = $(this).val().length;

		if (!e) var e = window.event
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		var character = String.fromCharCode(code);

		if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40 && code != 46) {
			if (length >= maxLength) {
				return false;
			}
		}

		return true;
	}

	function limitTextBox(e) {
		var maxLength = $(this).attr('maxLength');
		var length = $(this).val().length;

		if (!e) var e = window.event
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;

		var character = String.fromCharCode(code);

		if (!e.ctrlKey && code != 9 && code != 8 && code != 36 && code != 37 && code != 38 && (code != 39 || (code == 39 && character == "'")) && code != 40 && code != 46) {
			if (length >= maxLength) {
				return false;
			}

			var inclusions = new RegExp("[a-zA-Z0-9$ %&*+@#!]");

			if (inclusions.test(character) == false) {
				return false;
			}
		}

		return true;
	}

	return {

		//
		// Initialises the text counters on the page
		Initialise: function () {

			var areas = $('textarea.countText');
			var textboxes = $('input[type=text].countText');
			var spanHtml = '<span class="countTextSpan"></span>';

			// Setup all the INPUT elements on the form
			textboxes.keypress(limitTextBox);
			textboxes.keyup(changed);
			textboxes.after(spanHtml);
			textboxes.each(changed);

			// Setup all the TEXTAREA elements on the form
			// The length is given through the class definition
			// by specifying an extra class: len(x), where x
			// is the maxlength value.
			// The init code will parse this and store it in 
			// a manually added attribute.			
			areas.keypress(limitTextArea);
			areas.after(spanHtml);
			areas.each(function () {

				// Get the maxlength definition start
				var si = this.className.indexOf("len(");

				if (si == -1) {
					return;
				}

				si += 4;

				// Get the maxlength definition end
				var ei = this.className.indexOf(")");

				if (ei == -1) {
					return;
				}

				// Parse the maxlength
				var len = parseInt(this.className.substring(si, ei), 10);

				// Assign the element properties
				$(this).attr('maxLength', len)
				$(this)[0].onkeyup = changed;

				// Init the element by calling the event handler manually
				changed.call(this, len);
			});
		}
	};

})();
