// Funciones y variables que deben ir siempre
// Comun
// Funcion que gestiona el fullscreen
function setFullscreen(){
	var centerX = parseInt((screen.width - 381) / 2);
	var centerY = parseInt((screen.height - 278) / 2);
	var bottomY = parseInt(screen.height - 111);

	// Posiciono videoEnd
	$f('playerCHV').getPlugin('videoEnd').css('left', centerX);
	$f('playerCHV').getPlugin('videoEnd').css('top', centerY);			

	// Posiciono over
	$f('playerCHV').getPlugin('over').css('left', centerX);
	$f('playerCHV').getPlugin('over').css('top', bottomY);					
}
// Funcion que gestiona va vuelta del fullscreen
function setNormalscreen(){

	// Posiciono videoEnd
	$f('playerCHV').getPlugin('videoEnd').css('left', '60');
	$f('playerCHV').getPlugin('videoEnd').css('top', '169');					

	// Posiciono over
	$f('playerCHV').getPlugin('over').css('left', '0');
	$f('playerCHV').getPlugin('over').css('top', '167');					
}

// PreRoll
// Variable que indica cual es el indice del playlist donde se eucnetra el video principal
var iVideo = 0;
// Funcion que se ejecuta al terminar el spot
function finishSpot(){
	$f('playerCHV').getPlugin('clickTAG').hide();
}
// Funcion que se ejecuta cuando elusuario decide saltar el spot
function skipSpot(){
	$f('playerCHV').getPlugin('clickTAG').hide();
	$f('playerCHV').play(iVideo);
}
// Funcion que se ejecuta al terminar el video principal
function finishVideo(){
	$f('playerCHV').getPlugin('videoEnd').show();
	// $f('playerCHV').getPlugin('controls').hide();
}
// Funcion que se ejecuta al hacer clic sobre el spot
function clickTAG(){
	window.open(GAM_url);
}
// Funcion que se ejecuta al hacer clic en el boton Play Again
function playAgain(){
	// $f('playerCHV').getPlugin('controls').show();
	$f('playerCHV').play(iVideo);
	$f('playerCHV').getPlugin('videoEnd').hide();
}

// Sobreimpresion
// Funcion que se ejecuta al hacer clic sobre la sobreimpresion
function contentClickTAG(){
	window.open(GAM_content_url);
}
// Funcion que gestiona el inicio del video principal
function startVideo(){
	// Habilito boton fullscreen
	// $f('playerCHV').getControls().enable({fullscreen:true});
	// Oculto pantalla de fin de video
	if($f('playerCHV').getPlugin('videoEnd').display == 'block'){
		$f('playerCHV').getPlugin('videoEnd').hide();
	}
	// Muestro sobreimpresion, si esta activa
	if (typeof GAM_over != 'undefined'){
		if (GAM_over){
			$f('playerCHV').getPlugin('over').show();


		}
	}
}
// Funcion que oculta la sobreimpresion
function hideOver(){
	if (typeof GAM_over != 'undefined'){
		if (GAM_over){
			$f('playerCHV').getPlugin('over').hide();

			// Evito que se vuelva a mostrar
			GAM_over = false;
		}
	}
}

// Funcion que completa de cargar el player
function completePlayer(){
	// Control de piezas programadas
	// PreRoll
	if (typeof GAM_preroll != 'undefined'){
		if (GAM_preroll){
			// Sumo el spot al playlist
			$f('playerCHV').addClip({url: GAM_video}, 0);
			$f("playerCHV").getClip(0).onFinish(function() { 
					finishSpot(); 
			});

			// Modificamos iVideo
			iVideo = 1;

			// Deshabilito el boton fullscreen
			// $f('playerCHV').getControls().enable({fullscreen:false});

			// Muestro el plugin clickTAG
			$f('playerCHV').getPlugin('clickTAG').show();

			// Reproduzco el spot
			$f('playerCHV').play(0);
		}
	}

	// Sobreimpresion
	if (typeof GAM_over != 'undefined'){
		if (GAM_over){
			// Algo que setear?

		}
	}
	return true;
}				
function playerCHV(videoURL){
	// Cargo el player basico
	flowplayer('playerCHV', { src: 'http://videos.chilevision.cl/players/flowplayer.commercial-3.1.2.swf', wmode: 'transparent'}, {
		key: '#$2c6929c7c6e35634270',
		onLoad: completePlayer,
		onFullscreen: setFullscreen,
		onFullscreenExit: setNormalscreen,
		canvas: {
			backgroundImage: 'url(http://estaticos.chilevision.cl/home/templates/yingo_200908/swf/canvasYingo.swf)',
			backgroundColor: 'transparent',
			backgroundGradient: 'none',
			linkUrl: 'http://www.zimaelectronics.com/',
			linkWindow: '_blank'
		},
		// screen positioning inside "Hello world" screen. 
		screen: {         
			width: '381',
			height: '278',
			top: 169,
			left: 60 
		},
		playlist: [ 
	 
			// Video
			{
				url:videoURL,
				onStart: startVideo,
				onLastSecond: hideOver,
				onFinish: finishVideo
			}       
		], 
		// Boton play oculto
		play: { 
			opacity: 0, 
			label: null, 
			replayLabel: null
		},
		plugins: {
			// video ClickTAG
			clickTAG: { 
				display: 'none',
				url: 'flowplayer.content-3.1.0.swf', 
				zIndex: 1,
				top: '169',
				left: '60',
				width: '100%',
				height: '100%',
				border: 'none',
				borderRadius: '0',
				backgroundColor: 'transparent',
				backgroundGradient: 'none',
				html: '<img src="http://videos.chilevision.cl/players/videoClickTAG.swf" vspace="0" hspace="0"/>'
			},
			// Sobreimpresion de video
			over: {
				display: 'none',
				url: 'flowplayer.content-3.1.0.swf', 
				zIndex: 2,
				top: '360',
				left: '60',
				width: 380,
				height: 253,
				border: 'none',
				display: 'none',
				borderRadius: '0',
				backgroundColor: 'transparent',
				backgroundGradient: 'none',
				html: '<img src="http://videos.chilevision.cl/players/videoOver.swf" vspace="0" hspace="0"/>'
			},
			// Pantalla fin de video
			videoEnd: { 
				url: 'flowplayer.content-3.1.0.swf', 
				top: '169',
				left: '60',
				opacity: '1',
				width: '380',  
				height: '262',
				border: 'none',
				borderRadius: '0',
				backgroundColor: '#000000',
				backgroundGradient: 'none',
				display: 'none',
				html: '<img src="http://videos.chilevision.cl/players/videoEnd.swf" vspace="0" hspace="0"/>'
			},
			// Controles
			controls: null
		}
	});
}
