// declare a global  XMLHTTP Request object
var XmlHttpObj;

var slide_images= new Array();
var slideIndx=0;
var ie4=document.all;
var dom=document.getElementById;

// Set slideShowSpeed (milliseconds)
var slideShowSpeed = 3000;
var Pic = new Array();
var preLoad = new Array();


// do not edit anything below this line
var j = 0;

// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
    
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}
function Load_Slide_Show(){
	//set timer
	Load_Slides();
	setTimeout("preloadImages()", 2000);
	setTimeout("runSlideShow()", 2000);
}
function preloadImages(){
  for (var i = 0; i < slide_images.length; i++) {
    preLoad[i]=new Image(400,300);
    preLoad[i].src=slide_images[i];
  }
}
function runSlideShow() {
	j = j + 1;
	if (j > (slide_images.length - 1)) j = 0;
  fadeOut('imgSlide', preLoad[j].src);
}

function Load_Slides() 
{
  var requestUrl;
  // use the following line if using asp
  requestUrl = "/_includes/slides/slide.php";
    
	CreateXmlHttpObj();	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = ImageChangeHandler;
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}
// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function ImageChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			Populate_ImageList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
      alert("There was a problem retrieving the requested information: " + XmlHttpObj.status);
			//alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}
// populate the contents of the Video Div
function Populate_ImageList(ImageNode){
  //var ListingActivityNode='';
  if (ImageNode == null) {
    //NULLIFY
    slide_images[0] = document.getElementById("imgSlide").src;
  }
  else {
    var imageNode = ImageNode.getElementsByTagName('ImagePath');
    //General
    for (var i = 0; i < imageNode.length; i++) {
      slide_images[i] = GetInnerText(imageNode[i]);
    }
  }
  //return slide_images;
}
// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function encodeMyHtml(val) {
//     encodedHtml = escape(encodeHtml.htmlToEncode.value);
     var encodedHtml = escape(val);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     var newVal=encodedHtml;
     //encodeHtml.htmlEncoded.value = encodedHtml;
     return newVal;
   } 

function htmlentities( s ){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: htmlentities('Kevin & van Zonneveld');
    // *     returns 1: 'Kevin &amp; van Zonneveld'
 
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}

function html_entity_decode(str) {
  //jd-tech.net
  var  tarea=document.createElement('textarea');
    tarea.innerHTML = str; return tarea.value;
    tarea.parentNode.removeChild(tarea);
  }
/********FADING*********/
function setOpacity(eID, opacityLevel) {
    var eStyle = document.getElementById(eID).style;
    eStyle.opacity = opacityLevel / 100;
    eStyle.filter = 'alpha(opacity='+opacityLevel+')';
}  
function getElm(eID) {
    return document.getElementById(eID);
}
function fadeIn(eID, pSrc) {
    setOpacity(eID, 0); 
    getElm(eID).src=pSrc;
    var timer = 0;
    for (var i=1; i<=100; i++) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * 5);
        timer++;
        if (i == 100) {
         	setTimeout("runSlideShow()", 4000);
        }
    }

}
function fadeOut(eID, pSrc) {
    var timer = 0;
    for (var i=100; i>=1; i--) {
        setTimeout("setOpacity('"+eID+"',"+i+")", timer * 5);
        timer++;
        if (i == 100) {
          setTimeout("fadeIn('imgSlide', '" + pSrc + "')", 1000);
        }
    }

    
}
  
