$(document).ready(function(){
	$('.callout ul li:first-child').addClass('first-child');
	$('.callout ul li:last-child').addClass('last-child');
	$(".search-form .txt").focus(function(){
		if(this.value == this.title) {
			this.value = "";
		}
	});
	$(".search-form .txt").blur(function(){
		if(this.value == "") {
			this.value = this.title;
		}
	});
	$('.tab-nav li a').click(function(e){e.preventDefault();});
	var tab = $('.tab-nav li.active a').hash;
	$('.tab-nav li a').hover(function(e){
		$('.callout > div').hide();
		$('.callout > div').filter(this.hash).show();
		$('.tab-nav li').removeClass('active');
		$(this).parent().addClass('active');
	});

	if($(".page-show-center a").length > 0) {
		$(".page-show-center a").overlay({
			target: '#gallery',
			left: '18px',
			expose: {
				opacity: 0.85,
				maskId: 'exposeMask'
			},
			closeOnClick: true
		}).gallery({speed: 800, opacity: 1});
	}
	
	if($(".runChart").length > 0) {
		$(".runChart").overlay({
			target: '#runs',
			left: '18px',
			expose: {
				opacity: 0.85,
				maskId: 'exposeMask'
			},
			closeOnClick: true,
			onBeforeLoad: function() {
				if($(".inside").length == 0) {
					this.getOverlay().append('<div class="inside"></div>');
					$(".inside").load(this.getTrigger().attr('href')+"/?chart=1");
				}
			}
		});
	}
	
	$('#form-message').fadeOut(5000);
	
	$('#exposeMask').click(function(e){});

	var post = $('.switcher li a.active').attr("href");
	$('.switcher li a').click(function(e){
		e.preventDefault();
		var newpost = $(this).attr("href");
		if (post != newpost) {
			$('.switcher li a').removeClass('active');
			$(this).addClass('active');
			$(newpost).css("left", "500px");
			$(post).animate({"left": "-500px"},1000).hide();
			$(newpost).show();
			$(newpost).animate({"left": "20px"},1);
			$(newpost+" p").css("marginLeft", "500px");
			$(newpost+" p").animate({"marginLeft": "0px"},800);
			$(newpost+" .more").css("marginLeft", "500px");
			$(newpost+" .more").animate({"marginLeft": "0px"},1000);
			post = newpost;
		}

	});
	$('#sociable-header').hide();
	$('#topBar ul li:first-child').click(
		function(e){
			e.preventDefault();
			if($('#sociable-header').is(':visible')) {
				$('#sociable-header').hide();
			} else {
				$('#sociable-header').show();
			}
		}
	);

	var cfg = ($.hoverintent = {
		sensitivity: 7,
		interval: 100
	});

	$.event.special.hoverintent = {
		setup: function() {
			$(this).bind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		teardown: function() {
			$(this).unbind("mouseover", jQuery.event.special.hoverintent.handler);
		},
		handler: function(event) {
			event.type = "hoverintent";
			var self = this,
				args = arguments,
				target = $(event.target),
				cX, cY, pX, pY;

			function track(event) {
				cX = event.pageX;
				cY = event.pageY;
			};
			pX = event.pageX;
			pY = event.pageY;
			function clear() {
				target.unbind("mousemove", track).unbind("mouseout", arguments.callee);
				clearTimeout(timeout);
			}
			function handler() {
				if ( ( Math.abs(pX-cX) + Math.abs(pY-cY) ) < cfg.sensitivity ) {
					clear();
					jQuery.event.handle.apply(self, args);
				} else {
					pX = cX; pY = cY;
					timeout = setTimeout(handler, cfg.interval);
				}
			}
			var timeout = setTimeout(handler, cfg.interval);
			target.mousemove(track).mouseout(clear);
			return true;
		}
	};


	$(function() {
		$("#sidbarNav").accordion({
			autoHeight: false,
			active: $("#sidbarNav>.current_page_item, #sidbarNav>.current_page_ancestor").index(),
			event: "hoverintent"
		});

	//TODO: How to open the appropriate menu...
		//ul#sidbarNav li.current-page-ancestor ul,
		//ul#sidbarNav li.current_page_item ul {display: block;}


	});


});
