/**** Photobrowser JS Support ****/
var bigPhotoImage = {
	lastAction:false,
	lastOp:false,
	lastPath:false,
	obj:false,
	width:false,
	height:false,
	path:false,
	photoImage:false,
	
	bind:function(id){
		this.photoImage = document.getElementById(id);
		if (this.photoImage) {
			var area = this.photoImage.parentNode;
			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;
				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(){
							photoId = id;
							atag[i].onmouseover = function(e) {
								var url = this.data[0];
								var width = this.data[1];
								var height = this.data[2];
								bigPhotoImage.show(photoId,url,width,height);
								bigPhotoImage.photoImage.parentNode.href = this.href;
							};
							atag[i].onclick = function(e) {
								return false;
							};
						}());
					}
				}
				var instructions = photoSelect.firstChild;
				while (instructions && instructions.className != 'instructions')
					instructions = instructions.nextSibling;
				if (instructions)
					instructions.innerHTML = 'Move over thumb to view';
			}
		}
	},
	
	show:function(id,imgpath,imgwidth,imgheight){
		this.path = imgpath;
		this.width = imgwidth;
		this.height = imgheight;
		this.obj = document.getElementById(id);
		if (this.lastPath != this.path && this.obj) {
			this.lastPath = this.path;
			MM_preloadImages(this.path);
			if (this.lastAction) {
				clearTimeout(this.lastAction);
				this.lastAction = false;
			}
		}
		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) + '';
	}
};
