/*
Tweaked version of	
Slimbox v1.1 by Christophe Beyls (http://www.digitalia.be) - MIT-style license.	Inspired by the original Lightbox v2 by Lokesh Dhakar.
*/

var Lightbox = {

	init: function(options) {
		this.options = Object.extend({
			resizeDuration: 400,	// Duration of height and width resizing (ms)
			initialWidth: 250,		// Initial width of the box (px)
			initialHeight: 250,		// Initial height of the box (px)
			animateCaption: true	// Enable/Disable caption animation
		}, options || {});
		
		this.anchors = [];
		$A(document.getElementsByTagName('a')).each(function(el){
			var rel = el.getAttribute('rel');
			if(rel && el.getAttribute('href') && rel.test('^lightbox', 'i')) {
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		if (window.XMLHttpRequest){
		this.overlay = new Element('div').setProperty('id', 'lbOverlay').setProperty('class', 'lbOverlay').injectInside(document.body);
		} else {
		this.overlay = new Element('div').setProperty('id', 'lbOverlay').injectInside(document.body);
		this.overlayBG = new Element('div').setProperty('id', 'lbBg').injectInside(this.overlay);
		}
		
		this.center = new Element('div').setProperty('id', 'lbCenter').setStyles({width: this.options.initialWidth+'px', height: this.options.initialHeight+'px', marginLeft: '-'+(this.options.initialWidth/2)+'px', display: 'none'}).injectInside(document.body);
		this.image = new Element('div').setProperty('id', 'lbImage').injectInside(this.center);
		this.prevLink = new Element('a').setProperties({id: 'lbPrevLink', href: '#'}).setStyle('display', 'none').injectInside(this.image);
		this.nextLink = this.prevLink.clone().setProperty('id', 'lbNextLink').injectInside(this.image);
		this.prevLink.onclick = this.previous.bind(this);
		this.nextLink.onclick = this.next.bind(this);
		
		this.bottom = new Element('div').setProperty('id', 'lbBottom').setStyle('display', 'none').injectInside(document.body);
		new Element('a').setProperties({id: 'lbClose', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.next = new Element('a').setProperties({id: 'lbNext', href: '#'}).injectInside(this.bottom).onclick = this.next.bind(this);
		this.previous = new Element('a').setProperties({id: 'lbPrevious', href: '#'}).injectInside(this.bottom).onclick = this.previous.bind(this);
		this.caption = new Element('div').setProperty('id', 'lbCaption').injectInside(this.bottom);
		this.number = new Element('div').setProperty('id', 'lbNumber').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);
		
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', { duration: 500 }).hide(),
			resize: this.center.effects({ duration: this.options.resizeDuration, onComplete: nextEffect }),
			image: this.image.effect('opacity', { duration: 500, onComplete: nextEffect }),
			bottom: this.bottom.effects({ duration: 400, onComplete: nextEffect })
		};
		
		this.preloadPrev = new Image();
		this.preloadNext = new Image();
		
		$('lbClose').onmouseover = function() {
			this.redden()};
		$('lbClose').onmouseout = function() {
			this.normal()};
		$('lbOverlay').onmouseover = function() {
			$('lbClose').redden()};
		$('lbOverlay').onmouseout = function() {
			$('lbClose').normal()};
	},

	click: function(imageLink) {
		var imageLinkRel = imageLink.getAttribute('rel');
		var xSpace,ySpace,imageFolder;
		if (self.innerHeight) // all except Explorer
		{
			xSpace = self.innerWidth;
			ySpace = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			xSpace = document.documentElement.clientWidth;
			ySpace = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			xSpace = document.body.clientWidth;
			ySpace = document.body.clientHeight;
		}
		imageFolder = "images\/medium\/";
		if (ySpace <= 575){ // 800x600
			imageFolder = "images\/small\/";
		}
		if (ySpace >= 700){ // bigger than 1024x768
			imageFolder = "images\/large\/";
		}
		var imageLinkHref = imageLink.getAttribute('href');
		imageLinkHref = imageLinkHref.split("/");
		imageLinkHref = imageFolder +  imageLinkHref[(imageLinkHref.length-1)];
		
		if(imageLinkRel.length == 8)
			return this.show(imageLinkHref, imageLink.getAttribute('title'));
		
		var j, elHref, imageNum;
		var images = [];
		this.anchors.each(function(el){
			elHref = el.getAttribute('href');
		elHref = elHref.split("/");
		elHref = imageFolder +  elHref[(elHref.length-1)];
			if(el.getAttribute('rel') == imageLinkRel) {
				for(j = 0; j < images.length; j++)
					if(images[j][0] == elHref) break;
				if(j == images.length) {
					images.push([elHref, el.getAttribute('title'), el.getAttribute('rev')]);
					if(elHref == imageLinkHref) imageNum = j;
				}
			}
		}, this);
		return this.open(images, imageNum);	},
	
	show: function(url, title) {
		return this.open([[url, title]], 0);
	},

	open: function(images, imageNum) {
		this.images = images;
		this.fixes(true);
		var windowHeight = Window.getHeight();
		this.overlay.setStyle('height', Math.max(windowHeight, Window.getScrollHeight())+'px');
		this.fx.overlay.goTo(0.7);
		this.top = Window.getScrollTop() + (windowHeight / 15);
		this.center.setStyles({top: this.top+'px', display: ''});
		document.addEvent('keydown', this.eventKeyDown);
		return this.changeImage(imageNum);
	},

	fixes: function(open) {
		var elements = $A(document.getElementsByTagName('object'));
		if(window.ActiveXObject) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : ''; });
	},

	keyboardListener: function(event) {
		switch(event.keyCode) {
			case 27: case 88: case 67: this.close(); break;
			case 37: case 80: this.previous(); break;	
			case 39: case 78: this.next();
		}
	},

	previous: function() {
		return this.changeImage(this.activeImage-1);
	},

	next: function() {
		return this.changeImage(this.activeImage+1);
	},

	changeImage: function(imageNum) {
		$('lbNext').blur();
		$('lbPrevious').blur();	
		if(this.step || (imageNum < 0) || (imageNum >= this.images.length)) return false;
		this.step = 1;
		this.activeImage = imageNum;
	//	this.theImage="/gallery/image/"+this.images[this.activeImage][1];
	//	urchinTracker(this.theImage);
		
		this.prevLink.style.display = this.nextLink.style.display = 'none';
		if(this.activeImage != 0) {	
			$('lbPrevious').normal();	
			this.prevLink.onmouseover = function() {
				$('lbPrevious').redden()};
			this.prevLink.onmouseout = function() {
				$('lbPrevious').normal()};	
			$('lbPrevious').onmouseover = function() {
				this.redden()};
			$('lbPrevious').onmouseout = function() {
				this.normal()};
		} else {		
			$('lbPrevious').dim();	
			$('lbPrevious').onmouseover = function() {
				this.dim()};
			$('lbPrevious').onmouseout = function() {
				this.dim()};
		};
		if(this.activeImage != (this.images.length - 1)) {
			$('lbNext').normal();
			this.nextLink.onmouseover = function() {
				$('lbNext').redden()};
			this.nextLink.onmouseout = function() {
				$('lbNext').normal()};	
			$('lbNext').onmouseover = function() {
				this.redden()};
			$('lbNext').onmouseout = function() {
				this.normal()};
		} else {		
			$('lbNext').dim();	
			$('lbNext').onmouseover = function() {
				this.dim()};
			$('lbNext').onmouseout = function() {
				this.dim()};
		};
		this.bottom.setStyles({opacity: '0', height: '0px', display: 'none'});
		this.fx.image.hide();
		this.center.className = 'lbLoading';
		
		this.preload = new Image();
		this.preload.onload = this.nextEffect.bind(this);
		this.preload.src = this.images[imageNum][0];
		return false;
	},

	nextEffect: function() {
		switch(this.step++) {
		case 1:
			this.center.className = '';
			this.image.setStyles({backgroundImage: 'url('+this.images[this.activeImage][0]+')', width: this.preload.width+'px'});
			this.image.style.height = this.prevLink.style.height = this.nextLink.style.height = this.preload.height+'px';
			
			this.caption.setHTML(this.images[this.activeImage][1]);
			this.number.setHTML((this.images.length == 1) ? '' : 'Image '+(this.activeImage+1)+' of '+this.images.length+'&nbsp;&nbsp;|&nbsp;&nbsp;'+this.images[this.activeImage][2]|| '');
			
			if(this.activeImage != 0) this.preloadPrev.src = this.images[this.activeImage - 1][0];
			if(this.activeImage != (this.images.length - 1)) this.preloadNext.src = this.images[this.activeImage + 1][0];
			if(this.center.clientHeight != this.image.offsetHeight) {
				this.fx.resize.custom({height: [this.center.clientHeight, this.image.offsetHeight]});
				break;
			}
			this.step++;
		case 2:
			if(this.center.clientWidth != this.image.offsetWidth) {
				this.fx.resize.custom({width: [this.center.clientWidth, this.image.offsetWidth], marginLeft: [-this.center.clientWidth/2, -this.image.offsetWidth/2]});
				break;
			}
			this.step++;
		case 3:
			this.bottom.setStyles({top: (this.top + this.center.clientHeight)+'px', width: this.image.style.width, marginLeft: this.center.style.marginLeft, display: ''});
			this.fx.image.custom(0, 1);
			break;
		case 4:
			if(this.options.animateCaption) {
				this.fx.bottom.custom({opacity: [0, 1], height: [0, this.bottom.scrollHeight]});
				break;
			}
			this.bottom.setStyles({opacity: '1', height: this.bottom.scrollHeight+'px'});
		case 5:
			if(this.activeImage != 0) {this.prevLink.style.display = ''} else {$('lbPrevious').dim()};
			if(this.activeImage != (this.images.length - 1)) {this.nextLink.style.display = ''} else {$('lbNext').dim()};
			this.step = 0;
		}
	},

	close: function() {
		document.removeEvent('keydown', this.eventKeyDown);
		if(this.preload) {
			this.preload.onload = Class.empty;
			this.preload = null;
		}
		for(var f in this.fx) this.fx[f].clearTimer();
		this.center.style.display = this.bottom.style.display = 'none';
		this.fx.overlay.goTo(0);
		this.fixes(false);
		this.step = 0;
		return false;
	}
};
Element.extend({
	
	redden: function() {
		this.setStyle('background-position', '0% 0px');
		return this;
	},
	
	dim: function() {
		this.setStyle('background-position', '0% -48px');
		return this;
	},
	
	normal: function() {
		this.setStyle('background-position', '0% -24px');
		return this;
	}
});



Window.onDomReady(Lightbox.init.bind(Lightbox));
window.onresize=function(){Lightbox.init.bind(Lightbox)};


/*************************************************************************
  This code is [adapted] from Dynamic Web Coding at www.dyn-web.com
  Copyright 2003-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
************************************************************************
  Less glitchy than mootools slide.
*/

dynObj.holder = {}; 
// constructor
function dynObj(id) {
  var el = dynObj.getElemRef(id);
  if (!el) return;  this.id = id; 
  dynObj.holder[this.id] = this; this.animString = "dynObj.holder." + this.id;
  this.y = 50;	
  this.h = el.offsetHeight || 0;
}

dynObj.getElemRef = function(id) { 
  var el = document.getElementById? document.getElementById(id): null;
  return el;
} 

dynObj.getInstance = function(id) {
  var obj = dynObj.holder[id];
  if (!obj) obj = new dynObj(id);
  else if (!obj.el) obj.el = dynObj.getElemRef(id);
  return obj;
}

dynObj.prototype.shiftTo = function(y) {
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) {
    if (y != null) el.style.top = (this.y = y) + "px";
  }
}

dynObj.prototype.shiftBy = function(y) { this.shiftTo(this.y+y); }

dynObj.prototype.show = function() { 
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) el.style.visibility = "visible"; 
}
dynObj.prototype.hide = function() { 
  var el = this.el? this.el: dynObj.getElemRef(this.id)? dynObj.getElemRef(this.id): null;
  if (el) el.style.visibility = "hidden"; 
}

var dw_Bezier = {
  B1: function (t) { return t*t*t },
  B2: function (t) { return 3*t*t*(1-t) },
  B3: function (t) { return 3*t*(1-t)*(1-t) },
  B4: function (t) { return (1-t)*(1-t)*(1-t) },
  getValue: function (percent,startVal,endVal,c1,c2) {
    return endVal * this.B1(percent) + c2 * this.B2(percent) + c1 * this.B3(percent) + startVal * this.B4(percent);
  }
}

dw_Animation = {
  instances: [],
  add: function(fp) {
    this.instances[this.instances.length] = fp;
  	if (this.instances.length == 1) this.timerID = window.setInterval("dw_Animation.control()", 10);
  },
  
  remove: function(fp) {
    for (var i = 0; this.instances[i]; i++) {
  		if (fp == this.instances[i]) {
  			this.instances = this.instances.slice(0,i).concat( this.instances.slice(i+1) );
  			break;
  		}
  	}
  	if (this.instances.length == 0) {
  		window.clearInterval(this.timerID);	this.timerID = null;
  	}
  },
  
  control: function() {
    for (var i = 0; this.instances[i]; i++) {
  		if (typeof this.instances[i] == "function" ) this.instances[i]();
      else eval(this.instances[i]);
    }
  }
}

dynObj.prototype.slideTo = function (destY,slideDur,acc,endFn) {
  if (!document.getElementById) return;
  this.slideDur = slideDur || .0001; var acc = -acc || 0;
  if (endFn) this.onSlideEnd = endFn;
  this.destY = destY; 
  this.startY = this.y;
  this.st = new Date().getTime();
  this.yc1 = this.y + ( (1+acc) * (this.destY-this.y)/3 );
  this.yc2 = this.y + ( (2+acc) * (this.destY-this.y)/3 );
  this.sliding = true;
  this.onSlideStart();
  dw_Animation.add(this.animString + ".doSlide()");
}

dynObj.prototype.doSlide = function() {
	if (!this.sliding) return;	
	var elapsed = new Date().getTime() - this.st;
	if (elapsed < this.slideDur) {
    var y = dw_Bezier.getValue(elapsed/this.slideDur, this.startY, this.destY, this.yc1, this.yc2);
		this.shiftTo( Math.round(y) );
		this.onSlide();
	} else {	// if time's up
    dw_Animation.remove(this.animString + ".doSlide()");
		this.shiftTo(this.destY);
		this.onSlide();
		this.sliding = false;
		this.onSlideEnd();
	}
}

dynObj.prototype.slideBy = function(dx,dy,slideDur,acc,endFn) {
	var destY=this.y+dy;
	this.slideTo(destY,slideDur,acc,endFn);
}

dynObj.prototype.onSlideStart = function () {}
dynObj.prototype.onSlide = function () {}
dynObj.prototype.onSlideEnd = function () { if (this.el) this.el = null; }

var slide_in_speed = 1000;	
var slide_out_speed = 400;

function initContent() { 


$('aboutButton').addEvent('click', function(){ 
		slide('aboutContent');
		$('a').arrow('a'); 
		$('ab').selected('ab'); 
		urchinTracker('/index/about'); 
		});
$('galleryButton').addEvent('click', function(){ 
		slide('galleryContent');
		$('g').arrow('g'); 
		$('gb').selected('gb'); 
		urchinTracker('/index/gallery'); 
		});
$('extrasButton').addEvent('click', function(){ 
		slide('extrasContent');
		$('e').arrow('e'); 
		$('eb').selected('eb'); 
		urchinTracker('/index/extras'); 
		});

  var glideLyrs = new Array();
  
  glideLyrs[0] = new dynObj('aboutContent');
  glideLyrs[1] = new dynObj('galleryContent');
  glideLyrs[2] = new dynObj('extrasContent');
  
  for (var i=0; glideLyrs[i]; i++) {
		glideLyrs[i].yOff = -(glideLyrs[i].h + 10);
		if (i!=0) glideLyrs[i].shiftTo( glideLyrs[i].yOff );
		glideLyrs[i].show();
  }
  curGlideLyr = "aboutContent";
  $('a').arrow('a');
}

Element.extend({
    arrow: function(){
		$('a').removeClass('arrowOn').addClass('arrowOff');
		$('g').removeClass('arrowOn').addClass('arrowOff');
		$('e').removeClass('arrowOn').addClass('arrowOff');
        this.addClass('arrowOn');
    }
});

Element.extend({
    selected: function(){
		$('ab').addClass('navlink');
		$('gb').addClass('navlink');
		$('eb').addClass('navlink');
        this.removeClass('navlink').addClass('selected');
    }
});

scrollTo(0,0);
var curGlideLyr;
function slide(id) {
  var oldLyr, newLyr;
  // if link for current layer clicked, slide it out of view 
	if (curGlideLyr == id) { return false;
  } else {
		oldLyr = dynObj.getInstance(curGlideLyr);
		oldLyr.shiftTo( oldLyr.yOff );
	}
	// if layer currently in view, set up to slide new one into view
	// after current one slides away
	function newLayerIn(){
	if (navigator.userAgent.indexOf("MSIE") ==-1) {$(id).setOpacity(1)}; //ClearType glitch
    newLyr = dynObj.getInstance(id);
    newLyr.slideTo(50, slide_in_speed, -1);
	curGlideLyr = id;
	scrollTo(0,0);
	}
	if (navigator.userAgent.indexOf("MSIE") ==-1) {$(curGlideLyr).setOpacity(0)}; //ClearType glitch
	newLayerIn.delay(500, $('delayExample'));

}


