﻿var GalleryItem = Class.create({
	initialize: function(pID,pTitle,pMedia,pDescription) {
	this.ID = pID;
	this.Title = pTitle;
	this.Media = pMedia;
	this.Description = pDescription;
	},
	mediaHTML: function(w,h) {
		return '<img alt="" title="" src="image.asp?w=' + w + '&amp;h=' + h + '&amp;i=' + this.Media + '"/>';
	},
	popup: function() {
		
	}
});
var GalleryCollection = Class.create({
	initialize: function() {
		this._current = -1;
		this.width = 640;
		this.height = 480;
		this.item = [];
		this.debug = false;
		this.inProgress = false;
	},
	getMax: function() {
		return this.item.length-1;
	},
	addItem: function(i,t,m,d) {
		this.item.push(new GalleryItem(i,t,m,d));
	},
	random: function() {
		this.show(Math.floor(this.getMax()*Math.random()));
	},
	next: function() {
		if (this._current === this.getMax()) {
			this.show(0)
		} else {
			this.show(this._current+1);
		}
	},
	prev: function() {
		if (this._current === 0) {
			this.show(this.getMax())
		} else {
			this.show(this._current-1);
		}
	},
	toggleProgress: function() {
		this.inProgress = !this.inProgress;
	},
	show: function(i) {
		if (!this.inProgress) {
			if (i >= 0 && i <= this.getMax()) {
				this.toggleProgress();
				Display.Back.innerHTML(this.item[i].Title,this.item[i].mediaHTML(this.width,this.height),this.item[i].Description);
				//Display.Back.center(this.width,this.height);
				Display.Main.T.hide();
				Display.Main.M.fade({duration:1.5});
				Display.Main.D.hide();
				Display.Back.T.appear({delay:0.75, duration:0.75});
				Display.Back.M.appear({duration:1.5, afterFinish:function(){Gallery.toggleProgress()}});
				if(Display.Back.D.innerHTML != '') {
					Display.Back.D.appear({delay:0.75, duration:0.75});
				}
				Display.swap();
				List.show(i);
				this._current = i;
			} else if (this.debug) {
				alert('Invalid Item:' + i);
			}
		} else if (this.debug) {
			alert('In Progress Item:' + i);
		}
	},
	debugOutput: function() {
		var i = 0; var s = "";
		for (i = 0; i < this.item.length; i++) {
			s += i + ". " + this.item[i].Title + " | " + this.item[i].Media + "\n";
		}
		alert(s);
	},
	validItem: function() {
		return (this._current >= 0 && this._current <= this.getMax());
	},
	popup: function() {
		if (this.validItem()) {
			puw('../uploads/' + this.item[this._current].Media,0,0,'popupMedia');
		} else if (this.debug) {
			alert('Invalid Popup Item:' + this._current);
		}
	}
});

var GalleryItemDisplay = Class.create({
	initialize: function(elT,elM,elD) {
		this.T = $(elT);
		this.M = $(elM);
		this.D = $(elD);
	},
	innerHTML: function(t,m,d) {
		this.T.innerHTML = t;
		this.M.innerHTML = m;
		this.D.innerHTML = d;
	},
	center: function(w,h) {
		var mw = this.M.getWidth();
		var mh = this.M.getHeight();
		if (mw > 100 && mh > 100) {
			this.M.style.top = Math.floor((h/2)-(mh/2)) + 'px';
			this.M.style.left = Math.floor((w/2)-(mw/2)) + 'px';
		} else {
			this.M.style.top = '0px';
			this.M.style.left = '0px';
		}
		//alert(w + '\n' + h + '\n' + mw + '\n' + mh);
	}
});

var GalleryDisplay = Class.create({
	initialize: function() {
		this.mode = undefined;
		this.Main = undefined;	
		this.Back = undefined;
		this._Main = new GalleryItemDisplay('giTA','giMA','giDA');
		this._Back = new GalleryItemDisplay('giTB','giMB','giDB');
		this.swap();
	},
	instant: function() {
		this.Main.T.show();
		this.Main.T.show();
		this.Main.T.show();
		this.Back.T.hide();
		this.Back.T.hide();
		this.Back.T.hide();
	},
	swap: function() {
		if (this.mode === 'A') {
			this.Main = this._Back;
			this.Back = this._Main;
			this.mode = 'B';
		} else {
			this.Main = this._Main;
			this.Back = this._Back;
			this.mode = 'A';
		}
	}
});

var GalleryListItem = Class.create({
	initialize: function(pIndex,pTitle,pMedia) {
		this.I = pIndex;
		this.T = pTitle;
		this.M = pMedia;
		this.el = undefined;
	}
});

var GalleryList = Class.create({
	initialize: function() {
		this.item = [];
		this.listSize = 2;
	},
	show: function(i) {
		this.item.each(function(li) {
			li.el.hide();
			li.el.removeClassName('sel');
		});
		var s = i - this.listSize;
		if (s < 0) {s = 0;}
		else if (s > this.item.length-1-this.listSize*2) { s = this.item.length-1-this.listSize*2; }
		for (var j = s; j < s+(this.listSize*2+1); j++) {
			if (this.item[j]) {
				this.item[j].el.show();
			}
		}
		this.item[i].el.addClassName('sel');
	},
	addItem: function(i,t,m) {
		this.item.push(new GalleryListItem(i,t,m));
	},
	populate: function() {
		var i = 0; var u = $('giList');
		for (i = 0; i < this.item.length; i++) {
			this.item[i].el = new Element('li',{'style':'display:none;'});
			this.item[i].el.innerHTML = '<div><img title="' + this.item[i].T + '" src="image.asp?w=80&amp;h=80&amp;mode=crop&amp;i=' + this.item[i].M + '" onclick="Gallery.show(' + i + ')"/></div>';
			u.insert(this.item[i].el);
		}
	}
});

var Gallery = new GalleryCollection();
var Display = new GalleryDisplay();
var List = new GalleryList();
//Gallery.debug = true;
Event.observe(window, 'load', function() {
	Gallery.width = $('giCont').getWidth();
	Gallery.height = $('giCont').getHeight();
	List.populate();
	Gallery.random();
	//Gallery.show(4);
});

function puw (url,w,h,name,scroll,status,resize) {
	var left = (screen.width) ? (screen.width-w)/2 : 0;
	var top = (screen.height) ? (screen.height-h)/2 : 0;
	if (!name) { name = 'puw'; }
	if (!scroll) { scroll = 'yes'; }
	if (!status) { status = 'no'; }
	if (!resize) { resize = 'no'; }
	var nw = window.open(url,name,'scrollbars=' + scroll + ',status=' + status + ',width=' + w + ',height=' + h + ',top=' + top + ',left=' + left + ',resizable=' + resize + '');
	if (nw) { nw.focus(); }
}