// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 
// Coded by Travis Beckham
// http://www.squidfingers.com | http://www.podlob.com
// If want to use this code, feel free to do so, but please leave this message intact.
// If you do remove this, I will hunt you down :)
//
// |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// --- myh_API version date: 03/29/02 ------------------------------------------------------
//
// ---------------------------------------------------------------------------------------
// Several functions added or modified by Scott Upton, Uptonic.com
// January 2005
// ---------------------------------------------------------------------------------------

function h_c_getStyle(el,styleProp)
{
	var x = document.getElementById(el);
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}

h_Detect = function(){
	var agent = navigator.userAgent.toLowerCase(); 
	this._mac = agent.indexOf('mac') != -1;
	this._win = !this._mac;
	this._w3c = document.getElementById;
	this._iex = document.all;
	this._ns4 = document.layers;
}
h_Detect.prototype.getObj = function(name){
	if(this._w3c){
		return document.getElementById(name);
	}else if(this._iex){
		return document.all[name];
	}else if(this._ns4){
		return this.getObjNS4(document,name);
	}
}
h_Detect.prototype.getObjNS4 = function(obj, name){
	var d = obj.layers;
	var result,temp;
	for(var i=0; i<d.length; i++){
		if(d[i].id == name){
		 	result = d[i];
		}else if(d[i].layers.length){
			var temp = this.getObjNS4(d[i],name);
		}
		if(temp){
			result = temp;
		}
	}
	return result;
}
h_Detect.prototype.getStyle = function(obj){
	return (this._ns4) ? obj : obj.style;
}
h_Detect.prototype.getWindowWidth = function(){ // width of the window
	return this._iex ? document.body.clientWidth : window.innerWidth;
}
h_Detect.prototype.getWindowHeight = function(){ // height of the window
	return this._iex ? document.body.clientHeight : window.innerHeight;
}
h_Detect.prototype.getScrollTop = function(){ // top scroll position of the window
	return this._iex ? document.body.scrollTop : window.pageYOffset;
}
h_Detect.prototype.getScrollLeft = function(){ // left scroll position of the window
	return this._iex ? document.body.scrollLeft : window.pageXOffset;
}
h_Detect.prototype.setScrollTop = function(n){ // set the vertical scroll position of the window
	window.scrollTo(this.getScrollLeft(),n);
}
h_Detect.prototype.setScrollLeft = function(n){ // set the horizontal scroll position of the window
	window.scrollTo(n,this.getScrollTop());
}
h_Detect.prototype.setScroll = function(x,y){ // set the x,y scroll position of the window
	window.scrollTo(x,y);
}

// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

// h_HTMLobj Constructor

h_HTMLobj = function(name){
	if(name){
		this._inherit = h_Detect; this._inherit(name);
		this._id  = name;
		this._el  = this.getObj(this._id);
		this._css = this.getStyle(this._el);
		this._obj = name+'Object'; eval(this._obj+'=this');	
		this._timer = null;
		this._glideRunning = false;
		this._tweenRunning = false;
		this._fadeRunning = false;	// Added by SU, Couloir
		this._randNum = null;		// Added by SU, Couloir
		this._h_startFade = false;	// Added by SU, Couloir
		return this;
	}
}
h_HTMLobj.prototype = new h_Detect();

h_HTMLobj.prototype.getLeft = function(){ // left position of the element
	return parseInt(this._css.left || 0);
}
h_HTMLobj.prototype.getTop = function(){ // top position of the element
	return parseInt(this._css.top || 0);
}		
h_HTMLobj.prototype.getWidth = function(){ // width of the element
	if(this._ns4){
		 return this._el.document.width;
	}else{
		return this._el.offsetWidth;
	}
}
h_HTMLobj.prototype.getHeight = function(){ // height of the element
	if(this._ns4){
		 return this._el.document.height;
	}else{
		return this._el.offsetHeight;
	}
}
h_HTMLobj.prototype.getClipWidth = function(){ // clip width of the element
	if(this._ns4){
		 return this._el.clip.width;
	}else{
		return this._el.offsetWidth;
	}
}
h_HTMLobj.prototype.getClipHeight = function(){ // clip height of the element
	if(this._ns4){
		 return this._el.clip.height;
	}else{
		return this._el.offsetHeight;
	}
}
h_HTMLobj.prototype.setStyle = function(prop, val){ // change the value of any css property
	if(!this._ns4){
		this._el.style[prop] = val;
		if(this._iex && this._mac){
			this._el.innerHTML = this._el.innerHTML;
		}
	}
}
h_HTMLobj.prototype.show = function(){ // show the visibility of the element
	this._css.visibility = 'visible';
}
h_HTMLobj.prototype.hide = function(){ // hide the visibility of the element
	this._css.visibility = 'hidden';
}
h_HTMLobj.prototype.showhide = function(){ // toggle the visibility of the element
	if(this._css.visibility == 'hidden' || this._css.visibility == 'hide'){
		this._css.visibility = 'visible';
	}else{
		this._css.visibility = 'hidden';
	}
}
h_HTMLobj.prototype.setInner = function(html){ // change the contents of the element
	if(this._ns4){
		this._el.document.open();
		this._el.document.write(html);
		this._el.document.close();
	}else{
		this._el.innerHTML = html;
	}
}
h_HTMLobj.prototype.moveTo = function(x,y){ // move the element to a new position
	if(this._ns4){
		this._el.moveTo(x,y);
	}else{
		this._css.left = x;
		this._css.top  = y;
	}
}
h_HTMLobj.prototype.moveBy = function(x,y){ // move the element to a new position relative to it's current position
	if(this._ns4) {
		this._el.moveBy(x,y);
	}else{
		this._css.left = this.getLeft()+x;
		this._css.top  = this.getTop()+y;
	}
}
h_HTMLobj.prototype.sizeTo = function(w,h){ // set the size of the element
	if(!this._ns4){
		this._css.width = w+'px';
		this._css.height = h+'px';
	}
}
h_HTMLobj.prototype.sizeBy = function(w,h){ // set the size of the element relative to it's current size
	if(!this._ns4){
		this._css.width = this.getWidth()+w+'px';
		this._css.height = this.getHeight()+h+'px';
	}
}
h_HTMLobj.prototype.glideTo = function(x,y,callback){ // ease-out animation, callback function optional
	if(this._glideRunning){
		var left = this.getLeft();
		var top  = this.getTop();
		if(Math.abs(left-x)<=1 && Math.abs(top-y)<=1){
			this.moveTo(x,y);
			this.cancelGlide();
			if(callback){
				eval(this._obj+'.'+callback+'()');
			}
		}else{
			this.moveTo(left+(x-left)/2, top+(y-top)/2);
		}
	}else{
		var c = (callback) ? ',\"'+callback+'\"' : '' ;
		this._timer  = setInterval(this._obj+'.glideTo('+x+','+y+c+')',100);
		this._glideRunning = true;
	}
}
h_HTMLobj.prototype.cancelGlide = function(){ // cancel the glideTo method
	clearInterval(this._timer);
	this._timer = null;
	this._glideRunning = false;
}
h_HTMLobj.prototype.swapDepth = function(obj){ // swap the z-index of 2 elements
	var temp = this._css.zIndex;
	this._css.zIndex = obj._css.zIndex;
	obj._css.zIndex = temp;
}
// -------------------------------------------
// Modified by SU, Uptonic.com
// -------------------------------------------
h_HTMLobj.prototype.tweenTo = function(method, start, end, time){ // time-based animation, with multiple easing methods
// method: a function that takes 4 arguments: time, start, change, and duration
// start: array of starting width, height dimensions [w, h]
// end: array of ending width, height dimensions [w, h]
// time: number of 'frames' it takes to get to the end position
	if(!this._tweenRunning){
		this._tweenTime = 0;
		var s = '['+start.toString()+']';
		var e = '['+end.toString()+']';
		this._timer = setInterval(this._obj+'.tweenTo('+method+','+s+','+e+','+time+')', 33);
		this._tweenRunning = true;
	}
	if(++this._tweenTime > time){
		this.cancelTween();
	}else{
		var w = method(this._tweenTime, start[0], end[0]-start[0], time);
		var h = method(this._tweenTime, start[1], end[1]-start[1], time);
		this.sizeTo(w,h);
	}
}
h_HTMLobj.prototype.cancelTween = function(){ // cancel the tweenTo method
	clearInterval(this._timer);
	this._timer = null;
	this._tweenRunning = false;
	this._h_startFade = true;
}

// -> Easing Equations by Robert Penner - robertpenner.com -
linearTween = function(t, b, c, d){
	return c*t/d + b;
}
easeInQuad = function(t, b, c, d){
	t /= d;
	return c*t*t + b;
}
easeOutQuad = function(t, b, c, d){
	t /= d;
	return -c * t*(t-2) + b;
}
easeInOutQuad = function(t, b, c, d){
	t /= d/2;
	if (t < 1) return c/2*t*t + b;
	t--;
	return -c/2 * (t*(t-2) - 1) + b;
}
easeInExpo = function(t, b, c, d){
	return c * Math.pow( 2, 10 * (t/d - 1) ) + b;
}
easeOutExpo = function(t, b, c, d){
	return c * ( -Math.pow( 2, -10 * t/d ) + 1 ) + b;
}
// -------------------------------------------
// Added by SU, Uptonic.com
// December 2004 - January 2005
// -------------------------------------------
h_HTMLobj.prototype.getRandom = function(start,end){ // generate new random number
    this._randNum= Math.round(start + ((end-start) * Math.random()));
    return this._randNum;
}
h_HTMLobj.prototype.setOpacity = function(opacity){ // set opacity of the element
	// Fix for math error in some browsers
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Windows
	this._css.filter = "alpha(opacity:"+opacity+")";
	// Safari < 1.2, Konqueror
	this._css.KHTMLOpacity = opacity/100;	
	// Older Mozilla and Firefox
	this._css.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	this._css.opacity = opacity/100;
}
h_HTMLobj.prototype.fadeOut = function(opacity, change, speed){ // gradually decrease the opacity of the element
// opacity: starting opacity of element
// change: the size of the increments between steps
// speed: the rate of the animation
	if (opacity >= 0){
	  this._fadeRunning = true;
	  this.setOpacity(opacity);
	  opacity -= change;
	  setTimeout(this._obj+'.fadeOut('+opacity+','+change+','+speed+')', speed);
	} else {
		this._fadeRunning = false;
		this.hide();
	}
}
h_HTMLobj.prototype.fadeIn = function(opacity, change, speed){ // gradually increase the opacity of the element
// opacity: starting opacity of element
// change: the size of the increments between steps
// speed: the rate of the animation	
	if (opacity <= 99){
	  this.show();
	  this._fadeRunning = true;
	  this.setOpacity(opacity);
	  opacity += change;
	  setTimeout(this._obj+'.fadeIn('+opacity+','+change+','+speed+')', speed);
	} else {
		this._fadeRunning = false;
		this.setOpacity(99);
	}
}
h_HTMLobj.prototype.displayShow = function(){ // display the element as 'block'
	this._css.display = 'block';
}
h_HTMLobj.prototype.displayHide = function(){ // do not display the element
	this._css.display = 'none';
}
h_HTMLobj.prototype.setSrc = function(target){ // set the element's source to target
	this._el.src = target;
}
h_HTMLobj.prototype.setHref = function(target){ // set the element's link to target
	this._el.href = target;
}
h_HTMLobj.prototype.setInnerHtml = function(content){ // set the element's inner HTML to content
	this._el.innerHTML = content;
}
	// -----------------------------------------------------------------------------------
	// 
	// This page coded by Scott Upton
	// http://www.uptonic.com | http://www.couloir.org
	//
	// This work is licensed under a Creative Commons License
	// Attribution-ShareAlike 2.0
	// http://creativecommons.org/licenses/by-sa/2.0/
	//
	// Associated h_API copyright 2002, Travis Beckham (www.squidfingers.com)
	//
	// -----------------------------------------------------------------------------------
	// --- version date: 04/30/05 ------------------------------------------------------
	
	var h_PhotoDir = "/img/"; // Location of h_Photos for gallery
	var h_borderSize = 0;	 // = 2x CSS border size
	
	// get current h_Photo id from URL
	var h_thisURL = document.location.href;
	var h_splitURL = h_thisURL.split("#");
	var h_PhotoId = h_splitURL[1] - 1;
//	alert(h_PhotoId);
	// if no id in query string then set to 0
	h_PhotoId = (!h_PhotoId)? 0:h_PhotoId;
	h_PhotoId = (h_PhotoId == -1)?0:h_PhotoId;
	// Number of h_Photos in this gallery
	var h_PhotoNum = h_PhotoArray.length;
	
	// Create access to 'h_Detect' object and a place to put instances of 'h_HTMLobj'
	h_API = new h_Detect();
	
	// CREATE INSTANCES & LOAD
	h_loadAPI = function(){
		// Instantiate h_HTMLobj
		h_API.h_Container		= new h_HTMLobj('h_Container');
		h_API.h_Photo			= new h_HTMLobj('h_Photo');
		h_API.h_PhotoContainer	= new h_HTMLobj('h_PhotoContainer');
		h_API.h_LinkContainer	= new h_HTMLobj('h_LinkContainer');
		h_API.h_prevLink		= new h_HTMLobj('h_prevLink');
		h_API.h_nextLink		= new h_HTMLobj('h_nextLink');
		h_API.h_CaptionBlock	= new h_HTMLobj('h_CaptionBlock');
		h_API.h_CaptionBlockBg	= new h_HTMLobj('h_CaptionBlockBg');
		h_API.h_Counter			= new h_HTMLobj('h_Counter');
		h_API.h_Caption			= new h_HTMLobj('h_Caption');
		h_API.h_LoadImg			= new h_HTMLobj('h_LoadImg');
		
		// Show initial h_Photo
		h_cyclePhoto(h_PhotoId);
	}
	onload = h_loadAPI;
	
	// Fade in h_Photo when it is loaded from the server
	h_initFade = function(){
		// Show h_PhotoContainer again
		h_API.h_PhotoContainer.show();
		
		// Be certain the tween is complete before fading, too
		var h_fade_timer = setInterval('h_startFade()', 1000);
						
		// Fade h_Photo in when ready and clear listener
		h_startFade = function(){
			if(h_API.h_Container._tweenRunning == false){
				clearInterval(h_fade_timer);
				
				// Be certain fade is done running before allowing next/previous links to work
				// This avoids rh_APId fade-in when users click next/previous links in quick succession
				var h_adv_timer = setInterval('h_permitNextPrev()', 500);
				
				// Permit next/previous links to function normally when fade is completed
				h_permitNextPrev = function(){
					if(h_API.h_Photo._fadeRunning == false){
						clearInterval(h_adv_timer);
						
						// Only show links if there is more than one h_Photo in array
						if(h_PhotoNum > 1){
							h_API.h_LinkContainer.displayShow();
							document.getElementById('h_nextLink').onclick = h_nextPhoto;
							document.getElementById('h_prevLink').onclick = h_prevPhoto;
						}
					} else {
						return;
					}
				}
				// Swap out loading animation to spare CPU cycles when hidden anyway
				h_API.h_LoadImg.setSrc("/img/header_start.gif");
				
				// Show h_Caption again
				h_API.h_CaptionBlockBg.show();
				h_API.h_CaptionBlock.show();
				
				// Fade h_Photo in
				h_API.h_Photo.fadeIn(0,15,33);
				// Switch background to black
				document.getElementById("h_Container").style.backgroundColor = 'black';
			} else {
				return;
			}
		}
	}
	
	// Prevent next/previous
	h_falsify = function(){
		return false;
	}
	
	// Go to next h_Photo
	h_nextPhoto = function(){
		// Go to next h_Photo
		if(h_PhotoId == (h_PhotoArray.length - 1)){
			h_PhotoId = 0;
		} else {
			h_PhotoId++;
		}
		h_cyclePhoto(h_PhotoId);
	}
	
	// Go to previous h_Photo
	h_prevPhoto = function(){
		// If at start, go back to end
		if(h_PhotoId == 0){
			h_PhotoId = h_PhotoArray.length - 1;
		} else {
			h_PhotoId--;
		}
		h_cyclePhoto(h_PhotoId);
	}
	
	// Alter class of elements
	h_changeElementClass = function(objId,setClass) {
		document.getElementById(objId).className = setClass;
	}
	
	// Function to load subsequent h_Photos in gallery
	h_cyclePhoto = function(h_PhotoId){

		// Switch background to white
		document.getElementById("h_Container").style.backgroundColor = 'white';
				
		// Swap in loading animation
		h_API.h_LoadImg.setSrc("/img/header_loading.gif");
		
		// Hide link h_Container if it is not already hidden
		h_API.h_LinkContainer.displayHide();
		
		// Hide h_Photo h_Container and h_Caption temporarily
		h_API.h_Photo.hide();
		h_API.h_Photo.setOpacity(0);
		h_API.h_CaptionBlockBg.hide();
		h_API.h_CaptionBlock.hide();
		
		// Get dimensions of h_Photo
		var h_wNew = h_PhotoArray[h_PhotoId][1];
		var h_hNew = h_PhotoArray[h_PhotoId][2];
		
		// Start tween on a delay
		var h_wCur = h_API.h_Container.getWidth() - h_borderSize;
		var h_hCur = h_API.h_Container.getHeight() - h_borderSize;
		
		// Begin tweening on a short timer
		setTimeout('h_API.h_Container.tweenTo(easeInQuad, ['+h_wCur+', '+h_hCur+'], ['+h_wNew+','+h_hNew+'], 7)',500);
		setTimeout('h_API.h_LinkContainer.sizeTo('+h_wNew+','+h_hNew+')',500);
		setTimeout('h_API.h_prevLink.sizeTo('+h_wNew/2+','+h_hNew+')',500);
		setTimeout('h_API.h_nextLink.sizeTo('+h_wNew/2+','+h_hNew+')',500);
		setTimeout('h_API.h_CaptionBlock.sizeTo('+h_wNew+',18)',500);
		
		// Get new h_Photo source
		var h_newPhoto = h_PhotoDir + h_PhotoArray[h_PhotoId][0];
		
		// Set source, width, and height of new h_Photo
		h_API.h_Photo.setSrc(h_newPhoto);		
		h_API.h_Photo.sizeTo(h_wNew,h_hNew);
		
		// Set links to new targets based on h_PhotoId
		h_API.h_nextLink.setHref("#" + (h_PhotoId+1));
		h_API.h_prevLink.setHref("#" + (h_PhotoId+1));
		h_API.h_Counter.setInnerHtml((h_PhotoId+1)+"/"+h_PhotoNum+" |");
		h_API.h_Caption.setInnerHtml(h_PhotoArray[h_PhotoId][3]);
		
		// Event listeners for onload and onclick events
		document.getElementById('h_Photo').onload = h_initFade;
		
		// Block next/previous links until h_permitNextPrev() has fired
		document.getElementById('h_nextLink').onclick = h_falsify;
		document.getElementById('h_prevLink').onclick = h_falsify;
	}
