
var displays = new Object();

function slide() {
	
	display = document.getElementById(this.displayId);  
	movie = document.getElementById(this.movieId);
 	
	if (this.curSlide > this.countSlides) 
		this.curSlide = 1; 
	if (this.curSlide < 1) 
		this.curSlide = this.countSlides; 
       
	slideNr = this.curSlide;
	movie.style.top = -((slideNr-1) * display.offsetHeight) +'px';

	if ( this.lastPressed )
		this.lastPressed.className = this.classButtonNormal;	 
	 
	var bt = this.buttons[this.curSlide-1];
	if ( bt ) {
		bt.className = this.classButtonPressed;
		this.lastPressed = bt;
   }

	if( this.isRun ) {
	 //fun = new function() { ; }
	   //this.t=setTimeout( nextSlide(this.displayId), this.delay );
   	 //this.t=setTimeout(function(thisObj) {thisObj.next()},this.delay , this);
	var self = this;
	this.t = setTimeout(function() {self.next()}, this.delay );

   }	 

}

function joinMovie () {
	
	var movie = document.getElementById( this.movieId );
	//var slides = movie.getElementsByClassName( this.slideClass );
	var slides = getElementsByClassNameIE(movie, 'div', this.slideClass );
	//var slides = getElementsByClassNameIE ( movie, 'div', this.slideClass ); 
	this.countSlides = slides.length;
	//movie.appendChild( slides[0].cloneNode(true) this.slideClass);
}

function registerButtons () {

	var items = registerButtons.arguments.length;
   
	for (i = 0; i < items; i++)
	{
		this.buttons[i] = document.getElementById( registerButtons.arguments[i] );
      
	}		
}

function registerButtonsAuto ( count ) {
	   
	if (!count)
		count = this.countSlides;
	
	for (var i = 1; i <= count; i++)
		this.buttons[i-1] = document.getElementById( this.displayId + i );
			
}

function setButtonClass ( normal, pressed ) {
	
	this.classButtonNormal = normal;
	this.classButtonPressed = pressed;	
}

function next() {

	clearTimeout ( this.t );
	this.curSlide++;
	this.slide();
	
}

function prev() {

	clearTimeout ( this.t );
	this.curSlide--;
	this.slide();
	
}

function play() {

	this.isRun = true;
	clearTimeout ( this.t );	
	this.slide();
}

function stop() {

	this.isRun = false;
	clearTimeout ( this.t );
}


function Slider (displayId, movieId, slideClass, delay) {
	
	this.displayId = displayId;
	this.movieId = movieId;
	this.slideClass = slideClass;
	this.delay = delay*1000;
	this.curSlide = 1;
	this.buttons = Array();
	this.classButtonNormal = null;
	this.classButtonPressed = null;
	this.lastName = null;
	this.isRun = false;

   this.slide = slide;
   this.next = next;
   this.prev = prev;
   this.play = play;
   this.stop = stop;
   this.joinMovie = joinMovie;	
   this.registerButtons = registerButtons;
   this.registerButtonsAuto = registerButtonsAuto; 
   this.setButtonClass = setButtonClass;
   
   this.joinMovie();
   
	//this.t = setTimeout(function(thisObj) {thisObj.next()}, this.delay*1000, this);
  
}


function initSlider ( displayId, movieId, slideClass, delay ){
	
		displays[displayId] = new Slider( displayId, movieId, slideClass, delay );
		return displays[displayId];
}

function showSlide ( displayId, slideNr) {
		
		if (displays[displayId]) {
		
			clearTimeout ( displays[displayId].t );
			displays[displayId].curSlide = slideNr;
			displays[displayId].slide();
		}		
}

function nextSlide ( displayId ) {
		
		if (displays[displayId] ) 
			displays[displayId].next();
}

function prevSlide ( displayId ) {
		
		if (displays[displayId] ) 
			displays[displayId].prev();				
}

function playSlide ( displayId ) {
		
		if (displays[displayId] ) 
			displays[displayId].play();
}

function stopSlide ( displayId ) {
		
		if (displays[displayId] ) 
			displays[displayId].stop();
}

function setClassFor( id, className ){
    ob = document.getElementById(id);
    ob.className = className; 
}

function getElementsByClassNameIE(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}
