/*
	BRADLEY GALLERY!!!!
*/

var galM = null;
var galW = null;
var galF = false;

var fadeoutcmd = Html.isMSIE6 ? 'hide':'out';
var fadeincmd = Html.isMSIE6 ? 'show':'in';
var maxsize = 90;

window.addEvent('load', function() {
	$$('img.gallery').addEvent('click', function(e) {
		var coords = this.getCoordinates();
		var wcoords = document.getCoordinates();
		var wscroll  = document.getScroll();
		
		coords.width = this.getAttribute('owidth');
		coords.height = this.getAttribute('oheight');
		
		if (!galM) {
			galM = new Element('div', { 'class' : 'gallerymask' });
			galM.set('tween', { duration: 250, chain: 'link' }).addEvent('click', function(e) {
				e.stop();
				galW.gIMG.set('tween', { onComplete: function() {} }).fade(fadeoutcmd);
				galW.fade(fadeoutcmd);
				this.fade(fadeoutcmd);
				galF = false;
			}).set('styles', {
				position: 'absolute',
				top: wscroll.y,
				left: 0,
				bottom: -wscroll.y,
				right: 0,
				background: 'black',
				zIndex: 180
			}).inject(document.body).fade('hide');
			
			document.addEvent('scroll', function(e) {
				var wscroll  = document.getScroll();
				galM.set('styles', {
					top: wscroll.y,
					left: 0,
					bottom: -wscroll.y,
					right: 0
				});
				if (galF) {
					galW.morph({
						top: galW.dcoords.top-10+wscroll.y 
					});
				}
			});
			
			window.addEvent('resize', function(e) {
				var wscroll  = document.getScroll();
				galM.set('styles', {
					top: wscroll.y,
					left: 0,
					bottom: -wscroll.y,
					right: 0
				});
				if (galF) {
					var mcoords = document.getScrollSize();
					var wcoords = document.getCoordinates();
					var ocoords = galW.coords;
					if ((wcoords.width*maxsize/100) < ocoords.width) {
						// shrink
						var nwidth = Math.round(wcoords.width*maxsize/100);
						ocoords.height = Math.round(ocoords.height*(nwidth/ocoords.width));
						ocoords.width = nwidth;
					}
					
					if ((wcoords.height*maxsize/100) < ocoords.height) {
						// shrink
						var nheight = Math.round(wcoords.height*maxsize/100);
						ocoords.width = Math.round(ocoords.width*(nheight/ocoords.height));
						ocoords.height = nheight;
					}
					
					var dcoords = {
						left: (wcoords.width - ocoords.width)/2,
						top: (wcoords.height - ocoords.height)/2,
						width: ocoords.width,
						height: ocoords.height
					}
					galW.morph({
						top: dcoords.top-10+wscroll.y,
						left: dcoords.left-10+wscroll.x,
						width: dcoords.width,
						height: dcoords.height,
						border: '10px solid white'
					});
				}
			}).addEvent('keyup', function(e) {
				if (!galF)
					return true;
				switch(e.code) {
					case 27:
						galM.fireEvent('click', e);
						break;
					case 39:
						galW.gNext.fireEvent('click');
						break;
					case 37:
						galW.gPrev.fireEvent('click');
						break;
				}

				return true;
			});
		}
		if (!galW) {
			galW = new Element('div', { 'class' : 'gallerywindow' });
			galW.set('morph', { 
				duration: 500,
				chain: 'link',
				onComplete: function() {
				}
			}).set('tween', { duration: 250, chain: 'link' }).set('styles', {
				position: 'absolute',
				top: coords.top-5,
				left: coords.left-5,
				width: coords.width,
				height: coords.height,
				background: 'white',
				border: '5px solid white',
				overflow: 'hidden',
				zIndex: 190
			}).inject(document.body).fade('hide');

			galW.gIMG = new Element('img', { border: 0 });
			galW.gIMG.inject(galW).fade('hide').set('tween', { chain: 'link' }).addEvent('load', function() {
				this.set('tween', { onComplete: function() {
				} }).fade(fadeincmd);
			});

			var gClose = new Element('div', { 'class': 'close' });
			gClose.set('styles', {
				position: 'absolute',
				top: 0,
				right: 0,
				cursor: 'pointer',
				opacity: 0.6
			}).addEvent('click', function(e) {
				galM.fireEvent('click', e);
			}).addEvent('mouseover', function(e) {
				this.getChildren()[0].fade(fadeincmd);
			}).addEvent('mouseout', function(e) {
				this.getChildren()[0].fade(fadeoutcmd);
			}).inject(galW).innerHTML = '<div style=""></div>';
			gClose.getChildren()[0].fade('hide');

			galW.gPos = new Element('div', { 'class': 'pos' });
			galW.gPos.set('styles', {
				position: 'absolute',
				top: 0,
				left: 0,
				opacity: 0.6
			}).inject(galW);

			galW.gNext = new Element('div', { 'class': 'next' });
			galW.gNext.set('styles', {
				position: 'absolute',
				bottom: 0,
				right: 0,
				cursor: 'pointer',
				opacity: 0.6
			}).addEvent('click', function() {
				var imgs = $$('img.gallery');
				var img = this.getParent().img;
				var next = null;
				for(var i=0; i<imgs.length; i++) {
					if (imgs[i] == img) {
						next = imgs[i+1];
						break;
					}
				}
				if (!next)
					next = imgs[0];
				
				galW.gIMG.set('tween', { onComplete: function() { next.fireEvent('click'); } }).fade(fadeoutcmd);
			}).addEvent('mouseover', function(e) {
				this.getChildren()[0].fade(fadeincmd);
			}).addEvent('mouseout', function(e) {
				this.getChildren()[0].fade(fadeoutcmd);
			}).inject(galW).innerHTML = '<div style=""></div>';
			galW.gNext.getChildren()[0].fade('hide');
			
			galW.gPrev = new Element('div', { 'class': 'prev' });
			galW.gPrev.set('styles', {
				position: 'absolute',
				bottom: 0,
				left: 0,
				cursor: 'pointer',
				opacity: 0.6
			}).addEvent('click', function() {
				var imgs = $$('img.gallery');
				var img = this.getParent().img;
				var prev = null;
				for(var i=0; i<imgs.length; i++) {
					if (imgs[i] == img)
						break;
					prev = imgs[i];
				}
				if (imgs[i] != img || !prev)
					prev = imgs[imgs.length-1];
				
				galW.gIMG.set('tween', { onComplete: function() { prev.fireEvent('click'); } }).fade(fadeoutcmd);
			}).addEvent('mouseover', function(e) {
				this.getChildren()[0].fade(fadeincmd);
			}).addEvent('mouseout', function(e) {
				this.getChildren()[0].fade(fadeoutcmd);
			}).inject(galW).innerHTML = '<div style=""></div>';
			galW.gPrev.getChildren()[0].fade('hide');
			
		}
		
		if (!Html.isMSIE6)
			galM.fade(0.8);
		else
			galM.fade('show');
		galW.coords = coords;
		galW.img = this;

		var ocoords = eval('('+JSON.stringify(galW.coords)+')');
		if ((wcoords.width*maxsize/100) < ocoords.width) {
			// shrink
			var nwidth = Math.round(wcoords.width*maxsize/100);
			ocoords.height = Math.round(ocoords.height*(nwidth/ocoords.width));
			ocoords.width = nwidth;
		}
		
		if ((wcoords.height*maxsize/100) < ocoords.height) {
			// shrink
			var nheight = Math.round(wcoords.height*maxsize/100);
			ocoords.width = Math.round(ocoords.width*(nheight/ocoords.height));
			ocoords.height = nheight;
		}
		
		galW.dcoords = {
			left: (wcoords.width - ocoords.width)/2,
			top: (wcoords.height - ocoords.height)/2,
			width: ocoords.width,
			height: ocoords.height
		}

		var imgs = $$('img.gallery');
		for(var i=0; i<imgs.length; i++) {
			if (imgs[i] == this) {
				//alert(galW.dcoords.width+':'+galW.dcoords.height);
				galW.gPos.set('text', (i+1)+' / '+imgs.length);
				galW.gIMG.fade('hide').set('src', galList[i]);
				galW.gIMG.set('styles', {
						width: (galW.dcoords.width == galW.coords.width) ? null:galW.dcoords.width,
						height: (galW.dcoords.height == galW.coords.height) ? null:galW.dcoords.height
				});
				break;
			}
		}
		//Debug.alertObject(galW.dcoords.top-10+wscroll.y);
		//Debug.alertObject(galW.dcoords.left-10+wscroll.x);
		galW.fade('show');
		galW.morph({
			top: galW.dcoords.top-10+wscroll.y,
			left: galW.dcoords.left-10+wscroll.x,
			width: galW.dcoords.width,
			height: galW.dcoords.height,
			borderWidth: '10px'
		});

		galF = true;
	});
});


