var pImages = new Array();

function showPic(whichpic) {
  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href");
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  var text = source.split("/");
  if (text.length > 2)
    source = text[text.length-2] + "/" + text[text.length-1];
  placeholder.parentNode.setAttribute("href","images/big"+source);
  placeholder.parentNode.onclick = function(){
   newwindow = window.open(placeholder.parentNode.getAttribute("href").replace(/png/,"jpg"),'bigimage','width=817px,resizable=no,scrollbars=no,,left=0px,right=0px')
    if (window.focus) {newwindow.focus()}
    return false;
  }
  if (!document.getElementById("descrip")) return false;
  if (whichpic.getAttribute("title")) {
    text = whichpic.getAttribute("title").split("=");
  } else {
    text = [""];
  }
  
  var description = document.getElementById("descrip");	
  if(description.firstChild != null)
  {
    while(description.childNodes.length > 0)
      description.removeChild(description.childNodes[0]);
    for(var i = 0; i < text.length; i++){
      description.appendChild(document.createTextNode(text[i]));
      description.appendChild(document.createElement("br"));
    }
  }
  return false;
}

function prepareGallery() {
  if (!document.getElementsByTagName) return false;
  var gallery = document.getElementById("gallery");
  var links = gallery.getElementsByTagName("a");
  var j = 0;
  for ( var i=0; i < links.length; i++) {
    if(links[i].getAttribute("rel") == "thumb"){
      links[i].onclick = function() {
        return showPic(this);
      }
      if(links[i].getAttribute("href").length > 0)
      {
        links[i].onmouseover = function() {
          swap(this,"over_");
        }
        links[i].onmouseout = function() {
          swap(this,"");
        }
        links[i].onmousedown = function() {
          swap(this,"down_");
        }
        links[i].onmouseup = function() {
          swap(this,"up_");
        }
        pImages[j] = links[i].getAttribute("href");
        j++;
      }
      links[i].onkeypress = links[i].onclick;
    }
  }
	gallery = document.getElementById("preloader");
	for(var i=0;i<pImages.length;i++){
		temp = document.createElement("img");
		temp.setAttribute("src", preload_tn(pImages[i],""));
		gallery.appendChild(temp);
		temp = document.createElement("img");
		temp.setAttribute("src", preload_tn(pImages[i],"down_"));
		gallery.appendChild(temp);
		temp = document.createElement("img");
		temp.setAttribute("src", preload_tn(pImages[i],"up_"));
		gallery.appendChild(temp);
		temp = document.createElement("img");
		temp.setAttribute("src", preload_tn(pImages[i],"over_"));
		gallery.appendChild(temp);
		temp = document.createElement("img");
		temp.setAttribute("src", pImages[i]);
		gallery.appendChild(temp);
	}
}
function preload_tn(link, suffix){
	var temp = link.split("_");
  var path = "";
  for(var i =0; i < temp.length-1; i++)
  {
    path += temp[i] + "_";
  }
 return path += "tn_" + suffix +  temp[temp.length - 1];
}
function swap(whichpic, suffix)
{
  var temp = whichpic.getAttribute("href").split("_");
  var path = "";
  var i;
  for(i =0; i < temp.length-1; i++)
  {
    path += temp[i] + "_";
  }
  path += "tn_" + suffix +  temp[temp.length - 1];
  temp = whichpic.getElementsByTagName("img");
  temp[0].setAttribute("src", path);
}


