// ImageSwitcher 2.55 by tvst from varal.org
// Share, use and modify at will. Even for commercial purposes. Juse don't remove the credits.

var IS = 
{
	all : new Array(),
	active : new Array(),

	loadingImage : new Image(),

	makeViewports : function() 
	{
		var c = window.ISConf;
		var order = (c && c.order)? c.order : new Array('numbers', 'prevnext', 'viewport');
		var t = (c && c.defaultTooltip)? c.defaultTooltip : "";
		var countFrom = (c && c.countFrom)? c.countFrom : 0;
		IS.loadingImage.src = (c && c.loadingImgSrc) ? c.loadingImgSrc : "loading.gif";

		var all = IS.all;
		var a = document.getElementsByTagName("*");

		for (i = 0; i < a.length; i++)
		{
			if (a[i].className == 'imgsw') 
			{
				a[i].className = 'imgsw_toplevel';
				var b = a[i].getElementsByTagName("*");
				var bl = b.length;
				var x = all.length;
				var y = 0;
				var e = (c && c.elementsPerPage) ? c.elementsPerPage : 1;

				a[i].id = 'imgsw'+x;

				all[x] = new Array();

				var v, l, sp;

				// make number links

				var numbersdiv = document.createElement('div');
				numbersdiv.className = 'imgsw_numbers';

				// go through all children
				while (b.length > 0) 
				{
					if (b[0].tagName == 'IMG')
					{
						// save data on the image
						b[0].id = 'imgsw'+x+'_img'+y;
						b[0].realSrc = b[0].src;

						if (!b[0].title) b[0].title = t;
						if (c && !c.usePreloader) b[0].src = "";
					}

					if (y % e == 0)
					{
						// create the number link associated with each element
						var j = Math.round(y / e);
						l = IS.makeLink(j+countFrom, b[0].title, x, j, true);

						if (y == 0) l.className += ' active';

						numbersdiv.appendChild(l);
					}
					all[x][y] = b[0];
					y++;

					a[i].removeChild(b[0]);
				}

				// specify which image is active (image 0)
				IS.active[x] = 'imgsw'+x+'_link0';


				// make prev/next links

				var prevnextdiv = document.createElement('div');
				prevnextdiv.className = 'imgsw_prevnext';

				prevnextdiv.appendChild(IS.makeLink('<', 'previous image', x, -2, null, 'imgsw_prev'));
				prevnextdiv.appendChild(IS.makeLink('>', 'next image', x, -1, null, 'imgsw_next'));


				// make viewport

				var viewportdiv = document.createElement('div');
				viewportdiv.className = 'imgsw_viewport';
				viewportdiv.id = 'imgsw'+x+'_viewport';

				// write all divs to the DOM tree, in the correct order
				for (j = 0; j < order.length; j++) a[i].appendChild(eval(order[j]+'div'));

				IS.doTheSwitch(x, 0);
			}
		}
	},

	makeLink : function (txt, t, x, y, id, cl)
	{
		var l = document.createElement('a');

		l.title = t;
		l.href = 'javascript:IS.doTheSwitch('+x+', '+y+');';
		if (id) l.id = 'imgsw'+x+'_link'+y;
		if (cl) l.className = cl;

		sp = document.createElement('span');
		sp.appendChild(document.createTextNode(txt));
		l.appendChild(sp);
		return l;
	},

	doTheSwitch : function (x, y) 
	{
		var all = IS.all;
		var c = window.ISConf;
		var e = (c && c.elementsPerPage) ? c.elementsPerPage : 1;
		var n = !c.order || c.order.toString().indexOf("numbers") != -1;

		var current = eval(IS.active[x].substr(IS.active[x].indexOf('_link')+5));
		if (y == -1) y = current+1;
		if (y == -2) y = current-1;

		if (y >= all[x].length / e) y = 0;
		if (y < 0) y = Math.ceil(all[x].length / e)-1;

		// links
		if (n) document.getElementById(IS.active[x]).className = "";	
		IS.active[x] = 'imgsw'+x+'_link'+y;
		if (n) document.getElementById(IS.active[x]).className = "active";

		var v = document.getElementById('imgsw'+x+'_viewport');
		v.innerHTML = '';

		for (var i = 0; i < e; i++)
		{
			var j = e*y+i;
			if (j >= all[x].length) break;
			if (all[x][j].tagName == "IMG") IS.switchImg(x, j, v);
			else IS.switchEl(x, j, v);
		}
	},

	switchImg : function (x, y, v)
	{
		var all = IS.all;

		var i = all[x][y];
		var im = new Image();

		var c = window.ISConf;

		var j = v.childNodes.length;
		i.id = 'imgsw_img_'+x+'_'+y+'_'+j;

		im.original = i;
		im.src = i.realSrc;
		var li = c && c.useLoadingImage && !im.complete;

		// can't use DOM for IE to resize correctly
		var s = (li) ?
			'<img src="'+IS.loadingImage.src+'" id="'+i.id+'" class="imgsw_image '+i.className+' imgsw_loading" title="loading..." alt="loading..." />' :
			'<img src="'+i.realSrc+'" id="'+i.id+'" class="imgsw_image '+i.className+'" title="'+i.title+'" alt="'+i.alt+'" />';

		if (c && c.clickImageForNext)
		{
			l = document.createElement('a');
			l.href = 'javascript:IS.doTheSwitch('+x+', -1);';
			l.innerHTML+=s;
			v.appendChild(l);
		}
		else v.innerHTML+=s;

		if (li) 
		{
			im.src = "";
			im.onload = function () {IS.imgOnLoad(this);};
			im.onerror = function () {IS.imgOnError(this);};
			im.src = i.realSrc;
			return;
		}
				
		var im = document.getElementById(i.id);
		im.onclick = i.onclick;
	},

	switchEl : function (x, y, v) 
	{
		var i = IS.all[x][y].cloneNode(false); // for IE
		i.innerHTML = IS.all[x][y].innerHTML;

		v.appendChild(i);
	},

	imgOnLoad : function (that)
	{
		that.onload = "";

		var im = that.original;
		var i = document.getElementById(im.id);

		if (!i) return; // for IE

		i.src = im.realSrc;
		if (im.height) i.height = im.height;
		if (im.width) i.width = im.width;
		i.onclick = im.onclick;
		i.className = 'imgsw_image '+im.className;
	},

	imgOnError : function (that)
	{
		alert("Error! Can't load image "+that.realSrc);
	}
};


// "onparse" setup code
if (document.getElementsByTagName && !window.ParseCtl)
{
	var ParseCtl =
	{
		onparse : function () {;},
		complete : false,
		timer : 0,
		callOnParse : function () 
		{
			if (document.getElementsByTagName("body").length == 0 || ParseCtl.complete) return;
			if (document.readyState && document.readyState < 2) return;

			clearInterval(ParseCtl.timer);

			ParseCtl.complete = true; 
			ParseCtl.onparse();
		}
	};

	if (document.readyState) {
		ParseCtl.timer = setInterval(ParseCtl.callOnParse, 0); // for safari/konqueror
		document.onreadystatechange = ParseCtl.callOnParse;
		}
	else document.addEventListener("DOMContentLoaded", ParseCtl.callOnParse, null);
}


// run
if (document.getElementsByTagName) 
{
	// optionally use window.onload for compatibility
	var c = window.ISConf;
	if (c && c.useOnLoad) window.onload = IS.makeViewports;
	else ParseCtl.onparse = IS.makeViewports;
}
