// 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', '0');
	$f('playerCHV').getPlugin('videoEnd').css('top', '0');					

	// 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', 'http://videos.chilevision.cl/players/flowplayer-3.1.1.swf', {
		onLoad: completePlayer,
		onFullscreen: setFullscreen,
		onFullscreenExit: setNormalscreen,
		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: '0',
				left: '0',
				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: '167',
				left: '0',
				width: 380,
				height: 253,
				border: 'none',
				display: 'none',
				borderRadius: '0',
				backgroundColor: 'transparent',
				backgroundGradient: 'none',
				opacity: 1, 
				html: '<img src="http://videos.chilevision.cl/players/videoOverV2.swf" vspace="0" hspace="0"/>'
			},
			// Pantalla fin de video
			videoEnd: { 
				url: 'flowplayer.content-3.1.0.swf', 
				top: '0',
				left: '0',
				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: {
				backgroundGradient: 'medium',
				borderRadius: '0',
				bufferGradient: 'none',
				timeBgColor: '#555555',
				buttonColor: '#000000',
				sliderColor: '#f6e193',
				timeColor: '#ff2e32',
				progressColor: '#e6000a',
				volumeSliderColor: '#000000',
				buttonOverColor: '#f0000b',
				backgroundColor: '#333333',
				tooltipColor: '#5F747C',
				durationColor: '#ffffff',
				volumeSliderGradient: 'none',
				tooltipTextColor: '#ffffff',
				progressGradient: 'medium',
				sliderGradient: 'none',
				bufferColor: '#560b0f',
				opacity: 1.0
			}
		}
	});
}