$.extend({
	getUrlVars: function(str){
		var vars = [], hash;
		var hashes = str.slice(str.indexOf('?') + 1).split('&');
		for(var i = 0; i < hashes.length; i++) {
			hash = hashes[i].split('=');
			vars.push(hash[0]);
			vars[hash[0]] = hash[1];
		}
		return vars;
	},
		getUrlVar: function(str, name){
		return $.getUrlVars(str)[name];
	}
});

function initMenu() {
	$('#menu ul').hide();
	$('#menu li a').click(function() {
	  	var checkElement = $(this).next();
		if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
			return false;
		}
		if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
			$('#menu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
	});
	$('#menu ul li a').each(function() {
		if (($.getUrlVar(this.href, 'page') != null) && 
				($.getUrlVar(this.href, 'page') == $.getUrlVar(window.location.href, 'page'))) {
			$(this).addClass('active');
			var parent = $(this).parents("ul:first");
			parent.show();
			var checkElement = $(parent).prev();
			if (checkElement.is('a')) checkElement.addClass('active');
		}
	});
	$('#menu li a.menuitem').each(function() {
		if (($.getUrlVar(this.href, 'page') != null) &&
				($.getUrlVar(this.href, 'page') == $.getUrlVar(window.location.href, 'page'))) {
			$(this).addClass('active');
		}
	});
	$('#tabs li a').each(function() {
		if (($.getUrlVar(window.location.href, 'tab') == null) || ($.getUrlVar(window.location.href, 'tab') == 'false')) {
			if (
					(($.getUrlVar(this.href, 'tab') == null) && ($.getUrlVar(this.href, 'action') != null) && ($.getUrlVar(this.href, 'action') == $.getUrlVar(window.location.href, 'action'))) || 
					(($.getUrlVar(this.href, 'tab') == null) && ($.getUrlVar(this.href, 'page') != null) && ($.getUrlVar(this.href, 'page') == $.getUrlVar(window.location.href, 'page')) ||
					(($.getUrlVar(this.href, 'tab') == null) && ($.getUrlVar(window.location.href, 'page') == null) && ($.getUrlVar(window.location.href, 'action') == null)))
			   ) {
				$(this).parent().addClass('active');
			}
		} else {
			if ($.getUrlVar(window.location.href, 'tab') == $.getUrlVar(this.href, 'tab')) $(this).parent().addClass('active');
		}
	});
}
$(document).ready(function() {
	initMenu();
	$("a.box").colorbox();
});
