function pageLoad($) {
	$('.decorate ul li, .decorate td, .decorate tr, hr').decorate();
	
	$(".cycle.hide").removeClass("hide");
	$(".cycle").cycle();
	
	// changes the search label on click
	(function searchlabel() {
		var searchb = $('#CAT_Search');
		var defaultText = "search";
		searchb.onblur = function () { 
			if (this.value == '') { this.value = defaultText; }
		}
		searchb.onfocus = function () {
			if (this.value == defaultText) { this.value = ''; }
		}
		if (searchb.value == "") {
			searchb.value = defaultText;
		}
	}) ();
	
	sidebarLoader($);
	truncateBlogPost($);
	
	if (window.pageClass) {
		document.body.className += " " + window.pageClass;
	}
}

function truncateBlogPost($) {
	$("#inspire .truncate").each(function () {
		hel = $("#inspire .blog-post");
		el = $(this);
		if (hel.height() > 249) {
			var words = el.text().split(" ");
    		while (hel.height() > 249) {
				words.pop();
    			this.innerHTML = words.join(" ") + "...";
    		}
		}
	});
}

function imageResizer() {
	var ns = $("textarea[class*=imgresize]");
	ns.each(function () {
		var nshtml = this.value
		var attrs = this.className.replace(/.*imgresize\[([^\]]*)\].*/g, "$1").replace(/ /g,"&amp;");
		var imghtml = nshtml.replace(/(.*)<img.*src=\"([^\"]+)\"[^\/]*\/>(.*)/, '$1<img src="/Utilities/ShowThumbnail.aspx?USM=1'+attrs+'&amp;R=1&amp;Img=$2" />$3');
		this.parentNode.innerHTML = imghtml;	  
	});
	
	var ns = $("img[class*=imgresize]");
	ns.each(function () {
		var attrs = "&amp;W="+this.width+"&amp;H="+this.height;
		this.src = '/Utilities/ShowThumbnail.aspx?USM=1'+attrs+'&amp;R=1&amp;Img=$2'
	});
}


/**
 * e360 Sidebar Module Loader
 * Developed by Mark Haussmann
 * (c) 2010 Etraction Ltd
 * 
 * Using a specialized xml layout for the sidebar web app, this
 * loader will parse the parameters given and insert the modules
 * where specified.
 *
 * Usage:
 * in the URL field box, insert the urls you would like the module to display
 * with a single url on each line. Special Characters: * wildcard and a ~ (to exclude)
 * 
 * Acceptable urls:
 *   /*                   - matches all urls
 *   /AboutUs/*           - matches all subpages of the AboutUs folder
 *   ~/ContactUs.htm	  - excludes the contact us page (over rules all other filters)
 *   ~/AboutUs/*		  - excludes all subpages of the AboutUs folder
 */
function sidebarLoader($) {
	// function to test if a given url part (incl. wildcards) matches the current url
	var matchesCurrent = function (urls) {
		// get current url from pathname
		var currentURL = (window.location.pathname == "/") ? "/Home.htm" : window.location.pathname;
		var found = false;
		for(i in urls) {
			if (urls[i].replace(/\s*/,"") == "") continue;
			// if contains a ~ remove (used later), swap wildcards for regex equiv: .*
			// trim the string of extra white space
			var regex = new RegExp(".*"+urls[i].replace("~","").replace(/\*/g,".*").replace(/^\s+|\s+$/g,""), "im");
			// test against current url (case insensitive)
			if (currentURL.search(regex) != -1){
				// if first character is a 'sans' - exclude this url
				if ((urls[i].replace(/^\s+|\s+$/g,"").substr(0,1) == "~")) return false;
				found = true;
			}
		}
		return found;
	}
	
	// onclick video modal pop up (loads fancybox too)
	var linkVideo = function (id, title, url, content) {
		var width = 480;
		var height= 295;
		if (document.moduleVideoLoaded != true) {
			jQuery(document).ready(function(){
				jQuery("a.videolink").fancybox({
					frameWidth: 480,
					frameHeight: 295,
					overlayShow: true,
					overlayOpacity: 0.8,
					padding: 0,
					margin: 0,
					hideOnContentClick: false
				});
			});
			document.moduleVideoLoaded = true;
		}
		var videoPlayer = '<div id="'+ id +'" class="pop-up-video"><object width="'+ width +'" height="'+ height +'"><param name="movie" value="'+ url +'" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed src="'+ url +'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+ width +'" height="'+ height +'"></embed></object></div>';
		content = "<a href='#"+ id +"' class='videolink' title='"+ title +"'>" + content + "</a>" + videoPlayer;
		return content;
	}
	
	// onclick video modal pop up (loads fancybox too)
	var linkImage = function (id, title, url, content) {
		if (document.moduleImageLoaded != true) {
			jQuery(document).ready(function(){
				jQuery("a.imagelink").fancybox({
					overlayShow: true,
					overlayOpacity: 0.8,
					padding: 0,
					margin: 0,
					hideOnContentClick: false
				});
			});
			document.moduleImageLoaded = true;
		}
		content = "<a href='"+ url +"' class='imagelink' title='"+ title +"'>" + content + "</a>";
		return content;
	}
	
	$("textarea.sidebar").each(function () {
		// the replacenode will have it's innerhtml replaced
		var replaceNode = this.parentNode;
		// the final html to be inserted
		var repHtml = "";
		// a new xml object with jQuery's help
		if ($.browser.msie) {
			//var blog_content = this.value.replace(/[\s\S]*<blog_content>([\s\S]*)<\/blog_content>[\s\S]*/mi, "$1");
			//var events_content = this.value.replace(/[\s\S]*<events_content>([\s\S]*)<\/events_content>[\s\S]*/mi, "$1");
			
			//this.value = this.value.replace(blog_content, "").replace(events_content, "").replace(/([\s\S]*)<content>[\s\S]*<\/content>([\s\S]*)/mi,"$1 $2");
			
			var xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.loadXML(this.value);
			var xmlDoc = $(xml);
		} else {
			var xmlDoc = $(this.value);
			
			//var blog_content = $.browser.msie ? xmlDoc.find("blog_content").text() : xmlDoc.find("blog_content").html();
			//var events_content = $.browser.msie ? xmlDoc.find("events_content").text() : xmlDoc.find("events_content").html();
		}
		
		
		// modules are the xml elements containing both the urls and the content to be inserted
		xmlDoc.find("module").each(function () {
			var module = $(this);
			// split urls by new line
			var urls = module.find("urls").text().replace("\r","\n").split("\n");
			// if one of the filter urls matches the current url, get the module's content
			if ($.browser.msie) {
				//alert(""+module.find("imgbx").text());
			}
			if (matchesCurrent(urls)) {
				var id = module.attr("id");
				var title = module.attr("title");
				var type = module.find("type").text();
				var vidUrl = $.browser.msie ? module.find("video").text() : module.find("video").html();
				var imgUrl = $.browser.msie ? module.find("imgbx").text() : module.find("imgbx").html();
				if (type == "None") {
					if (vidUrl != "") {
						repHtml += linkVideo(id, title, vidUrl, $.browser.msie ? module.find("content")[0].xml : module.find("content").html());
					} else if (imgUrl != "") {
						repHtml += linkImage(id, title, imgUrl, $.browser.msie ? module.find("content")[0].xml : module.find("content").html());
					} else {
						repHtml += $.browser.msie ? module.find("content")[0].xml : module.find("content").html();
					}
				} else if (type == "Blog") {
					repHtml += "<div class='blog-module'>"+blog_content+"</div>";
				} else if (type == "Events") {
					repHtml += "<div class='events-module'>"+events_content+"</div>";
				} else {
					repHtml += $.browser.msie ? module.find("content")[0].xml : module.find("content").html();
				}
			}
		});
		replaceNode.innerHTML = repHtml;
	});
	
}













