/**** Photobrowser JS Support ****/
var bigPhotoImage = {
	lastAction:false,
	lastOp:false,
	lastPath:false,
	obj:false,
	width:false,
	height:false,
	path:false,
	className:false,
	photoImage:false,
	userFields:false,
	autoRotateDelay:8000,
	autoLinks:[],
	autoCurrent:0,
	autoTimeout:false,
	
	bind:function( id, rotateTime){
		this.photoImage = document.getElementById(id);
		this.autoRotate( rotateTime);
		if (this.photoImage) {
			var area = this.photoImage.parentNode;
			if ( area.nextSibling && area.nextSibling.tagName == 'DIV')
				this.userFields = area.nextSibling;
			while (area && area.className != 'photoBrowser')
				area = area.parentNode;
			var photoSelect = area.firstChild;
			while (photoSelect && photoSelect.className != 'photoSelect')
				photoSelect = photoSelect.nextSibling;
			if (photoSelect) {
				var atag = photoSelect.getElementsByTagName('a');
				var node;
				var initialPhoto = this.photoImage;
				this.obj = this.photoImage;
				for (var i = 0; i < atag.length; i++) {
					node = atag[i].firstChild;
					// find comment
					while (node && node.nodeType != 8)
						node = node.nextSibling;
					if (node) {
						atag[i].data = node.data.replace(/^ +/,'').split(' ');
						(function(){
							atag[i].onmouseover = function(e) {
								bigPhotoImage.showThumb( this);
								bigPhotoImage.autoRun( false);
							};
							atag[i].onmouseout = function(e) {
								bigPhotoImage.autoRun( this);
							};
						}());
						this.bindFullSizeLink( atag[i], atag[i].href, atag[i].className);
						this.autoLinks.push( atag[i]);
						if ( initialPhoto) {
							this.bindFullSizeLink( initialPhoto.parentNode, atag[i].href, atag[i].className);
							initialPhoto = null;
							this.autoRun( atag[i]);
							this.hiliteThumb( atag[i]);
						}
					}
				}
				var instructions = photoSelect.firstChild;
				while (instructions && instructions.className != 'instructions')
					instructions = instructions.nextSibling;
				if (instructions)
					instructions.innerHTML = 'Move over thumb to view';
			}
			else {
				this.bindFullSizeLink( this.photoImage.parentNode, this.photoImage.parentNode.href, this.photoImage.parentNode.className);
			}
		}
	},
	
	bindFullSizeLink:function( link, fullsizeurl, linkClassName){
		if ( link.tagName == 'A' && linkClassName.length > 0) {
			var width = linkClassName.match( /(^|\s)w(\d+)/);
			var height = linkClassName.match( /(^|\s)h(\d+)/);
			if ( width && height)
				link.onclick = function(e) {
					var winWidth = parseInt( width[2]) + 20;
					var winHeight = parseInt( height[2]) + 20;
					window.open( fullsizeurl, 'Photo' + this.id, 'width=' + winWidth.toString() + ', height=' + winHeight.toString());
					return false;
				}
		}
	},
	
	autoRotate:function( delay) {
		if ( typeof delay == 'number')
			this.autoRotateDelay = delay;
		if ( !isNaN( delay))
			this.autoRotateDelay = parseInt( delay);
	},
	
	autoRun:function( startLink) {
		var auto = true;
		if ( this.autoTimeout) {
			clearTimeout( this.autoTimeout);
			this.autoTimeout = false;
		}
		// Start or Stop Automatic
		if ( typeof startLink != 'undefined' && startLink !== true) {
			if ( startLink.tagName && startLink.tagName == 'A') {
				for ( this.autoCurrent = 0; this.autoCurrent < this.autoLinks.length; this.autoCurrent++)
					if ( startLink == this.autoLinks[ this.autoCurrent])
						break;
			}
			// Stop
			else
				auto = false;
		}
		// Automatic mode
		else {
			this.autoCurrent++;
			if ( this.autoCurrent >= this.autoLinks.length)
				this.autoCurrent = 0;
		
			this.showThumb( this.autoLinks[ this.autoCurrent]);
		}
		if ( auto && this.autoRotateDelay)
			this.autoTimeout = setTimeout( function() { bigPhotoImage.autoRun(); }, this.autoRotateDelay);

	},
	
	hiliteThumb:function( link) {
		for ( var i = 0; i < this.autoLinks.length; i++) {
			this.autoLinks[i].style.borderBottom = '1px solid #fff';
		}
		link.style.borderBottom = '1px solid #ccc';
	},
	
	showThumb:function( link) {
		var url = link.data[0];
		var width = link.data[1];
		var height = link.data[2];
		this.show( link, url, width, height);
		this.photoImage.parentNode.href = link.href;
		this.hiliteThumb( link);
		if ( this.userFields) {
			var div = link.firstChild;
			while ( div && div.tagName != 'DIV')
				div = div.nextSibling;
			if ( div)
				this.userFields.firstChild.innerHTML = div.innerHTML;
			this.userFields.style.display = ( div ? 'block' : 'none');
		}
	},
	
	show:function( thumbLink, imgpath, imgwidth, imgheight){
		this.path = imgpath;
		this.width = imgwidth;
		this.height = imgheight;
		if (this.lastPath != this.path && thumbLink) {
			this.lastPath = this.path;
			MM_preloadImages(this.path);
			if (this.lastAction) {
				clearTimeout(this.lastAction);
				this.lastAction = false;
			}
			this.bindFullSizeLink( thumbLink, thumbLink.href, thumbLink.className);
			this.bindFullSizeLink( this.photoImage.parentNode, thumbLink.href, thumbLink.className);
		}
		this.hideImage(this.lastOp);
	},
	
	hideImage:function(op) {
		if (typeof op != 'number')
				op = 100;
		if (op <= 10) {
			this.lastAction = setTimeout(function() { bigPhotoImage.showImage(op + 5); },30);
			this.obj.style.width = (this.width>0 ? this.width + 'px' : 'auto');
			this.obj.style.height = (this.height>0 ? this.height + 'px' : 'auto');
			this.obj.style.marginLeft = 'auto';
			this.obj.style.marginRight = 'auto';
			this.obj.src = this.path;
		}
		else
			this.lastAction = setTimeout(function() { bigPhotoImage.hideImage(op - 5); },30);
		this.lastOp = op;
		objectFader.setFade(this.obj,op);
	},
	
	showImage:function(op) {
		if (typeof op != 'number')
			op = 100;
		if (op >= 90)
			op = 100;
		else
			this.lastAction = setTimeout(function() { bigPhotoImage.showImage(op + 5); },30);
		this.lastOp = op;
		objectFader.setFade(this.obj,op);
	}
};

var objectFader = {
	menutime:100,
	lastn:0,
	fadeInStep:10,
	fadeOutStep:10,
	fadeTime:50,

	showSubnav:function(menu) {
		var m=1;
		var obj=false;
		do {
			if (obj=document.getElementById('subnav'+m)) {
				if (menu!=m)
					fadeOut(obj);
				else
					fadeIn(obj);
			}
			else
				break;
		} while (m++);
		this.lastn++;
	},
	
	hideSubnav:function() {
		this.lastn++;
		setTimeout(function(){ objectFader.closeSubnav(objectFader.lastn) }, objectFader.menutime);
	},
	
	closeSubnav:function(tick) {
		 if (tick == this.lastn)
			 this.showSubnav(0);
	},
	
	fadeIn:function(obj) {
		if (obj) {
			if (typeof obj.fade == 'undefined')
				obj.fade = 0;
			obj.step = this.fadeInStep;
			if (obj.fade == 0)
				this.doFade(obj.id);
		}
	},

	fadeOut:function(obj) {
		if (obj) {
			if (typeof obj.fade == 'undefined')
				obj.fade = 0;
			obj.step = -this.fadeOutStep;
			if (obj.fade == 100)
				this.doFade(obj.id);
		}
	},
	
	doFade:function(id) {
		var obj = document.getElementById(id);
		if (obj) {
			// add/subtract current step amount
			obj.fade = Math.min(100,Math.max(0,obj.fade + obj.step));
			// set opacity
			this.setFade(obj,obj.fade);
			// Still more to go - timeout for a bit and keep going
			if (obj.fade<100 && obj.fade>0)
				setTimeout(function(){ objectFader.doFade(id); },objectFader.fadeTime);
		}
	},
	
	setFade:function(obj,value) {
		// if completely faded, don't display
		obj.style.display = (value == 0 ? 'none' : 'block');
		// Set fade amount
		// CSS3
		obj.style.opacity = Math.min(99.999,value/100) + '';
		// IE5.5+
		obj.style.filter = 'alpha(opacity=' + value + ')';
		// Gecko before CSS3 support
		obj.style.MozOpacity = Math.min(99.999,value/100) + '';
		// Konquerer and Safari
		obj.style.KHTMLOpacity = Math.min(99.999,value/100) + '';
	}
};
