var h;
var root	=	"/";

$(function() {
	
	/* Browse Home Table START */
	
	var table_row	=	{
		index	:	0,
		
		click:function(index) {
			if (index == null)
				index	=	table_row.index;
			
			h	=	$("div#container div[box] > div[content='table'] > table > tbody > tr").size();
			if (index >= h)
				index	-=	(h * Math.floor(h / index));
			
			$("div#container div[box] > div[content='table'] > table > tbody > tr").removeAttr("chosen");
			$("div#container div[box] > div[content='table'] > table > tbody > tr").eq(index).attr("chosen", "css");
			
			$("div#container div[box] > div[content='table'] > div[hidden]").attr("hidden", "css");
			$("div#container div[box] > div[content='table'] > div[hidden]").eq(index).attr("hidden", "false");
			
			table_row.index	=	index;
			play_bar.start();
		}
	}
	
	$("div#container div[box] > div[content='table'] > table > tbody > tr").bind("click", function() {
		table_row.click($(this).index());
	});
	
	/* Browse Home Table END */
	
	/* Play Bar START */
	
	var play_bar	=	{
		default_width	:	"0%",
		delay_seconds	:	60
	}
		
	play_bar.start	=	function() {
		$("div#container div[box] > div[content='table'] > div[play_bar] > div[bar] > div[ball]").css("width", play_bar.default_width)
			.stop(true, false)
			.animate({
				"width"	:	"100%"
			}, (play_bar.delay_seconds * 1000), "linear", function() {
				table_row.index++;
				setTimeout(table_row.click(), 0);
			}).delay();
	}
	
	table_row.click(0);
	
	$("div#container div[box] > div[content='table'] > div[play_bar]").bind("mouseover", function() {
		$("div#container div[box] > div[content='table'] > div[play_bar] > div[bar] > div[ball]").stop(true, false);
	});
	
	$("div#container div[box] > div[content='table'] > div[play_bar]").bind("mouseout", function() {
		table_row.click()
	});
	
	/* Play Bar Table END */
	
	/* Facebook Authentication START */
	
	$("button#fb_auth, a#facebook_login").bind("click", function(e) {
		FB.login(null, {perms:'email, user_birthday'});
		e.preventDefault();
	});
	
	/* Facebook Authentication END */
	
	/* Register & Login Focus START */
	
	var register	=	{
		focus:function() {
			$("form#register > button[type='submit']").focus();
			$("form#register > input").each(function() {
				if ($(this).val() == "") {
					$(this).focus();
					return false;
				}
			});
		}
	}
	
	if ($("form#register > div[error]")[0]) {
		$("form#register > *").each(function() {
			if ($(this).attr("error")) {
				$("form#register > *").eq(($(this).index()) - 1).focus();
				return false;
			}
		});
	}
	
	if ($("form#login > div[error]")[0]) {
		$("form#login > *").each(function() {
			if ($(this).attr("error")) {
				h	=	$("form#login > *").eq(($(this).index()) - 1)
				h.focus();
				$("form#login > label[for='"+h.attr("id")+"']").hide();
				return false;
			}
		});
	}
	
	/* Register & Login Focus END */
	
	/* Scroll To Href START */
	
	$("a[href]").bind("click", function(e) {
		if ($(this).attr("href") == "#start") {
			register.focus();
			$.scrollTo("#start", "fast");
			e.preventDefault();
		} else if ($(this).attr("href") == "#recruit_link") {
			$("#recruit_link").select();
			$.scrollTo("#recruit_link", "fast");
			e.preventDefault();
		}
	});
	
	/* Scroll To Href END */
	
	/* Text in Login Box START */
	
	$("div#container div[box] > div[content] > form#login > input").each(function() {
		if ($(this).val())
			$("div#container div[box] > div[content] > form#login > label[for='"+$(this).attr("id")+"']").hide();
	});
	
	$("div#container div[box] > div[content] > form#login > input").bind("focus, focusin", function() {
		$("div#container div[box] > div[content] > form#login > label[for='"+$(this).attr("id")+"']").hide();
	});
	
	$("div#container div[box] > div[content] > form#login > input").bind("blur, focusout", function() {
		if (!$(this).val())
			$("div#container div[box] > div[content] > form#login > label[for='"+$(this).attr("id")+"']").show();
	});
	
	/* Text in Login Box END */
	
	/* Logout START */
	
	$("button#logout").click(function() {
		h	=	$("form#logout");
		FB.getLoginStatus(function(response) {
			if(response.session)
				FB.logout(function() {
					h.submit();
				});
			else h.submit();
		});
		return false;
	}); 
	
	/* Logout END */
	
	/* Campaign Slide START */
	
	$("div.campaign[status='closed'] > a[ad], div.campaign[status='closed'] > div[read_more]").live("click", function(e) {
		h	=	$(this).parent();
		h.attr("status", "open");
		h.find("> a[ad] > div[hidden]").hide()
			.slideDown("slow");
		e.preventDefault();
	});
	
	$("div.campaign[status='open'] > div[read_more]").live("click", function(e) {
		h	=	$(this).parent();
		h.find("> a[ad] > div[hidden]").slideUp("slow", function() {
			h.attr("status", "closed");
		});
		e.preventDefault();
	});
	
	/* Campaign Slide END */
	
	/* Dialog Management START */
	
	$(window).bind("hashchange", function() {
		h	=	"dialog";
		if (location.hash.substr(2, h.length) == h) {
			$.ajax({
				type:"POST",
				url:root+"ajax/"+location.hash.substr(2),
				dataType:"json",
				success:function(data) {
					if (data.success == "true") {
						dialog.open(data.html);
					} else dialog.close();
				}
			});
		}
	});
		
	$(function() {
		$("div#dialog > div > a[close], div#overlay").bind("click", function() {
			location.hash	=	"close_dialog";
			dialog.close();
		});
	});
		
	/* Dialog Management END */
	
	/* Show Ads START */
//	
//	var i	=	1;
//	$("div.campaign").each(function() {
//		if (i > 5)
//			$(this).hide();
//		i++;
//	});
//	$(window).bind("hashchange", function() {
//		h	=	"show_all_ads";
//		if (location.hash.substr(1, h.length) == h) {
//			$("div.campaign").show();
//			$("a#all_ads").remove();
//		}
//	});
//	
	/* Show Ads END */
	
	/* Fire Hashes */
	$(window).trigger("hashchange");
	
});
