
// var GetImgPHP = "../lib/img.php5";
// var GetImgPHP = "http://localhost/~rabit/sg09/websys/application/helpers/galleryimg_helper.php";
var GetImgPHP = "websys/application/helpers/galleryimg_helper.php";
var rootDir = "";

function test() {
    alert("test");
    //alert(self.location.host);
    //alert(self.location.hostname);
    //alert(self.location.href);
    //alert(self.location.pathname);
}



var galID = "1";

var imgID = "1";

var capt = "1";

var maximgs = "1";

function ajaxManager()
	{
	var args = ajaxManager.arguments;
	
	
	if ( args[3] ) {
		// alert(args[3] + GetImgPHP);
		GetImgPHP = args[3] + GetImgPHP;
		rootDir = args[3];
	}
	

	switch (args[0])
		{
		case "load_img":
			var obj = getHTTPObject(); // initialise the XMLHttpRequest object
			preload = document.getElementById("preloadMsg");
			gallery = document.getElementById("mainPic");
			showPreload("preloadMsg");
			
			if (obj)
				{
				obj.onreadystatechange = function()
					{
					if (obj.readyState == 0)
						{
						//preload.innerHTML = "initializing";
						preload.innerHTML = ".";
						}
					else if (obj.readyState == 1)
						{
						//preload.innerHTML = "processing request";
						preload.innerHTML = "..";
						}
					else if (obj.readyState == 2)
						{
						//preload.innerHTML = "request acknowledged";
						preload.innerHTML = "...";
						}
					else if (obj.readyState == 3)
						{
						//preload.innerHTML = "loading data..";
						preload.innerHTML = "....";
						setOpacity(0, 'mainPic');
						setOpacity(0, 'caption');
						}
					else if (obj.readyState == 4 && obj.status == 200)
						{
						el = document.getElementById(args[2]);
						el.innerHTML = obj.responseText;
						//preload.innerHTML = "Fading Image In...";
						// preload.innerHTML = "<img src=\"http://localhost/~rabit/sg09/assets/modules/gallery/image/preloadAnim.gif\" alt=\"preloadImg\" />";
						preload.innerHTML = "<img src=\"" + rootDir + "assets/modules/gallery/image/preloadAnim.gif\" alt=\"preloadImg\" />";
						document.getElementById('accessList').style.visibility = "hidden"; // noscript used instead
						setTimeout("fadeIn('mainPic', 0, '99.99')", 900);
						setTimeout("fadeIn('caption', 0, '99.99')", 1000);
						}
					}

				// load pic
				if (args[2] == 'mainPic')
				{
					// param = 'img='+ imgID +'&descr=&gal='+ galID;
					param = 'img='+ imgID +'&descr=&gal='+ galID +'&rd='+ rootDir;
				}
				// load caption
				if (args[2] == 'caption')
				{
					// param = 'img=&descr='+ capt +'&gal='+ galID;
					param = 'img=&descr='+ capt +'&gal='+ galID +'&rd='+ rootDir;
				}
				
				obj.open("POST", args[1], true);
				obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				obj.send(param);
				}
			break;

		case "cycle_gallery":
			if (args[1] == 'forw')
				{	
				if (imgID != maximgs)
					{
					imgID++;
					capt++;
					ajaxManager('load_img', GetImgPHP, 'mainPic');
					ajaxManager('load_img', GetImgPHP, 'caption');
					} else {
					imgID = "1";
					capt = "1";
					ajaxManager('load_img', GetImgPHP, 'mainPic');
					ajaxManager('load_img', GetImgPHP, 'caption');
					}
				}
			else if (args[1] == 'rev')
				{
				if (imgID != "1")
					{
					imgID--;
					capt--;
					ajaxManager('load_img', GetImgPHP, 'mainPic');
					ajaxManager('load_img', GetImgPHP, 'caption');
					} else {
					imgID = maximgs;
					capt = maximgs;
					ajaxManager('load_img', GetImgPHP, 'mainPic');
					ajaxManager('load_img', GetImgPHP, 'caption');
					}
				}
			break;
			
		case "change":
			imgID = args[1];
			capt = args[1];
			ajaxManager('load_img', GetImgPHP, 'mainPic');
			ajaxManager('load_img', GetImgPHP, 'caption');
			break;
		case "hide_access":
			document.getElementById('accessList').style.visibility = "hidden"; // noscript used instead
			break;
		case "start_up":
			galID = args[2]; // gallery ID
			maximgs = args[1]; // max number of images
			ajaxManager('hide_access'); // noscript used instead
			ajaxManager('load_img', GetImgPHP, 'mainPic');
			ajaxManager('load_img', GetImgPHP, 'caption');
			break;
		}
	}



function fadeIn(id, startfade, endfade)

	{

	timer = 0;

	if (startfade < endfade)

		{

		for (i = startfade; i <= endfade; i++)

			{

			setTimeout("setOpacity(" + i + ",'" + id + "')", (timer * 20));

			timer++;

			}
		setTimeout("hidePreload('preloadMsg')", 2000);
		}

	}


function hidePreload(id)
	{
	var el = document.getElementById(id).style;
	el.visibility = "hidden";	
	}
	
	
function showPreload(id)
	{
	var el = document.getElementById(id).style;
	el.visibility = "visible";	
	}


function setOpacity(opacity, id)

	{

	var el = document.getElementById(id).style;

	el.opacity = (opacity / 100);

	el.MozOpacity = (opacity / 100);

	el.KhtmlOpacity = (opacity / 100);

	el.filter = "alpha(opacity=" + opacity + ")";

	}




//initialise the XMLHttpRequest object
//see: http://www.webpasties.com/xmlHttpRequest
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


