$(document).ready(function(){
	branding.init();
});

branding.init = function(){

	this.verweildauer = 7000;
	this.dauer_uebergang = 1000;
	
	if (navigator.appVersion.indexOf("MSIE") != -1){
		var version = navigator.appVersion.match(/MSIE (\d)\.\d/);
		if (version[1] == 6){
			this.dauer_uebergang = 0;
		}
	}

	this.laden = function(daten){
	
		this.bilder = new Array();
		this.urls = daten.split(",");
		
		this.urls = this.mischen(this.urls, $("#branding").css("background-image"));
		
		if (this.urls.length != 0){
			this.ladeBild();
		}
		
	}


	this.mischen = function(elemente, erstes){
			
		var gemischt = new Array();

		if (elemente.length != 0){
					
			while (elemente.length != 0){
				var zufall = Math.floor(Math.random() * elemente.length);
				if (erstes.indexOf(elemente[zufall]) == -1){
					gemischt.push(elemente.splice(zufall, 1));
				} else {
					gemischt.unshift(elemente.splice(zufall, 1));
				}
			}
					
		}
		
		return gemischt;

	}
	
	
	this.ladeBild = function(){
	
		var id = this.bilder.length;
				
		if (id != 0){		
			this.bilder[id - 1].geladen = true;
		}
		
		if (id == 1){
			window.clearTimeout(this.timer);
			
	 		this.timer = window.setTimeout("branding.zeigen()", this.verweildauer);
		}
		
		if (id < this.urls.length){
			this.bilder[id] = new this.makeBild(id);
			this.bilder[id].bild.src = this.urls[id];
		}
		
	}
	
	this.makeBild = function(id){
	
		this.bild = new Image();
		this.geladen = false;

		this.bild.onload = function(){
			branding.ladeBild();
		};
				
	}
		
	this.zeigen = function(){
	
		window.clearTimeout(this.timer);
		
		if (this.aktuelles_bild == null){
			this.aktuelles_bild = 0;
		}
		
		this.aktuelles_bild++;
		
		if (this.aktuelles_bild == this.urls.length){
			this.aktuelles_bild = 0;
		}
		
		if (this.bilder[this.aktuelles_bild].geladen){
			
			if (this.container[1].css("background-image") != "none"){
				this.container[0].css("background-image", this.container[1].css("background-image"));
			}
			
			this.container[1].hide().css("background-image", "url(" + this.bilder[this.aktuelles_bild].bild.src + ")");
			
			this.container[1].fadeIn(this.dauer_uebergang, function(){
		 		branding.timer = window.setTimeout("branding.zeigen()", branding.verweildauer);
			});
			
		} else {
		
			this.timer = window.setTimeout("branding.zeigen()", 100);
			
		}
	}
		
	this.einblenden = function(dauer){
	
		$("#branding .motiv").fadeIn(dauer, function(){
	 		branding.timer = window.setTimeout("branding.zeigen()", branding.verweildauer);
		});
		
	}
		
	this.container = new Array();
	this.container[0] = $('<div class="motiv"><div class="motiv"></div></div>').prependTo("#branding");
	this.container[1] = $('.motiv', this.container[0]);
	
	/* Nur starten, wenn das Motiv nicht ausgeblendet ist (print.css) */
	
	if (this.container[0].is(":visible")){
	
		$.post(this.url, function(daten){
			branding.laden(daten);
		});
	
	}

}
