var used = [];

function find( key, stack ) { 
	for (u in stack) {
		if( stack[u] == key ) { 
			return true;
		}
	}
	return false;			
}


function Slider(host, name, timer, delay, special, sliders, available)
{
    
    this.host = host;
    this.name = name;
    this.repeat = false;    
    this.special = special;    
    this.sliders = sliders;
	this.available = available;

	this.timer = timer;
	this.delay = delay;
	    
    this.movie = document.createElement('div');
    this.movie.id = this.name;
    this.movie.style.display = 'none';
    this.movie.style.position = 'relative';
    this.movie.style.width = '166px';
    this.movie.style.height = '108px';
    this.movie.style.overflow = 'hidden';


	this.images = [];		
	this.images.length = Math.floor(this.available / this.sliders);

	for (var r=0; r < this.images.length;) {		

		var id = Math.floor(Math.random() * this.available) +1;
		
		if( ! find(id, used)) { 
			used.push( id );
			this.images[r] = 'images/banner/'+this.special+'/'+id+'.jpg';
			r++;
		}	
	}

	this.movieImagesPending = this.images.length;

	document.getElementById(host).appendChild(this.movie);
       
	for (var i=0; i<this.images.length; i++) {
		var img = document.createElement('img');
			img.id = 'img'+this.name+(i+1);
			img.style.position = 'absolute';
			var _this = this;
			img.style.border = '0px';
			this.movie.appendChild(img);

			img.onload = function() { _this.movieImagesPending--; _this.run(); }
			img.src = this.images[i];
			
	}
}

Slider.prototype.run = function()
{
	
	if (this.movieImagesPending > 0) { return; }
   
	document.getElementById(this.name).style.display = 'block';

    var animator = new Animator();
	    animator.repeat = true;
    	animator.onDone = function() { }

	//var pictureID = Math.round(Math.random() * this.images.length + 1);
	$(this.host).style.backgroundImage = "url('"+this.images[1]+"')";
		
	for (var a=0; a<this.images.length; a++) {
		animator.addBehavior(new AlphaBehavior($('img'+this.name+(a+1)), 0, 0, 0));
	}	
	
	for (var a=0; a<this.images.length; a++) {
	    animator.addBehavior(new Behavior(null, this.delay));
		animator.addBehavior(new AlphaBehavior($('img'+this.name+(a+1)), this.timer, 0, 255));
	}	
	    
    for (var a=this.images.length-1; a>=0; a--) {
	    animator.addBehavior(new Behavior(null, this.delay));
		animator.addBehavior(new AlphaBehavior($('img'+this.name+(a+1)), this.timer, 255, 0));
    }
	
	animator.run();	
}


