var thumbnail_only = false;

if ("undefined" != typeof(cookieName)) {
  var currentImg = getCookie(cookieName);
  if (currentImg == null) {
	currentImg = 1;
  }
}


function copyright() {
  document.write("&copy; Copyright 2002-09. ");
}


function lastmodify() {
  var lastmod = new Date(document.lastModified);
  if (lastmod != 0) {
	document.write("Last modified on " + lastmod.getFullYear() + "." + (lastmod.getMonth()+1) + "." + lastmod.getDate() + " ");
  }
}


function isThumbnailOnly() {
  thumbnail_only = true;
  document.getElementById("albumView").innerHTML = 'Only thumbnails are displayed here - please <a href="/contact.html">contact me</a> for original photo.';
}


function openPhoto(cookie,id,url) {
  if (thumbnail_only) {
	alert("Only thumbnails are displayed here.\nPlease contact me for original photo.\n- michael");
  } else {
	if (url == null) {
	  url = "photos.html";
	}
	setCookie(cookie,id);
	window.open(url,"_self");
  }
}


// Cookie path fixed for entire domain
function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString())) + "; path=/";
}


function getCookie(name) {
  var search = name + "=";
  if (document.cookie.length > 0) { // if there are any cookies
	var offset = document.cookie.indexOf(search);
	if (offset != -1) { // if cookie exists
	  offset += search.length;
	  var end = document.cookie.indexOf(";", offset);	// set index of beginning of value
	  if (end == -1) {end = document.cookie.length;}	// set index of end of cookie value
	  return unescape(document.cookie.substring(offset, end));
	}
  }
}


function clicker(id) {
  if ((id>0) && (id<=imgTotal)) {
	currentImg = id;
	chgImg(id);
  }
}


function flipper(mode) {
  if ((mode=='next') && (currentImg<=imgTotal)) {
	currentImg++;
  } else if ((mode=='previous') && (currentImg>0)) {
	currentImg--;
  } else {
	return;
  }
  chgImg(currentImg);
}


function chgImg(id) {
  var mHTML1 = '<div><span class="caption" id="previous"><a accesskey="p" title="Alt+P" href="javascript:flipper(' + "'" + 'previous' + "'"  + ');">&larr; Prev</a></span> ';
  mHTML1 += '<span class="caption"><a title="click to enlarge; right click to select save option" href="' + filenames[id-1] + '">' + filenames[id-1] + '</a></span> ';
  mHTML1 += '<span class="caption" id="next"><a accesskey="n" title="Alt+N" href="javascript:flipper(' + "'" + 'next' + "'" + ');">Next &rarr;</a></span>';
  var mHTML2;
  var file = filenames[id-1].toLowerCase();
  var fileExtension = file.substr(file.length-4, 4);

/*  var mHTML3 = comments[id-1];
  if (mHTML3.length!=0) {
	mHTML3 = '<br /><span class="caption">' + comments[id-1] + '</span>';
  }
*/

  if ((fileExtension==".avi") || (fileExtension==".mpg") || (fileExtension=="mpeg")) {
	mHTML2 = '<br /><object id="video" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" standby="Loading Windows Media Player components..." ';
	mHTML2 += 'type="application/x-oleobject"><param name="FileName" value="' + filenames[id-1];
	mHTML2 += '" /><param name="AutoStart" value="true" /><param name="ShowControls" value="true" /> ';
	mHTML2 += '<param name="AutoSize" value="true"> </object>';
/*	mHTML2 += mHTML3;*/
	mHTML2 += '<br /><span class="caption"><a href="' + filenames[id-1] + '">Save Video</a> <p></p>' + sizes[id-1] + '   (' +id +" of " + imgTotal +')</span></div>';
  } else {
	mHTML2 = '<br /><a href="' + filenames[id-1] + '" title="click to enlarge; right click to select save option"><img id="photo" src="' + filenames[id-1] + '" /></a>';
/*	mHTML2 += mHTML3;*/
	mHTML2 += '<br /><span class="caption">' + widths[id-1] + ' x ' + heights[id-1] + ' pixels - ' + sizes[id-1] + '   (' +id +" of " + imgTotal +')</span></div>';
  }

  document.getElementById("mainPhoto").innerHTML = mHTML1 + mHTML2;
  setCookie(cookieName,id);

  if ((id>1) && (id<imgTotal)) {		// in between
	document.getElementById("previous").style.visibility = "visible";
	document.getElementById("next").style.visibility = "visible";
  }
  else if (id>=imgTotal) {			// last image
	document.getElementById("previous").style.visibility = "visible";
	document.getElementById("next").style.visibility = "hidden";
  }
  else if (id<=1) {				// first image
	document.getElementById("previous").style.visibility = "hidden";
	document.getElementById("next").style.visibility = "visible";
  }
}

// Added 20090217; use for "Album Counter" in Album index page
function getElementsByClass(theClass,node) {
    var classElements = [];
    var i;
    if ( node == null ) {
    	node = document
    }
    if (node.getElementsByClassName) {
    	var tempCollection = node.getElementsByClassName(theClass);
        for (i = 0; i < tempCollection.length ; i++) {
    		classElements.push(tempCollection[i])
    	}
    }
    else {
    	var els = node.getElementsByTagName("*");
    	var elsLen = els.length;
    	var pattern = new RegExp("(^|\\s)"+theClass+"(\\s|$)");
    	for (i = 0; i < elsLen; i++) {
    		if ( pattern.test(els[i].className) ) {
    			classElements.push(els[i]);
    		}
    	}
    }
    return classElements;
};