/*
JS Functions for Reign of Shadow ItemDB
last changed 11-05-2010
*/

function switchLanguage(code) {
	var date = new Date();
	date = new Date(date.getTime() + 365 * 86400000);
	document.cookie = "idb_language=" + code + "; expires=" + date.toGMTString() + "; path=/";
	location.reload();
}

function toggleCubeLink(id,type) {
	div = document.getElementById("cubelink");
	if(div.style.display == "block") {
		div.style.display = "none";
	} else {
		div.style.display = "block";
		div.innerHTML = 'DO NOT MESS WITH THIS UNLESS YOU KNOW WHAT YOU ARE DOING!<br />\n<form action="' + idb_path + 'info/" method="post"><label for="infotype">Typ:</label>\n<select name="infoType" id="infotype"><option value="-1" selected="selected">' + idb_lang_select + '</option><option value="cuberecipe">cube recipe</option>\n<option value="cubeclass">recipe class</option>\n<option value="cubesubclass">recipe sub-class</option>\n</select>\n<label for="infoid">ID:</label> \n<input type="text" name="infoText" id="infoid" size="5" />\n<input type="submit" name="submit" value="OK" />\n<input type="hidden" name="itemid" value="' + id + '" />\n<input type="hidden" name="itemtype" value="' + type + '" /></form>';
	}
}

function toggleInfo(id) {
	div = document.getElementById(id);
	if(div.style.display == "block") {
		div.style.display = "none";
	} else {
		div.style.display = "block";
	}
}

/*source: http://bytes.com/topic/javascript/answers/148568-css-javascript-find-absolute-position-element */
function getElementPosition(elem){
	var offsetTrail = elem;
	var offsetLeft = offsetTrail.offsetLeft;
	var offsetTop = offsetTrail.offsetTop;
	offsetTrail = offsetTrail.offsetParent;
	while (offsetTrail){
		//offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft,top:offsetTop};
}

var http_request = false;

function getXMLData(url, outFunc) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Error receiving form data (1)...');
		return false;
	}
	eval('http_request.onreadystatechange = ' + outFunc);
	http_request.open('GET', url, true);
	http_request.send(null);
}

function getTXTData(url, outFunc) {
	createAJAXRequest(url, outFunc, 'text/txt');
}

function getXMLData(url, outFunc) {
	createAJAXRequest(url, outFunc, 'text/xml');
}

function createAJAXRequest(url, outFunc, mimetype) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType(mimetype);
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Error receiving form data (1)...');
		return false;
	}
	eval('http_request.onreadystatechange = ' + outFunc);
	http_request.open('GET', url, true);
	http_request.send(null);
}

function createPostAJAXRequest(url, outFunc, mimetype, poststr) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType(mimetype);
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Error receiving form data (1)...');
		return false;
	}
	
	eval('http_request.onreadystatechange = ' + outFunc);
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", poststr.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(poststr);
}

function autocomplete(id,loc) {
	inp = document.getElementById(id);
	w1 = document.getElementById('where1').value;
	w2 = document.getElementById('where2').value;
	getXMLData('/itemdb/' + loc + '?ac=1&w=' + w1 + '&l=' + w2 + '&text=' + inp.value, displayAutoComplete);
}

function displayAutoComplete() {
	var div = document.getElementById('autocomplete');
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var doc = http_request.responseText;
			div.innerHTML = doc;
			div.style.display = "block";
		} else {
			alert('Error receiving form data (2)...');
		}
	}
}

function setPageContent() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById("page_content").innerHTML = http_request.responseText;
		} else {
			alert('Error receiving form data (2)...');
		}
	}
}

function setItemInfoPosition(caller) {
	infoElem = document.getElementById("itemInfo");
	infoElem.style.display = "block";
	var x = 0;
	var y = 0;

	var main = document.getElementById("main");


	x = Math.round(getElementPosition(caller).left - getElementPosition(main).left - infoElem.clientWidth/2 + 24);
	if(x < 0 && getElementPosition(main).left + x < 0) {
		x = - getElementPosition(main).left;
	}

	/* move it to the top if the screen is too small */
	if((infoElem.clientHeight + getElementPosition(caller).top + 53) > window.innerHeight - 15) {
		y = Math.round(getElementPosition(caller).top - infoElem.clientHeight - 5);
	} else {
		y = Math.round(getElementPosition(caller).top + 53);
	}
	
	/* move it to the right if it's over the top ^^ */
	if(y < 0) {
		y = Math.round(getElementPosition(caller).top - (infoElem.clientHeight / 2));
		x = getElementPosition(caller).left - getElementPosition(main).left + 50;
	}
	
	infoElem.style.left = x + "px";
	infoElem.style.top = y + "px";
	infoElem.style.position = "absolute";
}

/* comments */
function getCommentPage(num, id) {
	createPostAJAXRequest("/itemdb/js/comments.php?action=view", updateComments, 'text/html', 'id=' + id + '&page=' + num);
}

function postComment(id) {
	var comment = document.getElementById('commentText').value;
	createPostAJAXRequest("/itemdb/js/comments.php?action=post", addComment, 'text/html', 'id=' + id + '&comment=' + encodeURIComponent(comment));
}

function addComment() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById("commentText").innerHTML = "";
			document.getElementById("commentText").value = "";
			document.getElementById("comments").innerHTML = http_request.responseText + document.getElementById("comments").innerHTML;
		}
	}
}

function updateComments() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById("comments").innerHTML = http_request.responseText;
		}
	}
}

// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;}
function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;}                    
