var pict = null;
var cpict = 1;
var ppause = 100;
var inum = null;
var lnflag = false;
var ua = navigator.userAgent;
var calpha = 0;

function init() {
	// only for IE because they're like that, the 'tards
	if (document.all && document.getElementById) {
		var navRoot = document.getElementById('nav-popup');

		for (var i=0; i<navRoot.childNodes.length; i++) {
			var node = navRoot.childNodes[i];

			if ((node.nodeName=="LI") || (node.nodeName=="li")) {
				node.onmouseover=function() { this.className="over"; }
				node.onmouseout=function() { this.className=""; }
				node.onfocus=function() { this.className="over"; }
				node.onblur=function() { this.className=""; }
				
				node.className = 'winline';
			}
		}
	}
	
	if (! this.gallery) return;

	// FireFox and Safari
	if (document.getElementById('canvas') && document.getElementById('canvas').getContext) {	
		pict = new Image();
		pict.src = '/picts/' + gallery[cpict];
		pict.gallery = 0;

		pict.set_alpha = function(v) {
			var ctx = document.getElementById('canvas').getContext('2d');
			ctx.globalAlpha = v / 100;
		}
			
		inum = setInterval('pfSwitch()',50);
	}
	else if ((ua.indexOf('MSIE') > 0) && (ua.indexOf('Opera') == -1)) {
		pict = document.createElement('img');
		pict.setAttribute('id', 'mpfimg');
		pict.setAttribute('src','/picts/' + gallery[cpict]);
		pict.setAttribute('width', '216');
		pict.setAttribute('height', '210');
		pict.className = 'imgtrans';
		pict.setAttribute('alt', '');

		pict.set_alpha = function(v) {
			if (this.filters.alpha) {
				this.filters.alpha.opacity = v;
			}
		}

		document.getElementById('stdpict').appendChild(pict);
		inum = setInterval('pfSwitch()',50);
	}
}

function uninit() {
	if (inum != null) {
		clearInterval(inum);
	}	
}

function pfSwitch() {	
	if (!pict.complete) return;

	if (ppause > 0) {
		if ((ppause < 80) && lnflag) {
			pict.set_alpha(0);
			pict.src = "/picts/" + gallery[cpict]; 
			lnflag = false;
		}
		
		ppause--;
		return;
	}

	var ctx = (pict.gallery >= 0) ? document.getElementById('canvas').getContext('2d') : null;

	pict.set_alpha(calpha);
	calpha+=5;

	if (ctx != null) {
	      ctx.clearRect(0,0,216,210);
	      ctx.globalAlpha = calpha / 100;
	      ctx.drawImage(pict,0,0);
	}

	if (calpha >= 100) {
		$('stdpict').style.backgroundImage = 'url(/picts/' + gallery[cpict] + ')';

		cpict++;
		if (cpict == gallery.length) cpict = 0;
		
		calpha = 0;		
		lnflag = true;	
		ppause = 100;
	}
}

function $(id) { 
	return document.getElementById(id); 
}
