(function() {
	function img(imsrc, width, left, num) {
		var sq = (num === 9) ? 3 : 2;
		this.rW = (num === 9) ? 150 : 225;
		this.tW = 450;
		this.im = imsrc;
		this.width = width;
		var mod = this.rW*(left%sq);
		this.pos = {
			l: left%sq,
			b: Math.floor(left/sq),
			sqr: sq-1
		};
		this.left = mod;
		this.bottom = this.rW * Math.floor(left/sq);
		this.setPos();
		this.addevs();
		this.nocaption();
	}
	img.z = 10;
	img.prototype.addevs = function() {
		var that = this;
		lib.addEvent(this.im, 'mouseover', function () {
			that.highlight(true);
		}, false);
		lib.addEvent(this.im, 'mousedown', function () {
			clearInterval(that.timer);
			that.zoomIn(that.tW, 6, true);
		}, false);
		lib.addEvent(this.im, 'mouseout', function () {
			clearInterval(that.timer);
			that.highlight(false);
			that.zoomIn(that.rW, 8, false);
		}, false);
	};
	img.prototype.move = function (m) {
		this.width += m;
		this.left -= (this.pos.l > 0 && this.pos.l < this.pos.sqr)?m/2:0;
		this.bottom -= (this.pos.b > 0 && this.pos.b < this.pos.sqr)?m/2:0;
		this.left -= (this.pos.l === this.pos.sqr)?m:0;
		this.bottom -= (this.pos.b === this.pos.sqr)?m:0;
		this.setPos();
	};
	img.prototype.setPos = function() {
		this.im.style.width = this.width + 'px';
		this.im.style.height = this.width + 'px';
		this.im.style.left = this.left + 'px';
		this.im.style.bottom = this.bottom + 'px';
	};
	img.prototype.highlight = function(n) {
		if (n) {
			this.im.style.zIndex=img.z++;
			this.im.style.borderColor='#fff';
		} else {
			this.im.style.borderColor='#000';
		}
	};
	img.prototype.zoomIn = function(fW, speed, capt) {
		var that = this;
		//~ (capt === true) ? that.caption():lib.removeSelf(lib.$('caption'), lib.$('desc'));
		(capt === true) ? that.caption():that.nocaption();
		this.timer = setInterval(function() {
			(Math.round(that.width) !== fW) ? that.move((fW-that.width)/speed) : clearInterval(that.timer);
		}, 20);
	};
	img.prototype.caption = function() {
		(lib.$('caption')) ? lib.removeSelf(lib.$('caption'), lib.$('desc')) : 0;
		var capt = lib.newTextElem('h2', this.im.title, 'caption');
		var desc = lib.newTextElem('p', this.im.alt, 'desc');
		lib.appendChildren(lib.$('pframe'), capt, desc);
	};
	img.prototype.nocaption = function() {
		lib.removeSelf(lib.$('caption'), lib.$('desc'))
		var capt = lib.newTextElem('p', "Click the image to view - Roll out to close", 'caption');
		lib.$('pframe').appendChild(capt);
	};
		
	lib.addEvent(window, 'load', function() {
		if (document.getElementById) {
 			if (lib.$('galbut')) {
				lib.addEvent(lib.$('galbut'), 'click', function() {
					var imags = [];
					imags = lib.$('sld', 'img');
					var num = imags.length;
					for (var i = 1; i <= imags.length; i++){
						var s1 = new img(imags[i-1], (num === 9) ? 150 : 225, i-1, num);
					}
					var hide = lib.$$('contentMain', 'branding', 'contentWrap', 'address');
					for (var i = 0; i < hide.length; i++) {
						hide[i].style.display = 'none';
					}
					document.body.style.backgroundColor = '#000';
					var show = lib.$$('sld', 'pframe');
					for (var i = 0; i < show.length; i++) {
						show[i].style.display = 'block';
					}
					lib.appendChildren(document.body, lib.$('sld'), lib.$('pframe'));
					lib.addEvent(lib.$('cBut'), 'click', function() {window.location.reload();}, false);
				}, false);
			}
		}
	}, false);
})();


