/*
*/



// 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;
}

/*Swapping images and texts*/

function newImgSize(imgSrc){
	var newImg = new Image();
	newImg.src = imgSrc;
	var newHeight = newImg.height;
	var newWidth = newImg.width;
	if (newWidth*450/newHeight>600){
		document.getElementById('IMG_Main').width = 600;
	}
	if (newHeight*600/newWidth>450){
		document.getElementById('IMG_Main').height = 450;
	}
}

function swapMainImage(thisTag, imgSrc, linkHref)
{
	document.getElementById('IMG_Main').removeAttribute('height',0);
	document.getElementById('IMG_Main').removeAttribute('width',0);
	newImgSize(imgSrc);
	description = thisTag.rev;
	description = description.replace(/%quot;/g,'"');
	document.getElementById('IMG_Main').alt = thisTag.title;
	document.getElementById('IMG_Main').src = imgSrc;
	document.getElementById('IMG_Link').href = linkHref;
	document.getElementById('IMG_Desc').innerHTML = description;
	document.getElementById('IMG_Title').innerHTML = thisTag.title;
	subtitle = thisTag.rel;
	subtitle = subtitle.replace(/%quot;/g,'"');
	document.getElementById('IMG_SubTitle').innerHTML = subtitle;
	
	var tagName = thisTag.name;
	var idNum = tagName.replace('Thumb','');
	thumbIndex = idNum;
	var movTag = document.getElementById('MOV_Main');
	if (movTag != null){
		document.getElementById('IMG_Link').removeChild(movTag);
	}
	var filename = linkHref.toString();
	if(filename.match(/.mov$/)){
		var linkTag = document.getElementById('IMG_Link');
		document.getElementById('IMG_Main').src = "images/spacer.gif";
		document.getElementById('IMG_Main').height = "0";
		var qtTag = ('<OBJECT id=\"MOV_Main\" classid=\'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\'\
			width=\"600\"height=\"420\" codebase=\'http://www.apple.com/qtactivex/qtplugin.cab\'>\
			<param name=\'src\' value=\"'+linkHref+'\">\
			<param name=\'autoplay\' value=\"false\">\
			<param name=\'controller\' value=\"true\">\
			<param name=\'loop\' value=\"false\">\
			<EMBED src=\"'+linkHref+'\" width=\"585\" height=\"440\" autoplay=\"true\" scale=\"ASPECT\"controller=\"true\"\
			loop=\"false\" bgcolor=\"#111111\" pluginspage=\'http://www.apple.com/quicktime/download/\'></EMBED></OBJECT>');
		linkTag.innerHTML = (linkTag.innerHTML + qtTag);
	}
	if(filename.match(/.swf$/)){
		var linkTag = document.getElementById('IMG_Link');
		document.getElementById('IMG_Main').src = "images/spacer.gif";
		document.getElementById('IMG_Main').height = "0";
		var swfTag = ('<OBJECT id=\"MOV_Main\" classid=\'D27CDB6E-AE6D-11cf-96B8-444553540000\' width=\"600\" \
			height=\"420\" codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\'>\
			<param name=\'movie\' VALUE=\"'+linkHref+'\">\
			<param name=\'quality\' VALUE=\"high\">\
			<param name=\'bgcolor\' VALUE=\"#111111\">\
			<param name=\'loop\' VALUE=\"false\">\
			<EMBED src=\"'+linkHref+'\" width=\"585\" height=\"440\" name=\"'+thisTag.rel+'\" quality=high bgcolor=\"#111111\"\
			autoplay=\"true\" controller=\"true\" loop=\"false\" type=\"application/x-shockwave-flash\"\
			pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></EMBED></OBJECT>');
		linkTag.innerHTML = (linkTag.innerHTML + swfTag);
	}
	//~ setImgMaxSize();
	return true;
}

function swapVerImage(thisTag, imgSrc, linkHref)
{
	document.getElementById('IMG_Main').removeAttribute('height',0);
	document.getElementById('IMG_Main').removeAttribute('width',0);
	newImgSize(imgSrc);
	document.getElementById('IMG_Main').alt = thisTag.rel;
	document.getElementById('IMG_Main').src = imgSrc;
	document.getElementById('IMG_Link').href = linkHref;
	subtitle = (thisTag.title+thisTag.rel);
	subtitle = subtitle.replace(/%quot;/g,'"');
	document.getElementById('IMG_SubTitle').innerHTML = subtitle;
	
	var movTag = document.getElementById('MOV_Main');
	if (movTag != null){
		document.getElementById('IMG_Link').removeChild(movTag);
	}
	
	
	var filename = linkHref.toString();
	if(filename.match(/.mov$/)){
		var linkTag = document.getElementById('IMG_Link');
		document.getElementById('IMG_Main').src = "images/spacer.gif";
		document.getElementById('IMG_Main').height = "1";
		document.getElementById('IMG_Main').width = "1";
		var qtTag = ('<OBJECT id=\"MOV_Main\" classid=\'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\'\
			width=\"600\"height=\"420\" codebase=\'http://www.apple.com/qtactivex/qtplugin.cab\'>\
			<param name=\'src\' value=\"'+linkHref+'\">\
			<param name=\'autoplay\' value=\"true\">\
			<param name=\'controller\' value=\"true\">\
			<param name=\'loop\' value=\"false\">\
			<EMBED src=\"'+linkHref+'\" width=\"585\" height=\"440\" autoplay=\"true\" scale=\"ASPECT\" controller=\"true\"\
			loop=\"false\" bgcolor=\"#111111\" pluginspage=\'http://www.apple.com/quicktime/download/\'></EMBED></OBJECT>');
		linkTag.innerHTML = (linkTag.innerHTML + qtTag);
	}
	if(filename.match(/.swf$/)){
		var linkTag = document.getElementById('IMG_Link');
		document.getElementById('IMG_Main').src = "images/spacer.gif";
		document.getElementById('IMG_Main').height = "1";
		document.getElementById('IMG_Main').width = "1";
		var swfTag = ('<OBJECT id=\"MOV_Main\" classid=\'D27CDB6E-AE6D-11cf-96B8-444553540000\' width=\"600\" \
			height=\"420\" codebase=\'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0\'>\
			<param name=\'movie\' VALUE=\"'+linkHref+'\">\
			<param name=\'quality\' VALUE=\"high\">\
			<param name=\'bgcolor\' VALUE=\"#111111\">\
			<param name=\'loop\' VALUE=\"false\">\
			<EMBED src=\"'+linkHref+'\" width=\"585\" height=\"440\" name=\"'+thisTag.rel+'\" quality=high bgcolor=\"#111111\"\
			autoplay=\"true\" controller=\"true\" loop=\"false\" type=\"application/x-shockwave-flash\"\
			pluginspage=\'http://www.macromedia.com/go/getflashplayer\'></EMBED></OBJECT>');
		linkTag.innerHTML = (linkTag.innerHTML + swfTag);
	}
	return true;
}

/*Read gallery*/

function readGallery(galleryXml)
{
	
	var sdeThumb = document.getElementById("IMG_sdeThumbs");
	var verThumb = document.getElementById("IMG_vThumbs");
	var xmlDoc=parseDocument(galleryXml);
	var galleryHead	=xmlDoc.getElementsByTagName("gallery");
	if(galleryHead.length==1){
		thumbdir	= galleryHead[0].getAttribute("thumbDir");
		imgdir		= galleryHead[0].getAttribute("imgDir");
	}
		
	var imgTags	=	xmlDoc.getElementsByTagName("imageTag");
	//~ document.writeln('length - '+imgTags.length+'<br />');
	for(var j=0; j<imgTags.length; j++){
		//~ document.writeln('j is '+j);
		var mainThumb	= imgTags[j].getAttribute("mainThumb");
		var hoverThumb	= imgTags[j].getAttribute("hoverThumb");
		var imgTitle	= imgTags[j].getAttribute("mainTitle");
		var imgIndex	= imgTags[j].getAttribute("index");
		var description	= imgTags[j].getElementsByTagName("description")[0].childNodes[0].nodeValue;
		var mainThumbUrl	= (thumbdir+mainThumb);
		var hoverThumbUrl	= (thumbdir+hoverThumb);
		
		var imgEle		= imgTags[j].getElementsByTagName("image");
		
		var filename_0	= imgEle[0].getElementsByTagName("filename")[0].childNodes[0].nodeValue;
		var title_0	= imgEle[0].getElementsByTagName("title")[0].childNodes[0].nodeValue;
		var link_0		= imgEle[0].getElementsByTagName("link")[0].childNodes[0].nodeValue;
		var imgUrl_0 	= (imgdir+filename_0);
		var linkUrl_0 	= (imgdir+link_0);
		
		var thumb_html=('<a href=\"javascript:;\" rev=\"'+description+'\" rel=\"'+title_0+'\" title=\"'+imgTitle+'\" name=\"Thumb'+j+' id=\"Thumb'+j+'\" \
			onMouseOver=\"MM_swapImage(\'Thumb'+j+'\',\'\',\''+hoverThumbUrl+'\',1);\" \
			onClick=\"swapMainImage(this,\''+imgUrl_0+'\',\''+linkUrl_0+'\');verThumb(\''+galleryXml+'\',\''+imgIndex+'\');\" \
			onMouseOut=\"MM_swapImgRestore();\">\
			<img id=\"Img_Thumb'+j+'\" name=\"Img_Thumb'+j+'\" src=\"'+mainThumbUrl+'\" width=\"50\" height=\"50\" \
			title=\"'+imgTitle+'\" alt=\"'+description+'\" class=\"sdeThumb\"></a>');
			
		document.write(thumb_html);
		
		if (j%2 == 0){
			document.write('');
		}else{
			document.write('<br />');
		}
	}
	return true;
}

function verThumb(galleryXml,imgId){
	var imgTag;
	var xmlDoc			=parseDocument(galleryXml);
	var galleryHead		=xmlDoc.getElementsByTagName("gallery");
	var verThumbTd = document.getElementById("IMG_vThumbs");
	// delete all current childnodes
	var len = verThumbTd.childNodes.length;
	while (verThumbTd.hasChildNodes())
	{
		verThumbTd.removeChild(verThumbTd.firstChild);
	}

	if(galleryHead.length==1){
		var thumbdir	= galleryHead[0].getAttribute("thumbDir");
		var imgdir		= galleryHead[0].getAttribute("imgDir");
	}
	
	var imgTags	=	xmlDoc.getElementsByTagName("imageTag");
	var tag;
	for (var j=0; j<imgTags.length; j++){
		if (imgTags[j].getAttribute("index") == imgId){
			imgTag = imgTags[j];
		}
	}
	
	var imgTitle	= imgTag.getAttribute("mainTitle");
	var description	= imgTag.getElementsByTagName("description")[0].childNodes[0].nodeValue;
	var imgEle	=	imgTag.getElementsByTagName("image");
	
	for(var i=0; i<imgEle.length; i++){
		var filename	= imgEle[i].getElementsByTagName("filename")[0].childNodes[0].nodeValue;
		var thumbnail	= imgEle[i].getElementsByTagName("thumbnail")[0].childNodes[0].nodeValue;
		var hover		= imgEle[i].getElementsByTagName("hover")[0].childNodes[0].nodeValue;
		var title		= imgEle[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
		var caption		= imgEle[i].getElementsByTagName("caption")[0].childNodes[0];
		if (caption == null){
			caption = " ";
		}else{
			caption = imgEle[i].getElementsByTagName("caption")[0].childNodes[0].nodeValue;
		}
		var link		= imgEle[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
		var thumbUrl 	= (thumbdir+thumbnail);
		var hoverUrl 	= (thumbdir+hover);
		var imgUrl 		= (imgdir+filename);
		var linkUrl 	= (imgdir+link);
		var verThumb_html=('<a href=\"javascript:;\" rel=\"'+caption+'\" rev=\"'+description+'\" title=\"'+title+'\" name=\"VerThumb'+i+'\" \
			onMouseOver=\"MM_swapImage(\'VerThumb'+i+'\',\'\',\''+hoverUrl+'\',1);\" \
			onClick=\"swapVerImage(this,\''+imgUrl+'\',\''+linkUrl+'\');\"\
			onMouseOut=\"MM_swapImgRestore();\">\<img id=\"VerThumb'+i+'\" \
			name=\"VerThumb'+i+'\" src=\"'+thumbUrl+'\" width=\"32\" height=\"32\" \
			title=\"'+title+'\" alt=\"'+title+'\" class=\"verThumb\"></a>');
		if (i == 6){
			verThumb_html = (verThumb_html+'<br />');
		}else{
			verThumb_html = verThumb_html;
		}
		verThumbTd.innerHTML = (verThumbTd.innerHTML+verThumb_html);
	}
	
	
}

