// JavaScript Document

// JScript File
var ignoreBackPage = 0;

//handles the click event for the previous page button.  sets the new page number and gets
//the image urls
function PreviousPageClick()
{
  if(page_number > 1)
  {
      page_number = page_number-1;
      GetImageUrls();
  }
}

//handles the click event for the next page button.  sets the new page number and gets
//the image urls
function NextPageClick()
{
  if(page_number < total_pages)
  {
      page_number = (page_number*1)+1;//boo stupid scripting language,.. yeah beer
      GetImageUrls();
  }
}


/*function PageClick(dv)
{
	dv.className = "pageSelected";
    page_number = (dv.id.replace("Page","")*1);
    GetImageUrls();
}*/

//gets the list of thumbnail image urls to display on a the gallery.  based on the selected page
//number, it displays only the subest of 7 images to be displayed on that page, which is loaded
//by LoadThumbnailTable()
function GetImageUrls()
{
  if(unescape(window.location).toLowerCase().indexOf("gallery.aspx")>-1)
  {
//		var backpage = getQueryVariable("page");
//		if (backpage.length > 0 && 
//			ignoreBackPage != 1 && 
//			document.referrer.indexOf('FullImage.aspx') > -1)
//		{
//			page_number = backpage;
//			ignoreBackPage = 1
//		}
		//shouldnt need these two lines, looks like it styles the selected page to be bolded in the nav
//		ClearClassFromPages();
//		document.getElementById("Page" + page_number).className = "pageSelected";
		
    //slice out the 7 images from the thumbnails array to be displayed on this page
    high_ix = page_number*7;
    if(high_ix > thumbnails.length)
        high_ix = thumbnails.length;
    low_ix = (page_number-1)*7;
    var thumbs = thumbnails.slice(low_ix,high_ix);
    LoadThumbnailTable(thumbs);
  }
}

///function to load the 7 thumbnails from the selected page into the thumbnail table.  dynamically
//creates the images and adds them to the table
function LoadThumbnailTable(urls)
{
	
  var thumb_table = document.getElementById("ThumbnailTable");
  //clear table
  while(thumb_table.childNodes.length > 0)
  {
		thumb_table.removeChild(thumb_table.firstChild);
  }
  var row = document.createElement('tr');
  
  var navLefttd = document.createElement('td');
  var imagestd = document.createElement('td');
  var navRighttd = document.createElement('td');
  
  var navLefta = document.createElement('a');
  navLefta.setAttribute("href", "javascript:PreviousPageClick()");
  
  var navLeft = document.createElement('img');
  navLeft.setAttribute("src", "images/gallery/arrowLeft_gallery.gif");
  navLeft.setAttribute("alt", "<");
  navLeft.setAttribute("class", "navImage");
  
  navLefta.appendChild(navLeft);
  navLefttd.appendChild(navLefta);  
  
  var imagestr = document.createElement('tr');
  for(i=0;i<7;i++)
  {
		if(i>=urls.length)
			break;
		var image_id = urls[i].split("_")[0];
		//var td = document.createElement('td');
		var a = document.createElement('a');
		
		//set the url of the full image.  this will be utilized in the swapImage() function
		//still needs to be tweaked
//		if (site != null && site.length > 0)
//		{
//			a.setAttribute("href","FullImage.aspx?ImageIndex="+(((page_number-1)*7)+i)+"&page=" + page_number + "&site=" + site);
//		}
//		else
//		{
	    //a.setAttribute("href","FullImage.aspx?ImageIndex="+(((page_number-1)*7)+i)+"&page=" + page_number);
		//a.setAttribute("href", "javascript:imageSwap('"+imgPath+"','"+imgAlt+"');");
		//a.setAttribute("onclick", "MM_swapImage()");
		//}
		var img = document.createElement('img');
		img.setAttribute('src',"images/gallery/"+urls[i].split("|")[0]);
		img.setAttribute("class", "galleryThumb");
		var imgPath = String(img.getAttribute('src'));
		imgPath = imgPath.replace("thumb","full");
		//img.setAttribute("border","0");
		var imgAlt = "";
		try
		{
			img.setAttribute('alt',urls[i].split("|")[1]);
			imgAlt =  String(img.getAttribute('alt'));
		}
		catch(err){;}
		//a.setAttribute("onclick", "imageSwap('"+imgPath+"','"+imgAlt+"');return false");
		//a.setAttribute("onClick", "alert('sadsasd')");
		a.setAttribute("href", "javascript:imageSwap('"+imgPath+"','"+imgAlt+"');");
		
		a.appendChild(img);
		//td.appendChild(a);
		imagestd.appendChild(a);
  }
  var navRighta = document.createElement('a');
  navRighta.setAttribute("href", "javascript:NextPageClick()");
  
  var navRight = document.createElement('img');
  navRight.setAttribute("src", "images/gallery/arrowRight_gallery.gif");
  navRight.setAttribute("alt", ">");
  navRight.setAttribute("class", "navImage");
  
  navRighta.appendChild(navRight);
  navRighttd.appendChild(navRighta);  
  
  row.appendChild(navLefttd);
  row.appendChild(imagestd);
  row.appendChild(navRighttd);
  thumb_table.appendChild(row)
    
}

function imageSwap(imgPath, imgAlt)
{
    var largeImage = document.getElementById("ctl00_mainContent_largeImage");
    largeImage.setAttribute("src", imgPath);
    largeImage.setAttribute("alt", imgAlt);
}

//clears the className from the selected page
function ClearClassFromPages()
{
	for (var i=1;i<=total_pages;i++)
	{
		document.getElementById("Page" + i).className = "";
	}
}

//i think this function is only necessary if we nav to a new page for full image views
//function getQueryVariable(variable) {
//  var query = window.location.search.substring(1);
//  var vars = query.split("&");
//  for (var i=0;i<vars.length;i++) {
//    var pair = vars[i].split("=");
//    if (pair[0] == variable) {
//      return pair[1];
//    }
//  } 
//  return "";
//}




startList = function() {
	GetImageUrls();
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";

				}
				node.onmouseout=function() {
	
this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
window.onload=startList;

function GalleryStart()
{
	GetImageUrls();
	startList();
}

