// Cache Engine version 1
(function () {
	if (window.google && google.gears) {
		return;
	}
	var factory = null;
	if (typeof GearsFactory != 'undefined') {
		factory = new GearsFactory();
	} else {
		try {
			factory = new ActiveXObject('Gears.Factory');
			if (factory.getBuildInfo().indexOf('ie_mobile') != -1) {
				factory.privateSetGlobalObject(this);
			}
		} catch(e) {
			if ((typeof navigator.mimeTypes != 'undefined') && navigator.mimeTypes["application/x-googlegears"]) {
				factory = document.createElement("object");
				factory.style.display = "none";
				factory.width = 0;
				factory.height = 0;
				factory.type = "application/x-googlegears";
				document.documentElement.appendChild(factory);
			}
		}
	}
	if (!factory) {
		return;
	}
	if (!window.google) {
		google = {};
	}
	if (!google.gears) {
		google.gears = {
			factory: factory
		};
	}
})();
var STORE_NAME = "ameoto_docset";
var MANIFEST_FILENAME = "/manifest.json";
var localServer;
var store;
var cookie;
var err;
// Called onload to initialize local server and store variables
function init() {
	if(!window.google || !google.gears){
		textOut("You must install Google Gears first.");
		ReverseDisplay('gears-enable');
	}else{
		cookie = getCookie("ameotoCache");
		try{
			localServer = google.gears.factory.create("beta.localserver","1.0")
		}
		catch(e){
			textOut(e);
			err = true;
		}
		if(cookie != version && cookie != "false" && err == undefined){
			localServer.removeManagedStore(STORE_NAME);
			store = localServer.createManagedStore(STORE_NAME);
			textOut("Google Gears is installed, Please wait until resources are cached...");
			createStore();
		}
	}
}
// Set Cookie
function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
// Get Cookie
function getCookie(c_name){
	if(document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if(c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if(c_end==-1){ c_end=document.cookie.length; }
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
// gtfo
function goAway(){
	var agree=confirm("Are you sure you don't want to use this cache? \n(Message will be displayed again in 190 days)");
	if(agree){
		setCookie("ameotoCache", "false", 190);
		ReverseDisplay('gears-enable');
	}
}
// Create the managed resource store
function createStore() {
	if (!window.google || !google.gears) {
		textOut("You must install Google Gears first.");
		return;
	}
	store.manifestUrl = MANIFEST_FILENAME;
	store.checkForUpdate();
	var timerId = window.setInterval(function() {
		if (store.currentVersion) {
			window.clearInterval(timerId);
			textOut("Scripts cached locally for faster access.");
			setCookie("ameotoCache", version, 30);
			window.setTimeout(function (){
				document.getElementById('gears-enable').style.display = "none";
			}, 500);
		}else if(store.updateStatus == 3) {
			textOut("Error: " + store.lastErrorMessage);
		}
	}, 500);  
}
// Remove the managed resource store.
function removeStore(){
	if (!window.google || !google.gears){
		alert("You must install Google Gears first.");
		return;
	}
	if(localServer != undefined){
		localServer.removeManagedStore(STORE_NAME);
	}
	textOut("The local store has been removed.");
	setCookie("ameotoCache", "0", 1);
}
// Utility function to output some status text.
function textOut(s) {
	var elm = document.getElementById("textOut");
	if(elm != null){
		while (elm.firstChild) {
			elm.removeChild(elm.firstChild);
		} 
		elm.appendChild(document.createTextNode(s));
	}
}
var actionsState = false;
var actionsSortState = false;
function actionsSort(){
	if(!actionsSortState){
		document.getElementById("moar-actions").style.display = "block";
		document.styleSheets[0].deleteRule(3);
		document.styleSheets[0].insertRule ("#actions ul li {display: list-item!important;}", 3)
		actionsSortState = true;
	}else{
		document.getElementById("moar-actions").style.display = "none";
		document.styleSheets[0].deleteRule(3);
		document.styleSheets[0].insertRule ("#actions ul li {display: block!important;}", 3)
		actionsSortState = false;
	}
}
function actions(){
	var selectedEffect = 'blind';
	var options = {};
	if(!actionsState){
		document.getElementById("docs").style.backgroundColor = "#369";
		document.getElementById("docs").style.color = "#FFF";
		$("#actions").toggle(selectedEffect,options,200);
		document.getElementById("actions").style.display = "block";
		actionsState = true;
		return false;
	}else{
		document.getElementById("docs").style.backgroundColor = null;
		document.getElementById("docs").style.color = null;
		$("#actions").toggle(selectedEffect,options,200);
		window.setTimeout(function (){
			document.getElementById("actions").style.display = "none";
		},200);
		actionsState = false;
		return false;
	}
}
var mouseIsOver = false;
function scrolled(){
	window.setTimeout(function (){
		if(actionsState == true && mouseIsOver == false){
			actions();
			//document.getElementById("actions").style.marginLeft = "20px";
			//document.getElementById("status").innerHTML = "tooth is watching you~";
		}
	},500);
}
var affilState = false;
function affil(){
	if(affilState){
		document.getElementById("id01_829818").style.display = "none";
		document.getElementById("mainContent").style.marginRight = "80px";
		document.getElementById("affil").style.right = "81px";
		affilState = false;
	}else{
		document.getElementById("id01_829818").style.display = "block";
		document.getElementById("mainContent").style.marginRight = "151px";
		document.getElementById("affil").style.right = "152px";
		affilState = true;
	}
}
$(document).ready(function(){
	function runEffect(id){
		var selectedEffect = 'blind';
		var options = {};
		$(id).toggle(selectedEffect,options,200);
	};
	cookie = getCookie("ameotoCache");
	if(cookie != ""){
		init();
	}else{
		setCookie("ameotoCache", "false", 190);
		ReverseDisplay('gears-enable');
	}
});