/* 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

// The Photo object
function Photo(id) {
  this.id = id;
  this.desc = "";
  this.loc = "";
  this.photog = "";


  this.renderThumbnail = function() {
    t = document.createElement("div");
      t.setAttribute("id",id+"-thumb");
      t.setAttribute("class","thumb");
      t.setAttribute("onclick","renderPhoto('"+id+"')");
      i = document.createElement("img");
        i.setAttribute("src",_photoDir+id+"-thumb.jpg");
        i.setAttribute("alt",this.desc);
       
      t.appendChild(i);
      $('collection').appendChild(t);
      
  }
  
  this.preload = function() {
    pImg = new Image();
    pImg.src = _photoDir + id + ".jpg";
  }

}


// Finds a photo object by id (by searching the _photos array)
function getPhoto(id) {
	return $A(_photos).find(function(p){
	  return p.id == id;
	});
}


// Thumbnail was clicked.  Render the photo.
function renderPhoto(p) {
  // Stop IE from moving forward
  if (mode == "full") {
    // If a string was passed in, it must be an ID.  Find the correct photo in _photos
    if ((typeof p) == "string") {
      p = getPhoto(p);
    }
    
    if ($(p.id+"-thumb").className != "thumb-disabled") {
      $(p.id+"-thumb").className = "thumb-disabled"
      _zIndex += 2;
    
      b = _box;
      photo = document.createElement("div");
        photo.setAttribute("id",p.id);
        photo.setAttribute("class","photo");
        photo.setAttribute("style","z-index: "+_zIndex+"; display: ; opacity: .4;");
        photo.setAttribute("onclick","photoSelect(event,this)");
        photo.setAttribute("onmouseover","showControls(this)");
        photo.setAttribute("onmouseout","hideControls(this)");
        photo.setAttribute("desc",p.desc);
        photo.setAttribute("loc",p.loc);
        photo.setAttribute("photog",p.photog);
  
          image = document.createElement("img");
          image.setAttribute("src",_photoDir + p.id + ".jpg");
          photo.appendChild(image);
    
          info = document.createElement("div");
          info.setAttribute("id",p.id+"-info");
          info.setAttribute("class","info");
          info.setAttribute("style","display: none;");
          photo.appendChild(info);
              
          close = document.createElement("div");
          close.setAttribute("id",p.id+"-close");
          close.setAttribute("class","close");
          close.setAttribute("style","display: none;");
          close.setAttribute("onclick","new Effect.Fade(this.parentNode.id, {duration: 0.4,afterFinish:destroyPhoto})");
          photo.appendChild(close);
    
          handle = document.createElement("div");
          handle.setAttribute("id",p.id+"-handle");
          handle.setAttribute("class","handle");
          handle.setAttribute("style","display: none;");
          photo.appendChild(handle);
    
      b.appendChild(photo);
      _newPhotoOffset = (_newPhotoOffset > 60) ? 0 : _newPhotoOffset + 20;
      photo.style.left = ((b.offsetWidth/2) - (photo.offsetWidth/2)) + _newPhotoOffset;
      photo.style.top = (Element.getHeight(b)/2) + _newPhotoOffset;
      new Effect.Appear(photo, {duration: 0.4});
  
      new Draggable(p.id,{change:photoDrag});
      new Draggable(p.id+'-handle',{change:handleDrag});
      ($('sub-header-help').style.display == "") ? new Effect.Fade($('sub-header-help'), {duration: 0.4}) : "";

    } else {
      photoSelect("",$(p.id));
    }
  } else {
    $('browser-error2').style.display = "";
  }
}


// Remove photo from the box and re-enable the thumbnail
function destroyPhoto(targNode) {
  destroyDetails();
  _box.removeChild(targNode.element);
  $(targNode.element.id+"-thumb").className = "thumb";
}


// Photo resize callback
function handleDrag(targ) {
 t = targ.element;
 $(t.parentNode.id+'-close').style.display = "none";
 _zIndex += 2;
 t.parentNode.style.zIndex = _zIndex;
 // Limit minimum width to 50
 if (t.offsetLeft > 30) {
  t.parentNode.style.width = (parseFloat(t.style.left) + t.offsetWidth);
 }
 // Limit size of photo to that of its parent (width only for now)
 if (t.parentNode.offsetWidth > (t.parentNode.parentNode.offsetWidth-10)) {
   t.parentNode.style.width = t.parentNode.parentNode.offsetWidth-10;
 }
 positionControls(t.parentNode.id);
}


// Photo drag callback
function photoDrag(targ) {
 // Set variables up front for use in bounds-checking
 photoTop = targ.element.offsetTop;
 boxTop = targ.element.parentNode.offsetTop;   
 photoLeft = targ.element.offsetLeft;
 boxLeft = targ.element.parentNode.offsetLeft;
 photoHeight = Element.getHeight(targ.element);
 boxHeight = Element.getHeight(_box);
 photoWidth = targ.element.offsetWidth;
 boxWidth = targ.element.parentNode.offsetWidth;
 
 // Check against box bounds to constrain photo dragging
 // Top
 if (photoTop < boxTop) {
   targ.element.style.top = targ.element.parentNode.offsetTop;
 }
 // Bottom
 if ((photoTop + photoHeight) > (boxTop + boxHeight)) {
   targ.element.style.top = (boxHeight - photoHeight) + boxTop;
 }
 // Left
 if (photoLeft < boxLeft) {
    targ.element.style.left = boxLeft;
 }
 // Right
 if ((photoWidth + photoLeft) > (boxWidth + boxLeft)) {
   targ.element.style.left = (boxWidth - photoWidth) + boxLeft;
 }
}


// Photo clicked, move to top (z-index)
function photoSelect(e,targ) {
 _zIndex += 2;
 targ.style.zIndex = _zIndex;
 // Is this photo already selected?
 if (targ.className != "photo photo-selected") {
   // No.  Clear all photos, select this one, and handle details if needed.
   clearSelected();
   targ.className = targ.className + " photo-selected";
   destroyDetails();
   if ((e != "") && (e.target.className == "info")) {
     renderDetails(targ);
   }
 } else {
   // Yes. This one is already selected.  Toggle details if info was clicked.
   if ((e != "") && (e.target.className == "info")) {
     _details.parentNode.style.display == "" ? destroyDetails() : renderDetails(targ);
   }
 }
}


// Make sure the handle and close button are positioned correctly
function positionControls(photoID) {
  h = $(photoID+'-handle');
  c = $(photoID+'-close');
  photoWidth = h.parentNode.offsetWidth;
  
  h.style.top = (Element.getHeight(h.parentNode) - Element.getHeight(h)) - 2;
  h.style.left = (photoWidth - h.offsetWidth) - 2;
  c.style.left = (photoWidth - c.offsetWidth) - 2;;
}


// Show all of the controls
// Called on photo mouseover
function showControls(photo) {
  for (j=0; j < photo.childNodes.length; j++) {
    photo.childNodes[j].style.display = "";
  }
  positionControls(photo.id);
}


// Hides all controls (except the drag corner)
// Called on photo mouseout
function hideControls(srcElement) {
  $(srcElement.id+'-close').style.display = "none";
  $(srcElement.id+'-info').style.display = "none";
}
