function addEvent(obj, evType, fn, useCapture) {
	if(obj.addEventListener) { obj.addEventListener(evType, fn, useCapture); return true; }else if(obj.attachEvent) { var r = obj.attachEvent('on'+evType, fn); return r; }else{ return false; }
}
function preventDefaultAction(evt) {
	if(evt.preventDefault) { evt.preventDefault(); } else { evt.returnValue = false; }; return false;
}
function getElementsByClassName(tagName, clsName) { 
	var arr = new Array(); var elems = document.getElementsByTagName(tagName); for (i = 0; ( elem = elems[i] ); i++ ) { if(elem.className.indexOf(clsName) != -1) arr[arr.length] = elem; } return arr;
}
function getFirstParent(elem,parentName) {
	if(elem.parentNode.tagName.toLowerCase() == parentName.toLowerCase()) { return elem.parentNode; }
	return getFirstParent(elem.parentNode,parentName);
}
function getIEVersion() {
	var rv = -1;
	if (navigator.appName == 'Microsoft Internet Explorer') { var ua = navigator.userAgent; var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); if (re.exec(ua) != null) {	rv = parseFloat(RegExp.$1);	} }
	return rv;
}

var ieVersion = -1;


function pageInit()
{
	ieVersion = getIEVersion();

	var horizontalShow = null;
	var verticalShow = null;

	if(typeof show !== 'undefined' && show.length > 0) {
		horizontalShow = new PictureShow(show, document.getElementById('show'));
	}
	if(typeof sideshow !== 'undefined' && sideshow.length > 0) {

		var sideUl = document.createElement('ul');
		sideUl.id = 'ver';

		// min-height for ie7 and below
		if(ieVersion !== -1 && ieVersion < 8) {
			if(sideUl.offsetHeight < document.body.offsetHeight) {
				sideUl.style.height = (document.body.offsetHeight - 110) + 'px';
			}
		}

		var firstLi = document.createElement('li');
		var firstImg = new Image();
		firstImg.src = sideshow[0][0];
		firstImg.alt = sideshow[0][1];

		firstLi.appendChild(firstImg);
		sideUl.appendChild(firstLi);

		document.getElementById('m').parentNode.appendChild(sideUl);

		verticalShow = new PictureShow(sideshow, document.getElementById('ver'));
		verticalShow.showShapes = true;
	}

	if((horizontalShow !== null || verticalShow !== null) && (ieVersion > 6 || ieVersion < 0))
	{
		var shows = new PictureShowWrapper(6000);

		if(horizontalShow !== null) {
			shows.mainShow = horizontalShow;
			shows.sideShow = verticalShow;
			shows.controls = false;
		}
		if(horizontalShow == null && verticalShow !== null) {
			shows.mainShow = verticalShow;
		}

		shows.start();
	}

	for(var i = 0; i < document.links.length; i++) {
		if(document.links[i].href.indexOf('.pdf') !== -1) {
			document.links[i].target = 'blank';
		}
	}
}



addEvent(window, 'load', pageInit, false);



var PictureShowWrapper = function(delay)
{
	this.delay = delay;

	this.mainShow = null;
	this.sideShow = null;
	this.timeoutId = null;
	this.controls = false;

	this.animating = false;

	var self = this;


	this.start = function()
	{
		this.mainShow.callback = this.loadedCallback;
		if(this.sideShow !== null) this.sideShow.callback = this.loadedCallback;

		if(this.controls) {
			var controlDiv = document.createElement('div');
			controlDiv.id = 'showCtrl';

			var nextLink = document.createElement('a');
			nextLink.href = '#';
			nextLink.className = 'next';

			var prevLink = document.createElement('a');
			prevLink.href = '#';
			prevLink.className = 'prev';

			nextLink.onclick = function() {
				self.loadImage(self.mainShow.current + 1);
			};

			prevLink.onclick = function() {
				self.loadImage(self.mainShow.current - 1);
			};

			controlDiv.appendChild(nextLink);
			controlDiv.appendChild(prevLink);

			this.mainShow.elem.parentNode.appendChild(controlDiv);
		}
		this.resetTimer();
	};


	this.step = function()
	{
		self.loadImage(self.mainShow.current + 1);
	};


	this.loadImage = function(newkey)
	{
		if(!self.animating)
		{
			if(newkey >= self.mainShow.images.length) newkey = 0;
			if(newkey < 0) newkey = self.mainShow.images.length - 1;

			self.mainShow.load(newkey);
			if(self.sideShow !== null) {
				self.sideShow.load(newkey);
			}
			self.animating = true;
			clearTimeout(self.timeoutId);
		}
	};


	this.loadedCallback = function()
	{
		self.animating = false;
		self.resetTimer();
	};


	this.resetTimer = function()
	{
		clearTimeout(self.timeoutId);
		self.timeoutId = setTimeout(self.step, self.delay);
	};
}


var PictureShow = function(images, elem)
{
	this.images = images;
	this.elem = elem;
	this.callback = null;
	this.previous = 0;
	this.current = 0;

	this.shapes = new Array();
	this.showShapes = false;

	var self = this;


	this.load = function(newkey)
	{
		if(typeof self.images[newkey] !== 'undefined')
		{
			self.previous = self.current;
			self.current = newkey;

			if(self.elem.getElementsByTagName('li')[self.current])
			{
				self.show();
			}else {
				var newImage = new Image();
				newImage.src = self.images[self.current][0];
				newImage.alt = self.images[self.current][1];

				var newP = document.createElement('p');
				newP.appendChild(document.createTextNode(self.images[self.current][1]));

				var newLi = document.createElement('li');
				newLi.appendChild(newImage);
				newLi.appendChild(newP);
				newLi.style.display = 'none';

				self.elem.appendChild(newLi);

				newImage.onload = function() {
					self.show();
				}
			}
		}
	};


	this.show = function()
	{
		self.elem.getElementsByTagName('li')[self.previous].className = '';

		self.elem.getElementsByTagName('li')[self.current].className = 's';
		self.elem.getElementsByTagName('li')[self.current].style.opacity = 0;
		self.elem.getElementsByTagName('li')[self.current].style.filter = 'alpha(opacity=0)';
		self.elem.getElementsByTagName('li')[self.current].style.display = 'block';

		var fadeAnim = new Animation(self.elem.getElementsByTagName('li')[self.current], 100,
			function()
			{
				var current_opacity = parseFloat(fadeAnim.elem.style.opacity);
				if(ieVersion !== -1 && ieVersion < 8) {
					current_opacity = parseFloat(fadeAnim.elem.filters.alpha.opacity / 100);
				}
				if(current_opacity < 1) {
					current_opacity += .1;
					fadeAnim.elem.style.opacity = current_opacity;
					fadeAnim.elem.style.filter = 'alpha(opacity='+ current_opacity * 100 +')';
				}else {
					fadeAnim.stop();
				}
			},
			function()
			{
				self.elem.getElementsByTagName('li')[self.previous].style.display = 'none';
				self.callback();

				fadeAnim = null;
			}
		);

		if(self.showShapes)
		{
			if(ieVersion == -1 || ieVersion >= 8)
			{
				self.resetShapes(self.elem.getElementsByTagName('li')[self.current]);

				var shapesAnim = new Animation(self.elem.getElementsByTagName('li')[self.current], 20,
					function()
					{
						if(self.shapes[self.current][0].opacity > 0)
						{
							for(var i = 0; i < self.shapes[self.current].length; i++) {
								self.shapes[self.current][i].setX(self.shapes[self.current][i].x - self.shapes[self.current][i].speed);
								self.shapes[self.current][i].setOpacity(Math.floor((self.shapes[self.current][i].opacity - .02) * 100) / 100);
							}
						}else {
							shapesAnim.stop();
						}
					},
					function()
					{
						shapesAnim = null;
					}
				);
				shapesAnim.start();
			}
		}

		fadeAnim.start();
	};


	this.resetShapes = function(li)
	{
		if(self.shapes[self.current])
		{
			for(var i = 0; i < self.shapes[self.current].length; i++) {
				self.shapes[self.current][i].setOpacity(1);
				self.shapes[self.current][i].setX(195);
			}
		}else
		{
			var shapesOpacity = 1;
			self.shapes[self.current] = [
				new Shape(100, 195, 0, '#ffffff', shapesOpacity, Math.ceil(Math.random() * 10)),
				new Shape(80, 195, 0, '#ffffff', shapesOpacity, Math.ceil(Math.random() * 10)),
				new Shape(50, 195, 0, '#ffffff', shapesOpacity, Math.ceil(Math.random() * 10)),
				new Shape(30, 195, 0, '#ffffff', shapesOpacity, Math.ceil(Math.random() * 10))
			];

			for(var i = 0; i < self.shapes[self.current].length; i++) {
				li.appendChild(self.shapes[self.current][i].elem);
			}
		}
	};
}


var Animation = function(elem, speed, tick, callback)
{
	this.elem = elem;
	this.speed = speed;
	this.tick = tick;
	this.callback = callback;
	this.running = false;
	this.intervalId = null;

	var self = this;

	this.start = function()
	{
		if(this.running) return;
		this.intervalId = setInterval(this.tick, this.speed);
		this.running = true;
	};

	this.stop = function()
	{
		clearInterval(this.intervalId);
		this.running = false;
		this.callback();
	};
}



var Shape = function(width, x, y, color, opacity, speed)
{
	this.width = width;
	this.x = x;
	this.y = y;
	this.color = color;
	this.opacity = opacity;
	this.speed = speed;

	this.setX = function(newx)
	{
		this.x = newx;
		this.elem.style.left = this.x +'px';
	};

	this.setOpacity = function(newopacity)
	{
		this.opacity = newopacity;
		this.elem.style.opacity = this.opacity;
		this.elem.style.filter = 'alpha(opacity='+ this.opacity * 100 +')';
	};

	this.elem = document.createElement('span');
	this.elem.style.position = 'absolute';
	this.elem.style.width = this.width +'px';
	this.elem.style.left = this.x +'px';
	this.elem.style.top = this.y +'px';
	this.elem.style.backgroundColor = this.color;
	this.elem.style.opacity = opacity;
	this.elem.style.filter = 'alpha(opacity='+ opacity * 100 +')';
}



