var currentOpenGroup = "none";
var currentDiv = "none";

function handleThumbClick(group) {
   //alert(group.parentNode.parentNode.parentNode.getAttribute("class"));
  var  currentDiv = group.parentNode.parentNode.parentNode;     
   //alert(currentDiv.getAttribute("id"));
  var  theGroup = currentDiv.getAttribute("id");
  //alert(theGroup);
 //if (!document.getElementById(theGroup + 'LargeImage') return true;
  
  var source = group.getAttribute("href");
  var placeholder = document.getElementById(theGroup + 'LargeImage');	
  placeholder.setAttribute("src",source);
  
  var text = group.getAttribute("title");
  var description = document.getElementById("description");
  description.firstChild.nodeValue = text;
  
  var description2 = document.getElementById("description2");
  description2.firstChild.nodeValue = text;

 var description3 = document.getElementById("description3");
  description3.firstChild.nodeValue = text;

  var description4 = document.getElementById("description4");
  description4.firstChild.nodeValue = text;
  
   var description5 = document.getElementById("description5");
  description5.firstChild.nodeValue = text;


  openGroup(theGroup);
  return false;
}


function openGroup(theGroup) {
	if (currentOpenGroup == theGroup) {
		return;
	} else if (currentOpenGroup != "none") {
		closeGroup(currentOpenGroup);
	}
	var bigPicture = document.getElementById(theGroup + "LargeImageArea");
	bigPicture.style.display = "block";
	document.getElementById(theGroup).className = "openPhotoSection"; //for css purposes
	currentOpenGroup = theGroup;
}

function closeGroup(theGroup) {
	document.getElementById(theGroup+ "LargeImageArea").style.display = "none";
	document.getElementById(theGroup).className = "photoSection";
}



function prepareGallery() {  
 
  var photoSecElements = getElementsByClass('photoSection',null,'div');
  //alert(photoSecElements.length);
  for ( var j=0; j < photoSecElements.length; j++) {
    //alert(photoSecElements[j].id);
    var theSection = photoSecElements[j].id;
    var gallery = document.getElementById(theSection);
    var links = gallery.getElementsByTagName("a");
    for ( var i=0; i < links.length; i++) {
      links[i].onclick = function() {
        return handleThumbClick(this);
	 }
	}
  }
  
 
 }

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(prepareGallery);
