var RecaptchaOptions = {
		theme : 'blackglass'
};

$(document).ready(function(){
	loadStyle();

	$("#form").submit(function() {
		hideForm();
		return true;
	});

	$('#bbcode').markItUp(settings);
	$(".ajax").hide();
	$(".ajax2").hide();

	if($("#accounts").length > 0) {
		setInterval("updateAccounts()", 500);
	}
});

function newAccountName() {
	// $("#row").hide();

	$.post("/lib/ajax/accountname.php",
			{
			"type": $("#type").val()
			},
			function(data) {
				if(data.response != 5) {
					return;
				}

				// $("#accid").html("#" + data.id);
				// $("#accname").html(data.name);
				$("#ajaxtable > tbody:last").append("<tr> <td id=\"accid\">#" + data.id + "</td> <td id=\"accname\"><b>" + data.name + "</b></td> </tr>");
			}, 'json');
}

function resetAccountNames() {
	$("#ajaxtable tbody tr:not(:first-child)").remove();
}

function updateAccounts() {
	$("#accounts").load("/lib/ajax/accounts.php?type=" + $("#type").val() );
}

function confirmDelete(url) {
	if(confirm("You are about to DELETE a thread or post. Are you sure?")) {
		document.location = url;
	}
}

function toggle(id) {
	for(var i=0; i<100; i++) {
		var i_ = document.getElementById(id + "_" + i);
		if(i_) {
			i_.className = (i_.className == 'hidden') ? 'unhidden' : 'hidden';
		}
	}
}

function showKillSwitchHelp() {
	TINY.box.show('Activating the Kill switch will force any currently running scripts to <b>stop</b> safely the next time they update. <br/> <br/> The only way to disable the kill switch is to either start a script or manually disable it.', 0, 350, 0, 1, 0);
}

function postPreview() {
	$(".ajax").hide(750);

	$.post("/lib/ajax/postpreview.php",
			{
		"subject": $("#subject").val(),
		"message": $("#bbcode").val(),
		"hide": $("#hide").is(":checked") ? "1" : "0",
				"uid": $("#uid").val()
			},
			function(data) {
				$(".ajax").html(data.response);

				$(".ajax").addClass("main");
				$(".ajax").show(1000);
			}, 'json');
}

function postPreview2() {
	$(".ajax").addClass("main");
	$("#main").hide(1000);
	$(".ajax").show(1000);
}

function resetPreview() {
	$(".ajax").hide(1000, function() {
		$(".ajax").removeClass("main");
		$("#main").show();
	});
}

function loadStats() {
	doLoading("#stats", "Note: May take up to 30 seconds to load depending on how many runs you have");

	$.post("loadstats.php", 
			{},
			function(data) {
				var response = data.response;

				$("#stats").html(response + "<br/><a onClick=\"loadStats()\">Refresh</a>");
			}, 'json');
}

function hideForm() {
	doLoading("#form_info", "Now hiding your runs..");
	switchClasses("#form", "hidden", "unhidden");
	switchClasses("#form_info", "info", "hidden");
}

function doLoading(id, message) {
	$(id).html("<img src=\"https://www.rs2reborn.net/images/loading.gif\" style=\"background:none;border:none; margin-left:5%;\" /> <br/> <p>" + message + "</p>");
}

function ajax(id, type) {
	$.post("check.php",
			{"u": $("#user").val(), "t": type },
			function(data) {
				var response = data.response; 

				if(response == 5) {
					switchClasses(id, "good", "error");
					// $("#usernotify").html("");
				} else {
					switchClasses(id, "error", "good");
				}
			}, 'json');

	if($(id).hasClass("success")) {
		return true;
	} else {
		return false;
	}
}

function switchClasses(object, switchTo, switchFrom) {
	if($(object).hasClass(switchFrom)) {
		$(object).removeClass(switchFrom);
	}
	$(object).addClass(switchTo);
}

var cookieName = "style";

function switchStyle(css) {
	$("#css").attr("href", "https://www.rs2reborn.net/css/" + css + ".css");
	setCookie(cookieName, css, 30);
}

function loadStyle() {
	var css = getCookie(cookieName);

	if (css.length > 0) {
		switchStyle(css);
	}
}

function setCookie(cookieName, cookieValue, nDays) {
	
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)
	+ ";expires="+expire.toGMTString() + ";path=/";
}

function getCookie(name) {
	var theCookies = document.cookie.split(/[; ]+/);
	for (var i = 0 ; i < theCookies.length; i++) {
		var aName = theCookies[i].substring(0,theCookies[i].indexOf('='));
		if (aName == name) {
			var str = theCookies[i].replace("style%3D", "");

			return str.substring(str.indexOf('=') + 1, str.length);
		}
	}
}

var TINY={};function T$(i){return document.getElementById(i)}TINY.box=function(){var p,m,b,fn,ic,iu,iw,ih,ia,f=0;return{show:function(c,u,w,h,a,t){if(!f){p=document.createElement('div');p.id='tinybox';m=document.createElement('div');m.id='tinymask';b=document.createElement('div');b.id='tinycontent';document.body.appendChild(m);document.body.appendChild(p);p.appendChild(b);m.onclick=TINY.box.hide;window.onresize=TINY.box.resize;f=1}if(!a&&!u){p.style.width=w?w+'px':'auto';p.style.height=h?h+'px':'auto';p.style.backgroundImage='none';b.innerHTML=c}else{b.style.display='none';p.style.width=p.style.height='100px'}this.mask();ic=c;iu=u;iw=w;ih=h;ia=a;this.alpha(m,1,80,3);if(t){setTimeout(function(){TINY.box.hide()},1000*t)}},fill:function(c,u,w,h,a){if(u){p.style.backgroundImage='';var x=window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject('Microsoft.XMLHTTP');x.onreadystatechange=function(){if(x.readyState==4&&x.status==200){TINY.box.psh(x.responseText,w,h,a)}};x.open('GET',c,1);x.send(null)}else{this.psh(c,w,h,a)}},psh:function(c,w,h,a){if(a){if(!w||!h){var x=p.style.width,y=p.style.height;b.innerHTML=c;p.style.width=w?w+'px':'';p.style.height=h?h+'px':'';b.style.display='';w=parseInt(b.offsetWidth);h=parseInt(b.offsetHeight);b.style.display='none';p.style.width=x;p.style.height=y}else{b.innerHTML=c}this.size(p,w,h,4)}else{p.style.backgroundImage='none'}},hide:function(){TINY.box.alpha(p,-1,0,3)},resize:function(){TINY.box.pos();TINY.box.mask()},mask:function(){m.style.height=TINY.page.theight()+'px';m.style.width='';m.style.width=TINY.page.twidth()+'px'},pos:function(){var t=(TINY.page.height()/2)-(p.offsetHeight/2);t=t<10?10:t;p.style.top=(t+TINY.page.top())+'px';p.style.left=(TINY.page.width()/2)-(p.offsetWidth/2)+'px'},alpha:function(e,d,a,s){clearInterval(e.ai);if(d==1){e.style.opacity=0;e.style.filter='alpha(opacity=0)';e.style.display='block';this.pos()}e.ai=setInterval(function(){TINY.box.twalpha(e,a,d,s)},20)},twalpha:function(e,a,d,s){var o=Math.round(e.style.opacity*100);if(o==a){clearInterval(e.ai);if(d==-1){e.style.display='none';e==p?TINY.box.alpha(m,-1,0,2):b.innerHTML=p.style.backgroundImage=''}else{e==m?this.alpha(p,1,100,5):TINY.box.fill(ic,iu,iw,ih,ia)}}else{var n=o+Math.ceil(Math.abs(a-o)/s)*d;e.style.opacity=n/100;e.style.filter='alpha(opacity='+n+')'}},size:function(e,w,h,s){e=typeof e=='object'?e:T$(e);clearInterval(e.si);var ow=e.offsetWidth,oh=e.offsetHeight,wo=ow-parseInt(e.style.width),ho=oh-parseInt(e.style.height);var wd=ow-wo>w?-1:1,hd=(oh-ho>h)?-1:1;e.si=setInterval(function(){TINY.box.twsize(e,w,wo,wd,h,ho,hd,s)},20)},twsize:function(e,w,wo,wd,h,ho,hd,s){var ow=e.offsetWidth-wo,oh=e.offsetHeight-ho;if(ow==w&&oh==h){clearInterval(e.si);p.style.backgroundImage='none';b.style.display='block'}else{if(ow!=w){e.style.width=ow+(Math.ceil(Math.abs(w-ow)/s)*wd)+'px'}if(oh!=h){e.style.height=oh+(Math.ceil(Math.abs(h-oh)/s)*hd)+'px'}this.pos()}}}}();TINY.page=function(){return{top:function(){return document.body.scrollTop||document.documentElement.scrollTop},width:function(){return self.innerWidth||document.documentElement.clientWidth},height:function(){return self.innerHeight||document.documentElement.clientHeight},theight:function(){var d=document,b=d.body,e=d.documentElement;return Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight))},twidth:function(){var d=document,b=d.body,e=d.documentElement;return Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))}}}();