// fotokey.js is the fotokey.com JavaScript library
// (c) 2003 Paul English
// $Id: fotokey.js,v 1.1 2003/08/29 01:13:17 pme Exp pme $

// var fotokey = "http://fotokey.com";

function unframe() { // js/dhtml cookbook, p.177
  if (top != self) {
    top.location.href = location.href;
  }
}

function toggle(obj) {
  obj.style.display = (obj.style.display=='') ? 'none' : ''; 
}
function fotokeyToggle(id,otherid) {
  var obj = document.getElementById(id);
  var other = document.getElementById(otherid);
  if (obj.style.display=='') { 	// change visible to invisible
    obj.style.display = "none";
    document.getElementById("vga").style.display = "";
    document.getElementById("thumb").style.display = "none";
  } else {			// change invisible to visible
    obj.style.display = "";
    other.style.display = "none";
    document.getElementById("vga").style.display = "none";
    document.getElementById("thumb").style.display = "";
  }
}

function fotokeyReset() {
  document.getElementById("vga").style.display = "";
  document.getElementById("thumb").style.display = "none";
  document.getElementById("exif").style.display = "none";
  document.getElementById("info").style.display = "none";
}

// -----------------------------------------------------------------
var buttonNames = new Array("email","camera","printer","info");

function buttonSetup() { // preload the button bar images
  if (document.images) {
    var buttons = new Array();
    var buttonsSel = new Array();
    for (var i=0; i<buttonNames.length; i++) {
      var url = "../lib/"+buttonNames[i];
      buttons[i] = new Image(18,18);
      buttons[i].src = url + ".gif";
      buttonsSel[i] = new Image(18,18);
      buttonsSel[i].src = url + "-selected.gif";
    }
  }
}
function buttonSet(img,selected) {
  var name = img.name.slice(1);	 // hack, strip initial disambiguating "b" 
  if (document.images) {
    for (var i=0; i<buttonNames.length; i++) {
      if (name==buttonNames[i]) {
        img.src = "../lib/" + name +
	  (selected ? "-selected" : "") + ".gif";
	return false;
      }
    }
  }
  return false;
}

// -----------------------------------------------------------------
// not needed; browser TAB and shift-TAB will move around thumbnails!
function fotokeyNextImage() {
  //alert("fotokeyNextImage "+document.images.length);
  for (var i=0; i<document.images.length; i++) {
    var image = document.images[i];
    if (image.border=1) {
      var j;
      if (i==(document.images.length-1))
        j = 0;
      else
        j = i+1;
      document.images[i].border = 0;
      document.images[j].border = 1;
      window.location.reload();
      return false;
    }
  }
  alert("can not find selected image");
  return false;
}

// -----------------------------------------------------------------

function setCookie(name, value, days) {
  document.cookie =  name + "=" + escape(value) +
    "; expires=" + getExpDate(days);
}
function getExpDate(days) {
  var expDate = new Date();
  expDate.setDate(expDate.getDate() + parseInt(days));
  return expDate.toGMTString();
}
function getCookie(name) {
  var dc = document.cookie;
  var index = dc.indexOf (name + "=");
  if (index == -1) return null;
  index = dc.indexOf ("=", index) + 1; // first character
  var endstr = dc.indexOf (";", index);
  if (endstr == -1) endstr = dc.length; // last character
  return unescape (dc.substring (index, endstr));
}
function deleteCookie(name) {
  document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
function viewSource() {
  window.location = "view-source:" + window.location.href;
}

// -----------------------------------------------------------------

var user_rating = -1;	// current user rating for this photo
var avg_rating = -1;	// average user rating for this photo

function starOnMouseOver(star)	{ starBarColor (star.id, -1); }
function starOnMouseOut(star)	{ starBarColor (-1, -1); }
function starOnClick(star)	{ starBarSelect (star.id); }
function starBarSelect(rating) { // mouse click or keyboard 1-5
  var uid = getCookie("uid");
  if (uid == null) {
    uid = randomID();
    setCookie("uid",uid,7);
    if (getCookie("uid")!=uid) {
      alert("You must enable JavaScript to allow photo rating.");
      return false;
    }
  }
  starBarColor (rating, rating);
  var frame = frames['iframe'];
  // send selection into to server for later http logfile analysis:
  frame.location.href = window.location.href + "?r=" + rating + "&uid=" + uid;
  starCookie (window.location.href,rating);
  return false;
}


function starSetCookie(url, id) {
  var cookie = getCookie("starBarRatings");
  // h = string2hash(cookie);
  var md5 = hex_md5(url);
  // if md5 in h, replace it; else add it
  // cookie = hash2string(h)
  // write new cookie
}
		    
function starBarColor(hover, rating) { // color the stars
  if (rating>=0) {
    user_rating = rating;
    ///alert(parent.frames[0].frames[0].ratingForm);
    ///parent.frames[0].ratingForm.rating.value = rating;
    ///alert("starBarColor "+document.ratingForm.rating.value);
  }
  for (var i=0; i<5; i++) {
    var star = document.images["star"+i];
    var color = "white";
    if (hover>=0) {
      color = (hover>=i) ? "yellow" : "white";
    } else if (user_rating>=0) {
      color = (user_rating>=i) ? "yellow" : "white";
    } else {
      color = (avg_rating>=i) ? "red" : "white";
    }
    star.src = "../lib/" + color + "star.gif";
  }
  return false;
}
function starBarDraw() {
  var r = user_rating;
  ///r = parent.frames[0].ratingForm.rating.value;
  ///alert("starBarDraw "+r);
  if (r<0) r = 0; /// hack
  for (var i=0; i<5; i++) {
    document.writeln("<img src=../lib/" +
      ((i<r) ? "redstar.gif" : "whitestar.gif") +
      " alt=\"Click to rate this photo\"\n" +
      " width=16 height=14 " +
      " id=" + i + " name=star" + i + "\n" +
      " onMouseOver='starOnMouseOver(this);'\n" +
      " onMouseOut='starOnMouseOut(this);'\n" +
      " onClick='starOnClick(this);'>\n");
  }
}

// -----------------------------------------------------------------

function fotokeyKey(event) {
  if (event==null) event = window.event; 
  if (event==null || event.keyCode==null) return true;
  var key = event.keyCode;
  if (key>=49 && key<=53) starBarSelect(key-49); // rating 1-5
  else if (key==37 || key==39) fotokeyNav(key); // prev or next arrow
  else if (key==38) location.href = "../"; // up arrow
  else {
    ///alert("fotokeyKey "+key);
    return true;
  }    
  return false;
}

// -----------------------------------------------------------------
    
function randomID() {
  return Math.floor(Math.random() * 0xffffffff);
}
<!-- end:fotokey.js --> 
