var vfImageSliderArr = Class.create();
vfImageSliderArr.prototype = {
	initialize: function(){
		this.all_slider = [];
	}
}
vf_isa = new vfImageSliderArr();

var vfImageSlider = Class.create();
vfImageSlider.prototype = {

	initialize: function(slider_width, width, height, href_base_url, img_base_url) {

		this.name				= this.createname();
		//tst = typeof width;
		//alert(tst);
		this.slider_width		= (slider_width!=undefined)?slider_width:360;
		this.width       		= (width!=undefined)?width:80;
		this.img_width       	= this.width;
		this.width				= this.width + (2 + 3 + 3 + 2);
		this.height				= (height != undefined)?height:101; 
		this.img_height			= this.height;
		this.height				= this.height + (2 + 3 + 3 + 2);
		this.slider_items 		= [];
		this.rotation_effect	= null;
		this.href_base_url		= (href_base_url != undefined)?href_base_url : "";
		this.img_base_url		= (img_base_url != undefined)?img_base_url : "";
		this.slider_elems		= this.name +'_slider_elems';
		this.slider_left		= this.name +'_slider_left';
		this.slider_right 		= this.name +'_slider_right';
		this.elems				= $(this.vf_slider_elems);
		this.current_index     	= 0;
		this.slider_stop		= false;
		this.timeout			= null;

		var slider_wrapper 	= "";
		slider_wrapper 		+= '<div id="' + this.name + '">';
		slider_wrapper 		+= '<a href="javascript:void(0);" id="'+ this.slider_left +'" class="vf_slider_left"></a>';
		slider_wrapper 		+= '<div class="vf_slider_rotation" style="width:'+this.slider_width+'px;">';
		slider_wrapper 		+= '<div id="' + this.slider_elems +'" class="vf_slider_elems">';
		slider_wrapper 		+= '<div style="padding: 20px;clear:both"><img src="images/loader.gif" /> &nbsp;&nbsp; Bilder werden geladen</div></div>';
		slider_wrapper 		+= '</div>';
		slider_wrapper 		+= '<a href="javascript:void(0);" id="'+ this.slider_right +'" class="vf_slider_right"></a>';
		slider_wrapper 		+= '</div>';
		
		
		vf_isa.all_slider[this.name]= this;
		document.write( slider_wrapper );
	},

	addItem: function(img, caption, url, width, height) {
			 h = (height != null)?height:this.img_height;
			 w = (width != null)?width:this.img_width;
			 this.slider_items.push(new SliderImageItem(this.img_base_url+img, caption, this.href_base_url+url, w, h) );
	},

	start: function(rotation_interval){
		var se = $(this.slider_elems);
		this.rotation_interval = (rotation_interval == undefined)?3000:rotation_interval*1000;
		factor = Math.ceil($(this.name).getDimensions().width/this.calcWidth())+2
		slider_content = "";  
		for(var j=0; j < factor; j++){
			for(var i=0; i < this.slider_items.length; i++){ slider_content += this.slider_items[i].source; }
		}
		se.innerHTML = "";
		se.current_index = 0;
		se.current_index = this.slider_items.length;
		se.setStyle({ 'width': this.calcWidth()*(factor) + "px" });
		se.setStyle({ 'left': ((this.calcWidth()+this.width) * -1 )+ "px" });
		new Insertion.Bottom($(this.slider_elems), slider_content);
		var tmp = this;	
		Event.observe(this.slider_left, 'click', function () {
			if(tmp.rotation_effect != undefined)tmp.rotation_effect.cancel();	
			tmp.rotate_back();
			tmp.slider_stop = true;	
			window.clearTimeout(tmp.timeout);	
		});
	
		Event.observe(this.slider_right,'click', function () {
			if(tmp.rotation_effect != undefined)tmp.rotation_effect.cancel();	
			tmp.rotate_forward();
			if(tmp.slider_stop == false) tmp.current_index--;
			tmp.slider_stop = true;	
			window.clearTimeout(tmp.timeout);	
			});
		this.timeout = setTimeout("vf_isa.all_slider['"+this.name+"'].rotate_back()", this.rotation_interval)
	},

	calcWidth: function(){
	   slider_items_width = 0;
	   for(var i=0; i < this.slider_items.length; i++){ 
			slider_items_width += this.slider_items[i].width + 10;
	   }
	   return slider_items_width;
	},			   

	rotate: function(){
		se = $(this.slider_elems);
		newpos = (this.width * this.current_index)*-1; 
		this.rotation_effect = new Effect.Move(se, { x: newpos , y: 0,
					mode: "absolute", duration: 0.9,
					transition: Effect.Transitions.sinoidal});
	},

	rotate_back: function(){
		this.current_index--;
		if(this.current_index <= 0){
			$(this.slider_elems).setStyle({ 'left': (this.calcWidth()+this.width) * -1 + "px" });
			this.current_index = this.slider_items.length;
		}
		this.rotate();
		if(this.slider_stop == false){
			this.timeout = setTimeout("vf_isa.all_slider['"+this.name+"'].rotate_back()", 3000)
		}		
	},			

	rotate_forward: function(){
		this.current_index++;
		if(this.current_index > this.slider_items.length){
			this.current_index = 1;
			$(this.slider_elems).setStyle({ 'left': "0px" });
		}
		this.rotate();

	},

	createname: function(){
		var str="a b c d e f g h i j k l m n o p q r s t u v w x y z".split(' ');
		var randname = "";
		for(var i=0; i<6; i++){
			randname += str[Math.floor(str.length*Math.random())]
		}
		if($(randname)==null){
			return randname;
		}else{
			this.createname()
		}
	},
	
	getObject: function(){
		return this;
	}				   
}

var SliderImageItem = Class.create();
SliderImageItem.prototype = {
	initialize: function(src, caption, url, width, height) {
					this.src = src;
					img = new Image();
					this.img = img;
					this.caption = caption;
					this.url = url;
					this.width = width;
					this.height = height;
					this.source =  '<div class="vf_slider_elem">';
					this.source += '<a href="' + this.url + '" ';
					this.source += 'onmouseover="this.childNodes[0].style.borderColor = \'#6188CD\';" ';
					this.source += 'onmouseout ="this.childNodes[0].style.borderColor = \'#FFFFFF\';"';
					this.source += '>';
					this.source += '<img src="' + this.src + '" width="'+this.width+'" height="'+this.height+'" /><br />';
					this.source += '<strong>' + this.caption + '</strong></a></div>';
	}
}
