/*<![CDATA[*/
              
// Document title
var title = document.title;

// Simple log
var log = function(msg) {
	if (!$('.log').length) {
		$('<div class="log"></div>').appendTo('body');
	}
	$('.log').append(msg.replace(/^([^:]*):(.*)$/, '<p><b>$1:</b> <span class="$1">$2</span></p>'))
		.attr({scrollTop: $("div").attr('scrollHeight')})
		.find('p:nth-child(even)').addClass('even');
}

// Serialization utility
var serialize = function(obj, re) {
	var result = [];
	$.each(obj, function(i, val) {
		if ((re && re.test(i)) || !re)
			result.push(i + ': ' + (typeof val == 'object' ? val.join 
				? '\'' + val.join(', ') + '\'' : serialize(val) : '\'' + val + '\''));
	});
	return '{' + result.join(', ') + '}';
}

// Init and change handlers
$.address.init(function(event) {
	log('init: ' + serialize({
		value: $.address.value(), 
		path: $.address.path(),
		pathNames: $.address.pathNames(),
		parameterNames: $.address.parameterNames(),
		queryString: $.address.queryString()
	}));
}).change(function(event) {
	log('change: ' + serialize(event, /parameters|parametersNames|path|pathNames|queryString|value/));
	var names = $.map(event.pathNames, function(n) {
		return n.substr(0, 1).toUpperCase() + n.substr(1);
	}).concat(event.parameters.id ? event.parameters.id.split('.') : []);
	var links = names.slice();
	var match = links.length ? links.shift() + ' ' + links.join('.') : 'Home';
	$('a').each(function() {
		$(this).toggleClass('selected', $(this).text() == match);
	});
	names.remove(0);
	$.address.title([title].concat(names).join(' | '));
});

function getPath(){
	fullpath=explode('/',$.address.value());
	return fullpath[1];
}

function getSecondPath(){
	fullpath=explode('/',$.address.value());
	return fullpath[2];
}

Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};

/*]]>*/

