$(document).ready(function() 
{
	/** Alternating table row styles - See reports.js for report styling. **/
	$("table.data-table tr:even").addClass("even");
	$("table.data-table tr:odd").addClass("odd");
	
	/** Password reset validation **/
	$('.password-reset').validate(
	{
		errorPlacement: function(error, element)
		{
			$('.rounded-grey-content-box p').html(error);
		},
		messages:
		{
			name: 		{ required: "All fields are required." },
			password: 	{ required: "All fields are required." },
			password_confirm:
			{
				required: 	"All fields are required.",
				equalTo: 	"The passwords you have entered do not match."
			}
		}
	});

	/** Reg form validation **/
	$("#registration").validate(
	{
		onkeyup:		false,
		rules:
		{
			alias:
			{ 
				required: true,
				minlength: 6,
				maxlength: 16,
				remote: 
				{
					url: "register/check_alias",
					type: "post",
					beforeSend: function()
					{
						$('#alias').addClass('checking');
					},
					complete: function(data)
					{
						$('#alias').removeClass('checking');
					}
				}
			},
			email:
			{
				required: true,
				email: true
			},
			password:
			{
				required: true,
				minlength: 6,
				maxlength: 16
			},
			confirmPassword:
			{
				required: true,
				minlength: 6,
				maxlength: 16,
				equalTo: '#password'
			},
			firstName: 'required',
			lastName: 'required',
			address1: 'required',
			city: 'required',
			country: 'required',
			phoneNumber:
			{
				required: true,
				pgs_phone_number: true
			},
			birthDay: 'required',
			birthMonth: 'required',
			birthYear: 'required',
			agreeterms: 'required'
		},
		groups: 
		{
			dob: "birthMonth birthDay birthYear"
		},
		errorPlacement: function(error, element)
		{
			if (element.attr("name") == "birthMonth" || element.attr("name") == "birthDay" || element.attr("name") == "birthYear" )
			{
				error.insertAfter("#birth-year");
			}
			else if (element.attr("name") == "agreeterms")
			{
				error.insertAfter(element.next());
			}
			else
			{
				error.insertAfter(element);
			}
		},
		messages:
		{
			alias: 
			{
				remote: 	"That username is not available"
			}
		}
	});

    var day_names = new Array(7);
    day_names[0] = "sunday";
    day_names[1] = "monday";
    day_names[2] = "tuesday";
    day_names[3] = "wednesday";
    day_names[4] = "thursday";
    day_names[5] = "friday";
    day_names[6] = "saturday";

    // Get the current date
    date_now = new Date();

    // Figure out the friendly day name
    day_value = date_now.getDay();
    date_text = day_names[day_value];
    if ($('#content-'+date_text).length)
    {
        $('#'+date_text).addClass('active');
        $('#content-'+date_text).show();
    }
	
	// Game Tile Hover states
	$('span.frame').hover
	(
		function()
		{
			$(this).addClass('hover');
			$(this).siblings('span.title').addClass('hover');
		},
		function()
		{
			$(this).removeClass('hover');
			$(this).siblings('span.title').removeClass('hover');
		}
	);
	
	$('span.title').hover
	(
		function()
		{
			$(this).addClass('hover');
			$(this).siblings('span.frame').addClass('hover');
		},
		function()
		{
			$(this).removeClass('hover');
			$(this).siblings('span.frame').removeClass('hover');
		}
	);
});

//------------------------------------------------------------------------

/**
 *	pop_dialog
 *
 *	Opens a modal dialog and centers it on screen.
 *
 *	url 	- The relative url to load into the dialog
 *	width	- The minimum width of the dialog
 *	height	- The minimum height of the dialog
 */
function pop_dialog(url, width, height)
{
	$('html, body').animate({scrollTop:0}, 'slow');

	$('div#dialog.modal').modal(
	{
		minWidth: width,
		minHeight: height,
		onOpen: function(dialog)
		{
			//Animate the overlay
			dialog.overlay.slideDown('slow', function () 
			{
				//Make sure the contents of the dialog are showing
				dialog.data.show();
				
				data = ajax_page_load(url, false, false);

				//On success, show dialog contents
				if(data.statusText == 'OK')
				{
					$('div#dialog div.container').html(data.responseText);
					dialog.container.fadeIn('fast');
				}
			});
		},
		onClose: function(dialog)
		{
			//Fade out the dialog
			dialog.container.fadeOut('slow', function()
			{
				//If we're closing the dialog, animate the overlay off.
				dialog.overlay.slideUp('slow', function()
				{
					//Clean up the mess.
					$.modal.close();
				});
			});
		}
	});
}

//------------------------------------------------------------------------

/**
 *	ajax_page_load
 *
 *	Performs an AJAX request for the given URL. Returns an
 *	XMLHttpRequest object.
 *
 *	url - The relative url to load.
 */
function ajax_page_load(url, update_nav, members_only)
{
	data = $.ajax(
	{
		dataType: 'html',
		url: webRoot + url,
		type: 'POST',
		async: false,
		beforeSend: function()
		{
			//Update the highlighted nav items
			if(update_nav)
			{
				ajax_nav_update(url);
			}
		},
		error: function(jqXHR)
		{
			//If a user's session is expired the request will return a 401 status
			//We use that as an indication that we should redirect the user.
			if(jqXHR.status == '401' && members_only == true)
			{
				window.location.href = webRoot + 'login/session_expired';
			}
		}
	});
	
	return data;
}

//------------------------------------------------------------------------

//Tabbed views
function currentNav(tab)
{
	$('.nav-tab[id]').map(function()
	{
		if( $('a#' + this.id).hasClass('active') )
		{
			$('a#' + this.id).removeClass('active');
		}
	});
	$('.content-div[id]').map(function()
	{
		if( $('#' + this.id).is(':visible') )
		{
			$('#' + this.id).hide();
		}
	});

	$('#' + tab).addClass('active');
	$('#content-' + tab).show();
}

//------------------------------------------------------------------------

function slotTab(lines,me)
{
	 var clicked_id = me.id;
	 $('.game-sub-tab[id]').map(function()
	 {
		if( $('a#' + this.id).hasClass('active') )
		{
			$('a#' + this.id).removeClass('active');
		}

	});
	$('.line-games[id]').map(function()
	{
		if( $('#' + this.id).is(':visible') )
		{
			$('#' + this.id).hide();
		}
	});
	$('a#'+clicked_id).addClass('active');
	$('#lines_'+lines).show();
}

