var GalleryItem = Class.create({
	initialize: function(pMedia) {
	this.Media = pMedia;
	},
	mediaHTML: function(w,h) {
		return '<img alt="" title="" src="image.asp?w=' + w + '&amp;h=' + h + '&amp;mode=crop&amp;i=' + this.Media + '"/>';
	}
});
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(m) {
		this.item.push(new GalleryItem(m));
	},
	random: function() {
		this.show(Math.floor(this.getMax()*Math.random()));
	},
	next: function() {
		if (this._current === this.getMax() || typeof this._current === 'undefined') {
			this.show(0)
		} else {
			this.show(this._current+1);
		}$('flashReplacement').insert('<div class="frText">Featured Subdivision<br/><a href="http://www.lotguyz.com/content/index.asp?page=53">Canyon View Ranches</a></div>');
	},
	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].mediaHTML(this.width,this.height));
				Display.Back.center(this.width,this.height);
				Display.Main.M.fade({delay:1.0,duration:1.5});
				Display.Back.M.appear({delay:1.0,duration:1.5, afterFinish:function(){Gallery.toggleProgress()}});
				Display.swap();
				this._current = i;
				if (i < this.getMax()) {
					setTimeout('Gallery.show(' + (i+1) + ');',4500);
				} else {
					$('flashReplacement').insert('<div class="frText">Featured Subdivision<br/><a href="http://www.lotguyz.com/content/index.asp?page=53">Canyon View Ranches</a></div>');
				}
			} else if (this.debug) {
				alert('Invalid Item:' + i);
			}
		} else if (this.debug) {
			alert('In Progress Item:' + i);
		}
	},
	validItem: function() {
		return (this._current >= 0 && this._current <= this.getMax());
	}
});

var GalleryItemDisplay = Class.create({
	initialize: function(elM) {
		this.M = $(elM);
	},
	innerHTML: function(m) {
		this.M.innerHTML = m;
	},
	center: function(w,h) {
		return;
		var mw = this.M.getWidth();
		var mh = this.M.getHeight();
		if (mw > 0 && mh > 0) {
			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';
		}
	}
});

var GalleryDisplay = Class.create({
	initialize: function() {
		this.mode = undefined;
		this.Main = undefined;	
		this.Back = undefined;
		this._Main = new GalleryItemDisplay('frImgA');
		this._Back = new GalleryItemDisplay('frImgB');
		this.swap();
	},
	instant: function() {
		this.Main.T.show();
		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 Gallery = new GalleryCollection();
var Display = new GalleryDisplay();
Gallery.debug = true;
Event.observe(window, 'load', function() {
	Gallery.width = $('flashReplacement').getWidth();
	Gallery.height = $('flashReplacement').getHeight();
	Gallery.show(0);
});
