if (!String.prototype.rot13) {
	String.prototype.rot13 = function () {
		return this.replace(/[a-zA-Z]/g, function (c) {
			return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
		});
	};
}

function storedhref(inobj, place) {
	inobj.href = outhref[place].rot13();
}

function graphchange(graphtype, typeid) {
	formobj = document.forms["formgraph-" + graphtype + "-" + typeid];
	graphobj = document.getElementById("graph-" + graphtype + "-" + typeid);

	var pricetype = "bin";
	var range = "fortnight";

	for (counter = 0; counter < formobj.elements["pricetype"].length; counter++) {
		if (formobj.elements["pricetype"][counter].checked) {
			pricetype = formobj.elements["pricetype"][counter].value;
			break;
		}
	}
	for (counter = 0; counter < formobj.elements["range"].length; counter++) {
		if (formobj.elements["range"][counter].checked) {
			range = formobj.elements["range"][counter].value;
			break;
		}
	}

	graphobj.style.visibility = "hidden";
	graphobj.style.display = "none";
	graphobj.src = "/graph.php?t=" + graphtype + "&ti=" + typeid + "&pt=" + pricetype + "&r=" + range + "&s=small";
}

function graphshow(graphtype, typeid) {
	graphobj = document.getElementById("graph-" + graphtype + "-" + typeid);
	graphobj.style.visibility = "visible";
	graphobj.style.display = "block";
}

function imageshow() {
	$("gallerymenu").innerHTML = imagecounter + " of " + gallerylist.length;
	var myEffect = new Fx.Morph("gallerybox", {
		duration: 400,
		transition: Fx.Transitions.Sine.easeOut
	});
	myEffect.start({
		"height": gallerylist[imagecounter - 1]["height"],
		"width": gallerylist[imagecounter - 1]["width"]
	}).chain(function () {
		if (gallerylist[imagecounter - 1]["type"] == "image") {
			$("gallerymovie").setStyle("visibility", "hidden");
			$("gallerymovie").setStyle("display", "none");
			$("galleryimg").setStyle("visibility", "visible");
			$("galleryimg").setStyle("display", "block");
			$("galleryimg").fade("toggle");
		} else if (gallerylist[imagecounter - 1]["type"] == "movie") {
			$("galleryimg").setStyle("visibility", "hidden");
			$("galleryimg").setStyle("display", "none");
			$("gallerymovie").setStyle("visibility", "visible");
			$("gallerymovie").setStyle("display", "inline");
		}
	});
}

function imageload() {
	$("galleryimg").setStyle("visibility", "hidden");
	$("galleryimg").setStyle("display", "none");
	$("gallerymovie").setStyle("visibility", "hidden");
	$("gallerymovie").setStyle("display", "none");

	if (gallerylist[imagecounter - 1]["type"] == "image") {
		$("galleryimg").fade("toggle");
		$("galleryimg").src = "/images/" + gallerylist[imagecounter - 1]["src"] + ".jpg";
	} else if (gallerylist[imagecounter - 1]["type"] == "movie") {
		imageshow();
	}
}

function galleryclick(direction) {
	if (direction == "back") {
		if (imagecounter > 1) {
			imagecounter--;
			imageload();
		}
	} else if (direction == "forward") {
		if (imagecounter < gallerylist.length) {
			imagecounter++;
			imageload();
		}
	}

	if (imagecounter == 1) {
		$("galleryback").setStyle("opacity", .2);
	}
	if (imagecounter > 1) {
		$("galleryback").setStyle("opacity", 1);
	}
	if (imagecounter < gallerylist.length) {
		$("galleryforward").setStyle("opacity", 1);
	}
	if (imagecounter >= gallerylist.length) {
		$("galleryforward").setStyle("opacity", .2);
	}
}

function galleryinit() {
	if (gallerylist != null && gallerylist.length > 0) {
		imagecounter = 1;

		$("galleryback").setStyle("opacity", .2);
		if (gallerylist[imagecounter - 1]["type"] == "image") {
			$("galleryimg").src = "/images/" + gallerylist[imagecounter - 1]["src"] + ".jpg";
		}
		imageshow();

		$("galleryback").removeEvents("click");
		$("galleryback").addEvent("click", function (e) {
			e = new Event(e).stop();
			galleryclick("back");
		});

		$("galleryforward").removeEvents("click");
		$("galleryforward").addEvent("click", function (e) {
			e = new Event(e).stop();
			galleryclick("forward");
		});
	}
}




// chatbox


function chatlistredraw(chatlist) {
	var chatboxdiv = $("chatboxdiv");
	chatboxdiv.empty();

	if (chatlist["chatbox"].length > 0) {
		chatlist["chatbox"].each(function (details) {
			var chatentry = new Element("div", {
				"class": "preview"
			});

			var chattop = new Element("div", {
				"class": "previewtop"
			}).inject(chatentry);
			var chatnum = new Element("span", {
				"class": "chatnum",
				"html": "#" + details["chatid"]
			}).inject(chattop);
			var username = new Element("h3", {
				"html": details["username"]
			}).inject(chattop);
			var chatrank = new Element("img", {
				"class": "chatrank",
				"src": "/images/chatbox/" + details["chatrank"] + ".png"
			}).inject(username);
			if (details["level"] > 1) {
				username.setStyle("color", "#930");
			}

			var localdatetime = new Element("span", {
				"class": "chatlocaldatetime",
				"html": details["localdatetime"]
			}).inject(chattop);
			var datetime = new Element("span", {
				"class": "chatdatetime",
				"html": details["datetime"]
			}).inject(chattop);
			var continent = new Element("img", {
				"src": "/images/continents/" + details["continent"] + ".png",
				"title": details["countrycode"]
			}).inject(chattop);

			var chatbottom = new Element("div", {
				"class": "previewbottom",
				"id": details["chatid"] + "-" + details["datetime"]
			}).inject(chatentry);
			var content = new Element("span", {
				"html": details["content"]
			}).inject(chatbottom);

			chatentry.inject(chatboxdiv);
		});
	} else {
		var chatentry = new Element("div", {
			"class": "preview"
		});
		var chattop = new Element("div", {
			"class": "previewtop"
		}).inject(chatentry);
		var chatbottom = new Element("div", {
			"class": "previewbottom"
		}).inject(chatentry);

		var datetime = new Element("span", {
			"html": ""
		}).inject(chattop);
		var username = new Element("h3", {
			"html": "No chat available"
		}).inject(chattop);
		username.setStyle("color", "#930");

		var content = new Element("span", {
			"html": ""
		}).inject(chatbottom);

		chatentry.inject(chatboxdiv);
	}
}


function chatboxupdate(withsend) {
	var senddata = new Object();
	senddata["action"] = "chatboxget";

	if (withsend == true) {
		var formobject = document.forms["chatboxsubmit"].elements;
		senddata["content"] = formobject["content"].value;

		var browsertime = new Date();
		var fullyear = browsertime.getFullYear();
		var month = browsertime.getMonth() + 1;
		if (month < 9) { month = "0" + month; }
		var day = browsertime.getDate() < 9 ? "0" + browsertime.getDate() : browsertime.getDate();
		var hour = browsertime.getHours() < 9 ? "0" + browsertime.getHours() : browsertime.getHours();
		var minute = browsertime.getMinutes() < 9 ? "0" + browsertime.getMinutes() : browsertime.getMinutes();
		var second = browsertime.getSeconds() < 9 ? "0" + browsertime.getSeconds() : browsertime.getSeconds();
		senddata["localtime"] = fullyear + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

		formobject["content"].disabled = true;
		formobject["submit"].value = "Wait";
		formobject["submit"].disabled = true;
		clearTimeout(chatboxtimeout);
		chatboxreload(30, 40);
	}

	var ajaxrequest = new Request({
		url: "/ajax/gateway.php",
		data: senddata,
		onSuccess: function (responseText) {
			chatlistredraw(JSON.decode(responseText));
			//			chatlistglow();
			if (withsend == true) {
				formobject["content"].disabled = false;
				formobject["content"].value = "";
				formobject["submit"].disabled = false;
				formobject["submit"].value = "Chat";
			}
		},
		onFailure: function () {
			alert("Chatbox Failure: Please try again later");
		}
	}).send();
}

function chatboxreload(reloadsec, reloadtimes) {
	if (reloadtimes > 0) {
		if (reloadsec <= 10 || reloadsec >= 19) {
			if (reloadsec == 19) {
				$("chatboxtimer").set("html", "&nbsp;");
			} else {
				$("chatboxtimer").set("html", "(Chat refresh in " + reloadsec + "sec)");
			}
		}

		if (reloadsec <= 0) {
			chatboxupdate(false);
			reloadsec += 30;
			reloadtimes--;
		}
		reloadsec--;
		chatboxtimeout = setTimeout("chatboxreload(" + reloadsec + "," + reloadtimes + ")", 1000);
	} else {
		$("chatboxtimer").set("html", "<a href=\"#\" onclick=\"chatboxreload(3,40); return false;\">Restart Auto Refresh</a>");
	}
}

function chatlistglow() {
	$$("div#chatboxdiv div.preview div.previewbottom").each(function (element) {
		var idarray = element.id.split("-");
		if (isFinite(idarray[1]) && idarray[1] > visitprev) {
			var effect = new Fx.Tween(element);
			var periodical;
			var fx = function () {
				effect.start('background-color', '#f8f8f8').chain(function () {
					effect.start('background-color', '#efefef');
				});
			}
			fx();
			periodical = fx.periodical(1700);
		}
	});
}

window.addEvent("domready", function () {
	if (pagelocation == "lootgame" || pagelocation == "lootcard") {
		galleryinit();
	} else if (pagelocation == "chatbox") {
		// chatlistglow();
		chatboxreload(30, 40);
	}

	$$("div.ltip").each(function (element, index) {
		var sellerid = element.get("rel");
		if (typeof sellerid != "undefined") {
			var details = outsellers[sellerid];
			if (typeof details != "undefined") {
				if (details["partner"] == "ebay") {
					element.store("tip:title", "<strong>" + details["userid"] + "</strong> ( " + details["fbscore"] + " " + (details["fbratingstar"] != "none" ? "<img style=\"background: none; border: none; vertical-align: middle; padding: 0px;\" src=\"/images/fbicons/" + details["fbratingstar"] + ".png\" />" : "") + "<img style=\"background: none; border: none; vertical-align: middle; padding: 0 0 0 5px;\" src=\"/images/icons/small_ebay.png\" /> )");
					element.store("tip:text", "Positive feedback: " + details["fbpercent"] + "%" + (details["userstore"] ? "<br />eBay Store: <strong>" + details["userstore"] + "</strong>" : ""));
				} else if (details["partner"] == "merchant") {
					element.store("tip:title", "<strong>WoWTCGLoot.com</strong> ( <img style=\"background: none; border: none; vertical-align: middle; padding: 0px;\" src=\"/images/icons/pp.gif\" /> )");
					element.store("tip:text", "Add to cart.");
				} else if (details["partner"] == "amazon") {
					element.store("tip:title", "<strong>" + details["userid"] + "</strong> ( " + details["fbscore"] + " " + (details["fbratingstar"] != "none" ? "<img style=\"background: none; border: none; vertical-align: middle; padding: 0px;\" src=\"/images/icons/small_amazon.png\" />" : "") + " )");
					element.store("tip:text", "Positive feedback: " + details["fbpercent"] + " / 5.0");
				}
			}
		}
	});

	var mytips = new Tips("div.ltip", {
		className: "ltip",
		offsets: {
			"x": 80,
			"y": 16
		},
		fixed: true,
		hideDelay: 50,
		showDelay: 50
	});
});