// ---------------------------------------------
// FROM http://javascript.internet.com/miscellaneous/random-image-rotator.html 
// ROTATE RANDOM IMAGE
function imageItem(image_location) {
  this.image_item = new Image();
  this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) {
  return(imageObj.image_item.src)
}
function generate(x, y) {
  var range = y - x + 1;
  return Math.floor(Math.random() * range) + x;
}
function getNextImage(image) {
  var new_image = image;
  while (image == new_image) {
    if (random_display) {
      image_index = generate(0, number_of_image-1);
    } else {
      image_index = (image_index+1) % number_of_image;
    }
    new_image = get_ImageItemLocation(image_list[image_index]);
  }
  return(new_image);
}
function dFadeIn(imageId, speed) {
  for (timer = 0; timer <= 100; timer++) {
    setTimeout("changeOpac(" + timer + ",'" + imageId + "')",(timer * speed))
  }
}
function dFadeOut(imageId, speed) {
  for (timer = 0; timer <= 100; timer++) {
    setTimeout("changeOpac(" + (100-timer) + ",'" + imageId + "')",(timer * speed))
  }
}
function rotateImage(place) {
  // wd's IMPLEMENT FROM FADING 
  //var divId = 'pre_mission';
  //var divbg = document.getElementById(divId);
  var divId = 'imagebackground';
  var imageId = 'blendimage';
  var image = document.getElementById(imageId);
  var speed = Math.round(1500 / 100);   //speed of fade
  //var speed = Math.round(2500 / 100);   //speed of fade
  //var speed = Math.round(500 / 100);   //speed of fade
  var timer = 0;

  //save last image background
  document.getElementById(divId).style.backgroundImage = "url(" + document.getElementById(imageId).src + ")";

  //fade to new image
  dFadeIn(imageId, speed);
  
  // make new image
  var new_image = getNextImage(image);
  changeOpac(0, imageId);
  document[place].src = new_image;

//recursive
  //var recur_call = "rotateImage('"+place+"')";
  //setTimeout(recur_call, interval);
//only once
  var recur_call = "FadeImage('"+place+"')";
  setTimeout(recur_call, interval);
}
function FadeImage(place) {
  var imageId = 'blendimage';
  var image = document.getElementById(imageId);
  //var speed = Math.round(2500 / 100);
  var speed = Math.round(1500 / 100);
// SOLVE SLOW SPEED: DROP FADE OUT CAPABILITY
//  dFadeOut(imageId, speed)
//  var recur_call = "rotateImage('"+place+"')";
//  setTimeout(recur_call, 2500);
}

// ---------------------------------------------
// FROM http://www.brainerror.net/scripts_js_blendtrans.php
function xblendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;
    
    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
    
    //make image transparent
    changeOpac(0, imageid);
    
    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for(i = 0; i <= 100; i++) {
        window.setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
        timer++;
    }
}
//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id);
    opacity = (opacity == 100)?99.99:opacity;
    // New Firefox 
    object.style.opacity = (opacity / 100);
    // Mozilla & Firefox
    object.style.MozOpacity = (opacity / 100);
    // Safari, Konqueror
    object.style.KhtmlOpacity = (opacity/100) ;
    // IE/Win
    object.style.filter = "alpha(opacity:" + opacity + ")";
}

// ---------------------------------------------
// FROM http://clagnut.com/sandbox/imagefades/
// FADE BETWEEN IMAGE
function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}
function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity > 0) {
      setOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
    }
  }
}

// ---------------------------------------------
//	This is the source file for the "Hello World of AJAX" tutorial
//	You may use this code in your own projects as long as this 
//	copyright is left	in place.  All code is provided AS-IS.
//	This code is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//	
//	Please visit http://www.DynamicAJAX.com for more great AJAX
//	source code and tutorials.
//	
//	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
		// xmlhttp = new XMLHttpRequest();
		// xmlhttp.overrideMimeType("text/html");
		// return xmlhttp;
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}			

//Get our browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();		

//Initiate the asyncronous request.
function sayHello() {
	//If our XmlHttpRequest object is not in the middle of a request, start the new asyncronous call.
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		//Setup the connection as a GET call to SayHello.html.
		//True explicity sets the request to asyncronous (default).
		receiveReq.open("GET", 'SayHello.html', true);
		//Set the function that will be called when the XmlHttpRequest objects state changes.
		receiveReq.onreadystatechange = handleSayHello; 
		//Make the actual request.
		receiveReq.send(null);
	}			
}

//wd's implement
function d_say(action) {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		document.getElementById('d_span').innerHTML = "Loading...";
		receiveReq.open("GET", action, true);
		// FIX KONQUEROR CHARSET BUG'S 
		// receiveReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		// FIX JAVA CHARSET BUG'S
		// receiveReq.overrideMimeType("text/html; charset=UTF-8");
		receiveReq.onreadystatechange = d_handle; 
		receiveReq.send(null);
	}			
}

//Called every time our XmlHttpRequest objects state changes.
function handleSayHello() {
	//Check to see if the XmlHttpRequests state is finished.
	if (receiveReq.readyState == 4) {
		//Set the contents of our span element to the result of the asyncronous call.
		document.getElementById('span_result').innerHTML = receiveReq.responseText;
	}
}

//wd's implement
function d_handle() {
	if (receiveReq.readyState == 4) {
		document.getElementById('d_span').innerHTML = receiveReq.responseText;
	}
}

