/* Light Bulb state changing */
function changeEnter(value)
{
	$("#light_bulb").css({"background-position":"0px "+(value*-95)+"px"});
	$("#btn_enter").hover(
		function(){
			$("#light_bulb").css("background-position",("-95px "+(value*-95)+"px"))
		},
		function(){
			$("#light_bulb").css("background-position",("0px "+(value*-95)+"px"))
		});
}

function resetJudges()
{
	$("#content #judges .judge").css({"background":"none"});
	$("#content #judges #desc").hide();
}

function tabClick(){
	
	var currentTab = $(this);
	var slider = $("#tab_slider");
	var entry = $("#tab_entry");
	var awards = $("#tab_awards");
	
	
	//see if current is open
	var isCurrent = currentTab.css("background-color") == "rgb(206, 206, 206)";
	
	//reset all tab colors
	$("#tab_header li").css({"background-color":"#e6e6e5"});
	
	//is first tab
	if( currentTab.index() == 0 )
	{
		//close open tab
		if(isCurrent){
			entry.hide();
			slider.hide();
			currentTab.css({"background-color":"#e6e6e5"});
		}
		//opne closed tab
		else
		{
			slider.css({"margin-left":"0px"});
			slider.show();
			entry.show();
			awards.hide();
			currentTab.css({"background-color":"#cecece"});
		}
		
	}
	//is second tab
	else
	{
		//close open tab
		if(isCurrent){
			awards.hide();
			slider.hide();
			currentTab.css({"background-color":"#e6e6e5"});
		}
		//open closed tab
		else
		{
			slider.css({"margin-left":"193px"});
			slider.show();
			entry.hide();
			awards.show();
			currentTab.css({"background-color":"#cecece"});
		}
		
	}
	
	return false;
	
}

function resetTab()
{
	$("#tab_header li").css({"background-color":"#e6e6e5"});
	
	$("#tab_slider").hide();
	$("#tab_entry").hide();
	$("#tab_awards").hide();
}

/* The click handlers */
function addClickHandlers()
{
	/*if($.browser.msie)
		$.fx.off = true;*/
	
	/* Nav click handler */
	$("#globalnav > li").find("a").click(function(){
		var tempString = (($(this).data("index") *-896) + 42)+"px";
		$("#globalheader").removeClass();
		var sClass = $(this).attr("href");
		sClass = sClass.substring(1,sClass.length);
		$("#globalheader").addClass(sClass);
		var navContent = $("#"+sClass, $("#content"));
		navContent.siblings().hide();
		//alert($("#"+sClass, $("#content")).siblings().length);
		navContent.show();
		
		$("#slides").animate({left:tempString});
		
		if(sClass == "info")
		{
			resetTab();
			$("#tab_menu").fadeIn('fast');
		}
		else
		{
			$("#tab_menu").fadeOut('fast');
		}
		if(sClass != "event")
		{
			$("#gallery_viewer").fadeOut('fast');
		}
		if(sClass != "judges")
		{
			resetJudges();
		}
		changeEnter($(this).data("index"));
		return false;
	});
	
	// Judge click handler
	$("#content #judges .judge").click(function(event)
	{
		var selected = $(this);
		selected.siblings().css({"background":"none"});
		selected.css({"background-color":"#fff"});
		var position = $(this).position();
		var desc = $("#content #judges #desc");
		desc.css("display", "block");
		desc.html(aJudgesDesc[selected.index()]);
		desc.css("top", position.top);
		desc.fadeIn('fast');
	}); 
	
	$(".logo").each(function(i, value){
		var logoItem = $(this);
		logoItem.css("background-image", "url("+$("img", logoItem).attr("src")+")");
		//var logoWidth = $("img", logoItem);
		//logoItem.css("width", logoWidth+"px");
		$("img", logoItem).attr("src", "").remove();
	});
	
	$("span", ".sponsors_item").css("opacity", "0.6");
	
	/* TAB MENU  */
	$( "#tab_header li" ).css({"cursor":"pointer"}).click( tabClick );
	
	
	/*  IMAGE GALLERY  */
	$("#gallery_viewer a").click(function(){
		$(this).parent().fadeOut("fast");
		$("img", viewer).attr("src", "");
		
		return false;
	});
	
	$("#event_gallery a").click(function(){
		
		var path = "assets/data/image" + ($(this).index() + 1) + ".jpg";
		var viewer = $("#gallery_viewer");
		$("img", viewer).attr("src", path);
		
		if( viewer.is(":hidden") )
		{
			viewer.fadeIn("fast");
		}
		
		return false;
	});
}

$(document).ready(function() {
	/* Setup the navigation */
	$("#slides > div").addClass("slide");
	$("#main", $("#content")).show();
	changeEnter(0);
	
	$("a", $("#globalnav")).each(function(i, value){
		$(this).data("index", i);
	});
	
	addClickHandlers();
	
	
});
