HeaderSlideshow = function(id,data,image_folder)
{
	that = this;

	this.id = id;
	this.data = data;
	this.image_folder = image_folder;

	this.div_slideshow = null;
	this.ul_buttons = null;
	this.div_image = null;

	this.timeout = 8000;
	this.timer_active = true;

	this.current_image = null;

	this.init = function()
	{
		if (this.data.length <=0) return;

		this.current_image = parseInt(Math.random()*this.data.length);

		this.div_slideshow = $("#"+this.id)
			.addClass('header_slideshow')
			.width(this.data[0].dimensions[0])
			.height(this.data[0].dimensions[1])
			.click(that.image_click)
		;

		this.div_image = $("<div>")
			.addClass("image")
			.appendTo(this.div_slideshow)
		;

		this.ul_buttons = $("<ul>")
			.addClass('buttons')
			.appendTo(this.div_slideshow)
		;
		$.each(this.data,function(index,image) {
			$("<li>").text(index+1).appendTo(that.ul_buttons).click({image:index},that.button_click);
			that.data[index].img = $("<img>").attr('src',that.image_folder+image.image);
		});

		this.set_image(this.current_image);
		window.setTimeout(this.next_image,this.timeout);
	}

	this.button_click = function(e)
	{
		e.stopPropagation();
		that.timer_active = false;
		that.set_image(e.data.image);
	}

	this.image_click = function(e)
	{
		link = that.data[that.current_image].link;
		if (link.length <= 0) return;

		if (link.indexOf("http://") == -1)
		{
			location.href = "/"+link;
		}
		else
		{
			window.open(link);
		}
	}

	this.next_image = function()
	{
		if (that.timer_active)
		{
			that.current_image++;
			if (that.current_image+1 > that.data.length) that.current_image = 0;
			that.set_image(that.current_image);
			window.setTimeout(that.next_image,that.timeout);
		}
	}

	this.set_image = function(index)
	{
		this.current_image = index;
		this.ul_buttons.children("LI").removeClass("current");
		$(this.ul_buttons.children("LI")[index]).addClass("current");
		this.div_image.css('width','100%');
		this.div_image.css('height','100%');
		this.div_image.fadeOut('slow',function() {
			that.div_image.css('background-image','URL("'+that.image_folder+that.data[index].image+'")').fadeIn('slow');
		});
		/*$("#slideBG").fadeOut('slow',function() {
			$(this).remove();
		});*/
		$('<div>').css('display','none').css('background-image','URL("'+that.image_folder+that.data[index].bg_image+'")').attr('id','slideBG2').appendTo($('#slideBGWrapper')).fadeIn('slow',function(){
			$('#slideBG').remove();
			$('#slideBG2').attr('id','slideBG');
		});
		
		
		
		//this.div_slideshow.attr('title','Link zu: '+this.data[index].link);

		this.div_slideshow.removeClass("linked");
		if (this.data[index].link.length > 0)
		{
			this.div_slideshow.addClass("linked");
		}

	}
}

