/*
	|---------------------------------------------------------------------------|
	|  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: 07/06/2006                            |
	|  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/	    |
	|---------------------------------------------------------------------------|

*/
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. Get a real browser!');
		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,8000);

}
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)+'" /><div id="text">'+decode(MYtext)+' ('+MYcurrent+'/'+MYtotal+')</div>'; 
				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, I guess i havn\'t put any images up yet, but if you keep this page open, the moment i do upload some, you\'ll see the page change! AJAX r0x</p></div>';
	clearTimeout(Mytimer);
	Mytimer = setTimeout(Mydisplay,30000);

}
function ScriptError() {
	loader(off);
	document.getElementById('nhp_photo').style.display="block";
	document.getElementById('nhp_photo').innerHTML = '<div class="error"><h1>uht oh!</h1><p>This AJAX script has been stopped since there has been an error, it could be that the site is down or you\'ve lost connection. Either way hit ctrl+d now to bookmark this site so you can come back later.</p></div>';

}