
$(document).ready(OnDomLoaded);

var BASE_URL = "./awards.php";
var DEFAULT_EMAIL_STRING = "<enter your email address>";
			
//**********************************************************************************************************
//*
//*  RatedItems object
//*
//*  Built to house the user ratings (id, rating, hour) in a cookie until they expire
//*
//**********************************************************************************************************
RatedItems = new Object();
RatedItems.cookie = "ratings"
RatedItems.ids = [];
RatedItems.ratings = [];
RatedItems.times = [];
RatedItems.load = function()
{
	var cookieData = getCookie(RatedItems.cookie)
	if (cookieData == null) return;
	
	var data = cookieData.split("~");
	if (data.length > 1)
	{
		this.ids = data[0].split(",");
		this.ratings = data[1].split(",");
		this.times = data[2].split(",");
	}
}
RatedItems.save = function()
{
	var data = [];
	data.push (this.ids.join(","));
	data.push (this.ratings.join(","));
	data.push (this.times.join(","));
	setCookie(RatedItems.cookie, data.join("~"), 365);
}
RatedItems.add = function(id, rating)
{
	currentHour = Math.floor((new Date()).getTime() / 3600000);
	for (var i = 0; i < this.ids.length; i++)
	{
		if (id > this.ids[i]) continue;
		if (id == this.ids[i])
		{
			// just update the time
			this.times[i] = currentHour;
			this.ratings[i] = rating;
			return i;
		}
		if (id < this.ids[i])
		{
			// insert the id into the array at this point
			this.ids.splice(i,0,id);
			this.ratings.splice(i,0,rating);
			this.times.splice(i,0,currentHour);
			return i;
		}
	}
	this.ids.push(id);
	this.ratings.push(rating);
	this.times.push(currentHour);
	return this.ids.length - 1;
}
RatedItems.exists = function(id)
{
	// TODO: implement a binary search
	for (var i = 0; i < this.ids.length && this.ids[i] <= id; i++)
	{
		if (this.ids[i] == id) return true;
	}
	return false;
}
RatedItems.getRating = function(id)
{
	// TODO: implement a binary search
	for (var i = 0; i < this.ids.length && this.ids[i] <= id; i++)
	{
		if (this.ids[i] == id) return this.ratings[i];
	}
	return null;
}
RatedItems.removeExpired = function(hours)
{
	if (!hours) hours = 23;
	
	var changed = false;
	
	var thisHour = Math.floor((new Date()).getTime() / 3600000);
	for (var i = 0; i < this.ids.length; i++)
	{
		if (thisHour - this.times[i] > hours)
		{
			changed = true;
			this.ids.splice(i,1);
			this.times.splice(i,1);
			this.ratings.splice(i,1);
			i--;
		}
	}
	
	return changed;
}

//**********************************************************************************************************
//*
//*  OnDomLoaded
//*
//*  Initializes the page
//*
//**********************************************************************************************************
function OnDomLoaded() 
{
	$.preloadCssImages();
	$('#email-submit').click(OnSubmitEmail);
	$('#email-input').focus(function()
	{
		var email = $('#email-input').val();
		if (email == DEFAULT_EMAIL_STRING)
		{
			$('#email-input').val("");
		}
	});
	$('#email-input').keyup(function(e)
	{
		if (e.keyCode == 13)
		{
			OnSubmitEmail();
		}
	});
}

function OnSubmitEmail()
{
	var email = fast_trim($('#email-input').val());
	if (email == "" || email == DEFAULT_EMAIL_STRING)
	{
		DisplaySubmitError();
		return;
	}
	
	if (!verifyEmail(email))
	{
		DisplaySubmitError();
		return;
	}
	
	$.facebox.settings.dismissOnEsc = false;
	$.facebox.settings.dismissOnClick = false;
	$.facebox(function($){
				ajaxPost(BASE_URL,
						{
							req:"emailsub",
							email:email
						},
						OnEmailSubmitted);}
			 );
}

//**********************************************************************************************************
//*
//*  DisplaySubmitError
//*
//*  Displays a general error to the user that all fields are required
//*
//**********************************************************************************************************
function DisplaySubmitError()
{
	var s = '<div style="overflow:auto" class="error-popup"><div style="font-weight:bold;padding:3px 0px 0px 70px;color:#555">Oops!</div><div style="font-weight:bold;padding:7px 0px 0px 70px;color:#555">Please enter a valid email address.</div></div>';
	jQuery.facebox(s);
}

//**********************************************************************************************************
//*
//*  OnAwardAdded
//*
//**********************************************************************************************************
function OnEmailSubmitted(success, data, textStatus)
{
	$.facebox.settings.dismissOnEsc = true;
	$.facebox.settings.dismissOnClick = true;
	if (!success || !data.success)
	{
		// oops!  just show an error and bail out
		var s = '<div style="overflow:auto" class="error-popup"><div style="font-weight:bold;padding:0px 0px 0px 70px;color:#555">Oops!</div><div style="font-weight:bold;padding:8px 0px 0px 70px;color:#555">Something, somewhere, went wrong!  Please try again later.</div></div>';
		jQuery.facebox(s);
		return;
	}

	var s = '<div style="overflow:auto" class="success-popup"><div style="font-weight:bold;padding:23px 0px 0px 85px;color:#555">Thank you for submitting your email!</div></div>';
	jQuery.facebox(s);
	
	// clear the input box
	$('#email-input').val("");

	// TODO: set the accepted username and email in a cookie
	// setCookie('username', fast_trim($('#input-new-user').val()), 365);
	// setCookie('email', fast_trim($('#input-new-email').val()), 365);
}

