$(document).ready(function(){
	
	// Top login fields
	
	$("#f_top_username").focus(function(){
		if( $(this).hasClass("empty") ){
			$(this).val("");
			$(this).removeClass("empty");
		}
	}).blur(function(){
		if( $(this).val() == "" ){
			$(this).val("email");
			$(this).addClass("empty");
		}
	}).blur();
	
	$("#f_top_password").focus(function(){
		if( $(this).hasClass("empty") ){
			$("label#t_top_pw_label").html("");
			$(this).removeClass("empty");
		}
	}).blur(function(){
		if( $(this).val() == "" ){
			$("label#t_top_pw_label").html("password");
			$(this).addClass("empty");
		}
	}).blur();
	
	// Form higlighting
	
	$("form.form input, form.form textarea, form.form select").not(".no_highlight").focus(function(){
		$(this).parents("div.field:first").addClass("field_selected");
	}).blur(function(){
		$(this).parents("div.field:first").removeClass("field_selected");
	});
	
	// Payment History
	
	$("a.payment_view").fancybox({
		frameWidth: 280,
		frameHeight: 400,
		zoomSpeedIn: 0,
		zoomSpeedOut: 0,
		hideOnContentClick: false
	});
	
	$("a.payment_print").fancybox({
		frameWidth: 280,
		frameHeight: 400,
		zoomSpeedIn: 0,
		zoomSpeedOut: 0,
		hideOnContentClick: false
	});
	
	// Admin menu
	
	$("ul#admin_menu > li").hover(function(){
		$(this).children("ul").show();
	},function(){
		$(this).children("ul").hide();
	});
	
	// Subscribe/Upgrade
	
	$("div#subs_upgr_link").hover(function(){
		$(this).children("div#subs_upgr_options").show();
	},function(){
		$(this).children("div#subs_upgr_options").hide();
	});
	
	// Various confirm boxes
	
	$("div#subs_upgr_options a, a.tournament-remove, a.user-remove").fancybox({
		frameHeight: 130,
		frameWidth: 400,
		zoomSpeedIn: 0,
		zoomSpeedOut: 0,
		hideOnContentClick: false
	});
	
	// Full screen results
	
	$("a#fullscreen").click(function(){
		createCookie( "fullscreen", 1, 365 );
		$("div#event_players").addClass("fullscreen");
		$("a#fullscreen").hide();
		$("a#unfullscreen").show();
		return false;
	});
	$("a#unfullscreen").click(function(){
		createCookie( "fullscreen", 0, 365 );
		$("div#event_players").removeClass("fullscreen");
		$("a#fullscreen").show();
		$("a#unfullscreen").hide();
		return false;
	});
	
	if( readCookie("fullscreen") == "1" ){
		$("div#event_players").addClass("fullscreen");
		$("a#fullscreen").hide();
		$("a#unfullscreen").show();
	}
	
	// Table column scroll
	
	var column_chunk = 0;
	var column_cols_per_chunk = 5;
	var columns = new Array();
	
	$("table:first th.col_normal").each(function(){
		columns[columns.length] = $(this).attr("class").split(" ",2)[0];
	});
	
	if( readCookie("column_chunk") ){
		column_chunk = readCookie("column_chunk");
		if( column_chunk * column_cols_per_chunk > columns.length ){
			column_chunk = 0;
		}
	}
	
	do_col_shift();
	
	$("a#table_cols_next").click(function(){
		$("a#table_cols_prev").removeClass("disabled");
		column_chunk = column_chunk + 1;
		do_col_shift();
		return false;
	});
	
	$("a#table_cols_prev").click(function(){
		$("a#table_cols_next").removeClass("disabled");
		column_chunk = column_chunk - 1;
		do_col_shift();
		return false;
	});
	
	function do_col_shift(){
		
		if( column_chunk <= 0 ){
			column_chunk = 0;
			$("a#table_cols_prev").addClass("disabled");
		}
		
		if( column_chunk >= 2 ){
			column_chunk = 2;
			$("a#table_cols_next").addClass("disabled");
		}
		
		createCookie( "column_chunk", column_chunk, 365 );
		
		var start_offset = column_chunk * column_cols_per_chunk;
		var end_offset = start_offset + column_cols_per_chunk;
		
		$(".col_normal").hide();
		
		for( i=start_offset; i<end_offset; i=i+1 ){
			$("."+columns[i]).show();
		}
		
	}
	
	// Flag
	
	$("a.flag").click(function(){
		
		var flag = 0;
		
		if( $(this).hasClass("flagged") ){
			$(this).removeClass("flagged").parents("tr:first").removeClass("flagged");
		}else{
			$(this).addClass("flagged").parents("tr:first").addClass("flagged");
			flag = 1;
		}
		
		var flag_vars = $(this).next("span").html().split(",", 3);
		
		$.ajax({
			url: 'members/event/flag/',
			data: {
				flag: flag,
				tournament_id: flag_vars[0],
				player_id: flag_vars[1],
				grouping: flag_vars[2]
			},
			type: "POST"
		});
		
		// Flag groups
		
		if( flag_vars[2] != 'outright' ){
			
			var curr_point = $(this).parents("tr:first");
			
			while( true ){
				if( curr_point.prev("tr").hasClass("emptyrowrow") || curr_point.prev("tr").hasClass("filler") ){
					break;
				}else{
					curr_point = curr_point.prev("tr");
					if( flag == 1 ){
						curr_point.addClass("flagged").find("a.flag").addClass("flagged");
					}else{
						curr_point.removeClass("flagged").find("a.flag").removeClass("flagged");
					}
				}
			}
			
			var curr_point = $(this).parents("tr:first");
			
			while( true ){
				if( curr_point.next("tr").hasClass("emptyrowrow") || curr_point.next("tr").hasClass("filler") ){
					break;
				}else{
					curr_point = curr_point.next("tr");
					if( flag == 1 ){
						curr_point.addClass("flagged").find("a.flag").addClass("flagged");
					}else{
						curr_point.removeClass("flagged").find("a.flag").removeClass("flagged");
					}
				}
			}
			
		}
		
		return;
	});
	
	// FB
	
	$("p.inline_photo a").fancybox({});
	
	// Column re-order
	
	$("a#col_reorder").fancybox({
		frameHeight: 550,
		frameWidth: 300,
		zoomSpeedIn: 0,
		zoomSpeedOut: 0,
		hideOnContentClick: false
	});
	
	// Event menus
	
	$("div#event_box_menus ul").hover(function(){
		$(this).addClass("hovered");
	},function(){
		$(this).removeClass("hovered");
	});
	
	// Hints
	
	$("div.hint div").css("opacity","0");
	
	$("div.hint").hover(function(){
		$(this).children("div").show().stop().fadeTo( 200, 0.9 );
	},function(){
		$(this).children("div").stop().fadeTo( 500, 0.0, function(){
			$(this).hide();
		});
	});
	
	$("span.phint").hover(function(){
		var raa = $(this);
		raa.css("position","relative");
		raa.append( '<span class="hint">Click Player Name to enter this Players Page<br />[ <a href="members/control-panel/my-settings/">Turn hints off</a> ]</span>' );
		setTimeout(function(){
			raa.children("span.hint").css("opacity","0").show().stop().fadeTo( 200, 0.9 );
		},400);
	},function(){
		$(this).css("position","static");
		$(this).children("span.hint").remove();
	});
	
	// Recent Results
	
	if( $("div#recent_results ul").length ){
		
		function scroll_result(){
			
			var width = $("div#recent_results ul li:first").width() + 30;
			
			$("div#recent_results ul").animate({left: -width}, width*20, "linear", function(){
				$(this).css("left", "0");
				$("div#recent_results ul li:first").appendTo("div#recent_results ul");
				$("div#recent_results ul li.first_blank").remove();
				scroll_result();
			});
			
		}
		
		scroll_result();
		
	}
	
});

// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
