//TrailVista core page includes xmlhttp and utils for the site
//Keep the code intact copyrights trailvista.in
var myDOM;
var xmlDoc;
var xslDoc;
var xmlhttpDoc;
var xhRequest=null;
var currentTn="Home";
//var baseUrl="http://vklc48184/trailvista/"; //this value will be the actual url name
//var baseUrl="../";
function statusText(sbText)
{
window.status=sbText;
}

function tnMouseOver(tnid)
{
//alert(tnid.style.borderColor);
tnid.style.backgroundColor="#000000";
tnid.style.borderColor="#000000";
}

function tnMouseOut(tnid)
{
tnid.style.backgroundColor="#006699";
}

function linkOut(tnid)
{
tnid.style.backgroundColor="#006699";
}

function linkOver(tnid)
{
tnid.style.backgroundColor="#006699";
}

function tnClick(tnid,tntype)
{
//alert("tnid"+tnid+" tntype"+tntype);
var test=1;
showLoader("visible");
if (tntype=="1" || tntype=="2")
{
b1=""+window.location;
var b2=b1.split('.in');
baseUrl=b2[0]+".in/";
xmTnFile=baseUrl+"trvData/tn"+tnid+".xml"
currentTn=tnid;
//alert(xmlDoc);
sendRequest(xmTnFile);
}

if(tntype=="0")
	{
	if(xmlDoc)
	{
	setCanvas(tnid);
	}
	}
showLoader("hidden");
}

function parse2Display(xmDoc,xsDoc,uiElement)
{
var ex=null;
// code for IE

if (window.ActiveXObject && xmDoc &&xsDoc)
	{
  ex=xmDoc.transformNode(xsDoc);
document.getElementById(uiElement).innerHTML="Loading Thumbnails...";
document.getElementById(uiElement).innerHTML=ex;
//alert(ex);
	}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor=new XSLTProcessor();
  xsltProcessor.importStylesheet(xsDoc);
  resultDocument = xsltProcessor.transformToFragment(xmlDoc,document);
  document.getElementById(uiElement).appendChild(resultDocument);
  }
}

function createXMLHTTPObject()
{
xhRequest=null;
if (window.XMLHttpRequest) 
{
  // If IE7, Mozila
  xhRequest = new XMLHttpRequest();
}

else
{
  if (window.ActiveXObject) 
  {
     //for IE5.x and IE6.
     xhRequest = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
}

}

function sendRequest(url) 
{
try
{
statusText("Requesting Server");
	createXMLHTTPObject();
	xhRequest.open("GET",url,true);
	xhRequest.onreadystatechange = xhCallback;
	if (xhRequest.readyState == 4) return;
	xhRequest.send();
}catch(ex)
{
statusText("Error Occured -"+ex.message);
}
}

function xhCallback(response)
{
try
{
//0 = uninitialized,1 = loading,2 = loaded,3 = interactive and 4 = complete
//	if (xhRequest.readyState == 4 && xhRequest.status == 200)
	if (xhRequest.readyState == 4)
		{
//	xmlhttpDoc=xhRequest.responseXML;
handleRequest(xhRequest);
		}
if (xhRequest.readyState == 200)
	{
	setTimeout('handleRequest(xhRequest)', 10000);
	}
}catch(ex)
{
statusText("Error Occured -"+ex.message);
}
}		

function handleRequest(req) 
{
//alert("Handle Request "+req.responseText);
xmlhttpDoc=req.responseXML;
if(xmlhttpDoc && xslDoc)
	{
	xmlTnDoc=xmlhttpDoc;
	parse2Display(xmlTnDoc,xslDoc,'trvTn');
	}
setTimeout('setCanvas("Zero")',2000);
showLoader("hidden");
statusText("Ready");
}

function loadDone(xdoc)
{
//   alert(xdoc.documentElement.tagName);
return;
}

function loadXMLDoc(fname,loadDone)
{
// code for IE
//alert("loadxmldoc "+fname);
if (window.ActiveXObject)
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async=false;
  xmlDoc.load(fname);
  
  //loadDone(xmlDoc);
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation 
&& document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);
  xmlDoc.load(fname);
  xdoc.onload = function()
      {
    //     loadDone(xmlDoc);
      }

  }
else
  {
  statusText('Error Occured - : TRV1001 [Browser compatibility AJAX]');
  }

//alert("calling load");
return xmlDoc;
}

function initTrails()
{
// load the xml file
xmlhttpDoc=null;
statusText("Loading Data...");
showLoader("visible");
b1=""+window.location;
var b2=b1.split('.in');
baseUrl=b2[0]+".in/";
sendRequest(baseUrl+"trvData/tnHome.xml");
//load xsl without xmlhttp
xmlDoc=xmlhttpDoc;
statusText("Loading Styles...");
xslDoc=loadXMLDoc(baseUrl+"trvData/tnCategories.xsl",loadDone);
setTimeout('tnClick("Home",0)',2000);
showLoader("hidden");
}


function setCanvas(xCat)
{
var x;
x=xmlDoc.getElementsByTagName('trvCategory');

b1=""+window.location;
var b2=b1.split('.in');
baseUrl=b2[0]+".in/";
if(xmlhttpDoc)
{
xmlDoc=xmlhttpDoc;
x=xmlDoc.getElementsByTagName('trvCategory');
}
else
	return;
if (xCat=='Zero')
{
	xCat=x[0].getAttribute('tnName');
}
for(ix=0;ix<x.length;ix++)
{
	if(x[ix].getAttribute('tnName')==xCat)
	{

//load image	
nImageUrl=baseUrl+x[ix].getAttribute('tnPhoto');
//alert(nImageUrl);
fadePhoto("trvImage",nImageUrl);
//load header text for the panel

xText=x[ix].getAttribute('tnName');
//xText=xText+"Place:"+x[ix].getAttribute('tnPlace');
setDescription("panelHeading",xText);
	xText=x[ix].getAttribute('tnDetails');
	setDescription("trvTopTxt",xText);
//load panel Text this is below the photo
xText=x[ix].getAttribute('tnPlace')+"<br>";
xText=xText+x[ix].getAttribute('tnPhotoInfo');
xText=xText+"<p>"+x[ix].getAttribute('tnSeeAlso')+"</p>";
fadeDescription("trvPad",xText);

//	phNode=x[ix].childNodes[0].nodeValue;
	break;
	}
}
}

function fadePhoto(cImageID,nImageUrl)
{
var picholder=document.getElementById(cImageID);
if (picholder.filters) picholder.filters[0].apply();
picholder.src=nImageUrl;
if (picholder.filters)picholder.filters[0].play();
}

function fadeDescription(cDescnID,newDescn)
{
//alert(document.getElementById("trvPad").innerHTML);
var desholder;
desholder=document.getElementById(cDescnID);
desholder.innerHTML=newDescn;
if (desholder.filters)
{
desholder.filters[0].apply();
desholder.filters[0].play();
}
}

function showLoader(torf)
{
	iml=document.getElementById("imgload");
	iml.style.visibility=torf;
}

function setDescription(cDescnID,newDescn)
{
//alert(document.getElementById("trvPad").innerHTML);
var desholder;
desholder=document.getElementById(cDescnID);
desholder.innerHTML=newDescn;
}
