jQuery(
	function()
	{
		// clear/reset searchbox when focus/blur
		$('#search-input').bind("blur", function() {
			if ($.trim($(this).val())) return;
			$(this).val('search');
		});

		// auto round corner
		$('.roundcorner').each(function() {
			var classes = this.className.split(/\s+/);
			for (i in classes) {
				var c = classes[i];
				if (c.substr(0,3) == 'rc:') {
					var opts = c.substr(3).replace('-',' ');
					$(this).corner(opts);
				}
			}
		});

		// auto nav links
		$('a.back').click(function(e) {
			if (this.className.match('no-go')) return true;
			window.back();
			return $u.cancel(e);
		});
		$('a.forward').click(function(e) {
			if (this.className.match('no-go')) return true;
			window.forward();
			return $u.cancel(e);
		});

		// auto select-urls
		$('select.select-url').change(function() {
			var url = this.value;
			if (url.match(/^https?:\/\//)) window.location = url;
		});

		var id = $('body').attr('id');
		if (id) {
			var pages = ['www','software','design','hardware','printing','networking','security'];
			for (i in pages) {
				var regex = new RegExp('^page-'+pages[i]+'-');
				if (id.match(regex)) {
					$('#nav>li>a[@title="Our Services"]')
						.parent('li')
						.addClass('current_page_parent');
				}
			}
		}

		$('.toggle-target').hide();
		$('.toggle-set').each(function() {
			var options = $u.parse_options(this.className, {'default':'close'});
			var method  = (options['default']=='close') ? 'hide' : 'show';
			$(this).find('.toggle-target')[method]();
		});
		$('.toggle-link').click(function(e) {
			if (this.href.match('#')) {
				var id = this.href.split('#',2)[1];
				$('#'+id).toggle();
			}
			return $u.cancel(e);
		});
		$('.toggle-showall').click(function(e) {
			$('.toggle-target').show();
			return $u.cancel(e);
		});
		$('.toggle-hideall').click(function(e) {
			$('.toggle-target').hide();
			return $u.cancel(e);
		});
	}
);

// Object: $u
// Utilities namespace
var $u = {
	// Method: cancel
	// Cancels an event, returns false
	cancel : function(e)
	{
		e.stopPropagation();
		e.preventDefault();
		return false;
	},
	// Method: mouse
	// Get mouse position
	mouse : function(e)
	{
		var origin = [0,0];
		if( !e ) {
			if( window.event ) {
				//Internet Explorer
				e = window.event;
			} else {
				//total failure, we have no way of referencing the event
				return origin;
			}
		}
		if( typeof( e.pageX ) == 'number' ) {
			//most browsers
			var xcoord = e.pageX;
			var ycoord = e.pageY;
		} else if( typeof( e.clientX ) == 'number' ) {
			//Internet Explorer and older browsers
			//other browsers provide this, but follow the pageX/Y branch
			var xcoord = e.clientX;
			var ycoord = e.clientY;
			var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
				( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
				( navigator.vendor == 'KDE' );
			if( !badOldBrowser ) {
				if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
					//IE 4, 5 & 6 (in non-standards compliant mode)
					xcoord += document.body.scrollLeft;
					ycoord += document.body.scrollTop;
				} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
					//IE 6 (in standards compliant mode)
					xcoord += document.documentElement.scrollLeft;
					ycoord += document.documentElement.scrollTop;
				}
			}
		} else {
			//total failure, we have no way of obtaining the mouse coordinates
			return origin;
		}
		return [xcoord, ycoord];
	},
	// Method: parse_options
	// Parses classname options
	parse_options : function(str, defaults, filters)
	{
		defaults = (typeof defaults != 'object') ? {} : defaults;

		opts = str.split(/\s+/);
		options = jQuery.extend({}, defaults);
		for (i in opts) {
			opt = opts[i].split(':', 2);
			key = opt[0];
			val = opt[1] ? opt[1] : false;
			if (!val) continue;

			if (filters) {
				ok = false;
				for (j in filters) {
					if (j == key) { ok=true; break; }
				}
				if (!ok) continue;

				filter = filters[key];
				if (typeof filter == 'function') {
					filter = filter(key, val);
				}
				key = filter[0];
				val = filter[1] ? filter[1] : val;
			}

			options[key] = val;
		}
		return options;
	}
}
