/* This script is based on Thickbox by Cody Lindley (http://jquery.com/demo/thickbox)
   Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
   Copyright (c) 2007 Rafael Holt (http://raffles.awardspace.com) */
//If you don't want Thickbox to be active when the page loads, comment (add //) at the start of the last two lines in this file.
//Alternatively, call Thickbox.init() elsewhere in a script of your own.
//To stop Thickbox's attachment to anchors in your script, call Thickbox.init('kill').
var Thickbox = {
  init: function(what) {
    if (what !== 'kill') {
      var o = document.createElement('div');
      var w = o.cloneNode(true);
      var ww = o.cloneNode(true);
      var l = o.cloneNode(true);
      var d = o.cloneNode(true);
      var li = document.createElement('img');
      var i = document.createElement('img');
      var c = document.createElement('span');
      var p = document.createElement('p');
      o.id = 'Tb_overlay';
      w.id = 'Tb_wra';
      ww.id = 'Tb_window';
      l.id = 'Tb_load';
      li.src = '/thickbox/loading.gif';
      li.alt = 'loading...';
      c.id = 'Tb_close';
      c.title = 'Close';
      c.appendChild(document.createTextNode('close'));
      l.appendChild(li);
      ww.appendChild(i);
      ww.appendChild(p);
      ww.appendChild(c);
      ww.appendChild(d);
      w.appendChild(ww);
      document.body.appendChild(o);
      document.body.appendChild(l);
      document.body.appendChild(w);
      var bits = {
        'overlay': o,
        'loading': l,
        'wra': w,
        'image': i,
        'p': p,
        'd': d,
        'closelink': c
      };
      for (var prop in bits) Thickbox[prop] = bits[prop];
      if (w.currentStyle && w.currentStyle.position === 'absolute') Thickbox.ie6 = true;
    }
    else {
      document.body.removeChild($i('Tb_overlay'));
      document.body.removeChild($i('Tb_wra'));
      document.body.removeChild($i('Tb_load'));
    }
    var anchors = $c('thickbox');
    for (var i = 0, j = anchors.length; i < j; i++) {
      var anchor = anchors[i];
      if (what !== 'kill') anchor.onclick = function(e) {
        var a = this.rel ? anchors : [this];
        var index = Thickbox.makeGroup(this, a);
        if (Thickbox.group.length && index !== false) {
          this.blur();
          if (!e) window.event.returnValue = false;
          else e.preventDefault();
          Thickbox.unhide('overlay', 'loading');
          Thickbox.show(index);
        }
      }
      else anchor.onclick = null;
    }
  },

  makeGroup: function(el, a) {
    Thickbox.group = [];
    var count = -1;
    for (var i = 0, j = a.length; i < j; i++) {
      var anchor = a[i], item;
      if (anchor.rel === el.rel) {
        item = makeItem(anchor);
        if (!item) continue;
        Thickbox.group.push(item);
        count++;
        if (el.href === anchor.href) var current = count;
      }
    }
    return current;
    function makeItem(anchor) {
      if (anchor.href.indexOf('#TB_inline') > -1) {
        var query = parseQuery(anchor.href.split('?')[1]);
        caption = query.caption === 'true' ? anchor.title : null;
        if (!query.id) return false;
        itemtype = 'inline';
        if (!Thickbox.inline) {
          Thickbox.inline = document.createElement('div');
          Thickbox.inline.id = 'Tb_inline';
          Thickbox.wra.firstChild.insertBefore(Thickbox.inline, Thickbox.wra.firstChild.firstChild);
        }
      }
      else {
        caption = anchor.title || null;
        itemtype = 'image';
      }
      var item = { type: itemtype, caption: caption };
      if (itemtype === 'image') {
        item.imageLoaded = false;
        item.url = anchor.href;
      }
      else {
        item.height = parseInt(query.height) || 400;
        item.width = parseInt(query.width) || 600;
        item.id = query.id;
      }
      return item;
    }
  },

  show: function(i) {
    if (Thickbox.ie6) Thickbox.fixed();
    if (Thickbox.group[i].type === 'image') Thickbox.preload(i);
    else Thickbox.output(i);
    Thickbox.overlay.onclick = Thickbox.end;
    document.onkeyup = function(e){
      if (!e) var e = window.event;
      if(e.keyCode === 27 || e.keyCode === 67 || e.keyCode === 88) Thickbox.end();
    }
  },

  preload: function(i) {
    if (Thickbox.overlay.style.display !== 'block') return;
    var prel = new Image();
    prel.onload = function() {
      Thickbox.fitsize = Thickbox.resize(prel);
      Thickbox.fullsize = [prel.width, prel.height];
      Thickbox.output(i);
    }
    prel.src = Thickbox.group[i].url;
  },

  resize: function(image) {
    var viewport = Thickbox.viewport();
    var x = viewport[0] - 60;
    var y = viewport[1] - 150;
    var imageWidth = image.width;
    var imageHeight = image.height;
    if (imageWidth > x) {
      imageHeight = imageHeight * (x / imageWidth);
      imageWidth = x;
      if (imageHeight > y) {
        imageWidth = imageWidth * (y / imageHeight);
        imageHeight = y;
      }
    } else if (imageHeight > y) {
      imageWidth = imageWidth * (y / imageHeight);
      imageHeight = y;
      if (imageWidth > x) {
        imageHeight = imageHeight * (x / imageWidth);
        imageWidth = x;
      }
    }
    return [imageWidth, imageHeight];
  },

  output: function(index) {
    var current = Thickbox.group[index];
    Thickbox.image.src = current.url;
    Thickbox.hide('image', 'inline', 'iframe', 'd', 'p');
    Thickbox.unhide(current.type);
    var caption = current.caption;
    if (current.type === 'inline') {
      var cid = $i(current.id);
      Thickbox.inline.innerHTML = cid ? cid.innerHTML : '<p>Bad id given.</p>';
      Thickbox.inline.style.height = current.height + 'px';
      Thickbox.inline.style.width = current.width + 'px';
    }
    if (caption) {
      if (Thickbox.p.childNodes.length) Thickbox.p.firstChild.nodeValue = caption;
      else Thickbox.p.appendChild(document.createTextNode(caption));
      Thickbox.unhide('p');
    }
    if (Thickbox.group.length > 1) {
      var curcount = 'Image '+(index+1)+' of '+Thickbox.group.length;
      if (!Thickbox.d.childNodes.length) {
        var c = document.createElement('span');
        c.appendChild(document.createTextNode(curcount));
        Thickbox.d.appendChild(c);
      }
      else Thickbox.d.firstChild.firstChild.nodeValue = curcount;
      Thickbox.unhide('d');
    }
    var sl = $i('Tb_sh');
    if (!sl && Thickbox.group.length > 1) {
      var sl = document.createElement('span');
      sl.appendChild(document.createTextNode('Start'));
      sl.id = 'Tb_sh';
      sl.title = 'Start slideshow';
      Thickbox.d.appendChild(sl);
    }
    var pl = Thickbox.nlinks(index-1, 'prev');
    var nl = Thickbox.nlinks(index+1, 'next');
    if (pl) pl.onclick = function() {Thickbox.handle(index-1)}
    if (nl) nl.onclick = function() {Thickbox.handle(index+1)}
    else if (sl) Thickbox.stop(sl);
    if (sl) sl.onclick = slhandle;
    function slhandle() {
      if (!Thickbox.sh) {
        if (!nl) Thickbox.handle(0);
        else Thickbox.start(this, index+1);
      }
      else Thickbox.stop(this);
    }
    document.onkeydown = function(e) {
      if (!e) var e = window.event;
      switch(e.keyCode) {
        case 80: // p
        case 188: if (pl) Thickbox.handle(index-1); break; // <
        case 78: // n
        case 190: if (nl) Thickbox.handle(index+1); break; // >
        case 83: slhandle.call(sl); break; // s
      }
    }
    if (pl) {
      var prev = new Image();
      prev.onload = function() {Thickbox.group[index-1].imageLoaded = true;}
      prev.src = Thickbox.group[index-1].url;
    }
    if (nl) {
      var next = new Image();
      next.onload = function() {Thickbox.group[index+1].imageLoaded = true;}
      next.src = Thickbox.group[index+1].url;
    }
    var resized = Thickbox.zoom('out', current);
    if (resized) Thickbox.image.onclick = function() {
      if (hasClass(Thickbox.wra, 'Tb_zoomed')) Thickbox.zoom('out', current);
      else Thickbox.zoom('in', current);
    }
    else Thickbox.image.onclick = null;
    Thickbox.closelink.onclick = Thickbox.end;
    Thickbox.overlay.onclick = Thickbox.end;
    Thickbox.wra.onclick = function(e) {
      if (!e) var e = window.event;
      var t = e.target || e.srcElement;
      if (t.id === 'Tb_wra') Thickbox.end();
    }
    Thickbox.wra.style.visibility = '';
    Thickbox.hide('loading');
    Thickbox.unhide('wra');
  },

  nlinks: function(index, ident) {
    var nid = $i('Tb_'+ident);
    var i = Thickbox.group[index];
    if (i && !nid) {
      var li = document.createElement('span');
      li.id = 'Tb_'+ident;
      li.title = i.caption;
      var fe = ident === 'prev' ? 'rev' : 'Next >';
      if (ident === 'prev') {
        var f = document.createElement('span');
        f.appendChild(document.createTextNode('P'));
        li.appendChild(document.createTextNode('< '));
        li.appendChild(f);
      }
      li.appendChild(document.createTextNode(fe));
      if (ident === 'prev' && $i('Tb_next')) Thickbox.d.insertBefore(li, Thickbox.d.firstChild.nextSibling);
      else Thickbox.d.insertBefore(li, Thickbox.d.lastChild);
      return li;
    }
    else if (!i && nid) {Thickbox.d.removeChild(nid); return false}
    else if (i && nid) {nid.title = i.caption; return nid}
  },

  handle: function(i) {
    if (Thickbox.group[i].imageLoaded !== true) {
      Thickbox.unhide('loading');
    }
    Thickbox.wra.style.visibility = 'hidden';
    Thickbox.show(i);
  },

  hide: function() {
		for (var i = 0; i < arguments.length; i++) {
  		var el = Thickbox[arguments[i]];
  		if (!el) continue;  		
  		el.style.display = 'none';
		}
  },

  unhide: function() {
    for (var i = 0; i < arguments.length; i++) {
      var el = Thickbox[arguments[i]];
  		if (!el) continue;
  		el.style.display = el.tagName.toLowerCase() === 'img' ? 'inline' : 'block';
		}
  },

  end: function() {
    Thickbox.hide('loading','wra','overlay');
    Thickbox.wra.style.position = '';
    if (Thickbox.sh) Thickbox.stop();
    if (Thickbox.ie6) Thickbox.fixed(true);
  },

  start: function(e, i) {
    Thickbox.sh = window.setInterval(function() {
      Thickbox.handle(i);
      i++;
    },2500);
    e.firstChild.nodeValue = 'Stop';
    e.title = 'Stop slideshow';
  },

  stop: function(e) {
    if (!e) e = $i('Tb_sh');
    window.clearInterval(Thickbox.sh);
    Thickbox.sh = null;
    e.firstChild.nodeValue = 'Start';
    e.title = 'Start slideshow';
  },

  position: function(zoom, docHeight, full, fit) {
    var scrolled = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    var vheight = Thickbox.viewport()[1];
    if (zoom) var boxheight = Thickbox.wra.offsetHeight || (zoom === 'in' ? full[1]+80 : fit[1]+80);
    if (zoom === 'in') {
      var diff = (full[1] - fit[1])/2;
      if (vheight > boxheight) Thickbox.wra.style.top = scrolled + (vheight/2 - boxheight/2) + 'px';
      else if (scrolled > diff && docHeight > boxheight + 40) {
        if (docHeight < scrolled + 18 + boxheight) {
          Thickbox.wra.style.top = 'auto';
          Thickbox.wra.style.bottom = '18px';
        }
        else Thickbox.wra.style.top = (scrolled - diff) + 'px';
      }
    }
    else {
      var sgap = (vheight/2 - boxheight/2);
      if (Thickbox.ie6) {
        Thickbox.loading.style.top = (vheight/2 + scrolled) + 'px';
        sgap += scrolled;
      }
      Thickbox.wra.style.top = sgap + 'px';
      Thickbox.wra.style.bottom = 'auto';
    }
  },

  zoom: function(z, current) {
    var full, fit, w = Thickbox.wra, box = Thickbox[current.type], resized = false;
    if (current.type !== 'image') full = fit = [current.width, current.height];
    else {
      full = Thickbox.fullsize;
      fit = Thickbox.fitsize;
    }
    var docHeight = document.documentElement.scrollHeight;
    if (z === 'out') {
      w.firstChild.style.width = Math.max(fit[0] + 20, 260) + 'px';
      removeClass(w, 'Tb_zoomed');
      box.width = fit[0];
      box.height = fit[1];
      if (current.type === 'image' && (fit[0] !== full[0] || fit[1] !== full[1])) {
        resized = true;
        var title = resized ? 'Click to zoom in to full size' : 'Full-size image';
        box.title = title + ' (' + full[0] + ' x ' + full[1] + ' pixels)';
      }
      if (Thickbox.ie6) window.onscroll = Thickbox.position;
    }
    else if (z === 'in') {
      box.removeAttribute('width');
      box.removeAttribute('height');
      w.firstChild.style.width = Math.max(full[0] + 20, 260) + 'px';
      box.title = 'Click to fit to window';
      w.className += ' Tb_zoomed';
      if (Thickbox.ie6) window.onscroll = null;
    }
    Thickbox.position(z, docHeight, full, fit);
    return resized;
  },

  fixed: function(end) {
    var sels = document.getElementsByTagName('select');
    var vis = end ? 'visible' : 'hidden';
    for (var i=0; i < sels.length; i++) sels[i].style.visibility = vis;
    if (end) return;
    w = Thickbox.viewport()[0];
    Thickbox.overlay.style.width = w + 'px';
    Thickbox.overlay.style.height = document.documentElement.scrollHeight + 'px';
    Thickbox.wra.style.width = w + 'px';
  },

  viewport: function() {
    var de = document.documentElement;
    var w = self.innerWidth || de.clientWidth;
    var h = self.innerHeight || de.clientHeight;
    return [w,h];
  }
};
function $i(e) {return document.getElementById(e)}
function $c(c) {
	var children = document.getElementsByTagName('*');
	var els = [];
	for (var i = 0, j = children.length; i < j; i++) if (hasClass(children[i], c)) els.push(children[i]);
	return els;
}
function hasClass(el, c) {
if (!el || !el.className.length) return false;
var bits = el.className.split(' '), has = false;
for (var j = 0; j < bits.length; j++) if (bits[j] === c) has = true;
return has;
}
function addClass(el, c) {
if (!el || hasClass(el, c) === true) return;
if (el.className.length) el.className += ' '+c;
else el.className = c;
}
function removeClass(el, c) {
  if (!el || !el.className.length || !hasClass(el, c)) return;
  var bits = el.className.split(' ');
  var newClass = '';
  for (var i=0; i < bits.length; i++) if (bits[i] !== c) newClass += bits[i]+' ';
  el.className = newClass;
}
function parseQuery(q) {
  var parameters = {}, bits = q.split('&'), pair;
  for (var q = 0, r = bits.length; q < r; q++) {
    pair = bits[q].split('=');    
    parameters[pair[0]] = pair[1];
  }
  return parameters;
}
if (window.addEventListener) window.addEventListener('load', Thickbox.init, false);
else if (window.attachEvent) window.attachEvent('onload', Thickbox.init);