function flashIsSupported(minimumVersion) {
	if(typeof flashIsSupported._supported != "undefined")
		return flashIsSupported._supported;

	var flashSupported = false;
	minimumVersion = minimumVersion || 8;
	var plugin = (navigator.mimeTypes
		&& navigator.mimeTypes["application/x-shockwave-flash"]
		&& navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)

	if(plugin) {
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
		var installedVersion = 0;
		for(var i = 0; i < words.length; i++) {
			if(isNaN(parseInt(words[i])))
				continue;
			var installedVersion = parseInt(words[i]);
		}
		flashSupported = (installedVersion >= minimumVersion);
	} else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE") != -1 && navigator.appVersion.indexOf("Win") != -1) {
		try {
			flashSupported = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + minimumVersion) != null;
		} catch(e) {}
	}
	
	flashIsSupported._supported = flashSupported;
	return flashSupported;
}

function createFlashHTML(path) {
	var flashhtml = "<object codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' type='application/x-shockwave-flash'>";
	flashhtml += "<param name='movie' value='" + path + "'>";
	flashhtml += "<embed src='" + path + "' pluginspage=\"http://www.macromedia.com/go/getflashplayer\"><" + "/embed>";
	flashhtml += "<" + "/object>";
	return flashhtml;
}


BannerWidget = Class({

	INTERVAL: 6000, // milliseconds
	FADE_DURATION: 500, // milliseconds

	initialize: function(banners) {
		this.banners = banners;
		this.currentIndex = 0;
		this.preload();
		this.setBanner(0, true);
		this.stopped = false;
		
		if (this.banners) {
			if (this.banners.length > 1) {
				this.startTimer();
			}
		}
	},
	
	preload: function() {
		var bannerText = $('banner_text');

		if (this.banners) {
			for (var i = 0; i < this.banners.length; i++) {
				var banner = this.banners[i];
	
				var banner_div = new Element('div', {'class': 'banner', 'id': 'banner_text_'+i});
				banner_div.style.display = 'none';
				banner_div.innerHTML = banner.content || "";
				banner_div.setStyle('height', 239);
	
				if(banner.background_image_url) {
					// Force preloading of the image
					new Image().src = banner.background_image_url;
					banner_div.style.backgroundImage = 'url("' + banner.background_image_url + '")';
				}
	
				if(banner.flash_url && flashIsSupported()) {
					var flash_div = new Element('div', {'class': 'flash'});
					flash_div.innerHTML = createFlashHTML(banner.flash_url);
					banner_div.appendChild(flash_div);
				}
	
				if(this.banners.length > 1)
					banner_div.appendChild(this.generateProgressBar(i));
	
				bannerText.insertBefore(banner_div, bannerText.firstChild)
			}
		}
	},
	
	setBanner: function(index, instant) {
		if (index >= this.banners.length) return;
		var duration = instant ? 0 : this.FADE_DURATION;
		if(this.banners[index].flash_url && flashIsSupported()) {
			var height = 370;
		} else {
			var height = 279;
		}
		$('banner_text').setStyle('height', height);
		$('banner_text').effect('opacity', {
			duration: duration,
			onComplete: function(index, duration) {
				if (index.length === 2) {
					// Chrome returns a two element array for index, we need the first element.
					index = index[0];
				}
				$('banner_text_' + this.currentIndex).style.display = 'none';
				$('banner_text_' + index).style.display = 'block';
				$('banner_text').effect('opacity', {duration: duration}).start(0, 1);
				this.currentIndex = index;
			}.bind(this, [index, duration])
		}).start(1, 0);
	},
	
	nextBanner: function() {
		var nextIndex = (this.currentIndex + 1) % this.banners.length;
		this.setBanner(nextIndex);
		this.timer = setTimeout(this.nextBanner.bind(this), this.INTERVAL);
	},
	
	startTimer: function() {
		this.stopped = false;
		this.timer = setTimeout(this.nextBanner.bind(this), this.INTERVAL);
	},
	
	stopTimer: function() {
		this.stopped = true;
		clearTimeout(this.timer);
	},
	
	generateProgressBar: function(currentIndex) {
		var span = new Element('span', {'styles': {
			'bottom': 40,
			'font-size': '150%',
			'position': 'absolute'
		}});
		for (var i = 0; i < this.banners.length; i++) {
			var bullet = new Element('span', {
				'styles': {'cursor': 'pointer'},
				'title': this.banners[i].name
				});
			bullet.setText(i == currentIndex ? '\u25CF' : '\u25CB');
			bullet.addEvent('click', function(index) {
				this.setBanner(index, true);
				this.stopTimer();
				this.startTimer();
			}.bind(this, i));
			if (i > 0) span.appendText('-');
			span.appendChild(bullet);
		}
		var paused = new Element('a', {
		'href': '#',
			'class': 'banner_pause',
			'styles': {
				'background': 'none',
				'margin-left': 20,
				'visibility': 'visible',
				'font-size': '50%',
				'position': 'relative',
				'top': 8
			}
		});
		var img = new Element('img');
		if(this.stopped){
			img.src = "/media/img/play.png";
			img.alt = "Play";
		}else{ 
			img.src = "/media/img/paused.png";
			img.alt = "Pause";
		}
		
		paused.adopt(img);
		paused.addEvent('click', function(event, img){
			if(this.stopped){
				this.startTimer();
				img.src="/media/img/paused_hover.png" 
				img.alt="Pause";
			}else{
				this.stopTimer();
				img.src="/media/img/play_hover.png" 
				img.alt="Play";
			}
			event.stop();
		}.bindWithEvent(this, img));
		
		paused.addEvent('mouseover', function(paused){
			if(this.stopped){
				img.src="/media/img/play_hover.png" 
				img.alt="Play";
			}else{
				img.src="/media/img/paused_hover.png" 
				img.alt="Pause";
			}
		}.bind(this, img));
		paused.addEvent('mouseout', function(paused){
			if(this.stopped){
				img.src="/media/img/play.png" 
				img.alt="Play";
			}else{
				img.src="/media/img/paused.png" 
				img.alt="Pause";
			}
		}.bind(this, img));
		span.appendChild(paused);
		span.addEvent('mouseenter', function(event, span) {
			span.setStyle('color', '#000');
		}.bindWithEvent(this, span));
		span.addEvent('mouseleave', function(event, span) {
			span.setStyle('color', '');
		}.bindWithEvent(this, span));
		return span;
	}
	
});

