// ImageSwitcher 2.9 by tvst from varal.org
// Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
// PS: cryptic variable names are for file size

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 tt = (c && c.tooltip)? c.tooltip : false;
		var lt = (c && c.listType)? c.listType.toLowerCase() : 'numbers';
		var th = (c && lt=='thumbnails');
		var ths = (th && c.thumbnailSuffix)? c.thumbnailSuffix : '';
		var thw = (th && c.thumbnailWidth)? c.thumbnailWidth : 0;
		var thh = (th && c.thumbnailHeight)? c.thumbnailHeight : 0;
		var countFrom = (c && c.countFrom)? c.countFrom : 0;
		var pt = (c && c.prevText)? c.prevText : '<';
		var nt = (c && c.nextText)? c.nextText : '>';
		if (c.useLoadingImage) 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 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;
				var needsTh = true;


				var listdiv = document.createElement('div');
				listdiv.className = 'imgsw_list_' + lt;

				// specify which image is active (image 0)
				IS.active[x] = new Array();
				IS.active[x][0] = x;
				IS.active[x][1] = 'imgsw'+x+'_link0';

				// go through all children
				while (b.length > 0) 
				{		
					var isImg = b[0].tagName == 'IMG';
					b[0].origTitle = (b[0].title)? b[0].title : false;
					if (tt) b[0].title = tt;
					
					if (isImg)
					{
						// save data about the image
						b[0].id = 'imgsw'+x+'_img'+y;
						b[0].realSrc = b[0].src;

						if (c && !c.usePreloader) b[0].src = '';
					}

					// if this is the first image in its group, make a link
					if (y % e == 0 || (th && isImg && needsTh))
					{
						// create the number link
						var j = Math.floor(y / e);
						
						//if (th)
						switch (lt)
						{
							case 'thumbnails':
							if (isImg) 
							{
								var src = b[0].realSrc;
								src = src.substr(0, src.lastIndexOf('.')) + ths + src.substr(src.lastIndexOf('.'));
								l = IS.imgLink(src, thw, thh, b[0].title, x, j, true);
								needsTh = false;
							} else needsTh = true;
							break;

							case 'titles':
							if (b[0].origTitle) {l = IS.textLink(b[0].origTitle, null, x, j, true); break;}
							
							case 'filenames':
							if (isImg) {l = IS.textLink(b[0].src.substring(b[0].src.lastIndexOf('/')+1), null, x, j, true); break;}
							
							default:
							l = IS.textLink(j+countFrom, null, x, j, true);
						}

						if (y == 0) l.className += ' active';

						listdiv.appendChild(l);
					}

					//b[0].style.display = "none";

					all[x][y] = b[0];
					y++;

					a[i].removeChild(b[0]);
				}

				// make prev/next links

				var prevnextdiv = document.createElement('div');
				prevnextdiv.className = 'imgsw_prevnext';

				prevnextdiv.appendChild(IS.textLink(pt, 'previous image', x, -2, null, 'imgsw_prev'));
				prevnextdiv.appendChild(IS.textLink(nt, 'next image', x, -1, null, 'imgsw_next'));


				// make viewport

				var viewportdiv = document.createElement('div');
				viewportdiv.className = 'imgsw_viewport';
				viewportdiv.id = 'imgsw'+x+'_viewport';


				// make title

				var titlediv = document.createElement('div');
				titlediv.className = 'imgsw_title';
				titlediv.id = 'imgsw'+x+'_title';
				
				// write all divs to the DOM tree, in the correct order
				for (var j = 0; j < order.length; j++) a[i].appendChild(eval(order[j]+'div'));

				IS.doTheSwitch(x, 0);
			}
		}
	},
	
	nodeLink : function (node, tt, x, y, id, cl)
	{
		var l = document.createElement('a');

		l.title = tt;
		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(node);
		l.appendChild(sp);
		return l;
	},

	textLink : function (txt, tt, x, y, id, cl)
	{
		return IS.nodeLink(document.createTextNode(txt), tt, x, y, id, cl);
	},

	imgLink : function (src, w, h, tt, x, y, id, cl)
	{
		var im = document.createElement('img');
		
		im.src = src;
		im.id = 'imgsw'+x+'_link'+y+'_thumb';
		if (w) im.style.width = w;
		if (h) im.style.height = h;
	
		return IS.nodeLink(im, tt, x, y, id, cl);
	},	

	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('list') != -1;
		var y0 = IS.active[x][0];
		var id = IS.active[x][1];

		if (y == -1) y = y0+1;
		if (y == -2) y = y0-1;

		if (y >= all[x].length / e) y = 0;
		if (y < 0) y = Math.ceil(all[x].length / e)-1;

		// title
		var el = IS.all[x][e*y];
		var titlediv = document.getElementById('imgsw'+x+'_title');
		if (titlediv) titlediv.innerHTML = (el.origTitle)? el.origTitle : el.src? el.src.substring(el.src.lastIndexOf('/')+1) : '';

		// links
		if (!document.getElementById(id)) return;
		if (n) document.getElementById(id).className = '';	
		IS.active[x][0] = y;
		id = IS.active[x][1] = 'imgsw'+x+'_link'+y;
		if (!document.getElementById(id)) return;
		if (n) document.getElementById(id).className = 'active';

		var v = document.getElementById('imgsw'+x+'_viewport');
		v.innerHTML = '';

		// "elements per page"-related stuff
		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.loadImg(x, j, v);
			else IS.switchEl(x, j, v);
		}
	},

	loadImg : 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.className = 'imgsw_link_img';
			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 (o)
	{
		o.onload = "";

		var im = o.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 (o)
	{
		alert("Error! Can't load image "+o.realSrc);
	}

};


// "onparse" setup code - should go before any window.onloads!
if (document.getElementsByTagName && !window.ParseCtl)
{
	window.appendHandler = function(eventStr, f) {var f0 = eval("window."+eventStr); eval("window."+eventStr+" = function () {f0(); f();}");};
	window.onparse = function() {};

	window.ParseCtl =
	{
		complete : false, timer : null,
		
		callOnParse : function () 
		{
			if (this.complete || !document.body || !document.getElementsByTagName('body')) return;
			clearInterval(this.timer);
			this.complete = true; 
			window.onparse();
		}
	};

	if 	(navigator.appName.indexOf('Netscape') != -1 
		&& eval(navigator.appVersion.substring(0,navigator.appVersion.indexOf('('))) >= 5 
		&& navigator.userAgent.indexOf('KHTML') == -1) 
		document.addEventListener("DOMContentLoaded", window.ParseCtl.callOnParse, null);

	else ParseCtl.timer = setInterval('ParseCtl.callOnParse()', 1);
	//window.onload = ParseCtl.callOnParse;
};


// run
if (document.getElementsByTagName) 
{
	// optionally use window.onload, if things are going terribly wrong	
	var c = window.ISConf;
	if (c && c.useOnLoad) window.onload = function() {IS.makeViewports();};
	else window.appendHandler("onparse", IS.makeViewports);
}

