
;(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 };
	
	// 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 ('tr'):
				$.fn.decorate.decorateTableRow(ob);
				break;
			case ('td'):
				$.fn.decorate.decorateTableCell(ob);
				break;
			case ('li'):
				$.fn.decorate.decorateListItem(ob);
				break;
			case ('hr'):
				ob.wrap("<div class='hr'></div>");
				break;
			default:
				break;
		}
		
	});
};

$.fn.decorate.decorateTableRow = function (ob) {
	if (ob.is(':first-child')) ob.addClass('first-row');
	if (ob.is(':last-child')) ob.addClass('last-row');
};

$.fn.decorate.decorateTableCell = function (ob) {
	if (ob.is(':first-child')) ob.addClass('first');
	if (ob.is(':last-child')) ob.addClass('last');
};

$.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 selected	= ($('a',ob).filter(function() {return (this.href.toLowerCase().indexOf($.fn.decorate._cURL) != -1) }).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);
    
    ob[0].onmouseover = null;
    ob[0].onmouseout  = null;

    ob.hover(
        function () { ob.addClass('hover') },
        function () { ob.removeClass('hover') }
    );
};

$.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);



