if(typeof(SlideshowConnector) === 'undefined') {
	
	SlideshowImage = Class.create( {
		_src: null,
		_alt: null,
		_link: null,
		_action: null,
		_errorHandler: null,
		initialize: function(src, alt, link, action) {
			if (src!=null)
				this._src=src;
			if (alt!=null)
				this._alt=alt;
			if (link!=null)
				this._link=link;
			if (action!=null)
				this._action=action;
		},
		setSrc: function(src) {
			this._src=src;
		},
		setAlt: function(alt) {
			this._alt=alt;
		},
		setLink: function(link) {
			this._link=link;
		},
		setAction: function(action) {
			this._action=action;
		},
		setErrorHandler: function(fun) {
			this._errorHandler=fun;
		},
		getSrc: function() {
			return this._src;
		},
		getAlt: function() {
			return this._alt;
		},
		getLink: function() {
			return this._link;
		},
		getAction: function() {
			return this._action;
		},
		getNode: function() {
			var node = document.createElement("img");
			if (this._errorHandler!=null)
				node.onerror = this._errorHandler;
			node.src = this._src;
			node.alt = this._alt;
			if (this._action!=null)
				node.onclick = function() {
					this._action(this.link);
					}.bind(this);
			else
				node.onclick = function() {
					window.location.assign(this._link);
					}.bind(this);
			node.className = "brandPromotionLogoImage";
			node.style.display="none";
		//	disabled because of IE bugs, might be necessary for early detecting files not found
		//	document.getElementsByTagName("body")[0].appendChild(node);
			return node;
		}
	});
	
	SlideshowConnector = Class.create( {
		_imageList: null,
		_index: 0,
		_loadIndex: 0,
		_oneFound: false,
		_fail: false,
		_outmostContainer: null,
		_slideshow: null,
		_timeoutHandler: null,
		_finished: false,
		_timeouts: {},
		_tmtCounter: 0,
		_defaultLogosDir: null, 
		_defaultOverviewUrl:null,
		initialize: function(containerEl, imageList, outmostContainer, tmt, transition, defaultLogosDir, defaultOverviewUrl) {
			imageList.shuffle = function(){
				for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
				return this;
			};
			imageList.shuffle();
			this._defaultLogosDir=defaultLogosDir;
			this._defaultOverviewUrl=defaultOverviewUrl;
			this._imageList = imageList;
			this._outmostContainer = outmostContainer;
			this.loadNextImage();
			this._slideshow = new Slideshow(this, containerEl, tmt, transition);
			var nc = this._tmtCounter++;
			//change the first image immediately
			$$("img.brandPromotionLogoImage").each(function(img) {
				img.src = (this._defaultLogosDir!=null?this._defaultLogosDir+"/":"")+imageList[0].src;
				img.alt = imageList[0].alt;
				img.onclick = function(imageList) {
					window.location.assign(imageList[0].link!=null?imageList[0].link:(this._defaultOverviewUrl+imageList[0].brandId));
				}.bind(this,imageList)
			}.bind(this));
			this._timeouts[nc]=setTimeout(function() {this._slideshow.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), tmt);
		},
		getNextImage: function() {	
			if (this._fail) {
				return null;
			}
			var nc = this._tmtCounter++;
			this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), 2501);
			this._timeoutHandler=this._timeouts[nc];
			var startIndex = this._index;
			for (this._index=(this._index+1)%this._imageList.length; this._index!=startIndex;this._index=(this._index+1)%this._imageList.length) {
				if (this._imageList[this._index]._image!=null)
					return this._imageList[this._index];
			}
				return "wait";
		},
		getPreviousImage: function() {		
			if (this._fail)
				return null;
			var nc = this._tmtCounter++;
			this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), 2501);
			this._timeoutHandler=this._timeouts[nc];
			var startIndex = this._index;
			for (this._index=(this._index+this._imageList.length-1)%this._imageList.length; this._index!=startIndex;this._index=(this._index+this._imageList.length-1)%this._imageList.length) {
				if (this._imageList[this._index]._image!=null)
					return this._imageList[this._index];
			}
				return "wait";
		},
		loadNextImage: function() {
			if ((this._imageList[this._loadIndex%this._imageList.length]._image!=null)&&(this._imageList[this._loadIndex%this._imageList.length]._image.width!=0)) {
				this._oneFound=true;
			}
			if (this._finished==true) {
				if (this._oneFound!=true) {
					this._fail=true;
					try {
						clearTimeout(this._timeoutHandler);
					} catch (e) {
						//already clear
						} 
					this._slideshow.kill();
				}
				return;
			}
			if ((this._loadIndex==this._imageList.length-1)&&(this._imageList[this._loadIndex%this._imageList.length]._failed==true)&&(!this._oneFound)) {
				this._fail=true;
				return;
			} else
				this._fail=false;
			var allDone = true;
			for (this._loadIndex=(this._loadIndex+1)%this._imageList.length; (this._imageList[this._loadIndex]._image==null)&&(!this._imageList[this._loadIndex]._failed); this._loadIndex=(this._loadIndex+1)%this._imageList.length) {
				var data = this._imageList[this._loadIndex];
				if (data._alreadyTried)
					this._finished=true;
				if (!data._failed) {
					allDone = false;
					if ((data._image==null)&&(!data._failed)) {
						var newImage = new SlideshowImage((this._defaultLogosDir!=null?this._defaultLogosDir+"/":"")+data.src, data.alt, data.link!=null?data.link:(this._defaultOverviewUrl+data.brandId));
						newImage.setErrorHandler(this.loadNextImageAfterFailure.bind(this, data), data);
						data._alreadyTried = true;
						data._image = newImage.getNode();
						return;
					}
				}
			}
		},
		loadNextImageAfterFailure: function(image) {
			image._image=null;
			if (image._failed==true)
				return;
			image._failed=true;
			if (this._imageList.length<2) {
				this._finished=true;
				this._fail=true;
				return;
			}
			this.loadNextImage();
		},
		jumpToNextImage: function() {
			this._slideshow.loadNextImage(true);
		},
		jumpToPreviousImage: function() {
			this._slideshow.loadPreviousImage(true);
		}
	});
	
	Slideshow = Class.create( {
		_parentEl: null,
		_slideshowConnector: null,
		_lastOk: null,
		_timeoutHandler: null,
		_switchLocked: false,
		_timeouts: {},
		_tmtCounter: 0,
		_timeout: 4000,
		_transition: "none",
		initialize: function(slideshowConnector, parentEl, timeout, transition) {
			this._parentEl = parentEl;
			this._slideshowConnector = slideshowConnector;
			this._timeout=timeout;
			this._transition=transition;
		},
		kill: function() {
			clearTimeout(this._timeoutHandler);
		},
		loadNextImage: function(cutEffect) {
			if (this._switchLocked)
				return;
			clearTimeout(this._timeoutHandler);
			var imageData = this._slideshowConnector.getNextImage();
			if (imageData=="wait") {
				var nc = this._tmtCounter++;
				this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), 3000);
				this._timeoutHandler=this._timeouts[nc];
				return;
			}
			if (imageData==null) {
				this._slideshowConnector._outmostContainer.style.display="none";
			}
			if ((imageData==null)||(this._lastOk==imageData.src)) 
				return;
			else {
				if (this.loadImage(imageData, cutEffect)) {
					var nc = this._tmtCounter++;
					this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), imageData.timeout!=null?imageData.timeout:this._timeout);
					this._timeoutHandler=this._timeouts[nc];
				}
			}
		},
		loadPreviousImage: function(cutEffect) {
			if (this._switchLocked)
				return;
			clearTimeout(this._timeoutHandler);
			var imageData = this._slideshowConnector.getPreviousImage();
			if (imageData=="wait") {
				var nc = this._tmtCounter++;
				this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), 3000);
				this._timeoutHandler=this._timeouts[nc];
				return;
			}
			if (imageData==null)
				this._slideshowConnector._outmostContainer.style.display="none";
			if ((imageData==null)||(this._lastOk==imageData.src)) 
				return;
			else {
				if (this.loadImage(imageData, cutEffect)) {
					var nc = this._tmtCounter++;
					this._timeouts[nc]=setTimeout(function() {this.loadNextImage(); clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), imageData.timeout!=null?imageData.timeout:this._timeout);
					this._timeoutHandler=this._timeouts[nc];
				}
			}
		},
		loadImage: function(imageData, cutEffect) {
			if ((this._lastOk!=null)||((imageData!=null)&&(imageData._image!=null))) {
				this._slideshowConnector._outmostContainer.style.display="block";
			} else
				return false;
			this._lastOk=imageData.src;
			var fadeTime = 1;
			if ((imageData.timeout!=null?imageData.timeout:this._timeout)/2<1000)
				fadeTime = (imageData.timeout!=null?imageData.timeout:this._timeout)/2000;
			for (var i=0; i<this._parentEl.childNodes.length; i++)
				if (this._parentEl.childNodes[i].nodeName.toLowerCase()=="img") {
					if (this._parentEl.childNodes[i].style.display!="none") 
						this.fadeOut(this._parentEl.childNodes[i], fadeTime, imageData.transition!=null?imageData.transition:this._transition, cutEffect);
				}
			var newImage = imageData._image.cloneNode(true);
			newImage.onclick=imageData._image.onclick;
			if (newImage.parentNode!=null)
				newImage.parentNode.removeChild(newImage);
			this._parentEl.appendChild(newImage);
			newImage.style.position="absolute";
			newImage.style.left="0px";
			newImage.style.top="0px";
			this.fadeIn(newImage, fadeTime, imageData.transition!=null?imageData.transition:this._transition, cutEffect);
			
			return true;
		},
		fadeOut: function(element, time, transition, cutEffect) {
			var cheatTheScope = function(element,time,transition,cutEffect) {
				if (cutEffect)
					time=time/5;
				element.style.zIndex=1001;
				if (transition==null)
					transition="appear";
				var nc = this._tmtCounter++;
				this._timeouts[nc]=setTimeout(function() {this._switchLocked=false; clearTimeout(this._timeouts[nc]); this._timeouts[nc] = null;}.bind(this,nc), time*220);
				this._switchLocked=true;
				switch(transition) {
					case "appear":
						Effect.Fade(element, {duration: time});
						break;
					case "blind_down":
					case "blind_right":
					case "grow":
					case "growTopLeft":
					case "growTopRight":
					case "growBottomLeft":
					case "growBottomRight":
						element.style.zIndex=999;
						var nc2 = this._tmtCounter++;
						this._timeouts[nc2]=setTimeout(function(element,nc2) {element.style.display="none"; clearTimeout(this._timeouts[nc2]); this._timeouts[nc2] = null;}.bind(this,nc2,element), time*1000);
						break;
					case "blind_up":
						Effect.BlindUp(element, { duration: time});
						break;
					case "blind_left":
						Effect.BlindUp(element, { duration: time, scaleFromCenter:true});
						break;
					case "drop_out":
						Effect.DropOut(element, { duration: time});
						break;
					case "fold":
						Effect.Fold(element, { duration: time});
						break;
					case "none":
						element.style.display="none";
						break;
					case "puff":
						Effect.Puff(element, { duration: time});
						break;
					case "shrink":
						Effect.Shrink(element, { duration: time});
						break;
					case "shrinkTopLeft":
						Effect.Shrink(element, { duration: time, direction: "top-left"});
						break;
					case "shrinkTopRight":
						Effect.Shrink(element, { duration: time, direction: "top-right"});
						break;
					case "shrinkBottomLeft":
						Effect.Shrink(element, { duration: time, direction: "bottom-left"});
						break;
					case "shrinkBottomRight":
						Effect.Shrink(element, { duration: time, direction: "bottom-right"});
						break;
					case "squish":
						Effect.Squish(element, { duration: time});
						break;
					case "switch_off":
						Effect.SwitchOff(element, { duration: time});
						break;
				}
				var nc3 = this._tmtCounter++;
				this._timeouts[nc3]=setTimeout(function(nc3,element) {try{removeOldImage(element);} catch(e) {}; clearTimeout(this._timeouts[nc3]); this._timeouts[nc3] = null;}.bind(this,nc3,element), time*1000);
			}.bind(this);
			cheatTheScope(element,time,transition,cutEffect);
		},
		fadeIn: function(element, time, transition, cutEffect) {
			if (cutEffect)
				time=time/10;
			element.style.zIndex=1000;
			if (transition==null)
				transition="appear";
			switch(transition) {
				case "appear":
					Effect.Appear(element, {duration: time});
					break;
				case "blind_down":
					Effect.BlindDown(element, { duration: time});
					break;
				case "blind_right":
					Effect.BlindDown(element, { duration: time, scaleFromCenter : true });
					break;
				case "blind_up":
				case "blind_left":
				case "drop_out":
				case "fold":
				case "none":
				case "puff":
				case "shrink":
				case "squish":
				case "switch_off":
				case "shrinkTopLeft":
				case "shrinkTopRight":
				case "shrinkBottomLeft":
				case "shrinkBottomRight":
					element.style.zIndex=999;
					element.style.display="block";
					break;
				case "grow": 
					Effect.Grow(element, { duration: time});
					break;
				case "growTopLeft": 
					Effect.Grow(element, { duration: time, direction: "top-left"});
					break;
				case "growTopRight":
					Effect.Grow(element, { duration: time, direction: "top-right"});
					break;
				case "growBottomLeft":
					Effect.Grow(element, { duration: time, direction: "bottom-left"});
					break;
				case "growBottomRight":
					Effect.Grow(element, { duration: time, direction: "bottom-right"});
					break;
			}
				
		}
	});
	
	SlideshowApplet = {
		arrowsVisible: false,
		checkMouseoverTimeoutHandler: false,
		slideshowConnector: null,
		loadNextImage: function() {
			if (SlideshowApplet.slideshowConnector!=null)
				SlideshowApplet.slideshowConnector.jumpToNextImage();
		},
		loadPreviousImage: function() {
			if (SlideshowApplet.slideshowConnector!=null)
				SlideshowApplet.slideshowConnector.jumpToPreviousImage();	
		},
		showSideArrows: function() {
			if (SlideshowApplet.arrowsVisible)
				return;
			SlideshowApplet.arrowsVisible=true;
			$("brandPromoSlideshowLeftArrow").appear({duration: 0.3});
			$("brandPromoSlideshowRightArrow").appear({duration: 0.3});
			setTimeout(function() {
				if (SlideshowApplet.arrowsVisible)  {
					$("brandPromoSlideshowLeftArrow").style.display="block";
					$("brandPromoSlideshowRightArrow").style.display="block";
				}
			}, 300);
		},
		hideSideArrows: function() {
			if (!SlideshowApplet.arrowsVisible)
					return;
			SlideshowApplet.arrowsVisible=false;
			$("brandPromoSlideshowLeftArrow").fade({duration: 0.3});
			$("brandPromoSlideshowRightArrow").fade({duration: 0.3});
			setTimeout(function() {
				if (!SlideshowApplet.arrowsVisible)  {
					$("brandPromoSlideshowLeftArrow").style.display="none";
					$("brandPromoSlideshowRightArrow").style.display="none";
				}
			}, 300);
		},
		start: function(slideArray, defaultTimeout, defaultTransition, defaultLogosDir, defaultOverviewUrl) {
			if (defaultTimeout==null)
				defaultTimeout = 4000;
			if (defaultTransition==null)
				defaultTransition = "none";
			if (slideArray.length>0) {
				$("brandPromoContainerSidebox").style.display="block";
				var slideshowConnector = null;
				SlideshowApplet.slideshowConnector = new SlideshowConnector($("brandPromoSlideshow"), slideArray, $("brandPromoSideboxContainer"), defaultTimeout, defaultTransition, defaultLogosDir, defaultOverviewUrl);
			} else
				$('brandPromoSideboxContainer').style.display="none";
		}
	}
	
	if ($("brandPromoSideboxContainer")!=null)
		$("brandPromoSideboxContainer").onmouseout = function(e){
			if (!e) var e = window.event;
			var target = e.relatedTarget;
			if (target==null)
				target = e.toElement;
			try {
				while (target!=null) {
					if (this==target) {
						return;
					}
					target = target.parentNode;
				}
				SlideshowApplet.hideSideArrows();		
			} catch (e) {
			} 		
		}
	
	if ($("brandPromoSideboxContainer")!=null)
		$("brandPromoSideboxContainer").onmouseover = function(e){
			if (!e) var e = window.event;
			var target = e.relatedTarget;
			try {
				while (target!=null) {
					if (this==target) {
						return;
					}
					target = target.parentNode;
				}
				SlideshowApplet.showSideArrows();		
			} catch (e) {} 		
		}
	
	function removeOldImage (element) {
		try{
			element.parentNode.removeChild(element);
		} catch(e) {}
	}
}
