function ajax_changecontent(node, content) {
	document.getElementById(node).innerHTML = content;
}

function ajax_changesub(msg, disable, enable) {
	$('submit').value = msg;
	if (disable == true) {
		$('submit').disabled = true;
	}
	
	if (enable == true) {
		$('submit').disabled = false;
	}
}

var loading = false;
var content = false;

function getstats(e, itemid, loot) {
	var y = 0;
	if (document.all) {
		var y = event.clientY + document.body.scrollTop;
		var x = event.clientX + document.body.scrollLeft;
	} else {
		var y = e.pageY;
		var x = e.pageX; 
	}
	
	if ($(itemid)) {
		showstats(x, y, $(itemid));
	} else {
		new Ajax.Request('includes/ajax_misc.php?do=getitem&ukey='+itemid+'&loot='+loot, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				$('etc').innerHTML = $('etc').innerHTML + response.responseText;
				showstats(x, y, $(itemid));
			}
		});
	}
}

function getobject(e, objid) {
	var obj = objid.split("_");
	
	var objid = obj[1];
	
	var y = 0;
	if (document.all) {
		var y = event.clientY + document.body.scrollTop;
		var x = event.clientX + document.body.scrollLeft;
	} else {
		var y = e.pageY;
		var x = e.pageX; 
	}

	if ($('obj_'+objid)) {
		showstats(x, y, $('obj_'+objid));
	} else {
		new Ajax.Request('includes/ajax_misc.php?do=getobj&ukey='+objid, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				$('etc').innerHTML = $('etc').innerHTML + response.responseText;
				showstats(x, y, $('obj_'+objid));
			}
		});
	}
}

function gettech(e, techid) {
	var y = 0;
	if (document.all) {
		var y = event.clientY + document.body.scrollTop;
		var x = event.clientX + document.body.scrollLeft;
	} else {
		var y = e.pageY;
		var x = e.pageX; 
	}
	
	if ($(techid)) {
		showstats(x, y, $(techid));
	} else {
		new Ajax.Request('includes/ajax_misc.php?do=gettech&ukey='+techid, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				$('etc').innerHTML = $('etc').innerHTML + response.responseText;
				showstats(x, y, $(techid));
			}
		});
	}
}

function getmonster(e, monsterid, nohunt) {	
	var y = 0;
	if (document.all) {
		var y = event.clientY + document.body.scrollTop;
		var x = event.clientX + document.body.scrollLeft;
	} else {
		var y = e.pageY;
		var x = e.pageX; 
	}
	
	if ($(monsterid)) {
		showstats(x, y, $(monsterid));
	} else {
		new Ajax.Request('includes/ajax_misc.php?do=getmonster&ukey='+monsterid+'&nohunt='+nohunt, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				$('etc').innerHTML = $('etc').innerHTML + response.responseText;
				showstats(x, y, $(monsterid));
			}
		});
	}
}

var stat = false;

function showstats(x, y, content) {
	content.style.position	= "absolute";
	content.style.top 		= y + "px";
	content.style.left 		= x + "px";
	content.style.display 	= "block";
	
	if ((parseInt(content.offsetLeft) + parseInt(content.width)) > parseInt(document.body.clientWidth)) {
		content.style.left = parseInt(document.body.clientWidth) - parseInt(content.width);
	}
	
	new Draggable(content, {
		handle: 'handle'
	});
}

function clearstats(itemid) {
	if (stat == false) {
		stat = true;
		setTimeout('stattimer()', 1000);
		$(itemid).style.display = 'none';
	}
}

//Prevents "flashing" of the stat window
function stattimer() {
	if (stat == true) {
		stat = false;
	} else {
		stat = true;
	}
}

function loot(itemid) {
	new Ajax.Request('includes/ajax_misc.php?do=loot&itemid='+itemid, {
		asynchronous: true,
		method: 'get',
		onComplete: function(response) {
			if (response.responseText == 'Looted') $('lootbutton'+itemid).disabled = true;
			$('lootbutton'+itemid).value = response.responseText;
		}
	});
}

function item_changepage(itemid, page) {
	var itempage = $(itemid+'_pages');
	
	var pages = itempage.getElementsByTagName('table');
	
	for (i = 0; i < pages.length; i++) {
		if (pages[i].hasAttribute("id")) {
			if (pages[i].getAttribute("id") != itemid+'_'+page) {
				pages[i].style.display = 'none';
			} else {
				pages[i].style.display = '';
			}
		}
	}
}

var books = new Array();

function readBook(itemid) {
	
	if (!books[itemid]) {
		new Ajax.Request('includes/ajax_compact.php?do=getbook&itemid='+itemid, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				if (response.responseText) {
					Control.Modal.open(response.responseText, {
						width: 400
					});
					
					books[itemid] = response.responseText;
				} else {
					Control.Modal.open('You cannot read this book.');
				}
			}
		});
	} else {
		Control.Modal.open(books[itemid], {
			width: 400
		});
	}
}

var questResponse = new Array();

function itemQuest(itemid) {
	
	if (!questResponse[itemid]) {
		new Ajax.Request('includes/ajax_compact.php?do=itemquest&itemid='+itemid, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				if (response.responseText) {
					Control.Modal.close();
					Control.Modal.open(response.responseText);
					
					questResponse[itemid] = response.responseText;
				}
			}
		});
	} else {
		Control.Modal.close();
		Control.Modal.open(questResponse[itemid]);
	}
}

function getLogin() {
	var response = new Ajax.Request('includes/ajax_noload.php?do=getlogin', {
		asynchronous: true,
		method: 'get',
		onComplete: function(response) {
			if (response.responseText != null) {
				Control.Modal.open(response.responseText, {
					width: 500,
				});
			}
		}
	});	
}

//############POLL############
var poll = Class.create();

poll.prototype = {
	
	initialize: function(pollid, options) {
		
		this.pollid = pollid;
		
		this.options = {
			choices: new Array('yes', 'no'),
			choice: false
		};
		
		Object.extend(this.options, options || {});
	},
	
	choose: function(value) {
		var found = $A(this.options.choices).find(function(c) {
			return (c == value);
		});
		
		if (found != undefined) {
			this.options.choice = found;
		}
	},
	
	vote: function() {
		new Ajax.Request('includes/ajax_noload.php?do=vote&p='+this.pollid+'&v='+this.options.choice, {
			asynchronous: true,
			method: 'get',
			onComplete: function(response) {
				if (response.responseText) {
					Control.Modal.open(response.responseText);
				}
			}
		});		
	}
}

//#########ITEM CHOOSER#########
var ItemChooser = Class.create();

ItemChooser.prototype = {
	
	initialize: function(chooserid, options) {
		this.chosen				= new Array();
		this.contained			= new Array();
		this.id					= chooserid;
		this.icons				= new Array();

		this.options = {
			maxChosen: 	-1,
			cf: 		Prototype.emptyFunction,
			onLoad: 	Prototype.emptyFunction,
			mode: 		false,
			chooserdiv:	document.createElement('div'),
			action:		false
		};
		
		Object.extend(this.options, options || {});
		
		this.mode 		= this.options.mode;
		this.cf			= this.options.cf;
		this.onLoad		= this.options.ol;
		this.chooserdiv	= this.options.chooserdiv;
		
		this.chooserdiv.setAttribute('align', 'center');
		
		if (!this.options.action) {
			this.action = 'includes/ajax_inventory.php?do=itemselect&id='+this.id+'&mode='+this.mode;
		} else {
			this.action = this.options.action;
		}
	},
	
	show: function(preload) {
		this.reset();
		
		var chooserdiv 	= this.chooserdiv;
		var close 		= false;
		var id			= this.id;
		var onLoad		= this.onLoad;
		var me			= this;
		
		if (!chooserdiv.innerHTML) {
			new Ajax.Request(this.action, {
				asynchronous: true,
				method: 'get',
				onComplete: function(response) {
					chooserdiv.innerHTML = response.responseText;
					
					if (!preload) {
						if (!response.responseText) {
							chooserdiv.innerHTML = 'There are no items to select.';
							var close = true;
						}
						
						Control.Modal.open(chooserdiv.innerHTML, {
							zIndex: 9996,
							overlayCloseOnClick: close,
							imageCloseOnClick: close,
							loading: true
						});
					}
					
					if (onLoad != undefined) {
						onLoad();
					}
					
					me.icons = $A(chooserdiv.getElementsByTagName('img'));
				}
			});
		} else {
			if (!preload) {
				if (chooserdiv.getElementsByTagName('img').length == 0) {
					chooserdiv.innerHTML = 'There is nothing to select.';
					var close = true;
				}
				
				Control.Modal.open(chooserdiv.innerHTML, {
					zIndex: 9996,
					overlayCloseOnClick: close,
					imageCloseOnClick: close
				});
			}
		}
	},
	
	choose: function(element) {	
		var id 			= this.id;
		var chosen 		= $A(this.chosen).compact();
		var thisicon	= $(element);
		
		if (thisicon != undefined) {
			var thisitem = this.chosen.find( function(item) {
				return (item.getAttribute('id') == thisicon.getAttribute('id'));
			});
			
			if (thisitem != false && thisitem != undefined) {
				chosen.each( function(i, key) {
					if (typeof i != undefined && i != undefined) {
						if (i.hasAttribute('id')) {
							if (i.getAttribute('id') == thisicon.getAttribute('id')) {
								chosen.splice(key, 1);
							}
						}
					}
				});
				
				thisicon.style.border = '2px solid transparent';
				
				this.chosen = chosen;
			} else {
				if (chosen.length >= this.options.maxChosen && this.options.maxChosen != -1) {
					var lastelem = this.chosen.pop();
					
					lastelem.style.border = '2px solid transparent';
				}
				
				this.chosen.push(thisicon);
					
				thisicon.style.border = '2px solid orange';
			}
		}
	},
	
	cancel: function() {
		this.reset();
		Control.Modal.close();
	},
	
	confirmfunction: function() {		
		if (this.cf != undefined) {
			this.cf();
		} else {
			this.confirm();
		}
	},
	
	confirm: function() {
		var chooserdiv 	= this.chooserdiv;
		var id			= this.id;
		var me			= this;
		
		if (this.chosen.length > 0) {
			this.chosen.each( function(item) {
				var icons = $A(chooserdiv.getElementsByTagName('img'));
				
				var icon = icons.find( function(i) {
					return (i.getAttribute('id') == item.getAttribute('id'));
				});

				icons.each( function(i, key) {
					if (i.getAttribute('id') == icon.getAttribute('id')) {
						chooserdiv.removeChild(icon);
					}
				});
			});
			
			this.icons = $A(chooserdiv.getElementsByTagName('img'));
			
			Control.Modal.close();
			this.reset();
		} else {
			Control.Modal.open('Please select at least one item by clicking its icon.');
		}
	},
	
	getitems: function(field) {
		if (this.chosen.length > 0) {	
			var chosen = new Array();
			
			this.chosen.each( function(icon) {
				chosen.push(icon.getAttribute(field));		
			});
	
			return ($A(chosen).compact()).toString();
		} else {
			return false;
		}
	},
	
	reset: function() {
		this.chosen = new Array();
		
		if (this.icons.length > 0) {
			this.icons.each( function(icon, key) {
				if (icon.style) icon.style.border = '2px solid transparent';
			});
		}
	}
};