
;(function($) {

var ver = '0.03';

function debug(s) {
	if ($.fn.decorate.debug)
		log(s);
}
function log() {
	if (window.console && window.console.log)
		window.console.log('[decorate] ' + Array.prototype.join.call(arguments,' '));
};

$.fn.decorate = function() {
	var o = { s: this.selector, c: this.context };

	// in 1.3+ we can fix mistakes with the ready state
	if (this.length === 0) {
		if (!$.isReady && o.s) {
			log('DOM not ready');
			return this;
		}
		// is your DOM ready?  http://docs.jquery.com/Tutorials:Introducing_$(document).ready()
		log('terminating; zero elements found by selector' + ($.isReady ? '' : ' (DOM not ready)'));
		return this;
	}
	
	// set up items for menu processing
	$.fn.decorate._cURL = $.fn.decorate.getCurrentURL();
	
	// iterate the matched nodeset
	return this.each(function() {
		ob = $(this);
		switch (ob.attr('tagName').toLowerCase()) {
			// doing a list
			case ('li'):
				$.fn.decorate.decorateListItem(ob);
				break;
			case ('hr'):
				ob.wrap("<div class='hr'></div>");
				break;
			default:
				break;
		}
		
	});
};

$.fn.decorate.decorateListItem = function (ob) {
	if (ob.is(':first-child')) ob.addClass('first');
	if (ob.is(':last-child')) ob.addClass('last');
	
	var selected 	= (ob.find("a[href*='"+$.fn.decorate._cURL+"']").length > 0);
	var parent 		= (ob.children('ul').length > 0);
	var textClass 	= ob.children('a').text().replace(/[ &]/g, "").toLowerCase();
	var fClass		= textClass;
	
	if (selected) 	fClass += ' selected '+textClass+'-selected';
	if (parent) 	fClass += ' parent';
	
	ob.addClass(fClass);
};

$.fn.decorate.getCurrentURL = function () {
	var _host = document.location.host;
	var _url = document.location.href.substring(document.location.href.indexOf(_host)+_host.length).toLowerCase();
	if (_url.indexOf('?') != -1) _url = _url.substring(0,_url.indexOf('?'));
	
	if (_url == "/") {
		_url += "Home.htm";
	}
	
	return _url;
};

$.fn.decorate.ver = function() { return ver; };

})(jQuery);


