
var historyMinDate = null;
var historyCurDate = null;

var claimBusy = false;

function datePickerDateChanged(dateText, inst) {
	$.get("/gethistory.php", { 'profileid' : profileId, 'date' : dateText, 'histtime' : historyTime }, function(data) {
		$("#historydateinfo").html(data);
	}, "json");
}

function enableHoverEffects() {
	$(".loadoutButtonEnabled").hover(function() {
		$(this).addClass("loadoutButtonHover");
	}, function() {
		$(this).removeClass("loadoutButtonHover");
	});
}

function installPageHandlers() {
	$("a.pageButtonLink").click(function() {
		var newPage = $(this).attr('id').substring(6);
		$.get("/showbackpack.php", { 'profileid' : profileId, 'p' : newPage, 'time' : historyTime, 'd' : debugMode }, function(data) {
			$("#backpackcontents").html(data.contents);
			$("#tooltips").html(data.tooltips);
			//$("#cacheinfo").html(data.cacheinfo);
			$("#gentime").text(data.gentime);
			installPageHandlers();
			installTooltips();
		}, "json");
		return false;
	});
	$("a.claimLink").click(function() {
		var username = $.cookie("username");
		var session = $.cookie("session");
		var cookie = $.cookie("cookie");

		if(claimBusy == true) {
			return false;
		}

		claimBusy = true;

		$.ajax({
		type: "POST",
		url: "/claim.php",
		cache: false,
		data: ({
			'profileid' : profileId,
			'username' : username,
			'session' : session,
			'cookie' : cookie
		}),
		dataType: "json",
		success: function(data) {
			claimBusy = false;
			if(data.error) {
				$("#claimStatus").text(data.errmsg);
			} else {
				$("#claimStatus").text("");
				window.location.reload(true);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			claimBusy = false;
			$("#claimStatus").text("An error occured on the server. Try again.");
		}
		});
		return false;
	});
	$("a.verifyLink").click(function() {
		var username = $.cookie("username");
		var session = $.cookie("session");
		var cookie = $.cookie("cookie");

		if(claimBusy == true) {
			return false;
		}

		claimBusy = true;
		$("#verifyStatus").html("<img src=\"/loadingAnimation.gif\" alt=\"Loading...\"/>");

		$.ajax({
		type: "POST",
		url: "/verify.php",
		cache: false,
		data: ({
			'profileid' : profileId,
			'username' : username,
			'session' : session,
			'cookie' : cookie
		}),
		dataType: "json",
		success: function(data) {
			claimBusy = false;
			if(data.error) {
				$("#verifyStatus").text(data.errmsg);
			} else {
				$("#verifyStatus").text(data.msg);
				$("#verifyInstructions").hide();
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			claimBusy = false;
			$("#verifyStatus").text("An error occured on the server. Try again.");
		}
		});
	});
	$("img.pageButtonNext").hover(function() {
		$(this).attr('src', '/buttons/next_hover.png');
	}, function() {
		$(this).attr('src', '/buttons/next.png');
	});
	$("img.pageButtonPrev").hover(function() {
		$(this).attr('src', '/buttons/prev_hover.png');
	}, function() {
		$(this).attr('src', '/buttons/prev.png');
	});
}
$(installPageHandlers);
$(enableHoverEffects);
$(function() {
	//$("#accordion").accordion({ header: "h2" });
	$(".accordion .head").click(function() {
		var content = $(this).next();
		if(content.is(":visible")) {
			$(this).prev().removeClass("ui-icon-triangle-1-s");
			$(this).prev().addClass("ui-icon-triangle-1-e");
			content.hide('slow');
		} else {
			$(this).prev().removeClass("ui-icon-triangle-1-e");
			$(this).prev().addClass("ui-icon-triangle-1-s");
			content.show('slow');
		}
		return false;
	}).next().hide();
	$("#datepicker").datepicker({
		inline: true,
		minDate: historyMinDate,
		maxDate: '+0d',
		defaultDate: historyCurDate,
		onSelect: datePickerDateChanged
	});

	$("#tabs").tabs();

	$("#classes").mousemove(function(e) {
		$.each($(this).children("img"), function(index, child) {
			var distance = Math.abs(e.pageX - (this.offsetLeft + (this.width / 2)));
			var scale;

			distance = distance - (this.width * 0.1);
			if(distance < 0)
				distance = 0;

			if(distance > this.width)
			{
				scale = 1;
			}
			else
			{
				scale = 1.5 - (0.5 * (distance / this.width));
			}
			$(this).css('-moz-transform', 'scale(' + scale + ')');
			$(this).css('-webkit-transform', 'scale(' + scale + ')');
		});
	});

	$("#classes").mouseout(function() {
		$(this).children("img").css('-moz-transform', 'scale(1)');
		$(this).children("img").css('-webkit-transform', 'scale(1)');
	});


	$(".loadoutclass").hover(function() {
		var currentSrc = $(this).attr('src');
		$(this).attr('src', currentSrc.replace("_inactive", "_red"));

		var classInfo = $("#classinfo_" + $(this).attr("id").substring(13));
		classInfo.show();
		var newLeft = $(this).position().left - (classInfo.width() / 2) + ($(this).width() / 2);
		classInfo.css("left", newLeft + "px");
	}, function() {
		var currentSrc = $(this).attr('src');
		$(this).attr('src', currentSrc.replace("_red", "_inactive"));

		var classInfo = $("#classinfo_" + $(this).attr("id").substring(13));
		classInfo.hide();
	});

	$(".loadoutclass").click(function() {
		var classname = $(this).attr("id").substring(13);
		$("#classloadout_" + classname).show();
		$("#classes").hide();
		$("#classinfos").hide();
		$("#showalldiv").hide();
	});

	$(".backButton").click(function() {
		$(this).parent().hide();
		$("#classes").show();
		$("#classinfos").show();
		$("#showalldiv").show();
	});

	$(".showAllButton").click(function() {
		$("#classloadout_all").show();
		$("#classes").hide();
		$("#classinfos").hide();
		$("#showalldiv").hide();
	});

	var currentTime
	if(historyCurDate == null)
		currentTime = new Date();
	else
		currentTime = historyCurDate;
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	datePickerDateChanged(month + "/" + day + "/" + year, null);

});
