var xmlDoc, strAlbumXML, strPath, strImageName, strGalleryXML, blnAlbumFile;
strPath = window.location.protocol + "//" + window.location.host + window.location.pathname;
strPath = replace(strPath, "\\", "/", false, false);
strPath = strPath.substr(0, strPath.lastIndexOf("/") + 1);
strGalleryXML = strPath + "gallery-" + getQueryVar("gid") + ".xml";
if (getQueryVar("gid") != "NONE") {
	strGalleryXML = strPath + "gallery-" + getQueryVar("gid") + ".xml";
} else {
	strGalleryXML = strPath + "gallery-" + gid.toString() + ".xml";
}
if (getQueryVar("aid") != "NONE") {
	strAlbumXML = strPath + "album-" + getQueryVar("aid") + ".xml";
	blnAlbumFile = true;
	loadXML(strAlbumXML);
} else {
	blnAlbumFile = false;
	loadXML(strGalleryXML);
}

function getQueryVar(strVariable) {  
	var strQuery = window.location.search.substr(1, window.location.search.length - 1);
	var arrVars = strQuery.split("&");
	for (var i = 0; i < arrVars.length; i++) {
		var arrPair = arrVars[i].split("=");
		if (arrPair[0] == strVariable) {
			return(arrPair[1]);
		}
	}
	return("NONE");
}

function loadXML(strXMLPath) {
	try {
    	xmlDoc = new XMLHttpRequest();
  	} catch (e) {
    	xmlDoc = new ActiveXObject("Msxml2.XMLHTTP");
  	}
  	xmlDoc.open("GET", strXMLPath, false);
  	xmlDoc.send(null);
	processFile();
}

function processFile() {
	if (!blnAlbumFile) {
		strAlbumXML = strPath + "album-" + xmlDoc.responseXML.getElementsByTagName("album-id")[0].childNodes[0].nodeValue + ".xml";
		blnAlbumFile = true;
		loadXML(strAlbumXML);
	} else {
		strImageName = xmlDoc.responseXML.getElementsByTagName("slide-imagecontent")[0];
		strImageName = strImageName.getElementsByTagName("path")[0].childNodes[0].nodeValue;
		strImageName = replace(strImageName, "\\", "/", false, false);
		strImageName = strImageName.substr(strImageName.lastIndexOf("/") + 1, strImageName.length - strImageName.lastIndexOf("/") + 1);
	}
}

function replace(target, oldTerm, newTerm, caseSens, wordOnly) {
    var work = target;
    var ind = 0;
    var next= 0;
    if(!caseSens){
        oldTerm = oldTerm.toLowerCase();
        work    = target.toLowerCase();
    }
    while((ind = work.indexOf(oldTerm, next)) >= 0){
        if(wordOnly){
            var before = ind - 1;
            var after = ind + oldTerm.length;
            if(!(space(work.charAt(before)) && space(work.charAt(after)))){
                next = ind + oldTerm.length;
                continue;
            }
        }
        target = target.substring(0, ind) + newTerm + target.substring(ind+oldTerm.length, target.length);
        work   = work.substring(0, ind)   + newTerm + work.substring(ind+oldTerm.length, work.length);
        next   = ind + newTerm.length;
        if(next >= work.length){ break; }
    }
    return(target);
}

function space(check) {
    var space=" .,/<>?!`';:@#$%^&*()=-|[]{}" + '"' + "\\\n\t";
    for(var i=0; i<space.length; i++)
    if(check==space.charAt(i)){ return true; }
    if(check=="")             { return true; }
    if(check==null)           { return true; }
    return(false);
}
