
// Replace this by the custom onload function.

window.onload = function(){ dnb_slideshow_init(); }

var dnb_slideshow_xml = null;
var dnb_slideshow_files = new Array();
var dnb_slideshow_timer = false;
var dnb_slideshow_slide = 0;
var dnb_slideshow_sequence = Array(0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10);
var dnb_slideshow_count = 0;

function dnb_slideshow_init(){
	if(document.getElementById){
		xmlHttp = getXmlHttpObject();
		xmlHttp.onreadystatechange = dnb_slideshow_parsexml;
		xmlHttp.open("GET", '/images/slideshowmain.xml.php?ms=' + new Date().getTime(), true);
		xmlHttp.send(null);
		}
	}

function dnb_slideshow_parsexml(){
	if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
		dnb_slideshow_xml = xmlHttp.responseXML.documentElement.getElementsByTagName('image');
	
		// Shove all the images into an array so we can check the load status.

		for(var i = 0; i < dnb_slideshow_xml.length; i++){
			dnb_slideshow_files[i] = new Image();
			dnb_slideshow_files[i].src = dnb_slideshow_getimageinfo(i).src;
			}
		dnb_slideshow_preload();
		}
	}

function dnb_slideshow_prepare(){

	// Transfer the current image to the background of the div.

	var t = document.getElementById('dnb_slideshow');
	t.style.backgroundImage = 'url(' + t.getElementsByTagName('img')[0].src + ')';

	// Locate the coordinates of the div so we can place the progress bar there.

	var coords = findpos(t);

	// Create a new div to be used to show the progress indicator.

	var c = document.createElement('div');
	c.style.width = dnb_slideshow_xml.length * 20 + 'px';
	c.style.height = 16 + 'px';
	c.style.backgroundImage = 'url(/images/dnb_slideshow_progress.png)';
	c.style.position = 'absolute';
	c.style.zIndex = 1000;
	c.style.top = coords[1] + 5 + 'px';
	c.style.left = (coords[0] + (503 - (dnb_slideshow_xml.length * 20))) + 'px';
	c.style.backgroundPosition = '-240px top';
	c.style.backgroundRepeat = 'no-repeat';
	c.id = 'dnb_slideshow_progressbar';
	c.style.cursor = 'pointer';
	c.onclick = function(){ self.location.href = document.getElementById('dnb_slideshow').getElementsByTagName('a')[0].href; };
	document.body.appendChild(c);

	// Start the sliding sequence.
	
	dnb_slideshow_run();
	}

function dnb_slideshow_run(){
	var t = document.getElementById('dnb_slideshow');

	// Initiate the timer if it's not yet running.
	
	if(dnb_slideshow_timer === false){
		dnb_slideshow_timer = setInterval('dnb_slideshow_run()', 70);
		}

	// Proceed through the sequence.

	dnb_slideshow_count++;

	// Set the opacity accordingly.

	dnb_slideshow_setopacity(t.getElementsByTagName('img')[0], dnb_slideshow_sequence[dnb_slideshow_count]);

	// Check if we finished the current sequence.

	if(dnb_slideshow_count >= dnb_slideshow_sequence.length){

		// Progress through the slides.

		dnb_slideshow_slide++;

		if(dnb_slideshow_slide >= dnb_slideshow_xml.length){
			dnb_slideshow_slide = 0;
			}

		// Reset the sequence.
		
		dnb_slideshow_count = 0;
	
		// Transfer the current image to the background of the div.

		t.style.backgroundImage = 'url(' + t.getElementsByTagName('img')[0].src + ')';

		// Replace the current image with the proper link, title and image src.
	
		if(newimage = dnb_slideshow_getimageinfo(dnb_slideshow_slide)){
			dnb_slideshow_setopacity(t.getElementsByTagName('img')[0], 0);
			t.getElementsByTagName('img')[0].src = newimage.src;
			t.getElementsByTagName('img')[0].title = newimage.title;
			t.getElementsByTagName('img')[0].alt = newimage.title;
			t.getElementsByTagName('a')[0].href = newimage.href;
			t.getElementsByTagName('a')[0].title = newimage.title;
			dnb_slideshow_progress(dnb_slideshow_slide);
			}
		}
	}

function dnb_slideshow_progress(int_image){
	c = document.getElementById('dnb_slideshow_progressbar');
	c.style.backgroundPosition = ((int_image * 20) - 240) + 'px top';
	}

function dnb_slideshow_setopacity(obj_element, int_opacity){
	if(int_opacity >= 10){
		int_opacity = 9.9;
		}
	obj_element.style.opacity = int_opacity / 10;
	obj_element.style.filter = 'alpha(opacity=' + int_opacity * 10 + ')';
	}

function dnb_slideshow_preload(){
	var loaded = 0;
	if(dnb_slideshow_xml.length > 1){
		for(var i = 0; i < dnb_slideshow_xml.length; i++){
			if(dnb_slideshow_files[i].complete){
				loaded++;
				}
			}
		if(loaded == dnb_slideshow_xml.length){
			setTimeout('dnb_slideshow_prepare()', 0);
			}
		else {
			setTimeout('dnb_slideshow_preload()', 100);
			}
		}
	}

function dnb_slideshow_getimageinfo(int_image){
	var imageproperties = false;
	if(int_image < dnb_slideshow_xml.length){
		imageproperties = new Object();
		imagexml = dnb_slideshow_xml[int_image];
		imageproperties.src = imagexml.getElementsByTagName('src')[0].childNodes[0].nodeValue;
		imageproperties.href = imagexml.getElementsByTagName('href')[0].childNodes[0].nodeValue;
		imageproperties.title = imagexml.getElementsByTagName('title')[0].childNodes[0].nodeValue;
		}
	return imageproperties;
	}

function getXmlHttpObject(handler){
	var objXMLHttp = null
	if(window.XMLHttpRequest){
		objXMLHttp = new XMLHttpRequest();
		}
	else if(window.ActiveXObject){
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	return objXMLHttp;
	}


function findpos(obj){
	var curleft = curtop = 0;
	if(obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
			} 
		while(obj = obj.offsetParent);
		}
	return [curleft,curtop];
	}

