/*
	BRADLEY GALLERY!!!!
*/

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

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

window.addEvent('load', function() {
	$$('img.gallery').addEvent('click', function(e) {
		var coords = this.getCoordinates();
		var wcoords = document.getCoordinates();
		var wscroll  = document.getScroll();
		
		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 dcoords = {
						left: (wcoords.width - (galW.coords.width*6))/2,
						top: (wcoords.height - (galW.coords.height*6))/2,
						width: galW.coords.width*6,
						height: galW.coords.height*6
					}
					galW.morph({
						top: dcoords.top-10+wscroll.y,
						left: dcoords.left-10+wscroll.x,
						width: dcoords.width,
						height: dcoords.height,
						border: '10px solid white'
					});
				}
			});
		}
		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: 'black',
				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',
				width: 50,
				height: 50,
				top: 0,
				right: 0,
				cursor: 'pointer',
				opacity: 0.6
			}).addEvent('click', function(e) {
				galM.fireEvent('click', e);
			}).inject(galW);

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

			var gNext = new Element('div', { 'class': 'next' });
			gNext.set('styles', {
				position: 'absolute',
				width: 50,
				height: 50,
				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);
			}).inject(galW);
			
			var gPrev = new Element('div', { 'class': 'prev' });
			gPrev.set('styles', {
				position: 'absolute',
				width: 50,
				height: 50,
				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);
			}).inject(galW);
			
		}
		
		if (!Html.isMSIE6)
			galM.fade(0.8);
		else
			galM.fade('show');
		galW.coords = coords;
		galW.img = this;
		galW.dcoords = {
			left: (wcoords.width - (coords.width*6))/2,
			top: (wcoords.height - (coords.height*6))/2,
			width: coords.width*6,
			height: coords.height*6
		}
		var imgs = $$('img.gallery');
		for(var i=0; i<imgs.length; i++) {
			if (imgs[i] == this) {
				galW.gPos.set('text', (i+1)+' / '+imgs.length);
				galW.gIMG.set('src', galList[i]).fade('hide');
				galW.gIMG.set('styles', {
						width: galW.dcoords.width,
						height: 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;
	});
});

