var currentPage  = null;
var currentVerse = null;
var login        = null;
var noVerseAnim  = false;
var queries      = null;

function initPage () {
	checkLogin();
	if (checkLogin() == true) {
		initHome();
	}
	else {
		initBible();
	}
}

function closeAll (otherWay) {
	$("#home").setAttributes({href:"javascript:goHome()"});
	$("#home").removeClass("back");

	scrollTo(0, 1);
	
	//btn
	$("#help").removeClass("active");
	
	if ($("#bibleDiv")) {
		$("#bibleDiv").move(null,(otherWay ? ($("#iphone").offsetLeft-$("#bibleDiv").offsetWidth) : $("#bibleDiv").offsetWidth),5);
		animate.setEvent("$('#bibleDiv').parentNode.removeChild($('#bibleDiv'))",5);
	}
	else if ($("#shareDiv")) {
		$("#shareDiv").move(null,(otherWay ? 400 : -400),5);
		animate.setEvent("$('#shareDiv').parentNode.removeChild($('#shareDiv'))",5);
	}
	else if ($("#historyDiv")) {
		var toLeft  = ($("#iphone").offsetLeft-$("#historyDiv").offsetWidth);
		var toRight = $("#historyDiv").offsetWidth;
		
		$("#historyDiv").move(null,(otherWay ? toLeft : toRight),5);
		animate.setEvent("$('#historyDiv').parentNode.removeChild($('#historyDiv'))",5);
	}
	else if ($("#helpDiv")) {
		$("#helpDiv").move(null,(400),5);
		animate.setEvent("$('#helpDiv').parentNode.removeChild($('#helpDiv'))",5);
	}
	else if ($("#settingsDiv")) {
		$("#settingsDiv").move(null,(otherWay ? ($("#iphone").offsetLeft-$("#settingsDiv").offsetWidth) : $("#settingsDiv").offsetWidth),5);
		animate.setEvent("$('#settingsDiv').parentNode.removeChild($('#settingsDiv'))",5);
	}
	else if ($("#logoutDiv")) {
		$("#logoutDiv").move(null,(otherWay ? ($("#iphone").offsetLeft-$("#logoutDiv").offsetWidth) : $("#logoutDiv").offsetWidth),5);
		animate.setEvent("$('#logoutDiv').parentNode.removeChild($('#logoutDiv'))",5);
	}
	else if ($("#signUpDiv")) {
		$("#signUpDiv").move(null,(otherWay ? ($("#iphone").offsetLeft-$("#signUpDiv").offsetWidth) : $("#signUpDiv").offsetWidth),5);
		animate.setEvent("$('#signUpDiv').parentNode.removeChild($('#signUpDiv'))",5);
	}
	else if ($("#loginDiv")) {
		$("#loginDiv").move(null,(otherWay ? ($("#iphone").offsetLeft-$("#loginDiv").offsetWidth) : $("#loginDiv").offsetWidth),5);
		animate.setEvent("$('#loginDiv').parentNode.removeChild($('#loginDiv'))",5);
	}
}

function closeHome () {
	//btn
	$("#home").removeClass("active");
	
	if ($("#homeDiv")) $("#homeDiv").move(null,($("#iphone").offsetLeft-$("#homeDiv").offsetWidth),5);
	if ($("#homeDiv")) animate.setEvent("$('#homeDiv').parentNode.removeChild($('#homeDiv'))",5);
}

function checkLogin () {
	if (getCookie("userEmail") != "" && getCookie("userEmail") != null) {
		login = new Array(getCookie("userID"),getCookie("userEmail"),getCookie("userPassword"));
		return true;
	}
	else {
		login = null;
		return false;
	}
}

function goHome () {
	checkLogin();
	if (currentPage != "home") {
		closeAll();
		initHome();
	}
}
function goBible () {
	if (currentPage != "bible") {
		closeAll(currentPage);
		if (currentPage != "history" || currentPage != "help" || currentPage != "shareVerse") closeHome();
		if (currentPage == "help" || currentPage == "shareVerse") { initBible(true); }
		else { initBible(false); }
	}
}
function goHistory () {
	if (currentPage != "history") {
		closeAll();
		closeHome();
		if (currentPage == "help") { initHistory(true); }
		else { initHistory(false); }
	}
}
function goHelp () {
	queries = null;
	if (currentPage != "help") {
		closeAll(currentPage);
		if (currentPage == "home") closeHome();
		initHelp();
	}
}
function goSettings () {
	if (currentPage != "settings") {
		closeAll();
		closeHome();
		if (currentPage == "help") { initSettings(true); }
		else { initSettings(false); }
	}
}
function goSignUp () {
	if (currentPage != "signup") {
		closeAll();
		closeHome();
		if (currentPage == "help") { initSignUp(true); }
		else { initSignUp(false); }
	}
}
function goLogin () {
	if (currentPage != "login") {
		closeAll();
		closeHome();
		if (currentPage == "help") { initLogin(true); }
		else { initLogin(false); }
	}
}
function goLogout () {
	if (currentPage != "logout") {
		closeAll();
		closeHome();
		initBible();
	}
}

function doLogin () {
	if ($("#emailInput") && $("#passwordInput")) {
		var emailInput    = $("#emailInput").value;
		var passwordInput = $("#passwordInput").value;
		
		fishing({method: "GET", url: "/ajax.php?action=login&email="+emailInput+"&password="+passwordInput, onready: processLogin});
	}
}

function doLogout () {
	document.cookie = "userID=";
	document.cookie = "userEmail=";
	document.cookie = "userPassword=";
	
	goBible();
}

function processLogin (request) {
	if (request.responseXML) {
		var data         = request.responseXML;
		var userID       = data.getElementsByTagName("login")[0].getAttribute("id");
		var userEmail    = data.getElementsByTagName("login")[0].getAttribute("name");
		var userPassword = data.getElementsByTagName("login")[0].getAttribute("password");
		document.cookie  = "userID="+userID;
		document.cookie  = "userEmail="+userEmail;
		document.cookie  = "userPassword="+userPassword;
		goHome();
	}
}

function initHome () {
	currentPage    = "home";
	
	//btn
	$("#home").addClass("active");
	
	//home
	var homeDiv    = dom.create("div",{id: "homeDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});
	
	//your tools header
	var h2Tools    = dom.create("h2", null, "Your Tools:");
	
	//tools list
	var toolsUL    = dom.create("ul", {id: "toolsUL"});
	var bibleSearch= dom.create("li", null, dom.create("a", {id: (login ? "bibleLinkLogged" : "bibleLink"), className: "buttonAlone", href: "javascript:goBible()"}, "Bible Search"));
	toolsUL.appendChild(bibleSearch);
	
	if (login) {
		//var bibleBookmarks = dom.create("li", null, dom.create("a", {id: "bookmarksLink", href: "javascript:goBookmarks()"}, "Bookmarks"));
		var bibleHistory   = dom.create("li", null, dom.create("a", {id: "historyLink", href: "javascript:goHistory()"}, "History"));
		//toolsUL.appendChild(bibleBookmarks);
		toolsUL.appendChild(bibleHistory);
	}
	
	//your account header
	var h2Account  = dom.create("h2", null, "Your Account:");
	
	//account list
	var accountUL  = dom.create("ul", {id: "accountUL"});
	
	if (login) {
		var liSettings = dom.create("li", null, dom.create("a", {id: "settingsLink", className: "buttonTop", href: "javascript:goSettings()"}, "Settings"));
		var liLogout   = dom.create("li", null, dom.create("a", {id: "logoutLink", className: "buttonBottom", href: "javascript:doLogout()"}, "Logout"));
	
		accountUL.appendChild(liSettings);
		accountUL.appendChild(liLogout);
	}
	else {
		var liSignUp   = dom.create("li", null, dom.create("a", {id: "signupLink", className: "buttonTop", href: "javascript:goSignUp()"}, "Sign-up"));
		var liLogin    = dom.create("li", null, dom.create("a", {id: "loginLink", className: "buttonBottom", href: "javascript:goLogin()"}, "Login"));
	
		accountUL.appendChild(liSignUp);
		accountUL.appendChild(liLogin);
	}
	
	//finish div
	homeDiv.appendChild(h2Tools);
	homeDiv.appendChild(toolsUL);
	homeDiv.appendChild(h2Account);
	homeDiv.appendChild(accountUL);
	
	//slideIn
	$("#iphone").appendChild(homeDiv);
	
	var finalDest  = homeDiv.offsetLeft;
	dom.addAnimate(homeDiv);
	homeDiv.setStyles({position: "absolute", left: ($("#iphone").offsetLeft-homeDiv.offsetWidth)+"px"});
	homeDiv.move(null,0,5);
}

function initBible (otherWay) {
	var otherWay = otherWay ? otherWay : false;
	
	currentPage  = "bible";
	//bible
	var bibleDiv = dom.create("div",{id: "bibleDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});

	//form
	var form  = dom.create("form",{id: "searchBible"});
	
	var input = dom.create("input",{type: "text", name: "bibleInput", id: "bibleInput", value: "John 3:16"});
	form.appendChild(input);
	
	var btn   = dom.create("a", {href: "javascript:loadVerse()"}, dom.create("img", {src: "/bible/images/searchbtn.png", alt: "search", id: "searchBtn"}));
	form.appendChild(btn);
	
	var span  = dom.create("span", {id: "bibleLabel"}, "(type in a verse to look up)");
	form.appendChild(span);
	
	//intro
	var intro = dom.create("div", {id: "bibleIntro"}, "<p>3onesix Ministry Tools Bible Search brings the Bible to your iPhone. All you need to do is type in a Bible verse above and click search to pull it up.</p><p>If you wish to store your search history, have Bible bookmarks (coming soon), and share verses with friends, sign-up for an account (for free) or login with your Prayrbox.com account.</p>");
	
	//finish div
	bibleDiv.appendChild(form);
	bibleDiv.appendChild(intro);
	
	form.onsubmit = function () {
		loadVerse();
		return false;
	};
	
	//slideIn
	$("#iphone").appendChild(bibleDiv);
	
	var finalDest  = bibleDiv.offsetTop;
	dom.addAnimate(bibleDiv);
	bibleDiv.setStyles({position: "absolute", left: (otherWay ? ($("#iphone").offsetLeft-bibleDiv.offsetWidth) : bibleDiv.offsetWidth)+"px"});
	bibleDiv.move(null,0,5);
	
	if (currentVerse) {
		$("#bibleInput").value = currentVerse;
		animate.setEvent("loadVerse('reload')",5);
	}
}

function initHistory (otherWay) {
	var otherWay = otherWay ? otherWay : false;
	
	currentPage    = "history";
	//bible history
	var historyDiv = dom.create("div",{id: "historyDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});
	
	//your tools header
	var h2History  = dom.create("h2", null, "Search History");
	historyDiv.appendChild(h2History);

	//create ul
	var historyUL  = dom.create("ul",{id: "historyUL"});
	historyDiv.appendChild(historyUL);
	
	//slideIn
	$("#iphone").appendChild(historyDiv);
	
	var finalDest  = historyDiv.offsetTop;
	dom.addAnimate(historyDiv);
	historyDiv.setStyles({position: "absolute", left: (otherWay ? ($("#iphone").offsetLeft-historyDiv.offsetWidth) : historyDiv.offsetWidth)+"px"});
	historyDiv.move(null,0,5);
	
	loadHistory();
}

function loadHistory () {
	fishing({method: "GET", url: "http://iphone.3onesix.com/ajax.php?action=getHistory", onready: processHistory});
}

function processHistory (request) {
	var data       = request.responseXML;
	var items      = data.getElementsByTagName("historyItem");
	var zebra      = false;
	for (var i=0; i<items.length; i++) {
		var date   = items[i].getAttribute("date");
		var verse  = items[i].firstChild.nodeValue;
		var li     = dom.create("li",{className: (zebra ? "zebra" : null)});
		var link   = dom.create("a",{href: "javascript:loadHistoryVerse('"+verse+"')"},verse);
		var span   = dom.create("span",{className: "date"},date);
		li.appendChild(link);
		li.appendChild(span);
		$("#historyUL").appendChild(li);
		
		zebra      = zebra ? false : true;
	}
}

function loadHistoryVerse (verse) {
	var verse      = verse;
	currentVerse   = verse;
	goBible();
	//loadVerse("reload");
}

function initHelp () {
	if (currentPage != "home") {
		switch (currentPage) {
			case "bible":
				$("#home").setAttributes({href:"javascript:goBible()", className: "back"});
				break;
			case "history":
				$("#home").setAttributes({href:"javascript:goHistory()", className: "back"});
				break;
			case "settings":
				$("#home").setAttributes({href:"javascript:goSettings()", className: "back"});
				break;
			case "login":
				$("#home").setAttributes({href:"javascript:goLogin()", className: "back"});
				break;
			default:
				$("#home").setAttributes({href:"javascript:goHome()", className: ""});
				break;
		}
	}

	currentPage    = "help";
	
	//btn
	$("#help").addClass("active");
	
	//bible history
	var helpDiv    = dom.create("div",{id: "helpDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});
	
	//your tools header
	var h2Help     = dom.create("h2", null, "Ministry Tools Help");
	helpDiv.appendChild(h2Help);

	//create ul
	var helpUL     = dom.create("ul",{id: "helpUL"});
	helpDiv.appendChild(helpUL);
	
	var array      = new Array(new Array("Translations: <span>(example: \"NIV: John 3:16\")</span>",new Array("NIV: New International Version","TNIV: Today's New International Version","KJV: King James Version","NKJV: New King James Version","NCV: New Century Version","NASB: New American Standard Bible","ItalRV: Italian Revised Version","ESV: English Standard Version","SpaRV: Spanish Reina-Valera Antigua","HCSB: Holman Christian Standard Bible","MSG: The Message")));
	var zebra      = false;
	
	for (var i=0; i<array.length; i++) {
		for (var j=0; j<array[i].length; j++) {
			if (j == 0) {
				var newLi = dom.create("li", {className: "header"}, array[i][j]);
				helpUL.appendChild(newLi);
			}
			else if (j > 0) {
				for (var k=0; k<array[i][j].length; k++) {
					var newLi = dom.create("li", {className: (zebra ? "zebra item" : "item")}, array[i][j][k]);
					helpUL.appendChild(newLi);
					zebra = zebra ? false : true;
				}
			}
			zebra = zebra ? false : true;
		}
	}
	
	//slideIn
	$("#iphone").appendChild(helpDiv);
	
	var finalDest  = helpDiv.offsetTop;
	dom.addAnimate(helpDiv);
	helpDiv.setStyles({position: "absolute", left: helpDiv.offsetWidth+"px"});
	helpDiv.move(null,0,5);
}

function initSignUp (otherWay) {
	var otherWay = otherWay ? otherWay : false;
	
	currentPage    = "signup";
	
	//bible history
	var signUpDiv    = dom.create("div",{id: "signUpDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});
	
	//your tools header
	var h2Sign     = dom.create("h2", null, "Sign-Up");
	signUpDiv.appendChild(h2Sign);
	
	//temp
	signUpDiv.create("p", null, "Temporarily, we do not have sign-up through this application. However, you can go over to <a href=\"http://prayrbox.com\">Prayrbox.com</a> and register for an account there, and use that username and password to login here.");
	
	//slideIn
	$("#iphone").appendChild(signUpDiv);
	
	var finalDest  = signUpDiv.offsetTop;
	dom.addAnimate(signUpDiv);
	signUpDiv.setStyles({position: "absolute", left: (otherWay ? ($("#iphone").offsetLeft-signUpDiv.offsetWidth) : signUpDiv.offsetWidth)+"px"});
	signUpDiv.move(null,0,5);
}

function initSettings (otherWay) {
	var otherWay = otherWay ? otherWay : false;
	
	currentPage    = "settings";
	
	//bible history
	var settingsDiv    = dom.create("div",{id: "settingsDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});
	
	//your tools header
	var h2Sign     = dom.create("h2", null, "Settings");
	settingsDiv.appendChild(h2Sign);
	
	//temp
	settingsDiv.create("p", null, "Settings are coming soon.");
	
	//slideIn
	$("#iphone").appendChild(settingsDiv);
	
	var finalDest  = settingsDiv.offsetTop;
	dom.addAnimate(settingsDiv);
	settingsDiv.setStyles({position: "absolute", left: (otherWay ? ($("#iphone").offsetLeft-settingsDiv.offsetWidth) : settingsDiv.offsetWidth)+"px"});
	settingsDiv.move(null,0,5);
}

function initLogin (otherWay) {
	var otherWay = otherWay ? otherWay : false;
	
	currentPage    = "login";
	//login
	var loginDiv   = dom.create("div",{id: "loginDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});

	//form
	var form       = dom.create("form",{id: "loginForm"});
	
	var h2Email    = dom.create("h2",null,"Email Address:");
	form.appendChild(h2Email)
	
	var input      = dom.create("input",{type: "text", name: "emailInput", id: "emailInput"});
	form.appendChild(input);
	
	var h2Password = dom.create("h2",null,"Password:");
	form.appendChild(h2Password);
	
	var input      = dom.create("input",{type: "password", name: "passwordInput", id: "passwordInput"});
	form.appendChild(input);
	
	var btn        = dom.create("a", {href: "javascript:doLogin()"}, dom.create("img", {src: "/bible/images/loginbtn.png", alt: "login", id: "loginBtn"}));
	form.appendChild(btn);
	
	//finish div
	loginDiv.appendChild(form);
	
	form.onsubmit  = function () {
		doLogin();
		return false;
	};
	
	//slideIn
	$("#iphone").appendChild(loginDiv);
	
	var finalDest  = loginDiv.offsetTop;
	dom.addAnimate(loginDiv);
	loginDiv.setStyles({position: "absolute", left: (otherWay ? ($("#iphone").offsetLeft-loginDiv.offsetWidth) : loginDiv.offsetWidth)+"px"});
	loginDiv.move(null,0,5);
}

function shareVerse (verse, translation) {
	closeAll(true);
	
	if (currentPage != "home") {
		switch (currentPage) {
			case "bible":
				$("#home").setAttributes({href:"javascript:goBible()", className: "back"});
				break;
			case "history":
				$("#home").setAttributes({href:"javascript:goHistory()", className: "back"});
				break;
			case "settings":
				$("#home").setAttributes({href:"javascript:goSettings()", className: "back"});
				break;
			case "login":
				$("#home").setAttributes({href:"javascript:goLogin()", className: "back"});
				break;
			default:
				$("#home").setAttributes({href:"javascript:goHome()", className: ""});
				break;
		}
	}
	
	currentPage    = "shareVerse";
	
	var query      = verse;
	var source     = translation;
	
	//get verse
	var url        = "http://www.ebible.com/api/ebibleSnippet?apikey=8ee1fc96f852d709d4617c8ad422cff5a63185dc";
	url           += "&callback=sharingText";
	url           += "&source="+source;
	url           += "&q="+escape(query);
	getScript(url);
	
	//build div
	var shareDiv   = dom.create("div",{id: "shareDiv", className: ($("#iphone").offsetWidth == 480 ? "wide" : null)});

	//form
	var form       = dom.create("form",{id: "shareForm"});
	
	var h2Email    = dom.create("h2",null,"From:");
	form.appendChild(h2Email);
	
	var input      = dom.create("input",{type: "text", name: "fromInput", id: "fromInput", value: login[1]});
	form.appendChild(input);
	
	var h2Email    = dom.create("h2",null,"To:");
	form.appendChild(h2Email);
	
	var input      = dom.create("input",{type: "text", name: "toInput", id: "toInput"});
	form.appendChild(input);
	
	var h2Text     = dom.create("h2",null,"Verse:");
	form.appendChild(h2Text);
	
	var text       = dom.create("textarea",{name: "verseInput", id: "verseInput"});
	form.appendChild(text);
	
	var btn        = dom.create("a", {href: "javascript:doShare()"}, dom.create("img", {src: "/bible/images/sharesubmit.png", alt: "submit", id: "submitBtn"}));
	form.appendChild(btn);
	
	//finish div
	shareDiv.appendChild(form);
	
	form.onsubmit  = function () {
		doShare();
		return false;
	};
	
	//slideIn
	$("#iphone").appendChild(shareDiv);
	
	dom.addAnimate(shareDiv);
	shareDiv.setStyles({position: "absolute", left: shareDiv.offsetWidth+"px"});
	shareDiv.move(null,0,5);
}

function sharingText (data) {
	if (data != null) {
		if (data[0].verse) {
			$("#verseInput").value = returnVerse(data[0].text);
		}
	}
}

function doShare () {
	var to      = $("#toInput").value;
	var from    = $("#fromInput").value;
	var message = $("#verseInput").value;
	
	fishing({method: "GET", url: "http://iphone.3onesix.com/ajax.php?action=shareVerse&to="+escape(to)+"&from="+escape(from)+"&message="+message, onready: returnShare});
}

function returnShare () {
	goHome();
}

function loadVerse (isset) {
	queries = null;

	if (isset) {
		noVerseAnim   = true;
	}
	if ($("#bibleInput")) {
		var bibleVerse = isset == "reload" && currentVerse ? currentVerse : $("#bibleInput").value;
	}
	else {
		return false;
	}
	var match = null;
	if (match = bibleVerse.match(/([a-zA-Z^:]+):(.+)/)) {
		var source = match[1];
		var query  = match[2];
	}
	else {
		var source = "NIV";
		var query  = bibleVerse;
	}
	
	if (getMultiQueries(query) != query) {
		queries = getMultiQueries(query);
		var query   = queries[0];
	}
	else {
		queries = null;
	}

	var url="http://www.ebible.com/api/ebibleSnippet?apikey=8ee1fc96f852d709d4617c8ad422cff5a63185dc";
	url  += "&callback=recievedText";
	url  += "&source="+source;
	url  += "&q="+escape(query);
	getScript(url);
	
	if (login) {
		if (!isset) {
			fishing({method: "GET", url: "/ajax.php?action=addHistory&verse="+bibleVerse});
		}
	}
	
	if ($("#loadedVerse") && !queries && !currentVerse) {
		$("#loadedVerse").parentNode.removeChild($("#loadedVerse"));
	}
	currentVerse = queries ? match ? match[2] : bibleVerse : query;
}
function getScript(url) {
	var scripttag = document.createElement("script");
	scripttag.setAttribute("type","text/javascript");
	scripttag.setAttribute("src",url);
	document.getElementsByTagName("head")[0].appendChild(scripttag);
}
function recievedText(data) {
	if (data != null) {
		if (queries) {
			if (queries[0] == data[0].verse) {
				if (data[0].text) {
					var newVerse = dom.create("div", {id: "loadedVerse", className: "bibleVerse"}, data[0].text);
					
					//Check for more link
					var spans = newVerse.getElementsByTagName("span");
					var more  = 0;
					more      = newVerse.getElementsByClassName("ebSnippet-more");
					
					if (more) {
						var verse     = data[0].verse;
						var verses    = newVerse.getElementsByClassName("verse");
						var lastVerse = verses.length-1;
						lastVerse     = verses[lastVerse].getElementsByClassName("verseNum")[0].firstChild.nodeValue;
					}
					
					//remove last br
					var brs = newVerse.getElementsByTagName("br");
					brs[(brs.length-1)].parentNode.removeChild(brs[(brs.length-1)]);
					
					if ($("#bibleIntro")) {
						$("#bibleDiv").removeChild($("#bibleIntro"));
					}
					if ($("#loadedVerse")) {
						$("#bibleDiv").removeChild($("#loadedVerse"));
					}
					$("#bibleDiv").appendChild(newVerse);
					
					if (login) {
						var shareVerse = dom.create("a",{href: "javascript:shareVerse('"+currentVerse+"','"+data[0].source+"')", className: "buttonAlone"}, "Share "+currentVerse);
						newVerse.appendChild(shareVerse);
					}
					
					if (noVerseAnim == false) {
						slideIn(newVerse);
					}
					else {
						noVerseAnim = false;
					}
				}
				else {
					var newError = dom.create("div", {id: "loadedVerse", className: "bibleError"}, "<div class=\"error\"><h1>Not Found</h1>I'm sorry, but the verse that you entered has returned an error.</div>");
					if ($("#bibleIntro")) {
						$("#bibleDiv").removeChild($("#bibleIntro"));
					}
					else {
						$("#bibleDiv").removeChild($("#loadedVerse"));
					}
					$("#bibleDiv").appendChild(newError);
					
					if (noVerseAnim == false) {
						slideIn(newError);
					}
					else {
						noVerseAnim = false;
					}
				}
			}
			else {
				if (data[0].text) {
					var newVerse = dom.create("div",null,data[0].text);
					var spans = newVerse.getElementsByTagName("span");
					for (var i=0; i<spans.length; i++) {
						if (spans[i].className == "verse") {
							if ($("#loadedVerse")) {
								$("#loadedVerse").getElementsByClassName("ebSnippet-body")[0].insertBefore(spans[i],$("#loadedVerse").getElementsByClassName("ebSnippet-body")[0].getElementsByClassName("ebSnippet-credit")[0]);
							}
						}
					}
				}
			}
		}
		else {
			if (data[0].text) {
				var newVerse = dom.create("div", {id: "loadedVerse", className: "bibleVerse"}, data[0].text);
				
				//Check for more link
				var spans = newVerse.getElementsByTagName("span");
				var more  = 0;
				more      = newVerse.getElementsByClassName("ebSnippet-more");
				
				if (more) {
					var verse     = data[0].verse;
					var verses    = newVerse.getElementsByClassName("verse");
					var lastVerse = verses.length-1;
					lastVerse     = verses[lastVerse].getElementsByClassName("verseNum")[0].firstChild.nodeValue;
				}
				
				//remove last br
				var brs = newVerse.getElementsByTagName("br");
				brs[(brs.length-1)].parentNode.removeChild(brs[(brs.length-1)]);
				
				if ($("#bibleIntro")) {
					$("#bibleDiv").removeChild($("#bibleIntro"));
				}
				if ($("#loadedVerse")) {
					$("#bibleDiv").removeChild($("#loadedVerse"));
				}
				$("#bibleDiv").appendChild(newVerse);
				
				if (login) {
					var shareVerse = dom.create("a",{href: "javascript:shareVerse('"+currentVerse+"','"+data[0].source+"')", className: "buttonAlone"}, "Share "+currentVerse);
					newVerse.appendChild(shareVerse);
				}
				
				if (noVerseAnim == false) {
					slideIn(newVerse);
				}
				else {
					noVerseAnim = false;
				}
			}
			else {
				var newError = dom.create("div", {id: "loadedVerse", className: "bibleError"}, "<div class=\"error\"><h1>Not Found</h1>I'm sorry, but the verse that you entered has returned an error.</div>");
				if ($("#bibleIntro")) {
					$("#bibleDiv").removeChild($("#bibleIntro"));
				}
				else {
					$("#bibleDiv").removeChild($("#loadedVerse"));
				}
				$("#bibleDiv").appendChild(newError);
				
				if (noVerseAnim == false) {
					slideIn(newError);
				}
				else {
					noVerseAnim = false;
				}
			}
		}
	}
	if (queries) {
		for (var i=0; i<queries.length; i++) {
			if (queries[i] == data[0].verse) {
				var url="http://www.ebible.com/api/ebibleSnippet?apikey=8ee1fc96f852d709d4617c8ad422cff5a63185dc";
				url  += "&callback=recievedText";
				url  += "&source="+data[0].source;
				url  += "&q="+escape(queries[(i+1)]);
				getScript(url);
			}
		}
	}
}

function slideIn (object) {
	if (!object.move) {
		dom.addAnimate(object);
	}
	var finalDest = object.offsetTop;

	object.setStyles({position: "absolute", top: document.documentElement.clientHeight-75+"px", opacity: .5});
	object.move(finalDest, null, 5);
	object.fade(100, 5);
}

function getCookie (name) {
	var start = document.cookie.indexOf( name + "=" );
	var len   = start + name.length + 1;
	if (!start && name != document.cookie.substring(0, name.length)) {
		return null;
	}
	if (start == -1) {
		return null;
	}
	var end   = document.cookie.indexOf(";", len);
	if (end == -1) {
		end = document.cookie.length;
	}
	return unescape(document.cookie.substring(len, end));
}
function getMultiQueries (verse) {
	var matches       = verse.match(/([a-z ]*) ([0-9]*):([0-9]*)-([0-9]*)/i);
	if (matches) {
		var book          = matches[1];
		var chapter       = parseInt(matches[2]);
		var verseStart    = parseInt(matches[3]);
		var verseEnd      = parseInt(matches[4]);
		var verseCount    = verseEnd-verseStart+1;
		
		var verseQueries  = new Array();
		if (verseCount > 4) {
			var count     = verseCount;
			var nextStart = verseStart;
			while (count > 0) {
				verseQueries[verseQueries.length] = book+" "+chapter+":"+nextStart+"-"+(nextStart+3 > verseEnd ? verseEnd : nextStart+3);
				count     = count-4;
				nextStart = nextStart+4;
			}
		}
		else {
			verseQueries[verseQueries.length] = verse;
		}
	}
	else {
		verseQueries = verse;
	}
	return verseQueries;
}
function returnVerse (verseText) {
	var newDiv = document.createElement("div");
	newDiv.innerHTML = verseText;
	
	var spans  = newDiv.getElementsByTagName("*");
	var divs   = newDiv.getElementsByTagName("div");
	var verses = "";
	var title  = null;
	var link   = null;
	
	for (var i=0; i<divs.length; i++) {
		if (divs[i].getAttribute("class") == "ebSnippet-title") {
			link    = divs[i].getElementsByTagName("a")[0].getAttribute("href");
			title   = divs[i].getElementsByTagName("a")[0].innerHTML;
			
			verses += "<strong>"+title+"</strong><br />";
		}
	}
	
	for (var i=0; i<spans.length; i++) {
		if (spans[i].nodeName == "DIV" || spans[i].nodeName == "SPAN") {
			if (spans[i].getAttribute("class") == "verse") {
				verses += " "+spans[i].innerHTML;
			}
			else if (spans[i].getAttribute("class") == "chapterHeading") {
				verses += "<br />\n"+spans[i].innerHTML+"<br />";
			}
		}
	}
	verses += "<br />\n<br />\n";
	verses += "Read more: <a href=\""+link+"\">"+link+"</a>";
	
	verses = verses.replace(/<span class="verseNum">([1-9]*)<\/span>/g,"<sup>$1</sup>");
	verses = verses.replace(/<span class="jesusTalk">([^<]*)<\/span>/g,"$1");
	verses = verses.replace(/<span class="[^"]*">/g," ");
	verses = verses.replace(/<\/span>/g," ");
	return verses;
}

dom.addLoadEvent(initPage);