// -----------------------------------------------------------------------------
//
//  Lightbox Extension v1.00.0
//  by Tomas Kavalek - http://www.kavalek.net
//
//  Very thanks to Dean Edwards for his cross browser event servicing functions
//
//  For more information on this script, visit:
//  http://www.kavalek.net/blog/web-development/lightbox-extension/
//
//  Licensed under the Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
//	
//  Extension for Lightbox v2.03.3 by Lokesh Dhakar - http://www.huddletogether.com
//
// -----------------------------------------------------------------------------
/*
  Table of Contents
  -----------------
  Configuration
  Global Variables

  Lightbox Extension Class Declaration
  - initialize()
  - makeLightboxImages()
  - randomName()
  - makeLightboxGalleries()
	
  Miscellaneous Functions
  - initLightbox()
  - addEvent()
  - removeEvent()
  - handleEvent()
  - fixEvent()
	
  Function Calls
  - addLoadEvent(initLightbox)
*/
// -----------------------------------------------------------------------------

//
//  Configuration
//
var iMinWidth = 300;      // Minimal image width to be accepted as image for lightbox
var iMinHeight = 200;     // Minimal image height to be accepted as image for lightbox
var iThumb = "thumbs";    // String in image path to be accepted as image for lightbox
                          // One of above have to be valid for lightbox - size or path
var gElement = "p";     // Enclosing element for gallery - images unification
var gStyle = "gallery";   // Class name for gallery enclosing element

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

//
//  Lightbox Class Declaration
//  - initialize()
//  - end()
//

var LightboxExt = Class.create();

LightboxExt.prototype = {

  // initialize()
  // Constructor runs on completion of the DOM loading. Calls makeLightboxImages
  // and makeLightboxGalleries which find all probably elements to use Lightbox
  //  
  initialize: function() {
    this.makeLightboxImages();
    this.makeLightboxGalleries(gElement, gStyle);
  },

  //
  // makeLightboxImages
  // Loops through document looking for img tags enclosed by anchor and applies 
  // lightbox references for Lightbox class. All images with pattern or greater
  // size defined in configuratian will be modified - due to not changing all 
  // images, especially images buttons and links. 
  //
  makeLightboxImages : function(e, g) {
    var e = (e == null) ? document : e;
    if(g == null) var g = 'lightbox';
    else g = 'lightbox[' + g + ']'; 
    var links = e.getElementsByTagName("a");
    var img = null;
    for(var i = 0; i < links.length; i++) {
      try {
        img = links[i].childNodes[0];
        if(img.tagName.toLowerCase() == "img" && ((img.width >= iMinWidth && img.height >= iMinHeight) || (img.src.indexOf(iThumb) != -1))) {
          links[i].setAttribute('rel', g);
        }
      } catch(err) {
      }
    }
  },
  
  //
  // randomName
  // Generate random string for unique gallery name - images unification. 
  //
  randomName : function() {
    var chars = "abcdefghijklmnopqrstuvwxyz123456789";
    var name = "";
    for(var i = 0; i < 8; i++)
      name += chars.charAt(Math.floor(Math.random() * chars.length))
    return name;
  },
  
  //
  // makeLightboxGalleries
  // Loops through document looking for 'gElement' tags with 'gClass' class 
  // and applies makeLightboxImages with gallery name via randomName.
  makeLightboxGalleries : function(e, c) {
    var galleries = document.getElementsByTagName(e);
    for(var i = 0; i < galleries.length; i++) {
      if(galleries[i].className == c) {
        this.makeLightboxImages(galleries[i], this.randomName());
      }
    }
  }
}

function initLightboxExt() { myLightboxExt = new LightboxExt(); }

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler) {
  if(element.addEventListener) {
    element.addEventListener(type, handler, false);
  } else {
    if(!handler.$$guid) handler.$$guid = addEvent.guid++;
    if(!element.events) element.events = {};
    var handlers = element.events[type];
    if(!handlers) {
      handlers = element.events[type] = {};
      if(element['on' + type]) handlers[0] = element['on' + type];
      element['on' + type] = handleEvent;
    }
    handlers[handler.$$guid] = handler;
  }
}

addEvent.guid = 1;

function removeEvent(element, type, handler) {
  if(element.removeEventListener)
    element.removeEventListener(type, handler, false);
  else if(element.events && element.events[type] && handler.$$guid)
    delete element.events[type][handler.$$guid];
}

function handleEvent(event) {
  event = event || fixEvent(window.event);
  var returnValue = true;
  var handlers = this.events[event.type];
  
  for(var i in handlers) {
    if(!Object.prototype[i]) {
      this.$$handler = handlers[i];
      if(this.$$handler(event) === false) returnValue = false;
    }
  }
  
  if(this.$$handler) this.$$handler = null;
  
  return returnValue;
}

function fixEvent(event) {
  event.preventDefault = fixEvent.preventDefault;
  event.stopPropagation = fixEvent.stopPropagation;
  return event;
}

fixEvent.preventDefault = function() {
  this.returnValue = false;
}

fixEvent.stopPropagation = function() {
  this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if(!window.addEventListener) {
  document.onreadystatechange = function() {
    if(window.onload && window.onload != handleEvent) {
      addEvent(window, 'load', window.onload);
      window.onload = handleEvent;
    }
  }
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

addEvent(window, 'load', initLightboxExt);
