/*
	|---------------------------------------------------------------------------|
	|  Yes this is my script, i believe it to me worthy due to the ease of use  |
	|  i hopefully fixed the safari bug since i replaced it with img.onload     |
	|                                                                           |
	|         nihaopaul.javascript@nihaopaul.com without the .javascript        |
	|                                                                           |
	|  if you would like the other parts to this file also email me, i will put |
	|  them online at a later date, but its changing daily at this rate.        |
	|  to comply with the liscense of the site, i'll ask that you keep in this  |
	|  Plug and to send me any improvements you make.                           |
	|                                                                           |
	|  				last updated: 18/12/2006                            		|
 	| 				Customized for Pivot 					    				|
	|  other ideas:                                                             |
	|  get the scripta.licio.us prototypes for fade in fade out, but i dont     |
	|  want to use it right now.                                                |
	|                                                                           |
	|  references:                                                              |
	|   http://developer.mozilla.org/en/docs/AJAX:Getting_Started               |
	|	http://www.sitepoint.com/blogs/2006/01/31/is-this-a-safari-bug/	    	|
 	|   https://beta.blogger.com/comment.g?blogID=19702294&postID=115333629804130655 |
	|---------------------------------------------------------------------------|

	Copyright (C) 2006 paul adams (nihaopaul.nospam.)at(.nihaopaul.com)

	All rights reserved.

*/
var http_request = false, id=0, Mytimer, MYoimg, MYimg, MyBadCount=0, AlreadyloadedImgs = new Array();

function nextImage() {
	clearTimeout(Mytimer);
	
	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Sorry. Your browser does not support AJAX!');
		return false;
	}

	http_request.onreadystatechange = alertContents;
	http_request.open('GET', '/mygallery.php?id='+id+'&group='+MyGroup, true);
	http_request.send(null);

}
function Mydisplay() {
	loadedimg();
	loader('off');
	document.getElementById('nhp_photo').style.display="block";

}

function loadedimg() {
	clearTimeout(Mytimer);
	Mytimer = setTimeout(nextImage,3000);

}
function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;

			MYoimg = MYimg;
			MYimg = xmldoc.getElementsByTagName("nimg")[0].firstChild.data;
			
			var MYtext = xmldoc.getElementsByTagName("text")[0].firstChild.data;
			var MYtotal = xmldoc.getElementsByTagName("total")[0].firstChild.data;
			var MYcurrent = xmldoc.getElementsByTagName("current")[0].firstChild.data;
			id = xmldoc.getElementsByTagName("id")[0].firstChild.data;
			if ((MYimg != MYoimg) && (MYimg != "none")) {

				loader('on');

				var newhtml = '<img src="'+decode(MYimg)+'" alt="'+decode(MYtext)+'" title="'+decode(MYtext)+'" />'; 
				var timg = new Image(); 
				timg.onload = function() { 
					document.getElementById("nhp_photo").innerHTML = newhtml;
					Mydisplay();
				}; 
				timg.onerror = function() {
					loadedimg();
				}
				timg.src = decode(MYimg); 
				
			} else if (MYimg == "none") {
				//this function just checks if we have any images.
				Noimages();
			} else {
				loadedimg();
			}
			
			MyBadCount = 0;
		} else {
			MyBadCount +=1;
			if (MyBadCount <= 5) {
				loadedimg();
			} else {
				ScriptError();
			}
		}
	}
}
function decode(str) {
	//works with & characters and shit
     return unescape(str.replace(/\+/g, " "));
}

function Noimages() {
	loader('off');
	document.getElementById('nhp_photo').style.display="block";
	document.getElementById('nhp_photo').innerHTML = '<div class="error"><h1>No Images!</h1><p>Sorry there are no images for this section yet.</p></div>';
	clearTimeout(Mytimer);
	Mytimer = setTimeout(Mydisplay,1200);

}
function ScriptError() {
	loader(off);
	document.getElementById('nhp_photo').style.display="block";
	document.getElementById('nhp_photo').innerHTML = '<div class="error"><h1>Halted!</h1><p>This script has preformed one or more errors and has been stopped.</p></div>';

}