// Copyright 2009 - 2010, Jason Sinclair Astorquia, All rights reserved

function gallery()
{
	this.splash				= null;
	
	this.galleryNode;
	this.infoNode;
	this.displayNode;
	this.copyNode;
	this.constructionNode;
	
	this.initSplash();
}
gallery.prototype.WIDTH  		= 1000;
gallery.prototype.MIN_HEIGHT 	= 630;
gallery.prototype.TOP_PAD		= 50;
gallery.prototype.HEADER_HEIGHT = 28;
gallery.prototype.GRAD_WIDTH	= 70;
gallery.prototype.COPY_HEIGHT	= 20;
gallery.prototype.IMG_LEFT		= 475;
gallery.prototype.CONSTRUCTION_HEIGHT	= 35;

function gallery_initSplash()
{
	var lPhoto;
	
	this.splash = new splash();
	
	// name, thumb, img, height, width, text
	lPhoto = new photo('', null, 'images/photo1.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo2.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo3.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo4.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo5.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo6.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo7.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo8.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)

	lPhoto = new photo('', null, 'images/photo9.jpg', 525, 582, '');
	this.splash.addPhoto(lPhoto)
}
gallery.prototype.initSplash = gallery_initSplash;


function gallery_draw()
{
	var section;
	var obj;
	var text;
	var top;
	var x;

	this.galleryNode = obtainElement('div');
	
	// header
	top = this.TOP_PAD;
	this.infoNode = obtainElement('div');
	this.infoNode.className = 'header';
	this.infoNode.style.top = top + 'px';
	
	text = new Array();
	text.push('Lara Taylor Interiors<P/>');
	text.push('<CENTER><SPAN CLASS="text"">3010 SW Cityview St<BR/>');
	text.push('Seattle, WA 98126<BR/>');
	text.push('206.265.0876<BR/>');
	text.push('<a class="text" href="mailto:lara@larataylor.com">lara@larataylor.com</a></SPAN></CENTER>');
	
	this.infoNode.innerHTML = text.join('');
	this.galleryNode.appendChild(this.infoNode);
	
	
	// display
	this.displayNode = obtainElement('div');
	this.displayNode.style.top = top + 'px';
	this.displayNode.style.height = slideShow.prototype.PHOTO_HEIGHT + 'px'; 
	this.displayNode.style.width = slideShow.prototype.PHOTO_WIDTH + 'px';
	this.displayNode.style.backgroundImage = 'url(images/loading.gif)';
	this.displayNode.style.backgroundRepeat = 'no-repeat';
	this.displayNode.style.backgroundPosition = 'center center';
	
	this.galleryNode.appendChild(this.displayNode);
	
	// copy
	this.copyNode = obtainElement('div');
	this.copyNode.className = 'copy';
	this.copyNode.style.height = this.COPY_HEIGHT + 'px'; 
	this.copyNode.style.width = this.WIDTH + 'px';
	//this.copyNode.style.paddingLeft = this.GRAD_WIDTH + 'px';
	this.copyNode.style.textAlign = 'right';
	
	var now = new Date();
	text = new Array();
	text.push('&copy; ');
	text.push(now.getFullYear());
	text.push('&nbsp; Lara Taylor');
	
	this.copyNode.innerHTML = text.join('');
	this.galleryNode.appendChild(this.copyNode);

	// construction node
	this.constructionNode = obtainElement('div');
	this.constructionNode.className = 'construction';
	this.constructionNode.style.height = this.CONSTRUCTION_HEIGHT + 'px'; 
	//this.constructionNode.style.width = this.WIDTH + 'px';
	//this.constructionNode.style.paddingLeft = this.GRAD_WIDTH + 'px';
	
	text = new Array();
	text.push('Website under construction.  Please inquire with<BR/> ');
	text.push('<a class="construction" href="mailto:lara@larataylor.com">lara@larataylor.com</a> ');
	text.push('for a complete portfolio sent via email.');
	
	this.constructionNode.innerHTML = text.join('');
	this.galleryNode.appendChild(this.constructionNode);

	
	obj = document.getElementById('galleryHome');
	obj.appendChild(this.galleryNode);
	
	this.redraw();
	
	// show the splash
	this.splash.load();
	this.selectedSection = this.splash;
}
gallery.prototype.draw = gallery_draw;

function gallery_redraw()
{
	var width;
	var height;
	var left;
	var top;
	var displayHeight;

	width = document.body.clientWidth;
	width = Math.max(width, this.WIDTH);

	height = document.body.clientHeight;
	height = Math.max(height, this.MIN_HEIGHT);

	this.galleryNode.style.height 	= height + 'px';
	this.galleryNode.style.width 	= width + 'px';
	
	left = Math.max(Math.floor((width - this.WIDTH) / 2), 0);
	this.infoNode.style.left 	= Math.max(left, 20) + 'px';
	
	this.displayNode.style.left = (left + this.IMG_LEFT) + 'px';
	this.displayNode.style.height = (this.MIN_HEIGHT - this.TOP_PAD - this.HEADER_HEIGHT - this.COPY_HEIGHT) + 'px';

	top = parseInt(this.displayNode.style.top);
	top += Math.floor((parseInt(this.displayNode.style.height) - this.infoNode.offsetHeight) / 2);
	this.infoNode.style.top = (top - 50) + 'px';

	top = parseInt(this.displayNode.style.top) + parseInt(this.displayNode.style.height) + this.COPY_HEIGHT;
	this.copyNode.style.top = top + 'px';
	this.copyNode.style.left = left + 'px';
	
	top -= this.COPY_HEIGHT + this.CONSTRUCTION_HEIGHT;
	this.constructionNode.style.top = top + 'px';
	this.constructionNode.style.left = Math.max(left, 20) + 'px';
}
gallery.prototype.redraw = gallery_redraw;


function gallery_setDisplay(node)
{	
	node.style.top 	= '0px';
	node.style.left = '0px';
	node.style.backgroundColor = '#FFFFFF';
	
	this.displayNode.appendChild(node);
}
gallery.prototype.setDisplay = gallery_setDisplay;

function gallery_mouseOver(id, evt)
{
}
gallery.prototype.mouseOver = gallery_mouseOver;


function gallery_mouseOut(id)
{
}
gallery.prototype.mouseOut = gallery_mouseOut;


function gallery_mouseMove(id, evt)
{
}
gallery.prototype.mouseMove = gallery_mouseMove;

function splash()
{
	this.photos = new Array();
	this.slideShow;
	
	this.oid = cortex.getOID();
	cortex.addObj(this);
}

function splash_addPhoto(photo) 
{
	photo.idx = this.photos.length;
	photo.parentObj = this;
	
	this.photos.push(photo);
}
splash.prototype.addPhoto = splash_addPhoto;

function splash_load()
{
	var lPhoto;
	var x;
	
	for (x = 0; x < this.photos.length; x++) {
		lPhoto = this.photos[x];
		lPhoto.load();
	}
}
splash.prototype.load = splash_load;

function splash_draw()
{
	var lPhoto;
	var x;
	
	// have all the photos been loaded for display?
	for (x = 0; x < this.photos.length; x++) {
		lPhoto = this.photos[x];
		if (!lPhoto.imgLoaded) return;
	}
	
	// create the slideShow
	if (this.slideShow == null) {
		this.slideShow = new slideShow(this.photos, false, false);
	}
	
	// play
	this.slideShow.play();
}
splash.prototype.draw = splash_draw;

function splash_deselect()
{
	if (this.slideShow) this.slideShow.pause();
}
splash.prototype.deselect = splash_deselect;



function photo(name, thumb, img, height, width, text)
{
	this.name 	= name;
	this.thumb 	= thumb;
	this.img 	= img;
	this.height = height;
	this.width 	= width;
	this.text 	= text;
	
	this.thumbLoaded 	= false;
	this.imgLoaded		= false;
	
	this.idx;
	this.parentObj;
	
	this.thumbNode;
	this.displayNode;
	
	this.oid = cortex.getOID();
	cortex.addObj(this);
}
photo.prototype.THUMB_WIDTH 	= 90;
photo.prototype.THUMB_HEIGHT 	= 60;


function photo_load()
{
	var obj = obtainElement('div');
	
	if (this.thumb) {
		obj.style.top = '0px';
		obj.style.left = '0px';
		obj.style.width = '1px';
		obj.style.height = '1px';
		obj.style.opacity = '0';
		obj.style.filter = 'alpha(opacity=0)';	
		obj.innerHTML = '<img src="' + this.thumb + '" onload="javascript:photo_thumbLoaded(\'' + this.oid + '\');"/>'
	
		document.body.appendChild(obj);
	} else {
		this.thumbLoaded = true;
	}
	
	obj = obtainElement('div');
	
	obj.style.top = '0px';
	obj.style.left = '0px';
	obj.style.width = '1px';
	obj.style.height = '1px';
	obj.style.opacity = '0';
	obj.style.filter = 'alpha(opacity=0)';	
	obj.innerHTML = '<img src="' + this.img + '" onload="javascript:photo_imgLoaded(\'' + this.oid + '\');"/>'

	document.body.appendChild(obj);
}
photo.prototype.load = photo_load;

function photo_thumbLoaded(oid)
{
	var lPhoto = cortex.getObjByID(oid);
	if (lPhoto == null) return;
	
	lPhoto.thumbLoaded = true;
	lPhoto.parentObj.draw();	
}

function photo_imgLoaded(oid)
{
	var lPhoto = cortex.getObjByID(oid);
	if (lPhoto == null) return;
	
	lPhoto.imgLoaded = true;
	lPhoto.parentObj.draw();	
}


function slideShow(photos, hasThumbs, hasNavigation)
{
	this.photos 		= photos;
	this.hasThumbs 		= hasThumbs;
	this.hasNavigation 	= hasNavigation;
	
	this.activePhoto;
	this.isPlaying;
	
	this.timer;
	
	this.opacity;
	
	this.displayNode;
	this.thumbsNode;
	this.photoNode;
		
	this.priorPhoto;
	
	this.oid = cortex.getOID();
	cortex.addObj(this);
}
slideShow.prototype.DURATION = 4500;
slideShow.prototype.PHOTO_HEIGHT = 582;
slideShow.prototype.PHOTO_WIDTH = 525;

function slideShow_play()
{
	// kill the clock
	this.killClock();
	
	// init the active photo?
	if (this.activePhoto == null) this.activePhoto = this.photos[0];
	
	// flag playing status
	this.isPlaying = true;

	// show the activePhoto
	this.showActive();	
}
slideShow.prototype.play = slideShow_play;

function slideShow_pause()
{
	// kill the clock
	this.killClock();
	
	// flag playing status
	this.isPlaying = false;
}
slideShow.prototype.pause = slideShow_pause;

function slideShow_showActive()
{
	if (this.activePhoto == null) return;
	
	// show the active photo
	if (this.displayNode == null) {
		this.displayNode = obtainElement('div');
		this.displayNode.id = this.oid;
		this.displayNode.style.height = this.PHOTO_HEIGHT + 'px'; 
		this.displayNode.style.width = this.PHOTO_WIDTH + 'px';	
	}
	
	// add the photo
	if (this.photoNode == null || (!ie && !mobile)) {
		if (this.priorPhoto && this.priorPhoto.parentNode) {
			this.priorPhoto.parentNode.removeChild(this.priorPhoto);
		}
		if (this.photoNode != null) this.priorPhoto = this.photoNode;
		this.photoNode = obtainElement('div');
		if (ie) this.photoNode.className = 'photo'; 
		this.displayNode.appendChild(this.photoNode);		
	}
	
	var img = new Array();
	img.push('<img src="');
	img.push(this.activePhoto.img);
	img.push('" border=0 width=');
	img.push(this.activePhoto.width);
	img.push(' height=')
	img.push(this.activePhoto.height);
	img.push('>');
	
	if (ie && this.photoNode.filters.length > 0) this.photoNode.filters[0].apply();
	this.photoNode.style.height = this.activePhoto.height + 'px'; 
	this.photoNode.style.width = this.activePhoto.width + 'px';	
	this.photoNode.style.top = '0px';
	this.photoNode.style.left = '0px';
	this.photoNode.innerHTML = img.join('');
	if (!ie && !mobile) {
		this.photoNode.style.opacity = '.60';
		this.opacity = 60;
	}
	if (ie && this.photoNode.filters.length > 0) this.photoNode.filters[0].play();
		
	// set the display
	gallery.setDisplay(this.displayNode);
	
	// not ie, manual fade in
	if (!ie && !mobile) {
		setTimeout('slideShow_fadeIn(\'' + this.oid + '\')', 0);
		setTimeout('slideShow_fadeIn(\'' + this.oid + '\')', 0);
		setTimeout('slideShow_fadeIn(\'' + this.oid + '\')', 0);
		setTimeout('slideShow_fadeIn(\'' + this.oid + '\')', 0);
	}
	
	// if we're playing, schedule the next for showing
	if (this.isPlaying) this.timer = setTimeout('slideShow_next(\'' + this.oid + '\')', this.DURATION);
}
slideShow.prototype.showActive = slideShow_showActive;


function slideShow_fadeIn(oid)
{
	var lSlideShow = cortex.getObjByID(oid);
	if (lSlideShow == null) 			return;
	if (lSlideShow.photoNode == null) 	return;
	if (lSlideShow.opacity >= 100)	{
		if (lSlideShow.priorPhoto && lSlideShow.priorPhoto.parentNode) {
			lSlideShow.priorPhoto.parentNode.removeChild(lSlideShow.priorPhoto);
			lSlideShow.priorPhoto = null;
		}
		return;
	}
	
	lSlideShow.opacity += 1;
	lSlideShow.photoNode.style.opacity = (lSlideShow.opacity / 100) + '';
	
	if (lSlideShow.opacity < 100) setTimeout('slideShow_fadeIn(\'' + oid + '\')', 0);
}


function slideShow_killClock()
{
	if (this.timer != null) clearTimeout(this.timer);
}
slideShow.prototype.killClock = slideShow_killClock;

function slideShow_showThumbs()
{
}
slideShow.prototype.showThumbs = slideShow_showThumbs;


function slideShow_next(oid)
{
	var lSlideShow = cortex.getObjByID(oid);
	if (lSlideShow == null) return;
	
	var nextIdx = 0;
	if (lSlideShow.activePhoto) {
		nextIdx = lSlideShow.activePhoto.idx + 1;
		if (nextIdx >= lSlideShow.photos.length) nextIdx = 0;
	}
	
	lSlideShow.activePhoto = lSlideShow.photos[nextIdx];
	lSlideShow.showActive();
}
slideShow.prototype.next = slideShow_next;


function slideShow_previous(oid)
{
	var lSlideShow = cortex.getObjByID(oid);
	if (lSlideShow == null) return;
	
	var nextIdx = 0;
	if (lSlideShow.activePhoto) {
		nextIdx = lSlideShow.activePhoto.idx - 1;
		if (nextIdx < 0) nextIdx = lSlideShow.photos.length - 1;
	}
	
	lSlideShow.activePhoto = lSlideShow.photos[nextIdx];
	lSlideShow.showActive();
}
slideShow.prototype.previous = slideShow_previous;


