/**
 * Scripts for scenery
 * @author miken, stark
 */
var Scenery = {
	toFriendAnchor: null,
	deleteAnchor: null,
	addToClubHomeAnchor: null, // what are these anchors good for?
	toFriend:function(friendId, userSceneryId, shownUserId) {
		var friendId = document.getElementById('sendtofriendselect').value;
		var userSceneryId = document.getElementById('usersceneryid').value;
		var shownUserId = document.getElementById('shownuserid').value;
		popover.openFramed(Lang.get('scenery_sendtofriend'), '<b class="big">'+Lang.get('sending')+'</b><br/>'+Lang.get('please_wait'), Scenery.toFriendAnchor, 1);
		$.ajax({
			url: '/'+language+'/ajax/sceneryToFriend.php',
			type: 'POST',
			data: 'friendId='+friendId+'&userSceneryId='+userSceneryId+'&shownUserId='+shownUserId+'&closeId='+popover.closeId,
			dataType: 'json',
			success: function(msg) {
				if (msg.status != 'success') {
					popover.openFramed(Lang.get('scenery_sendtofriend'), Lang.get('scenery_send_failed'), Scenery.toFriendAnchor, 1);
				} else {
					popover.close();
				}
			},
			error: function(a,b,c) {
				popover.openFramed(Lang.get('scenery_sendtofriend'), Lang.get('scenery_send_failed'), Scenery.toFriendAnchor, 1);
			}
		});
	},
	sendToFriend: function(eSrc, userSceneryId, shownUserId, senderUserId) {
		Scenery.toFriendAnchor = eSrc;
		var html = '<form method="post" action="scenery.php"><input type="hidden" id="usersceneryid" value="' + userSceneryId
			+'"/><input type="hidden" id="shownuserid" value="' + shownUserId + '"/>'
			+'<table><tr><td colspan="2"><strong>'+Lang.get('scenery_choosefriend')+'</strong></td></tr>'
			+'<tr><td><select name="friend" id="sendtofriendselect"><option value="">'+Lang.get('scenery_choose_option')+'</option>';
		for (var id in friendsList) {
			html += '<option value="'+id+'">'+friendsList[id]+'</option>';
		}
		html += '</select></td><td><a class="button" href="javascript:void(0);" onclick="Scenery.toFriend();">'+Lang.get('btn_send')+'</a></td>'
			+'</tr></table></form>';
		popover.openFramed(Lang.get('scenery_sendtofriend'), html, eSrc, 1);
	},
	boxDelete: function(eSrc, scenId, clubId) {
		Scenery.deleteAnchor = eSrc;
		var html = '<form method="post" action="scenery.php" class="bigwhite">'
			+'<p>'+Lang.get('scenery_confirm_delete')+'</p>'
			+'<div>'
			+' <a class="button" href="javascript:void(0);" onclick="Scenery.doDelete('+scenId+', '+clubId+');">'+Lang.get('btn_yes')+'</a> '
			+' <a class="button" href="javascript:popover.close();">'+Lang.get('btn_no')+'</a>'
			+'</div></form>';
		popover.openFramed(Lang.get('scenery_delete_scenery'), html, eSrc, 1);
	},
	doDelete: function(scenId, clubId) {
		popover.openFramed(Lang.get('scenery_deleting_scenery'), '<b class="big">'+Lang.get('deleting')+'</b><br/>'+Lang.get('please_wait'), Scenery.deleteAnchor, 1);
		$.ajax({
			url: '/'+language+'/ajax/doDeleteScenery.php',
			type: 'POST',
			data: 'sceneryId='+scenId+(clubId > 0 ? '&clubId='+clubId : ''),
			dataType: 'json',
			success: function(msg) {
				if (msg.status != 'success') {
					popover.openFramed(Lang.get('scenery_delete_scenery'), Lang.get('scenery_failed_delete'), Scenery.deleteAnchor, 1);
				} else {
					// Success
					popover.openFramed(Lang.get('scenery_delete_scenery'), '<b>'+Lang.get('scenery_is_deleted')+'</b><br/>', Scenery.deleteAnchor, 1);
					document.location.href = 'scenery.php'+(clubId > 0 ? '?id='+clubId : '');
				}
			},
			error: function(a,b,c) {
				popover.openFramed(Lang.get('scenery_delete_scenery'), Lang.get('scenery_failed_delete'), Scenery.deleteAnchor, 1);
			}
		});
	},
	addToClubHomeBox: function(eSrc, scenId, clubId) {
		Scenery.addToClubHomeAnchor = eSrc;
		var html = '<form method="post" action="scenery.php" class="bigwhite">'
			+'<p>'+Lang.get('scenery_confirm_add_clubhome')+'</p>'
			+'<div>'
			+' <a href="javascript:void(0);" onclick="Scenery.addToClubHome('+scenId+','+clubId+');" class="button">'+Lang.get('btn_yes')+'</a>'
			+' <a href="javascript:popover.close();" class="button">'+Lang.get('btn_no')+'</a>'
			+'</div></form>';
		popover.openFramed(Lang.get('scenery_add_scenery_clubhome'), html, eSrc, 1);
	},
	addToClubHome: function(scenId, clubId) {
		popover.openFramed(Lang.get('scenery_adding_scenery_clubhome'), '<b class="big">'+Lang.get('adding')+'</b><br/>'+Lang.get('please_wait'), Scenery.addToClubHomeAnchor, 1);
		$.ajax({
			url: '/'+language+'/ajax/doAddSceneryClubHome.php',
			type: 'POST',
			data: 'sceneryId='+scenId+'&clubId='+clubId,
			dataType: 'json',
			success: function(msg) {
				if (msg.status != 'success') {
					popover.openFramed(Lang.get('scenery_add_scenery_clubhome'), Lang.get('scenery_failed_add_clubhome'), Scenery.addToClubHomeAnchor, 1);
				} else {
					// Success
					popover.openFramed(Lang.get('scenery_add_scenery_clubhome'), '<b>'+Lang.get('scenery_is_added_clubhome')+'</b><br/>', Scenery.addToClubHomeAnchor, 1);
				}
			},
			error: function(a,b,c) {
				popover.openFramed(Lang.get('scenery_add_scenery_clubhome'), Lang.get('scenery_failed_add_clubhome'), Scenery.addToClubHomeAnchor, 1);
			}
		});
	}
};

/** Comment stuff - avaiting mikens update! */

//Get latest commented sceneries
var scenPageTarget = null;
function scenCommentPages( iPage ) {
	if (scenPageTarget == null)
		scenPageTarget = document.getElementById('latestCommentedScene');
	ajaxEngine.sendRequest('latestCommentedSceneries', 'page='+iPage);
}
ajaxEngine.registerRequest('latestCommentedSceneries', '/'+language+'/ajax/latestCommentedSceneries.php');

function getComments(userIdSceneryCreator, id, page) {
	ajaxEngine.sendRequest('sceneryComments', 'userIdSceneryCreator='+userIdSceneryCreator, 'id='+id, 'page='+page);
}
function getCommentsClub(id, page, clubId) {
	ajaxEngine.sendRequest('sceneryComments', 'sceneryId='+id, 'page='+page, 'id='+clubId);
}

var commentArea;
function addComment(userIdSceneryCreator, id) {
	if (!commentArea) {
		commentArea = document.getElementById("sceneryCommentArea");
	}

	var comment = commentArea.value;
	comment = encodeURIComponent(comment);
	if (comment.length > 0) {
		ajaxEngine.sendRequest('sceneryComments', 'action=add', 'userIdSceneryCreator='+userIdSceneryCreator, 'id='+id, 'comment='+comment);
	}
	commentArea.value="";
}

function addCommentClub(id, clubId) {
	if (!commentArea) {
		commentArea = document.getElementById("sceneryCommentArea");
	}

	var comment = commentArea.value;
	comment = encodeURIComponent(comment);
	if (comment.length > 0) {
		ajaxEngine.sendRequest('sceneryComments', 'action=add', 'sceneryId='+id, 'comment='+comment, 'id='+clubId);
	}
	commentArea.value="";
}

function deleteComment(userIdSceneryCreator, userScenery, id, page) {
	ajaxEngine.sendRequest('sceneryComments', 'action=delete', 'userIdSceneryCreator='+userIdSceneryCreator, 'id='+userScenery, 'cid='+id, 'page='+page);
}

function deleteCommentClub(userScenery, id, page, clubId, authorId) {
	ajaxEngine.sendRequest('sceneryComments', 'action=delete', 'sceneryId='+userScenery, 'cid='+id, 'page='+page, 'id='+clubId, 'authorId='+authorId);
}