$(document).ready(function(){

	$('a[href|=#]').click(function() {
		return false;
	});
	
	// console
	$("#console tr:nth-child(2n)").addClass("even");
	
	// quick access
	function expand(){ var target=$(this).attr('class'); $('#' + target).slideDown(150); }
	function collapse(){}
	$("#qa a").hoverIntent(expand,collapse);
	//$('#qa a').hover(expand,collapse);
	
	$('.box').hover( function () {}, function () {
		$(this).slideUp(200);
	});
	
	// home featured
	$('#home li a').hover( function () {
		$(this).find('img').animate({opacity:0},100);
	}, function () {
		$(this).find('img').animate({opacity:1},200);
	});
	
	// sampler
	$('#sampler .slides')
	.cycle({
		fx: 'fade',
		speed: 0,
		timeout: 0,
		pause: 1,
		pager: '#sampler .nav',
		pagerEvent: 'mouseover',
		pagerAnchorBuilder: function(idx, slide) {
			return '.nav li:eq(' + (idx) + ') a';
		}
	});

	$('#sampler li a').hover( function () {
		$(this).append("<div class='fade'></div>");
		$(this).find('.fade').css({opacity:0.25}).fadeOut(300);
	}, function () {});
	
	// features 
	$('#features .slides')
	.cycle({
		fx: 'fade',
		speed: 0, 
		timeout: 0,
		pause: 1,
		pager: '#features .nav',
		pagerEvent: 'mouseover',
		pagerAnchorBuilder: function(idx, slide) {
			return '.nav li li:eq(' + (idx) + ') a';
		}
	});
	
	// tooltips
	$('a.tooltip').hover( function () {
		$(this).children('span').show();
		$('a.tooltip').css({zIndex:1000});
		$(this).css({zIndex:1001});
	}, function () {
		$(this).children('span').hide();
	});
	
	$('a.tooltip').mousemove(function(e){
		if($(this).hasClass('viewer_tap')) {
			var offset_x = 0;
			var offset_y = 0;
		} else {
			var offset_x = 15;
			var offset_y = -5;
		}
		var pos = $(this).offset();
		var posx = pos.left;
		var posy = pos.top;
		var width = $(this).children('span').width();
		var x = e.pageX - posx + offset_x;
		var y = e.pageY - posy + offset_y;
		$(this).children('span').css({left: x + "px"});
		$(this).children('span').css({top: y + "px"});		
	});
	
	// align twitter box vertically
	var twitter_margin = ($('#twitter').height()-$('#twitter span').outerHeight())/2;
	$('#twitter span').css({marginTop: twitter_margin});

	// set padding to content (for footer)
	var footer_height = $('#footer').outerHeight();
	$('#content').css({paddingBottom: footer_height});
	
	// set padding to container (for console)
	var console_height = $('#console').outerHeight();
	$('#container').css({paddingTop: console_height});
	
	// set all columns in #data to equal height
	var data_height = $('#data').height();
	$('#data .column').css({height: data_height + "px"});
	
	// viewer
	//var fsh = $('#viewer ul li:first-child .image').height();
	//$('#viewer ul').css({height: fsh});
	
	$('#viewer ul')
	.cycle({
		fx: 'fade',
		speed: 200, 
		timeout: 0,
		prev: '#prev',
		next: '#next, #viewer .image',
		after: onAfter,
	});
	
	$(document.documentElement).keyup(function(event) {
		if (event.keyCode == 37) {
			$('#viewer ul').cycle('prev');
			var url = $('#prev').attr('href');
		} else if (event.keyCode == 39) {
			$('#viewer ul').cycle('next');
			var url = $('#next').attr('href');
		}
		if($('#viewer').length) { window.location = url; }
	});
	
	//$('#viewer_nav').delay(1000).fadeOut(300);
	/*$('#viewer').hover( function () {
		$('#viewer_nav').fadeIn(300);
	}, function () {
		$('#viewer_nav').fadeOut(300);
	});*/

});

$(window).load(function(){

	/*$('#home li').hide();
	$('#home li').each(function(index) {
		$(this)
			.delay(index * 50)
			.fadeIn(200);
	});*/
	$('#loader').hide();
	$('#list_large li').each(function(index, element) {
		$(element).find('a')
			.delay(index * 100)
			.animate({width:'toggle'},100);
			//.fadeIn(200);
	});
});

function onAfter(curr, next, opts) {
	var caption = (opts.currSlide + 1) + ' of ' + (opts.slideCount);
	$('#viewer_nav span').html(caption);
	var h = $(this).find('img').height();
	$('#viewer ul').animate({height: h},100);
}

function writeTo(u, d) {
	var l="\x6D\x61\x69";
	l+="\x6C\x74\x6F\x3A"+u;
	window.location=l+"\x40"+d;
}

/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);
