otgLB = null;
otgLBImages = [];

function OTGLightBox (id, width, frameWidth) {
	// Properties
	this.id = id;
	this.frameWidth = frameWidth;
	this.objLightBox = document.getElementById(id);
	this.objFrame = getFirstChildElement(this.objLightBox);
	this.objArea = getFirstChildElement(this.objFrame);
	this.objImage = getFirstChildElement(this.objArea);
	this.objNav = getLastChildElement(this.objArea);
	this.objNavDivCount = getFirstChildElement(this.objNav);
	this.width = width;
	this.height = 0;
	this.posArray = [];
	OTGLightBoxPosCallback(this.posArray,f_clientWidth(),f_scrollTop());
	this.left = this.posArray[0];
	this.top = this.posArray[1];
	this.imgWidth = this.width-(this.frameWidth*2);
	this.imgHeight = 0;
	this.imgArray = [];
	this.pos = 0;
	this.multi = false;
	// Methods
	this.setSizes = function(srcObj) {
		OTGLightBoxPosCallback(this.posArray,f_clientWidth(),f_scrollTop());
		this.left = this.posArray[0];
		this.top = this.posArray[1];
		this.imgHeight = Math.round((srcObj.height/srcObj.width)*this.imgWidth);
		this.height = this.imgHeight+(this.frameWidth*2);
		if (this.multi) this.height += this.objNav.offsetHeight;
		this.objLightBox.style.left = this.left+'px';
		this.objLightBox.style.top = this.top+'px';
		this.objLightBox.style.width = this.width+'px';
		this.objLightBox.style.height = this.height+'px';
		this.objFrame.style.width = (this.width-this.frameWidth)+'px';
		this.objFrame.style.height = (this.height-this.frameWidth)+'px';
		this.objArea.style.width = (this.width-(this.frameWidth*2))+'px';
		this.objArea.style.height = (this.height-(this.frameWidth*2))+'px';
		this.objImage.style.width =  this.imgWidth+'px';
		this.objImage.style.height = this.imgHeight+'px';
	}
	this.showBoxThis = function(obj) {
		this.multi = false;
		this.changeSource(obj.src);
		this.setSizes(this.objImage);
		this.objLightBox.style.visibility = 'visible';
	}
	this.showBoxSrc = function(src) {
		this.multi = false;
		this.changeSource(src);
		this.setSizes(this.objImage);
		this.objLightBox.style.visibility = 'visible';
	}
	this.showBoxMulti = function(pos) {
		this.multi = true;
		this.pos = pos;
		this.changeSource(this.imgArray[pos]);
		this.setSizes(this.objImage);
		this.objNavDivCount.innerHTML = 'Photo '+(pos+1)+' of '+this.imgArray.length;
		this.objNav.style.visibility = 'visible';
		this.objLightBox.style.visibility = 'visible';
	}
	this.changeSource = function(src) {
		var newImg = document.createElement('IMG');
		newImg.className = this.objImage.className;
		newImg.src = src;
		newImg.otgObj = this;
//		newImg.onload = function() {this.otgObj.setSizes(this.otgObj);}
		this.objImage.parentNode.replaceChild(newImg, this.objImage);
		this.objImage = newImg;
	}
	this.hideBox = function() {
		this.objLightBox.style.visibility = 'hidden';
		this.objNav.style.visibility = 'hidden';
	}
	this.navNext = function() {
		this.pos++;
		if(this.pos>=this.imgArray.length) this.pos = 0;
		otgLB.showBoxMulti(this.pos);
	}
	this.navPrev = function() {
		this.pos--;
		if(this.pos<0) this.pos = this.imgArray.length-1;
		otgLB.showBoxMulti(this.pos);
	}
	this.clear = function() {
		this.imgArray = [];
	}
	this.addImg = function(src) {
		this.imgArray.push(src);
	}
}

function showOTGLBImgs(pos) {
	otgLB.clear();
	for (i=0;i<otgLBImages.length;i++) {
		otgLB.addImg(otgLBImages[i]);
	}
	otgLB.showBoxMulti(pos);
}

function otgLBPreload() {
	imgHold = [];
	for (i=0;i<otgLBImages.length;i++) {
		imgHold[i] = new Image();
		imgHold[i].src = otgLBImages[i];
	}
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
function getFirstChildElement(parentNode) {
	var node = parentNode.firstChild;
	while (node.nodeType != 1) {
		node = node.nextSibling;
	}
	return node;
}
function getLastChildElement(parentNode) {
	var node = parentNode.lastChild;
	while (node.nodeType != 1) {
		node = node.previousSibling;
	}
	return node;
}

