/*
$Id: js_functions.inc.js 1015 2005-04-11 22:54:12Z fesh $
*/
function popup_image(image_url, title) {
	var winl = (screen.width - 200)/2;
	var wint = (screen.height - 300)/2;
	var settings ='top='+wint+',';
	settings +='left='+winl+',';
	settings +='width=200, ';
	settings +='height=300, ';
	settings +='scrollbars=no,';
	settings +='resizable=yes';

	title = title.replace(/\s/g, '%20');

	win=window.open('image_popup.php?image_url='+image_url+'&title='+title, 'image',settings);

	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function popup_gallery(g, i, w, h) {
	var winl = (screen.width - 700)/2;
	var wint = (screen.height - 500)/2;
	var settings ='top='+wint+',';
		settings +='left='+winl+',';
		settings +='width=700, ';
		settings +='height=500, ';
		settings +='scrollbars=no,';
		settings +='resizable=yes';

	win=window.open('/gallery_popup.php?g='+g+'&i='+i+'&w='+w+'&h='+h, 'image',settings);

	if(parseInt(navigator.appVersion) >= 4) {
	   win.window.focus();
	}
}

/* preload images */
if (document.images) {
	var spacerOn = new Image();
	spacerOn.src = "images/spacer.gif";
}
var slideshow = {
	id: "gallery_inner",
	fadeSpeed: 5,
	fadeDegree: 0.02,
	autoSpeed: 2000,
	currentlyVisible: 0,
	images: [],
	fadeIn_i: "",
	fadeOut_i: "",
	auto_i: "",
	nxtnxt: 0,
	init: function(){
		if(document.getElementById && document.getElementById(slideshow.id)){
			var container = document.getElementById(slideshow.id);
			var imgs = container.getElementsByTagName('div');
			var i=0;
			for(z in imgs){
				//nodelist to array. there's probably a better way to do it, but I'm not a js guru.
				if (imgs[z].style){
					slideshow.images[i] = imgs[z];
					i++;
				}
			}
			if(slideshow.images.length > 1){
				//the last item is a delimiter therefore we don't want it to be scrambled (or hidden for that matter);
				var delimiter = slideshow.images.pop();
				
				for(n in slideshow.images){
					slideshow.hide(n); 	
				}
				var o = slideshow.images;
				// javascript fisher yates shuffle via http://jsfromhell.com/array/shuffle
				for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
				//I don't have a clue how it does what it does, but that's ok, because it does it well.
				slideshow.images = o;
				slideshow.images.push(delimiter);
				slideshow.show(slideshow.images.length -1);
				slideshow.auto()
			}
		}
	},
	show: function(n){
		slideshow.currentlyVisible = n;
		if(slideshow.fadeIn_i){clearInterval(slideshow.fadeIn_i)}
		slideshow.images[n].style.display = 'block';
		slideshow.images[n].style.opacity = '1';
		slideshow.images[n].style.filter = "alpha(opacity=100)";
		
	},
	hide: function(n){
		if(slideshow.fadeOut_i){clearInterval(slideshow.fadeOut_i)}
		slideshow.images[n].style.display = 'none';
		slideshow.images[n].style.opacity = '0';
		slideshow.images[n].style.filter = "alpha(opacity=0)";
	},
	auto: function(){
		if(slideshow.auto_i){clearInterval(slideshow.auto_i)}
		slideshow.auto_i = setInterval("slideshow.fadeInOut()", slideshow.autoSpeed);
	},
	fadeInOut: function(){
		var cur = Number(slideshow.currentlyVisible);
		var nxt = cur == slideshow.images.length - 2 ? 0 : cur + 1;
		if (cur == slideshow.images.length -1){
			nxt = slideshow.nxtnxt; 
		}	else {
			slideshow.nxtnxt = nxt;
			nxt = slideshow.images.length -1;
		}
		slideshow.fadeOut(cur);
		slideshow.fadeIn(nxt);
	},
	fadeIn: function(n){
		slideshow.images[n].style.display = 'block';		
		if(slideshow.fadeIn_i){clearInterval(slideshow.fadeIn_i)}
		slideshow.fadeIn_i = setInterval("slideshow.run("+n+", 1)", slideshow.fadeSpeed);
	},
	fadeOut: function(n){
		if(slideshow.fadeOut_i){clearInterval(slideshow.fadeOut_i)}
		slideshow.fadeOut_i = setInterval("slideshow.run("+n+", -1)", slideshow.fadeSpeed);
	},
	run: function(n, dir){
		var o = Number(slideshow.images[n].style.opacity) + slideshow.fadeDegree * dir;
		if(o >= 1 && dir > 0){
			slideshow.show(n);
		} else if(o <= 0 && dir < 0) {
			slideshow.hide(n);
		} else {
			slideshow.images[n].style.opacity = o;
			slideshow.images[n].style.filter = "alpha(opacity="+o*100+")";
		}
	}
}