/* Copyright (c) 2006 Agile Partners Corporation

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */

// This prototype explained here: http://agilepartners.com/blog/2006/02/09/browser-based-light-table

// Global variables
_zIndex = 2;
_newPhotoOffset = 0
_photoDir = "photos/";
_photos = new Array();
_box = "";
_details = "";
_detailsEdit = "";

// A really dumb browser detector that just excludes IE from the fun
agent = navigator.userAgent;
d = document;
mode = ((!d.layers) && (agent.indexOf('MSIE') == -1)) ? "full" : "degrade";


// * * * * * * * * * * * * * * * * * *
//
// Init function puts things in motion
// Photos are defined here
//
// * * * * * * * * * * * * * * * * * *

function init() {
  _box = $('box');
  _details = $('details');
  _detailsEdit = $('details-edit');
  
  photo = new Photo("f1-1"); 
  photo.desc = "Rubens Barachello practices a pit stop at the 2004 Italian Grand Prix.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);

  photo = new Photo("f1-2");
  photo.desc = "Another shot of Rubens Barachello practicing a pit stop at the 2004 Italian Grand Prix.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);
  
  photo = new Photo("f1-3");
  photo.desc = "Yet another shot of Rubens practicing.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);

  photo = new Photo("f1-4");
  photo.desc = "Fernando Alonso on a warmup lap at the 2004 Italian Grand Prix.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);
  
  photo = new Photo("f1-5");
  photo.desc = "A track marshal oversees pit lane activities.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);
  
  photo = new Photo("f1-6");
  photo.desc = "An engineer uses a laptop to warm up the Ferrari.";
  photo.loc = "Monza, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);
  
  photo = new Photo("m-1");
  photo.desc = "A business man in a hurry in front of il Duomo.";
  photo.loc = "Milan, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);
  
  photo = new Photo("m-2");
  photo.desc = "A floating pool at Lake Como.";
  photo.loc = "Lake Como, Italy";
  photo.photog = "John Berry";
  _photos.push(photo);

  // Loop the _photos collection, render thumbs and pre-load photos
  if (mode == "full") {
    
  	$A(_photos).each(function(p){
  	  p.renderThumbnail();
  	});
  	$A(_photos).each(function(p){
  	  p.preload();
  	});
  }

}



// Draws the details sidebar when the "i" button is clicked
function renderDetails(photo) {

  _details.parentNode.style.display = "none";
  
  close = document.createElement("div");
  close.setAttribute("class","details-close");
  close.setAttribute("onclick","destroyDetails()");
  
  title = document.createElement("h2");
  title.appendChild(document.createTextNode(photo.id+".jpg"));
  
  descLabel = document.createElement("h3");
  descLabel.appendChild(document.createTextNode("Description"));
  desc = document.createElement("h4");
  desc.appendChild(document.createTextNode(photo.getAttribute("desc")));
  
  locLabel = document.createElement("h3");
  locLabel.appendChild(document.createTextNode("Location"));
  loc = document.createElement("h4");
  loc.appendChild(document.createTextNode(photo.getAttribute("loc")));
  
  photogLabel = document.createElement("h3");
  photogLabel.appendChild(document.createTextNode("Photographer"));
  photog = document.createElement("h4");
  photog.appendChild(document.createTextNode(photo.getAttribute("photog")));
  
  
  editD = document.createElement("div");
  editD.setAttribute("class","details-edit-toggle");
    editA = document.createElement("a");
    editA.setAttribute("href","#");
    editA.setAttribute("onclick","toggleEdit(); return false;");
    editA.appendChild(document.createTextNode("Edit"));
  editD.appendChild(editA);
  
  _details.appendChild(close);
  _details.appendChild(title);
  _details.appendChild(descLabel);
  _details.appendChild(desc);
  _details.appendChild(locLabel);
  _details.appendChild(loc);
  _details.appendChild(photogLabel);
  _details.appendChild(photog);
  _details.appendChild(editD);
  _details.style.display = "";
  _details.parentNode.style.display = "";
  _details.style.zIndex = "999999";
  
  renderDetailsForm(photo);
}

// Renders the form for photo details editing
function renderDetailsForm(photo) {
  form = document.createElement("form");
  form.setAttribute("class","details-form");
  
  close = document.createElement("div");
  close.setAttribute("class","details-close");
  close.setAttribute("onclick","destroyDetails()");
  
  title = document.createElement("h2");
  title.appendChild(document.createTextNode(photo.id+".jpg"));
  
  descLabel = document.createElement("h3");
  descLabel.appendChild(document.createTextNode("Description"));
  desc = document.createElement("textarea");
  desc.appendChild(document.createTextNode(photo.getAttribute("desc")));
  
  locLabel = document.createElement("h3");
  locLabel.appendChild(document.createTextNode("Location"));
  loc = document.createElement("input");
  loc.setAttribute("type","text");
  loc.setAttribute("value",photo.getAttribute("loc"));
  
  photogLabel = document.createElement("h3");
  photogLabel.appendChild(document.createTextNode("Photographer"));
  photog = document.createElement("input");
  photog.setAttribute("type","text");
  photog.setAttribute("value",photo.getAttribute("photog"));
  
  
  editD = document.createElement("div");
  editD.setAttribute("class","details-edit-toggle");
    editA = document.createElement("a");
    editA.setAttribute("href","#");
    editA.setAttribute("onclick","toggleEdit(); return false;");
    editA.appendChild(document.createTextNode("Save (disabled)"));
  editD.appendChild(editA);

  
  form.appendChild(close);
  form.appendChild(title);
  form.appendChild(descLabel);
  form.appendChild(desc);
  form.appendChild(locLabel);
  form.appendChild(loc);
  form.appendChild(photogLabel);
  form.appendChild(photog);
  form.appendChild(editD);
  _detailsEdit.appendChild(form);

}

function toggleEdit() {
  if (_detailsEdit.style.display == "none") {
    _detailsEdit.parentNode.className = "details-c-edit";
    _detailsEdit.style.display = "";
    _details.style.display = "none";
  } else {
    _detailsEdit.parentNode.className = "details-c";
    _detailsEdit.style.display = "none";
    _details.style.display = "";
  }
}

// Destroy the details box
function destroyDetails() {
  _details.style.display = "none";
  _detailsEdit.style.display = "none";
  _details.parentNode.style.display = "none";
  _details.parentNode.className = "details-c";
  removeChildren(_details);
  removeChildren(_detailsEdit);
}


// De-select all photos
function clearSelected() {
  photos = document.getElementsByClassName("photo photo-selected");
  for (i=0; i < photos.length; i++) {
    photos[i].className = "photo";
  }
}

// Launch a popup with the fullsized image on doubleclick
function doubleClick(e) {
  if (e.target.parentNode.className == "photo photo-selected") {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open('"+_photoDir+e.target.parentNode.id+".jpg', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=620,height=420');");
  }
}


// Remove children from target node
function removeChildren(targ) {
  var kids = targ.childNodes;
  for (j=kids.length-1; j > -1; j--) {
    targ.removeChild(kids[j]);
  }
}


window.ondblclick=doubleClick;

