/*
this script can add this html tag - 

<li><a href="images/Artwork/Drawing_Img/Drawing_03.jpg" rel="Gallery" title="Drawing 03">
<img src="images/Artwork/Drawing_Thumb/sDrawing_03.jpg" height="43" alt="Scrolling"/>
</a></li>

to an html document

sample code to place in html within an <ul></ul> tag
<script type="text/javascript"><!--
	readGallery(galleryXmlFile);
//-->
</script>


*/
// parse xml document
function parseDocument(docurl)
{
  var xmlDoc=null;

  // first we try to load the XML file using an XMLDocument
  // we still need this since IE won't support loading from file system using XMLHttpRequest 
  try
  {

	// first create an xml document using the mozilla or IE way
	if(document.implementation && document.implementation.createDocument)
	  xmlDoc=document.implementation.createDocument("", "", null);
	else if(window.ActiveXObject)
	  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

	if(xmlDoc)
	{
	  xmlDoc.async=false;
	  xmlDoc.load(docurl);
	}
  }
  catch(e)
  {
	xmlDoc=null;
  }

  if(xmlDoc)
  {
	if(xmlDoc.parseError)         // handle IE errors
	{
	  if(xmlDoc.parseError.errorCode!=0)
		throw xmlDoc.parseError.reason;
	}
	else if(xmlDoc.documentElement && xmlDoc.documentElement.tagName=="parsererror")        // handle mozilla errors
	{
		throw xmlDoc.documentElement.firstChild.nodeValue;
	}

	if(xmlDoc.firstChild)
	  return xmlDoc;

// we ignore this throw to make it work with konquerer
//    throw "Error: failed to get XML document from "+docurl;
  }

  // now let's try to load using an XMLHttpRequest
  var xmlReq=null;

  // first let's create an XMLHTTPRequest and load the document using the mozilla or IE way
  if(window.XMLHttpRequest) 
	xmlReq=new XMLHttpRequest();
  else if(window.ActiveXObject)
  {
	try 
	{
	  xmlReq=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
	  try
	  {
		xmlReq=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  catch(e2) { }
	}
  }

  if(!xmlReq)
	throw "Error: XMLHttpRequest not supported by your browser";

  xmlReq.open("GET", docurl, false);
  xmlReq.send(null);

  // 0 is returned when loading from file system not using HTTP !!!
  if(xmlReq.status>0 && (xmlReq.status<200 || xmlReq.status>=400))	
	throw "Error during XMLHttpRequest: "+xmlReq.status+" - "+xmlReq.statusText;

  return xmlReq.responseXML;
}

String.prototype.startsWith = function(str){
	return (this.match("^"+str)==str)
}

function readGallery(galleryXml)
{
	var xmlDoc=parseDocument(galleryXml);
	var headImages=xmlDoc.getElementsByTagName("images");
	if(headImages.length==1)
		thumbdir=headImages[0].getAttribute("thumbDir");
		imgdir=headImages[0].getAttribute("imgDir");
	
	var imgEle=xmlDoc.getElementsByTagName("image");
	for(var i=0; i<imgEle.length; i++)
	{
		//~ var video=imgEle[i].getElementsByTagName("video");
		var filename=imgEle[i].getElementsByTagName("filename")[0].childNodes[0].nodeValue;
		var thumbnail=imgEle[i].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
		var title=imgEle[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
		var caption=imgEle[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
		var thumbUrl = (thumbdir+thumbnail);
		var imgLink = (imgdir+filename);
		//~ document.writeln(thumbUrl);
		//~ document.writeln(imgLink);
		var fileCheck = ("s"+filename);
		if(fileCheck == thumbnail){
			var tagHtml=('<li><a href=\''+imgLink+'\' title=\''+title+'\' rev=\''+caption+'\' rel=\'Gallery\'><img src=\''+thumbUrl+'\' alt=\''+title+'\' width=43 height=43/></a></li>');
			document.write(tagHtml);
		}else{
			var link = filename;
			//~ if(filename.startsWith("http")){
				//~ link = filename;
			//~ }else{
				//~ link = imgLink;
			//~ }
			var tagHtml=('<li><a href=\''+link+'\' title=\''+title+'\' rev=\''+caption+'\' rel=\'extLink\'><img src=\''+thumbUrl+'\' alt=\''+title+'\' width=43 height=43/></a></li>');
			document.write(tagHtml);
		}
		
	}
	
}


