
// javascript auto loader.
function javascriptInclude(scriptFilename) {

	// get the html document
	var htmlDoc = document.getElementsByTagName('head').item(0);

	// create a stylesheet link
	var cssInclude = document.createElement('link')

	cssInclude.setAttribute('type', 'text/css');
	cssInclude.setAttribute('href', '/css/' + scriptFilename + '.css');
	cssInclude.setAttribute('rel', 'stylesheet');
	cssInclude.setAttribute('media', 'screen');

	// add the element to the html document.
	htmlDoc.appendChild(cssInclude);

	// create a new script element.
	var javascriptInclude = document.createElement('script');

	// tag on the required elements.
	javascriptInclude.setAttribute('language', 'javascript');
	javascriptInclude.setAttribute('type', 'text/javascript');
	javascriptInclude.setAttribute('src', '/javascript/' + scriptFilename + '.js');

	// add the element to the html document.
	htmlDoc.appendChild(javascriptInclude);

	// drop through.
	return false;
}

// array of elements already loaded.
var loadedElements = [];


// basic set up.
$(document).ready(function() {

	// check auto loaders
	if ( $('[special*=confirmAction]') ) {
		if ( $('[special*=confirmAction]').size() > 0 ) {
			if ( !loadedElements['confirmAction'] ) {
				javascriptInclude('utils/jQueryPlugin/jconfirmaction');
			}
		}
	}

	if ( $('[special*=datePicker]') ) {
		if ( $('[special*=datePicker]').size() > 0 ) {
			if ( !loadedElements['datePicker'] ) {
				javascriptInclude('utils/datePicker/datePicker');
			}
		}
	}

	if ( $('[special*=colorPicker]') ) {
		if ( $('[special*=colorPicker]').size() > 0 ) {
			if ( !loadedElements['colorPicker'] ) {
				javascriptInclude('utils/colorPicker/colorpicker');
			}
		}
	}

	if ( $('[special*=lightbox]') ) {
		if ( $('[special*=lightbox]').size() > 0 ) {
			if ( !loadedElements['lightbox'] ) {
				javascriptInclude('utils/fancybox/jquery.fancybox');
			}
		}
	}

	if ( $('[special*=inlineEdit]') ) {
		if ( $('[special*=inlineEdit]').size() > 0 ) {
			if ( !loadedElements['inlineEdit'] ) {
				javascriptInclude('utils/jQueryPlugin/inlineedit');
			}
		}
	}

  	if ( $('[special*=richTextEditor]').size() > 0 ) {
  		if ( !loadedElements['richTextEditor'] ) {
  			javascriptInclude('open_wysiwyg/scripts/wysiwyg');
  		}
  	}

	if ( $('[special*=tooltip]') ) {
		if ( $('[special*=tooltip]').size() > 0 ) {
			if ( !loadedElements['tooltip'] ) {
				javascriptInclude('utils/tooltip/tipTip');
			}
		}
	}

	if ( $('[special*=jumpOut]') ) {
		if ( $('[special*=jumpOut]').size() > 0 ) {
			if ( !loadedElements['jumpout'] ) {
				javascriptInclude('utils/jQueryPlugin/jumpOut');
			}
		}
	}

	if ( $('[special*=textLabel]') ) {
		if ( $('[special*=textLabel]').size() > 0 ) {
			if ( !loadedElements['textLabel'] ) {
				javascriptInclude('utils/jQueryPlugin/textLabel');
			}
		}
	}

	if ( $('[special*=bannerRotator]') ) {
		if ( $('[special*=bannerRotator]').size() > 0 ) {
			if ( !loadedElements['bannerRotator'] ) {
				javascriptInclude('utils/jQueryPlugin/banner-rotator');
			}
		}
	}

	if ( $('[special*=rating]') ) {
		if ( $('[special*=rating]').size() > 0 ) {
			if ( !loadedElements['rating'] ) {
				javascriptInclude('utils/rating/jquery.ui.stars');
			}
		}
	}

	// attach a target to all the external links.
	if ( $('a[rel*=external]') ) {
		$('a[rel*=external]').click( function() {
			window.open(this.href);
			return false;
		});
	}

	// attach to links that are used to open and close divisions.
	if ( $('a[jsToggle*=true]') ) {
		$('a[jsToggle*=true]').click( function(event) {
	
			event.preventdefault;

			var linkId = $(this).attr('id');
			var div = linkId.substr(0, linkId.length-4);
		
			if ( $('#' + div).is(':visible') ) {
				$('#' + div).hide();
			} else {
				$('#' + div).show();
			}
	
			// stop the event from carrying on.
			return false;
	
		});
	}

	// hide any elements that are supposed to not be there.
	if ( $('[jsHidden*=true]') ) {
		$('[jsHidden*=true]').each( function(index, value) {
			$(value).hide();
		});
 	}


	// input handlers.

	// attach to any buttons that act as links.
	if ( $('input[jsButtonLink]') ) {
		$('input[jsButtonLink]').each( function(index, value) {
			$(value).click(function () {
				window.location = $(this).attr('jsButtonLink');
			});
		});
	}
    // attach to any checkboxes that toggle a hidden layer.
	if ( $('input[jsCheckboxToggle]') ) {
	    $('input[jsCheckboxToggle]').each(function(index, value){
			$(value).click(function (e) {
				toggleCheckboxElement(this, $(this).attr('name'));
			});
			// also fire the event now.
			toggleCheckboxElement($(value), $(value).attr('name'));
	    });
	}

	// attach to any tab navigation.
    if ( $('[special*=tabNavigation]') ){
	    $('[special*=tabNavigation]').each(function(index, value){
			$(value).find('a').each(function (index, value) {
				// we attach to the click event for each a element.
				$(value).click(function(event){
					// we need to find all other a elements and hide the divisions that they relate to.
					$(this).closest('ul').find('a').each(function(index, value) {
						var corename = $(value).attr('id').substr(0, $(value).attr('id').length-3);
						// now hide each division in turn
						$('#' + corename + 'Content').hide();
						// remove the highlight class
						$(value).removeClass('active');
					});

					var corename = $(this).attr('id').substr(0, $(this).attr('id').length-3);
					// now show the one that we want
					$('#' + corename + 'Content').show();

					// add the highlight class
					$(this).addClass('active');

					// stop the event from carrying on.
					event.preventDefault();
				});
			});
	    });
	}

    if ( $('[special*=autoComplete]') ){
	    $('[special*=autoComplete]').each(function(index, value){
	
	    	// add a division to use as the lookup options.
			var newDivision = document.createElement('div');
			$(newDivision).addClass('autocomplete');
			$(newDivision).css('display', 'none');
			$(newDivision).css('width', $(value).css('width'));
			$(newDivision).attr('id', $(value).attr('id') + 'AutocompleteChoices');
	
			// add the division to the element
			$(value).after($(newDivision));
	
			var ajaxUrl = $(value).attr('autoCompleteUrl');
	
			$(value).autocomplete(ajaxUrl, {
				minChars: 2,
				max: 20,
				mustMatch: false,
				dataType: "json",
				parse: function(data) {
					return parseAutoCompleteJson(data);
				},
				formatItem: function(item) {
					return formatAutoCompleteItem(item);
				}
			}).result(function(event, item) { processAutoCompleteReturn(item); });
	
	    });
	}

});

function toggleCheckboxElement(checkbox, element) {
	if ( $(checkbox).is(':checked') ){
		$('#' + element).show();
	} else {
		$('#' + element).hide();
	}
}

function editToggle(name) {

	var editDiv = $('#' + name + '_edit');
	var viewDiv = $('#' + name + '_view');
	var toggle = $('#' + name + '_toggle');

	if ( $(editDiv).is(':visible') ) {
		toggle.html('(Edit)');
		$(editDiv).hide();
		$(viewDiv).show();
	} else {
		toggle.html('(Finished editing)');
		$(editDiv).show();
		$(viewDiv).hide();
	}
	return false;
}

function toggleElement(element) {
	if ( $(element).is(':visible') ) {
		$(element).hide();
	} else {
		$(element).show();
	}
}

function showElement(element) {
	if ( $(element).is(':visible') ) {
		// do nothing
	} else {
		$(element).show();
	}
}

function hideElement(element) {
	if ( $(element).is(':visible') ) {
		$(element).hide();
	} else {
		// do nothing
	}
}

function initTree() {

	$('.node_toggle').each( function(index, value) {
    	$(value).click(function(event) {
			var thisDiv = $(this).closest('div.node').find('div.node_contents');
			if ( $(thisDiv).is(':visible') ) {
				$(this).attr('src', '/images/icons/node-closed.gif');
				$(thisDiv).hide();
			} else {
				$(this).attr('src', '/images/icons/node-open.gif');
				$(thisDiv).show();
			}
		});
	});
}

// function to trim text to a required length.
function cropText(textInput, trimTo) {

	var returnValue = textInput;

	if ( textInput.length <= trimTo ) {
		return returnValue;
	}

	returnValue = textInput.substr(0, (trimTo-3));
	returnValue += '...';

	return returnValue;

}

(function($) {

	if(typeof $.fn.actualWidth === "undefined") {

	    var defaults = {
		};

	    $.fn.actualWidth = function(options) {

		    // build main options before element iteration
		    var runOptions = $.extend(defaults, options);

			var actualWidth = $(this).width();

			// work out the various elements that can move the center.
			var leftPadding = $(this).css('padding-left');
			var rightPadding = $(this).css('padding-right');

			// add on any padding.
			if ( leftPadding != '' && leftPadding != 'undefined' ) {
				if ( leftPadding.substr(leftPadding.length-2, 2) == 'px' ) {
					actualWidth += parseFloat(leftPadding.substr(0, leftPadding.length-2));
				} else if ( leftPadding.substr(leftPadding.length-1, 1) == '%' ) {
					actualWidth += parseFloat(leftPadding.substr(0, leftPadding.length-1));
				}
			}

			if ( rightPadding != '' && rightPadding != 'undefined' ) {
				if ( rightPadding.substr(rightPadding.length-2, 2) == 'px' ) {
					actualWidth += parseFloat(rightPadding.substr(0, rightPadding.length-2));
				} else if ( rightPadding.substr(rightPadding.length-1, 1) == '%' ) {
					actualWidth += parseFloat(rightPadding.substr(0, rightPadding.length-1));
				}
			}

			return actualWidth;

		};
	};

	if(typeof $.fn.actualHeight === "undefined") {

	    var defaults = {
		};

	    $.fn.actualHeight = function(options) {

		    // build main options before element iteration
		    var runOptions = $.extend(defaults, options);

			var actualHeight = $(this).height();

			// work out the various elements that can move the center.
			var topPadding = $(this).css('padding-top');
			var bottomPadding = $(this).css('padding-bottom');

			// add on any padding.
			if ( topPadding != '' && topPadding != 'undefined' ) {
				if ( topPadding.substr(topPadding.length-2, 2) == 'px' ) {
					actualHeight += parseFloat(topPadding.substr(0, topPadding.length-2));
				} else if ( topPadding.substr(topPadding.length-1, 1) == '%' ) {
					actualHeight += parseFloat(topPadding.substr(0, topPadding.length-1));
				}
			}

			if ( bottomPadding != '' && bottomPadding != 'undefined' ) {
				if ( bottomPadding.substr(bottomPadding.length-2, 2) == 'px' ) {
					actualHeight += parseFloat(bottomPadding.substr(0, bottomPadding.length-2));
				} else if ( bottomPadding.substr(bottomPadding.length-1, 1) == '%' ) {
					actualHeight += parseFloat(bottomPadding.substr(0, bottomPadding.length-1));
				}
			}

			return actualHeight;

		};
	};


})(jQuery);




