/* misc
 * ===================================================================== */
$.extend({
	min: function(a, b) { return a < b ? a : b; },
	max: function(a, b) { return a > b ? a : b; },
	isset: function(x) { return ((typeof x) != 'undefined'); },
	isempty: function(x) { return (!$.isset(x) || x == ''); },
	defval: function(x, val) { return ($.isset(x) ? x : val); },
	isie6: function() {
		return ($.browser.msie && $.browser.version == '6.0');
	},
	cookie: function(name, value, options) {
		// name and value given, set cookie
		if (typeof value != 'undefined') {
			options = options || {};
			if (value === null) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if (options.expires &&
				(typeof options.expires == 'number' ||
				 options.expires.toUTCString)) {
				var date;
				if (typeof options.expires == 'number') {
					date = new Date();
					date.setTime(date.getTime() +
							(options.expires * 24 * 60 * 60 * 1000));
				} else {
					date = options.expires;
				}
				// use expires attribute, max-age is not supported by IE
				expires = '; expires=' + date.toUTCString();
			}
			// CAUTION: Needed to parenthesize options.path and
			// options.domain in the following expressions,
			// otherwise they evaluate to undefined in the packed
			// version for some reason...
			var path = options.path ? '; path=' + (options.path) : '';
			var domain = options.domain ?
				'; domain=' + (options.domain) : '';
			var secure = options.secure ? '; secure' : '';
			document.cookie = [name, '=',
				encodeURIComponent(value), expires,
				path, domain, secure].join('');
		} else { // only name given, get cookie
			var cookieValue = null;
			if (document.cookie && document.cookie != '') {
				var cookies = document.cookie.split(';');
				for (var i = 0; i < cookies.length; i++) {
					var cookie = jQuery.trim(cookies[i]);
					// Does this cookie string begin with
					// the name we want?
					if (cookie.substring(0, name.length + 1) ==
							(name + '=')) {
						cookieValue = decodeURIComponent(
								cookie.substring(name.length + 1));
						break;
					}
				}
			}
			return cookieValue;
		}
	}
});

/* plugins
 * ===================================================================== */
$.fn.clickOutside = function(func) {
	if (!func) {
		this.trigger('clickOutsideCallback');
		return this;
	}

	var target = this;

	target.bind('clickOutsideCallback', func);

	$('html').click(function(e) {
		if (!target.is(':visible'))
			return true;
		if (e.pageX == 0 && e.pageY == 0) {
			/* fix for webkit */
			return true;
		}
		if (e.pageX >= target.offset().left &&
			e.pageX <= target.offset().left +
				target.outerWidth() &&
			e.pageY >= target.offset().top &&
			e.pageY <= target.offset().top +
				target.outerHeight())
			return true;
		target.trigger('clickOutsideCallback', e);
		return true;
	});

	return this;
};

/* menus
 * ===================================================================== */
$.fn.menus = function() {
	var me = $(this).append($('<span></span>')
		.css('display', 'block')
		.css('width', $(this).width() + 'px')
		.css('height', $(this).height() + 'px')
		.css('opacity', '0')
		.css('background-image', $(this).css('background-image'))
		.css('background-position',
			'0 -' + ($(this).height() + 1) + 'px')
	).hover(function() {
		if ($(this).hasClass('active'))
			return;

		$('.dhnav').find('> li > a')
		.not(this).not('.hover').children('span').animate({
			opacity: '0'
		}, {
			duration: 320,
			queue: false
		});

		$(this).children('span').animate({
			opacity: '1'
		}, {
			duration: 320,
			queue: false
		});
	}, function() {
		if ($(this).hasClass('hover'))
			return;
		$(this).children('span').animate({
			opacity: '0'
		}, {
			duration: 320,
			queue: false
		});
	});

	return this;
};

$.firstLast = function() {
	$('ul').each(function() {
		$(this).children('li:first').addClass('first');
		$(this).children('li:last').addClass('last');
	});
	$('table').each(function() {
		$(this).children('tr:first').addClass('first');
		$(this).children('tr:last').addClass('last');
		$(this).find('> tbody > tr:first').addClass('first');
		$(this).find('> tbody > tr:last').addClass('last');
		$(this).find('> thead > tr:first').addClass('first');
		$(this).find('> thead > tr:last').addClass('last');
	});
	$('tr').each(function() {
		$(this).children('th:first, td:first').addClass('first');
		$(this).children('th:last, td:last').addClass('last');
	});

	$('#path > a:last').addClass('last');
};

$(function() {
	$('#nav.accordion').each(function() {
		var me = $(this);
		var ul = me.children('ul');

		ul.children('li').children('ul').hide()
			.end().css('height', 'auto');

		ul.children('li').children('a').click(function() {
			var li = $(this).parent();
			li.children('ul').slideDown();
			ul.children('li').not(li).children('ul').slideUp();
			return false;
		});

		ul.children('li').children('a.active')
			.parent().children('ul').show();
	});

	$('#page-header .path > a:last').addClass('active');
});

/* common
 * ===================================================================== */
$(function() {
	/* focus-blur
	 * ----------------------------------------------------------------- */
	$('a').focus(function() {
		$(this).blur();
	});

	/* first, last
	 * ----------------------------------------------------------------- */
	$.firstLast();

	/* dhnav */
	$('#nav > ul').dhnav({
		fade: true,
		centered: true
	});

	/* menus */
	$('a.menus').each(function() {
		$(this).menus();
		if (!$(this).hasClass('active')) {
			$(this).parent().find('ul.sub').hide();
		}
	});
});

