/**
 * 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;

		var html = '<b class="big">'+Lang.get('sending')+'</b><br/>'+Lang.get('please_wait');
		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
		
		$.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') {
					Overlay.open(Lang.get('scenery_send_failed'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
				} else {
					Overlay.close();
				}
			},
			error: function(a,b,c) {
				Overlay.open(Lang.get('scenery_send_failed'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
			}
		});
	},
	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();"><span>'+Lang.get('btn_send')+'</span></a></td>'
			+'</tr></table></form>';
		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
	},
	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+');"><span>'+Lang.get('btn_yes')+'</span></a> '
			+' <a class="button" href="javascript:Overlay.popoverClose();"><span>'+Lang.get('btn_no')+'</span></a>'
			+'</div></form>';

		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"}); 
	},
	doDelete: function(scenId, clubId) {
		var html = '<b class="big">'+Lang.get('deleting')+'</b><br/>'+Lang.get('please_wait');
		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
		$.ajax({
			url: '/'+language+'/ajax/doDeleteScenery.php',
			type: 'POST',
			data: 'sceneryId='+scenId+(clubId > 0 ? '&clubId='+clubId : ''),
			dataType: 'json',
			success: function(msg) {
				if (msg.status != 'success') {
					Overlay.open(Lang.get('scenery_failed_delete'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
				} else {
					// Success
					var html = '<b>'+Lang.get('scenery_is_deleted')+'</b><br/>';
					Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
					document.location.href = 'scenery.php'+(clubId > 0 ? '?id='+clubId : '');
				}
			},
			error: function(a,b,c) {
				Overlay.open(Lang.get('scenery_failed_delete'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
			}
		});
	},
	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"><span>'+Lang.get('btn_yes')+'</span></a>'
			+' <a href="javascript:Overlay.popoverClose();" class="button"><span>'+Lang.get('btn_no')+'</span></a>'
			+'</div></form>';
		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
	},
	addToClubHome: function(scenId, clubId) {
		var html = '<b class="big">'+Lang.get('adding')+'</b><br/>'+Lang.get('please_wait');
		Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
		$.ajax({
			url: '/'+language+'/ajax/doAddSceneryClubHome.php',
			type: 'POST',
			data: 'sceneryId='+scenId+'&clubId='+clubId,
			dataType: 'json',
			success: function(msg) {
				if (msg.status != 'success') {
					Overlay.open(Lang.get('scenery_failed_add_clubhome'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
				} else {
					// Success
					var html = '<b>'+Lang.get('scenery_is_added_clubhome')+'</b><br/>';
					Overlay.open(html, { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
				}
			},
			error: function(a,b,c) {
				Overlay.open(Lang.get('scenery_failed_add_clubhome'), { addClass: "medium", closeable: true, topPosition: 560, parentNode: "pageContent"});
			}
		});
	}
};

/** 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);
}