// Copyright (c) 2008 Christian Schramm (http://www.cplinux.de)
// 
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

/* build new object */
function xjig_gallery() {

   /* the xml source! */
  this.xjig_file = "index.xml";
  this.xjig_thumbdir = "";
  this.xjig_basedir = "data/";
  this.xjig_base_album = "";

  /* some necessary variables */
  this.xjig_data = "";
  this.xjig_xmlhttp = false;
  this.xjig_object = false;
  this.xjig_root = false;

  /* define object functions */
  this.xjig_request = xjig_request;
  this.xjig_directories = xjig_directories;
  this.xjig_contents = xjig_contents;
  this.xjig_locate = xjig_locate;
 
}

/* build ajax request */
function xjig_request() {

	//Firefox
	if (document.implementation.createDocument) {
		this.xjig_xmlhttp = new XMLHttpRequest();
		try {
		 this.xjig_xmlhttp.open("GET", this.xjig_file, false);
		} catch (e) {
		//Patch for firefox3 users:
		 netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
		 //netscape.security.PrivilegeManager.enablePrivilege("UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead");
		 this.xjig_xmlhttp.open("GET", this.xjig_file, false);
		}
		this.xjig_xmlhttp.send(null);
		if (this.xjig_xmlhttp.readyState == 4) {
			this.xjig_object = this.xjig_xmlhttp.responseXML;
		}
	//IE7
	} else if (window.XMLHttpRequest) {
		this.xjig_xmlhttp = new XMLHttpRequest();
		this.xjig_xmlhttp.open("GET", this.xjig_file, false);
		this.xjig_xmlhttp.send();
		if (this.xjig_xmlhttp.readyState == 4) {
			this.xjig_object = this.xjig_xmlhttp.responseXML;
		}
	//IE5, IE5.5, IE6
	} else if (window.ActiveXObject) {
		this.xjig_xmlhttp = new ActiveXObject("Microsoft.XMLDOM");
		this.xjig_xmlhttp.async = false;
		this.xjig_xmlhttp.load(this.xjig_file);		
		this.xjig_object = this.xjig_xmlhttp;
	}
	

	if (typeof(this.xjig_object)=='object' && typeof(this.xjig_xmlhttp)=='object') {	
		this.xjig_root = this.xjig_object.getElementsByTagName("index")[0];
	} else {
		alert("ERROR: ajax request failed!");
	}
	
}

/* find the current location based on get parameter dir */
function xjig_locate(new_basetmp) {
	var xjig_location = window.location; //alternatively we could use window.location.search, doesnt matter..
	var tmp = "?dir=";

        var basetmp = '';
        if (new_basetmp) basetmp = new_basetmp;
        else basetmp = this.xjig_base_album;

	xjig_location = xjig_location.toString();


	if (xjig_location.lastIndexOf(tmp)!="-1") {
		xjig_location = xjig_location.substr((xjig_location.lastIndexOf(tmp)+tmp.length));
        } else if ((basetmp) && (basetmp.length > 0)) {
                xjig_location = basetmp;
	} else {
		xjig_location = false;
	}
 return xjig_location;
}

/* write available directories to the screen, using the given html code */
function xjig_directories(html) {
	var xjig_directory = "";
	var tmp = "";
	if (typeof(this.xjig_object)=='object' && typeof(this.xjig_xmlhttp)=='object') {
		for (i=0; i<this.xjig_root.getElementsByTagName("directory").length; i++) {

		  xjig_directory = html.replace(/%CONTENT%/g, this.xjig_root.getElementsByTagName("title")[i].firstChild.nodeValue);
		  xjig_directory = xjig_directory.replace(/%DIR%/g, encodeURI(this.xjig_root.getElementsByTagName("basedir")[i].firstChild.nodeValue));

		  tmp = this.xjig_root.getElementsByTagName("basedir")[i].firstChild.nodeValue;
		  if (encodeURI(tmp)==xjig_locate()) {
		   xjig_directory = xjig_directory.replace(/%ACTIVE%/g, "_active");
		  } else {
		   xjig_directory = xjig_directory.replace(/%ACTIVE%/g, "");
		  }

		  document.write(xjig_directory);

		}
	}
}

/* write available contents to the screen, using the active directory and the given html code */
/* if theres no active directory, fetch the content of the cdata section ´default´ */
function xjig_contents(html) {
	var xjig_content = false;
	var xjig_content_root = false;
	var tmp = false;
	var tmp_basedir = "";

	if (typeof(this.xjig_object)=='object' && typeof(this.xjig_xmlhttp)=='object') {

	 //if no param given, show default page, else directory content
	 if (!this.xjig_locate()) {
		if (this.xjig_root.getElementsByTagName("default")[0].firstChild.nodeValue!="") {
			document.write(this.xjig_root.getElementsByTagName("default")[0].firstChild.nodeValue);			
		}
	 	return false;
	 }

	//get current content
	for (i=0; i<this.xjig_root.getElementsByTagName("directory").length; i++) {
	 tmp = this.xjig_root.getElementsByTagName("title")[i].firstChild.nodeValue;
	 if (this.xjig_root.getElementsByTagName("basedir")[i].firstChild.nodeValue) {
	  tmp_basedir = this.xjig_root.getElementsByTagName("basedir")[i].firstChild.nodeValue;
	 }
	 if (encodeURI(tmp_basedir)==xjig_locate(this.xjig_base_album)) {

		xjig_content_root = this.xjig_root.getElementsByTagName("directory")[i];
		for (j=0; j<xjig_content_root.getElementsByTagName("file").length; j++) {

		var xjig_regexp = new RegExp("^[a-z]*\\[([0-9]+)-([0-9]+)\\]\\.[a-z]*$");
		var xjig_regexp_res = xjig_regexp.exec(xjig_content_root.getElementsByTagName("image")[j].firstChild.nodeValue);
		if (xjig_regexp_res == null) {
		//one file entry per image
			
		 xjig_content = html.replace(/%IMAGE%/g, this.xjig_basedir + "/" + tmp_basedir + "/" + xjig_content_root.getElementsByTagName("image")[j].firstChild.nodeValue);
		 xjig_content = xjig_content.replace(/%THUMB%/g, this.xjig_basedir + "/" + tmp_basedir + "/" + this.xjig_thumbdir + "/" + xjig_content_root.getElementsByTagName("thumb")[j].firstChild.nodeValue);
		 xjig_content = xjig_content.replace(/%NAME%/g, xjig_content_root.getElementsByTagName("name")[j].firstChild.nodeValue);
		 xjig_content = xjig_content.replace(/%DESC%/g, xjig_content_root.getElementsByTagName("desc")[j].firstChild.nodeValue);

		 document.write(xjig_content);

		} else {
		 //use of the regexp, example: images[1-20].jpg
			
		  if (xjig_regexp_res[1] < xjig_regexp_res[2]) {
		   for (k=xjig_regexp_res[1]; k<=xjig_regexp_res[2]; k++) {
			
		 	 xjig_content = html.replace(/%IMAGE%/g, this.xjig_basedir + "/" + tmp_basedir + "/" + xjig_content_root.getElementsByTagName("image")[j].firstChild.nodeValue.replace("["+xjig_regexp_res[1]+"-"+xjig_regexp_res[2]+"]", k));
			 xjig_content = xjig_content.replace(/%THUMB%/g, this.xjig_basedir + "/" + tmp_basedir + "/" + this.xjig_thumbdir + "/" + xjig_content_root.getElementsByTagName("image")[j].firstChild.nodeValue.replace("["+xjig_regexp_res[1]+"-"+xjig_regexp_res[2]+"]", k));
			 xjig_content = xjig_content.replace(/%COMMENT%/g, xjig_content_root.getElementsByTagName("comment")[j].firstChild.nodeValue);

			 document.write(xjig_content);

		   }
		  }		  

		}

		}

	 }
	}
	
	}
}
