//Global variables:

var verse_timer;
var last_rotate_time;
var now1;
var now2;
//var runornot;

window.onload = gbinit;

function gbinit(){
	new Effect.Appear('wrapper2', {duration: 1}); //script.aculo.us effects.js v1.7.0
	now1 = new Date();	
	last_rotate_time = now1.getTime();
	setTimeout("rotate_verse('yes')", 500);
//	getwx();
//	rotate_verse('yes');
	clockID = document.getElementById('clock');
    doClock();
}

function rotate_verse(runornot){
	if (runornot== "yes"){
	now2 = new Date();
	var duration = now2.getTime()-last_rotate_time;
	if(duration >= 30000){
		get_another_verse('get_verse.php');
	}
	}
	x=runornot;
	setTimeout("rotate_verse(x)", 3000);
//	document.getElementById("testdiv2").innerHTML = runornot;
}

function pause_verse(){
	document.getElementById("pause-run_div").innerHTML ="<img src=\"images/web_page_elements/fwd.gif\" alt=\"fwd\" onclick=\"run_verse()\"/>";
	rotate_verse ('no');
}
function run_verse(){
	document.getElementById("pause-run_div").innerHTML ="<img src=\"images/web_page_elements/pause.gif\" alt=\"pause\" onclick=\"pause_verse()\"/>";
	last_rotate_time = now1.getTime()-30000000;
//document.getElementById("testdiv1").innerHTML = last_rotate_time;
	rotate_verse('yes');
}

function doNothing() {}
//The famous Bass-O-Matic Element Fader-----------------------------------------------------------------------------------
//Fade an element from one color to the other (or to and from the background color to make it fade in or out)
//Copyright 2007 by Greg Bass.  Free for personal use if attributed.  Thanks!
//color fade from one color to another:

//global variables:
var r,g,b,r0,g0,b0,r1,g1,b1;
var color_step ;
var element;

//function called from the document, used to set up intial variables
//in document call use an odd value for color step so that it converges to zero
function colorfadetext(r0,g0,b0,r1,g1,b1,color_step,element){
	r=r0;
	g=g0;
	b=b0;
	fade_loop(r,g,b,r1,g1,b1,color_step,element);

}

//the function that does the fading from the inital (0) color to the final (1) color:
function fade_loop(r,g,b,r1,g1,b1,color_step,element){
	var rcolor_step=gcolor_step=bcolor_step=color_step;
	if(r!==r1 || g!==g1 || b!==b1){ //if the current color is not the final color
		var rdif,gdif,bdif,absr,absg,absb,new_color;
		
		rdif = r1-r;
		gdif = g1-g;
		bdif = b1-b;
		absr=Math.abs(rdif);
		absg=Math.abs(gdif);
		absb=Math.abs(bdif);
		if (rdif == 0){ absr = 1;}//to keep from dividing by zero below
		if (gdif == 0){ absg = 1;}
		if (bdif == 0){ absb = 1;}
		if (Math.abs(rdif)<color_step){rcolor_step=1;}//this is so it converges to the new color not oscillating around it at a color_step distance
		if (Math.abs(gdif)<color_step){gcolor_step=1;}
		if (Math.abs(bdif)<color_step){bcolor_step=1;}
		r = r + (rdif/absr)*rcolor_step;
		g = g + (gdif/absg)*gcolor_step;
		b = b + (bdif/absb)*bcolor_step;
		if (r<0) {r=0;}//to stay in range, it hits the stop and stays there
		if (g<0) {g=0;}
		if (b<0) {b=0;}
		if (r>255) {r=255;}//same the other way
		if (g>255) {g=255;}
		if (b>255) {b=255;}
		new_color = r+","+g+","+b;
		document.getElementById(element).style.color="rgb("+new_color+")";
		setTimeout("fade_loop("+r+","+g+","+b+","+r1+","+g1+","+b1+","+color_step+",'"+element+"')",1);
}
else 	document.getElementById(element).style.color="rgb("+r1+","+g1+","+b1+")";
}
//end of the famous Bass-O-Matic Element Fader----------------------------------------------------------------------------

//My first Ajax script----------------------------------------------------------------------------------------------------
//this gets the new bible verse and displays it

var XMLHttpRequestObject = false;
function get_another_verse(new_verse_script){
		now1 = new Date();	
		last_rotate_time = now1.getTime();
		//I nested a function to build a delay using setTimeout	
		//need to fade out the old verse here before we get the new one (like to do after I get it but 
		//I can't figure out how to stop the subsequent function to delay for the fade out	
//			colorfadetext(153,255,255,0,102,255,13,'verse_holder');
//			colorfadetext(153,255,255,0,102,255,13,'verse_number_holder');
//**************************these are scriptaculous functions: Fade and Appear:
//	new Effect.Opacity('bible_holder', {duration:1.0, from:1.0, to:0.0});
	Element.setOpacity('bible_holder', 1.0)
Effect.Fade('bible_holder', {from:1.0, to: 0.01}); //if you let it go to zero the div goes away totally momentarily and everything 
                                                //jerks up. this way you just a shift from the old to the new size.
//Effect.SlideUp('verse_number_holder');
			new_verse_script_name = new_verse_script;//have to carry over script name between functions for some reason that I cannot identify
			setTimeout("get_another_verse2(new_verse_script_name)", 900);//this is to hold up the ajax call until the old verse has faded
																		// if it takes more than 0.5 secs for the fade out to finish, you'll get a flash of a verse and a blank div											
}
function get_another_verse2(new_verse_script){//had to nest this otherwise the ajax function finished while the fade out was happening and the div ended up blank
	if (window.XMLHttpRequest){				//and I also wanted the div to fade, resize, then fade in, in that order.
	
		XMLHttpRequestObject = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject){
			try{XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {}
		}
	}
	//this keeps IE from just going to the cache and not downloading an updated file
	new_verse_script = new_verse_script + "?kill_cache=" + new Date().getTime();
	
	
	var new_verse_script_name = new_verse_script;
	if(XMLHttpRequestObject){
		XMLHttpRequestObject.open("GET", new_verse_script);
		XMLHttpRequestObject.onreadystatechange = function(){
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				var xml_new_verse = XMLHttpRequestObject.responseXML;
				var new_verse = false;
				var new_verse_number = false;
				new_verse_nodes = xml_new_verse.getElementsByTagName("new_verse");
				new_verse_number_nodes = xml_new_verse.getElementsByTagName("new_verse_number");
				new_verse = new_verse_nodes[0].firstChild.nodeValue;
				new_verse_number = new_verse_number_nodes[0].firstChild.nodeValue;

							//need to fade out the old here	
//				colorfadetext(153,255,255,0,102,255,1,'verse_holder');
//			colorfadetext(153,255,255,0,102,255,1,'verse_number_holder');		
			
				
//now write the new one but it's invisible because it is the same color as the background			
				document.getElementById("verse_holder").innerHTML = new_verse;
				document.getElementById("verse_number_holder").innerHTML = new_verse_number;

//need to fade in the new here
//				colorfadetext(0,102,255,153,255,255,2,'verse_holder');
//				colorfadetext(0,102,255,153,255,255,2,'verse_number_holder');
//new Effect.Opacity('bible_holder', {duration:1.0, from:0.0, to:1.0});
Effect.Appear('bible_holder');
//Effect.Appear('verse_number_holder');
				
//clear memory
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;
			}
		}
		XMLHttpRequestObject.send(null);


	}

	else {
		document.getElementById("verse_holder").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
	}	
}
//end of my first ajax script---------------------------------------------------------------------------------------------

