
function mran(ma,mi)
{return(Math.round(Math.random()*(ma-mi))+mi)}

var Slideshow = Class.create({
	 initialize: function(delay, totalElmt, elmtName) {
		this.delay = delay;
		this.totalElmt = totalElmt;
		this.elmtName = elmtName;
		this.paused = 0;
		this.arrSlideElmt = [totalElmt];
		this.curElmtNum = 1; //randomize first element
		this.bulletUpdate(this.curElmtNum);
		
	 	//preload all elements into array and preload all images within element
		for (i = 1; i <= totalElmt; i++) {
			this.arrSlideElmt[i] = $(elmtName + i);
			this.arrSlideElmt[i].observe('mouseover', function() { this.paused = 1; }.bind(this));  //pause on mouseover
			this.arrSlideElmt[i].observe('mouseout', function() { this.paused = 0; }.bind(this));  //resume on mouseout
			$$(this.arrSlideElmt[i].img).each(function(img) {	
				img = new Image();
			});
		}
		$$('img.arrow').each(function(node) {  //looks for all arrows (pointing right) and handles click event
			node.observe('click', function(s){
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'none'
				});
				this.checkSlide();
				this.arrSlideElmt[this.curElmtNum].setStyle({
				  display: 'block'
				});
				s.stop();
			}.bind(this));
		}.bind(this));			
	 },
	start: function() {
		//show first element without effect
		this.arrSlideElmt[this.curElmtNum].setStyle({
		  display: 'block'
		});
		this.executor = new PeriodicalExecuter(function() { 
			this.next(); //start slidehow
		}.bind(this), this.delay);		
	},
	
	next: function(){
		if (!this.paused) {
			this.update();
		}
	},
	hardNext: function(){
		if (!this.paused) {
			for (i = 1; i <= this.totalElmt; i++) {
				this.arrSlideElmt[i].setStyle({
				  display: 'none'
				});
			}
			
			this.checkSlide();
			this.arrSlideElmt[this.curElmtNum].setStyle({
			  display: 'block'
			});
		}
	},
	hardPrev: function(){
		if (!this.paused) {
			for (i = 1; i <= this.totalElmt; i++) {
				this.arrSlideElmt[i].setStyle({
				  display: 'none'
				});
			}
			
			if (this.curElmtNum == 1) { this.curElmtNum = this.totalElmt; }
			else { this.curElmtNum --; }	
			
			this.arrSlideElmt[this.curElmtNum].setStyle({
			  display: 'block'
			});
		}
	},
	jump: function(slide){
		if (!this.paused) {		
			for (i = 1; i <= this.totalElmt; i++) {
				this.arrSlideElmt[i].setStyle({
				  display: 'none'
				});
			}
			
			this.curElmtNum = slide;
			this.bulletUpdate(this.curElmtNum);
			
			this.arrSlideElmt[this.curElmtNum].setStyle({
			  display: 'block'
			});
		}
	},
	bulletUpdate: function(slide)
	{
		for (i = 1; i <= this.totalElmt; i++) {
			$(this.elmtName + 'b_' + i).innerHTML = '<img src="images/bullet_on.png" border="0" alt="" />';
		}
		
		$(this.elmtName + 'b_' + slide).innerHTML = '<img src="images/bullet_off.png" border="0" alt="" />';
	},
	update: function() {
		this.paused = 1;
		new Effect.Fade(this.arrSlideElmt[this.curElmtNum],{afterFinish:function(){
			this.checkSlide();
			this.paused = 0;
			new Effect.Appear(this.arrSlideElmt[this.curElmtNum]);
		}.bind(this)});
	},
	checkSlide: function() {
		if (this.curElmtNum == this.totalElmt) { this.curElmtNum = 1; }
		else { this.curElmtNum ++; }
		this.bulletUpdate(this.curElmtNum);	
	}
});

