// JavaScript Document
function BlessGame(id, special) {
	this._id = id;
	this._special = special;
	
	this._functionsURL = 'games/game_bless.php';
	this._sep = "==LOH==";
	this.init();
	
	this._options = new Array();
}
		
BlessGame.prototype.init = function() {
	var me = this;
	$('#game_content #game_status').remove();
	var content = $('#game_content').html();
	$('#game_content').html("Loading...");
	var callback = function(xml){ 
		me.handleResponse(xml, content);
	};
	var query = {
		type : 1,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
};
BlessGame.prototype.choose = function(id) {
	var me = this;
	$('#game_content #game_status').remove();
	var content = $('#game_content').html();
	$('#game_content').html("Loading...");
	var callback = function(xml){ 
		me.handleResponse(xml, content);
	};
	var option_correct = me._options[id-1];
	if (id == 1)
		var option_incorrect = me._options[1];
	else
		var option_incorrect = me._options[0];
	var query = {
		option_correct : option_correct,
		option_incorrect : option_incorrect,
		type : 2,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
};
/* MAP OF ARRAY returned : 
	0 - totaltimesplaced
	1 - todaytimesoplayed
	2 - choice1 src
	3 - choice1 height
	4 - choice1 width
	5 - choice1 firstname
	6 - choice1 lastname
	7 - choice2 src
	8 - choice1 height
	9 - choice1 width
	10 - choice2 firstname
	11 - choice2 lastname
	12 - choice1 id
	13 - choice2 id
	14 - total questions today
	15 - choice1 line position
	16 - choice2 line position
	17 - choice1 line points
	18 - choice2 line points
	19 - choice1 blurb
	20 - choice2 blurb
*/
BlessGame.prototype.handleResponse = function(xml, content) {
	var me = this;
	var response = xml.split(me._sep);
	// assign game
	$('#game_content').html(content);
	$('#game_content #game_table').show();
	$('#game_pointstotal').html(response[0]);
	$('#game_pointstoday').html(response[1]);
	$('#game_questionnumber').html(parseInt(response[14])+1);
	$('#choice1_image').html('<img src="'+response[2]+'" height="'+response[3]+'" width="'+response[4]+'"/>');
	$('#choice1_firstname').html(response[5]);
	$('#choice1_lastname').html(response[6]);
	$('#choice2_image').html('<img src="'+response[7]+'" height="'+response[8]+'" width="'+response[9]+'"/>');
	$('#choice2_firstname').html(response[10]);
	$('#choice2_lastname').html(response[11]);
	//asign options
	me._options[0] = response[12];
	me._options[1] = response[13];
	// line positions
	$('#choice1_lineposition').html(response[15]);
	$('#choice2_lineposition').html(response[16]);
	$('#choice1_points').html(response[17]);
	$('#choice2_points').html(response[18]);
	$('#choice1_blurb').html(response[19]);
	$('#choice2_blurb').html(response[20]);
	if (parseInt(response[21]) == 1)
		addPoints(1);
	$('#bless_game_history').html(response[22]);
	
	// check if played too many times today
	if (parseInt(response[1]) >= 20) {
		$('#game_maximumstr').html('<span class="profile_title">You have played more than 20 times.  Only the people who you bless will now get points!</span>');	
		$('#game_maximumstr').css("background-color", "#f1b909");
		$('#game_maximumstr').show();
	}
	$("div.choice").each(function(i) {
		$(this).mouseover( function() {
			if (i == 0)
			me.gameAddEffect(this, response[3], response[4], i+1);
			else
			me.gameAddEffect(this, response[8], response[9], i+1);
			//$(this).css("background", "#243054");
			//alert($(this).css("background"));
			//$(this).append(me.buildAddButton());
		});
		$(this).mouseout( function() { 
			me.gameRemoveEffect(this, i+1);
		});
	});
};

BlessGame.prototype.gameAddEffect = function(element, height, width, index) {
	var me = this;
	$(element).css("background", "#3C6A94");
	$('#choice'+index+'_firstname, #choice'+index+'_lastname').css("color", "#fff");
	$(element).css("text-decoration", "none");
	width = parseInt(width)+1;
	$('.bless_overlay_'+index).css("display", "block");
	$('.bless_overlay_'+index).css("width", width+"px");
	$('.bless_overlay_'+index).css("height", height+"px");
	//var height = Math.floor(height / 2);
	var left_margin = Math.floor(((315 - width) / 2)+8);
	//$('.bless_overlay').css("margin-top", "-"+height+"px");
	$('.bless_overlay_'+index).css("margin-left", left_margin+"px");
};
BlessGame.prototype.gameRemoveEffect = function(element, index) {
	var me = this;
		$(element).css("background", "#f1b909");
		
	$('#choice'+index+'_firstname, #choice'+index+'_lastname').css("color", "#243054");
	$('.bless_overlay_'+index).css("display", "none");
};
BlessGame.prototype.buildAddButton = function(element) {
	var me = this;
	var html = '<div class="bless_button"><img src="/images/games/button_blessme.gif"></div>';
	return html;
};
/****************************************
REPENT GAME
*****************************************/
function RepentGame(id, special, profile_id) {
	this._id = id;
	this._special = special;
	this._profileid = profile_id;
	
	this._functionsURL = 'games/game_repent.php';
	this._sep = "==LOH==";
	
	this._options = new Array();
	
	this._statusText = '';
}
RepentGame.prototype.repent = function(choice, strOption) {
	var me = this;
	if (choice == 1)
		choice = 5;
	else if (choice == -1)
		choice = -5;
	else {
		alert('That is an invalid choice!');
		return false;
	}
	strName = $('#hidden_useralias').html();
	$('#repent_table').html('<tr><td align="center" style="border:0;padding-top:7px;padding-bottom:5px;"><span class="special_field_box">Saving...</span></td></tr>');
	$('#repent_table').css("width","100%");
	var callback = function(xml){
		if (parseInt(xml) < 0)
			alert("There has been an error.  Try again or report a bug.  Thanks");
		else {
			// update old choice
			var oldChoice = parseInt($('#counter_confession').html());
			oldChoice += choice;
			if (oldChoice >= 0)
			oldChoice = "+"+oldChoice;
			$('#counter_confession').html(oldChoice.toString());
			var congrats = '';
			
			if (me._profileid == me._id) {
				if (parseInt(xml) == 2)
					congrats = '<div style="margin-top:10px;" class="special_field_box">You have been forgiven and just received +25 points!</div>';
				var span = '<span class="special_field_box">You have <span style="text-decoration:underline;">'+strOption+'</span> your own sin.</span>'+congrats;
			}
			else {
				if (parseInt(xml) == 2)	
					congrats = '<div style="margin-top:10px;" class="special_field_box">'+strName+' has been forgiven and just received +25 points!</div>';
				var span = '<span class="special_field_box">You have <span style="text-decoration:underline;">'+strOption+'</span> '+strName+'\'s sin.</span>'+congrats;
			}
			$('#repent_table td').html(span);
			if (parseInt(xml) == 2) {
				var usersPoints = parseInt($('#soul_karma_points').html())+25;
				$('#soul_karma_points').html(usersPoints);
			}
			addPoints(1);
		}
	};
	//alert(oldChoice);
	var query = {
		choice : choice,
		profile_id : me._profileid,
		type : 1,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
};
RepentGame.prototype.bless = function() {
	var me = this;
	if (me._statusText != '') {
		alert(me._statusText);
		return false;
	}
	var points = parseInt($('#profile_points').html())+1;
	$('#profile_points').html(points);
	if (me._profileid == me._id)
		$('#blessme_content').html('<span class="special_field_box"><span id="message_info">You have blessed yourself today.</span>  <a href="bless.php">Keep blessing here</a>.</span>');
	else
		$('#blessme_content').html('<span class="special_field_box"><span id="message_info">You have blessed this person today.</span>  <a href="bless.php">Keep blessing here</a>.</span>');
	$('#blessme_content').show();
	$('#blessme_button').addClass("clickable_selected");
	me._statusText = $('#message_info').text();
	var callback = function(xml){
		//alert('xml: '+xml);
		var usersPoints = parseInt($('#soul_karma_points').html())+1;
		$('#soul_karma_points').html(usersPoints);
		$('#soul_karma_points_box').addClass("clickable_selected");
		if (me._profileid == me._id)
			addPoints(1);
	};
	
	var query = {
		profile_id : me._profileid,
		type : 2,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
}
RepentGame.prototype.cause = function() {
	var me = this;
	strName = $('#hidden_useralias').html();
	$('#causes_row').html('<div style="border:0;padding-top:7px;padding-bottom:5px;"><span class="special_field_box">Saving...</span><div>');
	var callback = function(xml){
		if (parseInt(xml) < 0)
			alert("There has been an error.  Try again or report a bug.  Thanks");
		else {
			// update old choice
			var oldChoice = parseInt($('#counter_causes').html());
			oldChoice += 5;
			if (oldChoice >= 0)
			oldChoice = "+"+oldChoice;
			$('#counter_causes').html(oldChoice.toString());
			var span = '<div style="border:0;padding-top:7px;padding-bottom:5px;"><span class="special_field_box">You have <span style="text-decoration:underline;">supported</span> this cause.</span></div>';
			$('#causes_row').html(span);
			addPoints(5);
		}
	};
	//alert(oldChoice);
	var query = {
		profile_id : me._profileid,
		type : 3,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
}
RepentGame.prototype.message_send = function() {
	var me = this;
	var message = $('#message').val();
	var subject = $('#subject').val();
	if (subject == '') {
		alert('You must enter a subject!');
		return false;
	}
	else if (message == '') {
		alert('You must enter a message!');
		return false;
	}
	$('#message_button').removeClass("clickable_selected");
	$('#message_form').html('<div style="border:0;padding-top:7px;padding-bottom:5px;text-align:center;"><span class="special_field_box">Sending...</span><div>');
	var callback = function(xml){
		if (parseInt(xml) < 0)
			alert("There has been an error.  Try again or report a bug.  Thanks");
		else {
			// update old choice
			
			var span = '<div style="border:0;padding-top:7px;padding-bottom:5px;text-align:center;"><span class="special_field_box">Your message has been sent.</span></div>';
			$('#message_form').html(span);
		}
	};
	//alert(oldChoice);
	var query = {
		message : message,
		subject : subject,
		profile_id : me._profileid,
		type : 4,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();
}
RepentGame.prototype.message_show = function() {
	var me = this;
	if ($('#message_form').css("display") == "none") {
		$('#message_form').show();
		$('#subject').focus();
		$('#message_button').addClass("clickable_selected");
		$('#send_message_button').html("Discard");
	}
	else {
		$('#message_form').hide();
		$('#message, #subject').val('');
		$('#message_button').removeClass("clickable_selected");
		$('#send_message_button').html("Send Message");
	}
};
RepentGame.prototype.repentHistory = function(id) {
	if (id == 1) {	
		$('#show_repenthistory_link').html('<a href="#" onclick="game.repentHistory(0);return false;">View Past Sins</a>');
		$('#confession_history').hide();
		return true;
	}
	var me = this;
	$('#show_repenthistory_link').html('Loading...');
	
	var callback = function(xml){
		if (parseInt(xml) == 0) {
			$('#confession_history').html('<div class="profile_content repent_history" id="" style="border-bottom:1px dotted #3C6A94;text-align:center;font-weight:bold;">There were no sins found.</div>');
		}
		else {
			$('#confession_history').html(xml);
		}
		$('#show_repenthistory_link').html('<a href="#" onclick="game.repentHistory(1);return false;">Hide Past Sins</a>');
		$('#confession_history').show();
	};
	var query = {
		profile_id : me._profileid,
		type : 5,
		id : me._id, 
		special : me._special
	};
	var ajax = new Ajax(me._functionsURL,query,callback);
	ajax.send();	
};