var regiotv = {
	version: '1.0.1',
	author: 'Andreas Moor <amoor@snowflake.ch>',
	plugin: 'tx_regiotv_pi1',

	adCounter: 0,
	rotateAds: true,

	initialize: function() {
		// register event handlers
		regiotv.addOnClickEvents();
		// start rotation of large advert
		window.setInterval("regiotv.rotateAdverts()",4000);
	},

	addOnClickEvents: function() {
		// click action on advert thumbnails to load main advert
		$$('img.regiotv-advert-small').each( function(e) {
			var uid = e.get('id').split('-')[1];
			e.getParent().addEvent('click',function(){ regiotv.loadMainAdvert(uid); regiotv.rotateAds = false; return false; });
		});
		// click action on statements to load statement movie
		$$('img.statementImg').each( function(e) {
			var movie = e.getParent().getParent().getLast().get('text');
			e.getParent().addEvent('click', function() { regiotv.reinstallPlayer(movie); return false; });
		});
		// click action on top left button to reload main movie
		var anchor = $('regiotv-mainmovie-link');
		if (anchor != null) {
//			var movie = anchor.get('href');
			var movie = regiotv_vars.projectMovie;
			anchor.set('href','#');
			anchor.addEvent('click', function(){ regiotv.reinstallPlayer(movie); return false; });
		}
	},

	/**
	 * (Re)load the player with a specified url as clip
	 */
	reinstallPlayer: function(movie) {
		/* map the various domain names to the different product keys */
		var keys = {
			'regiotv.ch' : '#@6202a003a41ca5476ff',
			'www.regiotv.ch' : '#@6202a003a41ca5476ff',
			'webdev.regiotv.ch' : '#@6202a003a41ca5476ff',
			'film.regiotv.ch' : '#@6202a003a41ca5476ff',
			'regio1.tv' : '#@7eb111d82af893b03a5',
			'film.regio1.tv' : '#@7eb111d82af893b03a5'
		};
		flowplayer("player","../typo3conf/ext/regiotv/pi1/res/player/flowplayer.commercial-3.1.5.swf", {
			/* use the correct key in the installation */
			key: keys[location.host] || '',

			// we need at least this Flash version
			version: [9, 115],
			contextMenu: [
				'swissweb.tv gmbh',
				'Herbergstrasse 11',
				'CH-9524 Zuzwil SG',

				'-',

				{'www.regiotv.ch' : function() {location.href = "http://www.regiotv.ch";}},
			],
			play: {
				//opacity: 0,
				//label: null,
				//replayLabel: true,

				//replayLabel: 'nochmals abspielen',

				//fadeSpeed: 500,
				//rotateSpeed: 50,
			},
			clip:movie,
			plugins: {
				controls: {
					autoHide: 'always',
					sliderColor: '#000000',
					sliderGradient: 'none',
					bufferColor: '#93929b',
					buttonOverColor: '#a6a6a6',
					volumeSliderColor: '#000000',
					backgroundGradient: 'low',
					tooltipTextColor: '#ffffff',
					bufferGradient: 'none',
					timeColor: '#9c9abc',
					durationColor: '#000000',
					volumeSliderGradient: 'none',
					tooltipColor: '#5F747C',
					timeBgColor: '#555555',
					progressGradient: 'medium',
					borderRadius: '0',
					buttonColor: '#757575',
					backgroundColor: '#000000',
					progressColor: '#cbc9e3',
					height: 24,
					opacity: 1.0,
					fullscreen:false,
					stop:true
				}
			}
		});
	},

	loadMainAdvert: function(uid) {
		var htmlRequest = new Request.HTML({
			url: JSFE['TYPO3_SITE_URL']+'index.php',
			update: 'regiotv-advert-large',
			onComplete: function() {
				// add click actions to miniButtons
				// map viewer pop-up window
				var anchor = $('regiotv-advert-small-button-map');
				if (anchor != null && anchor.getParent() != null && anchor.getParent().get('href') != null) {
					var url = anchor.getParent().get('href');
					anchor.addEvent('click', function(){ window.open(url,"Karte","width=800,height=650,left=200,top=100");return false; });
				}
				// advert movie
				var movieAnchor = $('regiotv-advert-small-button-film');
				if (movieAnchor != null && movieAnchor.getParent() != null && movieAnchor.getParent().get('href') != null) {
					var movieHref = movieAnchor.getParent().get('href');
					movieAnchor.addEvent('click', function(){ regiotv.reinstallPlayer(movieHref); return false; });
				}
			}
		}).get({
				"id": JSFE['pid'],
				"tx_regiotv_pi1[ajax]":"1",
				"tx_regiotv_pi1[get]":"advertLarge",
				"tx_regiotv_pi1[showUid]":uid
		});
	},

	preloadAds: function() {
		// the global shuffledAdverts array is generated by the plugin
		regiotv_vars.shuffledAdverts.each(function(uid){
			htmlRequest = new Request.HTML({
				url: JSFE['TYPO3_SITE_URL']+'index.php',
				onSuccess: function(result){
					var el = new Element('div');
					el.set('html',result);
				}
			}).get({
				"id": JSFE['pid'],
				"tx_regiotv_pi1[ajax]":"1",
				"tx_regiotv_pi1[get]":"advertLarge",
				"tx_regiotv_pi1[showUid]":uid
			});
		});
	},

	rotateAdverts: function() {
		if (this.rotateAds == true) {
			var uid = regiotv_vars.shuffledAdverts[regiotv.adCounter % regiotv_vars.shuffledAdverts.length];
			regiotv.loadMainAdvert(uid);
			regiotv.adCounter++;
		}
	}

};

window.addEvent('domready', function() {
    regiotv.initialize();
	// preload large ads
	regiotv.preloadAds();
});

