// $Name:  $ $Id: ss.js 55 2010-07-06 00:29:33Z  $
var ss;

function Slideshow(idDiv,idButtons) {

	var current=0;
	var oId=document.getElementById(idDiv);
	var oButtons=document.getElementById(idButtons);
	var that=this;
	this.sleep=7000;
	this.doloop=true;

	function setImage(direction,index) {

		var old=current;
		if(direction===0) {
			current=index;
		}
		else if(direction === 1) {

			if(current+1 === oId.childNodes.length) {
				current=0;
			} else {
				current++;
			}
		}
		else {
			if(current - 1 < 0) {
				current=oId.childNodes.length -1;
			}else  {
				current--;
			}
		}

		oId.childNodes[old].style.display='none';
		oId.childNodes[current].style.display='block';
		return {oi:old,ni:current};
	}

	this.addImage=function (src,w,h) {

		var img=document.createElement('img');
		img.src=src;
		img.style.width=w+'px';
		img.style.height=h+'px';
		img.style.padding='4px';
		img.className='shadow';
		img.id='ss'+oId.childNodes.length;
		img.style.display='none';
        img.onclick=function() {

            // replace 320x234. with nothing
            window.open(img.src.replace(/-\d+x\d+\./,'.'));
        }
        
        oId.appendChild(img);
	};

	this.show=function (direction,index) {

		var n;
		var a;
		var i;
		var state;
		var oa;
		var na;
		var div;

		if(oButtons.firstChild === null) {

			current=0;
			for(i=0;i<oId.childNodes.length;i++) {

				a=document.createElement('a');	
				a.id='p'+i;
				a.index=i;
				a.href='#';
				a.onclick=function () {

					that.doloop=false;
					that.show(0,this.index);
					this.blur();
					return false;
				};
				a.onmouseout=function() {
					if(this.index === current) {
						return;
					}
					this.style.color='#42a0ff';
					this.style.backgroundColor='#fff';
					this.style.border='none';
				};
				a.onmouseover=function() {
					this.style.color='#47c64a';
					this.style.backgroundColor='#fff';
					this.style.border='1px solid #47c64a';
				};
				a.innerHTML=i+1;
				oButtons.appendChild(a);
			}
		}

		state=setImage(direction,index);
		oa=document.getElementById('p'+state.oi);
		na=document.getElementById('p'+state.ni);

		// old
		oa.style.color='#42a0ff';
		oa.style.backgroundColor='#fff';
		oa.style.border='none';
		// new
		na.style.color='#47c64a';
		na.style.backgroundColor='#fff';
		na.style.border='1px solid #47c64a';
	};
}

function do_the_loop(firstime) {

	if(ss.doloop) {
		if(firstime === undefined) {
			ss.show(1,0);
		}else {
			ss.show(0,0);
		}
		setTimeout('do_the_loop()',ss.sleep);
	}
}
