var curimg=0
var random_images = new Array();
var slide_images = new Array();
//var slide_cache = new Array();
var domain = document.location.hostname;
var default_image_dir = "images/default/"
var image_dir = "images/";

var path_array = document.location.pathname.split("/");
var path_element = path_array.length;
//alert(document.location.pathname);
if(path_element >=4)   //pathname should have : /dir1/dir2/htmlpagename
{
	
	for(var i = 3; i<path_element; i++)
	{
		if(i==path_element-1)
		{
			var idx = path_array[i].indexOf(".html");
			if(idx != -1)
			{
				path_array[i] = path_array[i].substr(0,idx);
			}
		}
		image_dir = image_dir + path_array[i] + "/";
	}
	
	
}
else
{
	image_dir = default_image_dir;	
}

//functions
function xmlhttpPost(strURL, imgtagid) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   // self.xmlHttpReq.setRequestHeader('Content-Type', 'text/xml');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText, imgtagid);
        }
    }
    self.xmlHttpReq.send(null);
}


function updatepage(str, imgtag)
{
  //document.write(str);
  //document.close();
  //alert(str);
  eval(str);
  if(random_images.length >0)
  {
  	//var imgindex = Math.round(Math.random()*100) % random_images.length;
	var imgindex = Math.floor(Math.random()*random_images.length);
	//alert("http://"+domain +"/"+image_dir + random_images[imgindex]);
	var myobj = document.getElementById(imgtag);
	//alert(myobj);
	myobj.src = "http://"+domain +"/"+image_dir + random_images[imgindex];
	
  }
  if(document.getElementById("slideshow")) {
	//alert("show slides");
	/* preloading
	for(i=0;i<slide_images.length;i++) {
		slide_cache[i] = new Image();
		slide_cache[i].src = "/"+slide_image_dir + slide_images[i];
	}
	*/
	setInterval('updatepageSlide("slideshow")', 2500);
  } else {
	//alert("don't show slides");
  }
//  if(window.xmlhttpAuctionPost) {
//	xmlhttpAuctionPost("http://" + domain+ "/gallery.php?table="+path_array[path_element-1]); 
//  }
  if(window.xmlhttpStorePost) {
	xmlhttpStorePost("http://" + domain+ "/store.php"); 
  }
  if(window.xmlhttpArtistsFillPost) {
	xmlhttpArtistsFillPost("http://" + domain+ "/artistsfill.php?perf="+path_array[path_element-2]); 
  }
  if(window.xmlhttpPortfoliosPost) {
	var e = document.getElementById("years");
	if(e == undefined) {
		getyear(path_array[path_element-1]);
//		xmlhttpPortfoliosPost("http://" + domain+ "/portfolios.php?year="+path_array[path_element-1]); 
	} else {
		if(e.childNodes.length > 0) {
			var i = Math.floor(Math.random()*((e.childNodes.length-3)/2));
		}
		var y = e.childNodes[i*2].id;
//		document.getElementById(y).className = "yearactive";
//		document.getElementById(2006).className = "yearactive";
		//set the which year to start...actually the next year
		document.getElementById(2008).className = "yearactive";
		getyear('slide');
//		xmlhttpPortfoliosPost("http://" + domain+ "/portfolios.php?year="+y); 
	}
  }
}

function updatepageSlide(imgtag)
{
  if(slide_images.length >0)
  {
	var myobj = document.getElementById(imgtag);
	myobj.src = "http://"+domain +"/"+slide_image_dir + slide_images[curimg];
	curimg=(curimg<slide_images.length-1)? curimg+1 : 0
	
  }
}

function init_random_image(imgtagid)
{
	//alert("http://" + domain+ "/imgcount.php?scandir="+image_dir);
	xmlhttpPost("http://" + domain+ "/imgcount.php?scandir="+image_dir, imgtagid) 
}


