var headline_count;
var current_headline = 0;
var old_headline = 0;
var headline_interval;
var headline_height;


$(function() {
	
	// Install custom scroll panes
	//$('.scroll-pane').jScrollPane();
	
	// Select jumplists
	$('select.jumplist').change(function() {
		window.location = $(this).val();
	});
	
	applyBubbleCode();
	
	// Support for form_submit a-links
	$("form a.form_submit").click(function() {
		$(this).parents().filter("form").trigger("submit");//get(0).submit();//trigger("submit");
		return false;
	});
	
	// Code for subscriptions
	$("a.button-subscribe,a.button-subscribed").click(function() {
		var pagelink = $(this).attr("value");
		var descr = $(this).attr("descr");
		var subscr_page;
		
		if ($(this).hasClass("button-subscribe"))
		{
			$(this).addClass("button-subscribed");
			$(this).removeClass("button-subscribe");
			
			subscr_page = "/members/subscription-set.php?link="+escape(pagelink)+"&descr="+escape(descr)+"&subscribe=1";
			$(this).attr("title", "You are subscribed to "+descr+", click to remove subscription");
		}
		else
		{
			$(this).addClass("button-subscribe");
			$(this).removeClass("button-subscribed");
			
			subscr_page = "/members/subscription-set.php?link="+escape(pagelink)+"&subscribe=0";
			$(this).attr("title", "Subscribe to "+descr);
		}
		$.get(subscr_page);
		
		return false;
	});
	
	// Setup confirmation dialog
	$("#confirm_dialog").dialog({
		autoOpen: false,
		resizable: false,
		modal: true,
		overlay: {backgroundColor: "#ffffff", opacity: 0.5}
	});
	
	$(".confirm").live("click", function() {
		$("#confirm_dialog_msg").html($(this).attr("confirm"));
		$("#confirm_dialog").data("confirm_link", $(this).attr("href"));
		$("#confirm_dialog").dialog("open");
		return false;
	});
	
	$("#confirm_dialog_cancel").live("click", function() {
		$("#confirm_dialog").dialog("close");
		return false;
	});
	
	$("#confirm_dialog_ok").live("click", function() {
		$("#confirm_dialog").dialog("close");
		document.location = $("#confirm_dialog").data("confirm_link");
		return false;
	});
	// Load quick-links
	loadQuicklinks();
	
	// Handle location chooser
	$("#sel_country").change(function() {
		$("#sel_region").val("");
		$("#sel_region").attr("disabled", true);
		$("#sel_city").val("");
		$("#sel_city").attr("disabled", true);
		
		$("#span_region").load("/util/loc-region.php?country="+$("#sel_country").val(), function() {
			$("#sel_region").change(function() {
				$("#sel_city").val("");
				$("#sel_city").attr("disabled", true);
				$("#span_city").load("/util/loc-city.php?country="+$("#sel_country").val()+"&region="+escape($("#sel_region").val()));
			});
		});
	});
	
	// Polls
	$(".poll_opt").click(function() {
		var poll_id = $(this).attr("poll_id");
		var opt = $(this).val();
		$("#poll_"+poll_id).load("/util/pollvote.php?pid="+poll_id+"&opt="+opt);
	});
	
	/*
	$("#sel_region").live("change", function() {
		alert("sel_region");
		$("#sel_city").val("");
		$("#sel_city").attr("disabled", true);
		
		$("#span_city").load("/util/loc-city.php?country="+$("#sel_country").val()+"&region="+escape($("#sel_region").val()));
	});
	*/
	
	var country = $("#sel_country").val();
	var region = "";
	var city = "";
	if (country != "")
	{
		region = $("#sel_region").attr("default");
		$("#span_region").load("/util/loc-region.php?country="+country+"&region="+escape(region));
		if (region)
		{
			city = $("#sel_city").attr("default");
			$("#span_city").load("/util/loc-city.php?country="+country+"&region="+escape(region)+"&city="+escape(city));
		}
	}
	
	// Achievements
	$("div.achievement").hover(function() {
		$(this).css("z-index", 50);
		var id = $(this).attr("achievement_id");
		$("#"+id).show();
	}, function() {
		$(this).css("z-index", 0);
		var id = $(this).attr("achievement_id");
		$("#"+id).hide();
	});
	
	
	// Tourneys
	$("div.tourney").hover(function() {
		$(this).css("z-index", 50);
		var id = $(this).attr("tourney_id");
		$("#"+id).show();
	}, function() {
		$(this).css("z-index", 0);
		var id = $(this).attr("tourney_id");
		$("#"+id).hide();
	});
	
	// Code for hiding items when clicking outside of them
	$(".outclick-remove").click(function(evt) {
		evt.stopPropagation();
	});
	$("body").click(function() {
		$(".outclick-remove").hide();
		$(".outclick-remove").parent().css("z-index", 0);
	});
	$("a.bubble-close").click(function() {
		$(".outclick-remove").hide();
		$(".outclick-remove").parent().css("z-index", 0);
	});
	
	// Autoselectable text fields
	$(".autoselect").click(function () {
		$(this).select();
		return false;
	});
	
	// Go to selected value
	$("select.change-go").change(function() {
		var sel = $(this).val();
		document.location = sel;
	});
	
	// Links for slide downs
	$(".showlink").click(function() {
		var show = $(this).attr("showid");
		$("#"+show).slideToggle("normal");
		return false;
	});
	
	// Converting links to posts
	$(".postlink").click(function() {
		var thelink = $(this).attr("href");
		$("#link-post").attr("action", thelink);
		$("#link-post").submit();
		return false;
	});
	
	// Replace YouTube images with actual videos
	enableYoutubeClips();
	
	// Function for updating zollar counter
	setInterval("zollarTick()", 1000);
});

function enableYoutubeClips()
{
	$('.youtubevideo').each(function() {
		var videoID = $(this).attr("src").replace("http:\/\/i1.ytimg.com\/vi\/", "").replace("\/hqdefault.jpg", "");
		$(this).replaceWith('<object width="320" height="265"><param name="movie" value="http://www.youtube.com/v/'+videoID+'&hl=en_EN&fs=1&rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+videoID+'&hl=en_EN&fs=1&rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="320" height="265" wmode="transparent"></embed></object>');
	});
}

function loadQuicklinks()
{
	$("#quicklinks").load("/members/quicklinks.php", function() {
		applyBubbleCode();
		setupNewsTicker();
	});
	setTimeout("loadQuicklinks()", 2*60*1000);
}

function applyBubbleCode()
{
	$(".popupbubble").hover(function() {
		// Enter
		$(this).css("z-index", 50);
		var id = $(this).attr("pop_id");
		$("#"+id).show();
	}, function() {
		// Leave
		$(this).css("z-index", 0);
		var id = $(this).attr("pop_id");
		$("#"+id).hide();
	});
}

function setupNewsTicker()
{
	$("#newsticker_close").click(function(){
		var notif_id = $("#newsticker").attr("ticker_id");
		$.post("/util/ignore-newsticker.php?id="+notif_id);
		$("#newsticker_bg").hide();
		return false;
	});
	
	clearInterval(headline_interval);
	
	headline_count = $(".newsticker_headline").size();
	headline_height = parseFloat($("#newsticker").css("height"));
	
	$(".newsticker_headline:eq(0)").css("top", "0px");
	if (headline_count <= 1) return;
	
	headline_interval = setInterval(newsTickerRotate, 4000);
	
	$("#newsticker").hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(newsTickerRotate, 4000);
		newsTickerRotate();
	});
}

function newsTickerRotate()
{
	current_headline = (old_headline + 1) % headline_count;
	$(".newsticker_headline:eq("+old_headline+")").animate({top:-headline_height}, "normal", function(){
		$(this).css("top", headline_height+"px");
	});
	$(".newsticker_headline:eq("+current_headline+")").animate({top:0}, "normal");
	old_headline = current_headline;
}

function selectFrontTab(tab)
{
	// Deselect all 4 tabs
	for (var i = 1; i <= 4; i++)
	{
		$("#front-tab-"+i).removeClass("front-tab-"+i+"-sel");
		$("#front-featured-"+i).hide();
	}
	
	$("#front-tab-"+tab).addClass("front-tab-"+tab+"-sel");
	$("#front-featured-"+tab).show();
}

function zollarTick()
{
	// Update the zollars ticker
	var totalSecs = $("#zollar-secs").attr("curtime");
	totalSecs-=1;
	if (totalSecs <= 0)
	{
		totalSecs = $("#zollar-secs").attr("interval");
		// Add a zollar cent
		var zollars = parseFloat( $("#zdisplay-bubble").html() );
		zollars += 0.01;
		
		$("#zdisplay-bubble").html(zollars.toFixed(2));
		$("#zdisplay-menu").html(Math.floor(zollars));
	}
	$("#zollar-secs").attr("curtime", totalSecs);
	
	
	var secs = totalSecs%60;
	var mins = Math.floor(totalSecs/60);
	$("#zollar-secs").html(""+mins+":"+lz(secs));
}

function lz(s) {
	var t = String(s);
	return t.length==1 ? "0"+t : t;
}

function quicklinksRemoveChallenges()
{
	$("#quicklink_invite_icon").removeClass("quick-invite-new").addClass("quick-invite");
	$("#quicklink_invite_num").html("0");
	$("#quicklink_challenge_list").slideUp("normal");
	$("#quicklink_challenge_empty").slideDown("normal");
	$.post("/util/clear-challenges.php");
}