// ImageSwitcher 2.615 by tvst from varal.org
// Share, use and modify at will. Even for commercial purposes. Just don't remove the credits.
// Any ugliness in the code is a courtesy of Internet Explorer. Download now!

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 : "click for next.";
		var th = (c && c.showThumbnails);
		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;
		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 numbersdiv = document.createElement('div');
				numbersdiv.className = (th)? 'imgsw_thumbs' : 'imgsw_numbers';

				// go through all children
				while (b.length > 0) 
				{		
					var isImg = b[0].tagName == 'IMG';
					
					if (isImg)
					{
						// 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 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)
							if (isImg) 
							{
								var src = b[0].realSrc;
								src = src.substr(0, src.lastIndexOf('.')) + ths + src.substr(src.lastIndexOf('.'));
								l = IS.makeThumb(src, thw, thh, b[0].title, x, j, true);
								needsTh = false;
							}
							else needsTh = true;
							
						else l = IS.makeLink(j+countFrom, b[0].title, x, j, true);

						if (y == 0) l.className += ' active';

						numbersdiv.appendChild(l);
					}

					//b[0].style.display = "none";

					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 (var j = 0; j < order.length; j++) a[i].appendChild(eval(order[j]+'div'));

				IS.doTheSwitch(x, 0);
			}
		}
	},
	
	makeThumb : function (src, w, h, t, 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;
	
		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(im);
		l.appendChild(sp);
		return l;
	},	

	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 y_curr = eval(IS.active[x].substr(IS.active[x].indexOf('_link')+5));
		if (y == -1) y = y_curr+1;
		if (y == -2) y = y_curr-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 = '';

		// "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.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 : null,
		callOnParse : function () 
		{			
			if (document.body && document.getElementsByTagName('body')) 
			{
				clearInterval(ParseCtl.timer);
				ParseCtl.complete = true; 
				ParseCtl.onparse();
			}
		},
		
		catchAnnoyingBrowsers : function ()
		{
			if (!ParseCtl.complete) ParseCtl.onparse();
		}
	};
	

	if (navigator.appName.indexOf('Netscape') != -1 && navigator.appVersion.indexOf('5.0') >= 0) 
		document.addEventListener("DOMContentLoaded", ParseCtl.callOnParse, null);
	else ParseCtl.timer = setInterval('ParseCtl.callOnParse()', 1);
	
	window.onload = ParseCtl.catchAnnoyingBrowsers;
};


// run
if (document.getElementsByTagName) 
{
	// optionally use window.onload, if things are going terribly wrong	
	var c = window.ISConf;
	if (c && c.useOnLoad) window.onload = IS.makeViewports;
	else ParseCtl.onparse = IS.makeViewports;
}
