
jQuery.noConflict();

/*
 *  error trapping code
 *  
 */
	/*
		onerror=handleErr
		
		function handleErr(msg,url,l)
		{
		//Handle the error here
		return true or false
		}
	*/
	
		
	/*
	 * Console logger
	 * 
	 * Message variables
	 *  %s	String
	 *  %d, %i	Integer (numeric formatting is not yet supported)
	 *  %f	Floating point number (numeric formatting is not yet supported)
	 *  %o	Object hyperlink
	 * 
	 *  only logs if globalVars.log = true
	 */
	
	jQuery.log = function() {
	  	if(window.console) {
			if(jQuery.browser.safari){
				// Safari console
				var args = arguments[0];
				window.console.log(args); // fix to show args, Safari doesn't like .apply
			}
			if (jQuery.browser.mozilla) {
				// Firefox with firebug
				window.console.log.apply(this, arguments);
			}
	  	} else {
	  		// no Firebug
	    	//alert(message);
		}
	};
	
	
// load jQuery when DOM is ready
jQuery(document).ready(function(){
	
	/*
	 *  De-obfuscate email addresses
	 * 
	 *  replaces email address @ in "mailto" links and link text
	 * 
	 */
		function deobfuscate(x){
			return x.replace("--/@/--","@");
		}
		jQuery("a").each(function (i){
			var h = this.href;
			if (h.indexOf("mailto")==0) {
				var t = deobfuscate(jQuery(this).html());
				this.href = deobfuscate(h);
				jQuery(this).html(t);
			}
		});
		
	/*
	 *  external links
	 *  tags with class="external" target="_blank"
	 *  optional Google Analytics tracking code trigger
	 */
		// (add https and don't include urls that start with site domain
		var pre = "/LINK/";				// prefix to add to URL for GA tracking code
		jQuery('a[@href^="http://"]')
			.addClass('external')			// add CSS class for styling
			.attr('target', '_blank')		// open in popup window
			.click(function(){				// GA tracking code
				var href = jQuery(this).attr("href");
				href = href.substr(7,9999);	// trim off http://
				var i = href.indexOf("www.");	// trim off www.
				if (i===0){
					href = href.substr(4,9999);
				}
				href = encodeURI(href);		// encode URL
				jQuery.log(pre + href);			// log href to console
				//pageTracker._trackPageview(pre + href)	// GA tracker code
				//return false;				// cancel link action
			})
		;

	/*
	 *  PNG fixer for IE6
	 *  requires jquery.pngfix.js to be loaded before this script
	 */
		// jQuery("img[@srcjQuery=png]").pngFix();

	/* navigation animation */

	// sidebar nav menu animation
	jQuery.fn.AccordianMenu = function(){
		return jQuery(this).each(function(i){
			var container = jQuery(this);
			jQuery.log("Setting up accordian on %o",container);
			// set options
			var nav_prefix_group = "";
			var nav_last_group = "";
			// setup click event if has children
			container.find("h2 a").each(function(){
				//jQuery.log(jQuery(this));
				var hasChildren = jQuery(this).parent().parent().find("ul");
				if (hasChildren.size()){
					jQuery(this).click(function(){
						var sel;
						// get group id
						var gid = jQuery(this).parent().parent().attr("id");
						gid = gid.replace(nav_prefix_group,"");
						//jQuery.log(jQuery(this));
						//jQuery.log("nav heading %o clicked",gid);
						// quit if group is active
						if (jQuery(this).attr("class") == "active"){
							//jQuery.log("group is already active");
							return false;
						}
						// quit if same group as opened
						if (gid == nav_last_group){
							jQuery.log("last group same as this group %o",nav_last_group);
							return false;
						}
						nav_select(gid,"slide");
						// cancel default action
						return false;
					});
				}
			});
			function nav_select(gid,action){
				// exit if empty
				if(typeof gid == "undefined" || gid == "") { return false; }
				// check if selected group exists
				sel = "#" + nav_prefix_group + gid;
				var grp = jQuery(container).find(sel);
				if (!grp.size()){
					//jQuery.log("id doesn't exist %o",sel);
					return false;
				} 
				// close open groups
				//sel = "#" + nav_prefix_group + nav_last_group;
				sel = jQuery("#nav_main li.active ul");
				if (nav_last_group != null) {
					switch (action){
						case "slide":
							sel.slideUp('fast');
							break;
						case "show":
							sel.hide();
							break;
					}
					sel.parent().removeClass("active").addClass("closed");
				}
				// open selected group
				sel = "#" + nav_prefix_group + gid;
				switch (action){
					case "slide":
						jQuery(container).find(sel + " ul").slideDown('fast');
						//jQuery(container).find(sel + " ul").show();
						break;
					case "show":
						jQuery(container).find(sel + " ul").show();
						break;
				}
				jQuery(container).find(sel).addClass("active").removeClass("closed");
				jQuery.log("showing group %o (%s)",gid,sel);
				// set last group
				nav_last_group = gid;
				//jQuery.log("last group = %o",nav_last_group);	
			}
			// open current group
			if (typeof nav_current_group != "undefined"){
				nav_select(nav_current_group,"show");
			} else {
				nav_current_group = null;
			}
			//jQuery.log("nav_current_group = %o",nav_current_group);
		});
	}
	// apply plugin to nav menus
	jQuery("#nav_main").AccordianMenu();
			
});
