// JavaScript Document

$(document).ready(function(){
	$('#body .tab .navigation a').focus(function(event){
		event.preventDefault();
		tabNavigate($(this),true);
	});
	$('#body .tab .navigation a').click(function(event){
		event.preventDefault();
		tabNavigate($(this));
	});
	$('#home_featured .tab .navigation *').css('cursor','default');
	$('#home_featured .tab .navigation a').click(function(event){
		event.preventDefault();
	});
	$('#box_dinner div.previous a').click(function(event){
		event.preventDefault();
		$onlink = $('.tab .navigation .selected',$(this).parents('.tab-box'));
		if ($onlink.parent().index()==0) {
			tabNavigate($('.tab .navigation li:last a',$(this).parents('.tab-box')));
			return;
		}
		tabNavigate($('a',$onlink.parent().prev()));
	});
	$('#box_dinner div.next a').click(function(event){
		event.preventDefault();
		$onlink = $('.tab .navigation .selected',$($(this).parents('.tab-box')));
		var totalLinks = 0;
		$('.tab .navigation li',$(this).parents('.tab-box')).each(function(i){
			totalLinks ++;
		});
		if ($onlink.parent().index()==totalLinks-1) {
			tabNavigate($('.tab .navigation li:first a',$(this).parents('.tab-box')));
			return;
		}
		tabNavigate($('a',$onlink.parent().next()));
	});

	/* Psuedo Selection Box Code */
	$('.ui-select').data('state','closed');
	$('.ui-select').hover(
		function () {
			if ($(this).data('state')!="closed") {
				return;	
			} else {
				$(this).data('state','opening');
			}
			$('ul',this).animate({
				'height':'show'
			},'fast',function() {
					$(this).parents('.ui-select').data('state','open');
				}
			);
		},
		function () {
			$(this).data('state','closing');
			$('ul',this).animate({
				'height':'hide'
			},'fast',function() {
					$(this).parents('.ui-select').data('state','closed');
				}
			);
		}
	);
	$('.tab .navigation a.selected').each(function(i){
		tabNavigate($(this),true);
	});
	$('body.dinnerseries #box_social .navigation a.selected').removeClass();
	$('body.dinnerseries #box_social .navigation .flickr a').addClass('selected');
	$('body.dinnerseries #box_social .navigation a.selected').each(function(i){
		tabNavigate($(this),true);
	});

	// Home photo rotator	
	$('#home_photo a:first').show();
	if ($('#home_photo').length > 0) {
		setInterval("homePhotoRotate($('#home_photo a:visible'))",8000)
	}
});

function homePhotoRotate($this) {
	$this.css('z-index','19');
	$old = $this;
	$next = ($this.next().length > 0) ? $this.next() : $('#home_photo a:first');
	tabNavigate($('#home_featured .navigation a').eq($next.index()),'fade');
	$next.css('z-index','20').fadeIn('slow',function(){
		$old.hide();
	});
}

function tabNavigate($this,fade) {
	if ($this.is("#body *")) {
		var tabpos = $this.parents('.tab').position();
		var mypos = $this.position();
		var tabwidth = Math.round($this.width() /2);
		if ( tabwidth<13) {
			tabwidth=3;	
		}
		$this.parents('.tab').css('background-position', (-448 + tabwidth + mypos.left - tabpos.left) + 'px bottom');
	}
	var target = $this.attr('href');
	$container = $this.parents('.tab-box');
	$('.story', $container).hide();
	if (fade!='fade') {
		$(target).show();
	} else {
		$(target).fadeIn('slow');
	}
	$('.tab .navigation a', $container).removeClass('selected');
	$this.addClass('selected');
}
