// Funciones y variables que deben ir siempre
// Comun
// Funcion que gestiona el fullscreen
function setFullscreen(){
	var centerX = parseInt((screen.width - 385) / 2);
	var centerY = parseInt((screen.height - 281) / 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', '77');
	$f('playerCHV').getPlugin('videoEnd').css('top', '54');					

	// Posiciono over
	$f('playerCHV').getPlugin('over').css('left', '80');
	$f('playerCHV').getPlugin('over').css('top', '256');					
}

// 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;
}				
// Funcion que genera el reproductor
// Paramentros
// Modo: Solo HD, solo SD, ambos
// videoHD: Videos HD en streaming
// appURL: URL de la aplicacion streaming
// videoSD: Video SD en pseudo streaming
function playerHD(modo, videoHD, appURL, videoSD){
	// Trabajo en funcion del modo
	switch(modo){
	case 1:
		// Solo HD
		// Cargo el player basico
		flowplayer('playerCHV', {src: 'http://videos.chilevision.cl/players/flowplayer-3.1.5.swf', cachebusting: jQuery.browser.msie, wmode: 'transparent'},
		{
			// key: '#$2c6929c7c6e35634270',
			onLoad: completePlayer,
			onFullscreen: setFullscreen,
			onFullscreenExit: setNormalscreen,
			playlist: [ 
	 
				// Video
				{
					url: videoHD,
					provider: 'streamchv',
					onStart: startVideo,
					onLastSecond: hideOver,
					onFinish: finishVideo
				}       
			], 
			// Boton play oculto
			play: { 
				opacity: 0, 
				label: null, 
				replayLabel: null
			},
			plugins: {
				// servidor rtmp
				streamchv: {
					url: 'flowplayer.rtmp-3.1.3.swf', 
					netConnectionUrl: appURL
				},
		
				// 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/videoHDClickTAG.swf" vspace="0" hspace="0"/>'
				},
				// Sobreimpresion de video
				over: {
					display: 'none',
					url: 'flowplayer.content-3.1.0.swf', 
					zIndex: 2,
					top: '256',
					left: '80',
					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: '54',
					left: '77',
					opacity: '1',
					width: '385',  
					height: '262',
					border: 'none',
					borderRadius: '0',
					backgroundColor: '#000000',
					backgroundGradient: 'none',
					display: 'none',
					html: '<img src="http://videos.chilevision.cl/players/videoEndV2.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
				}
			}
		});
		break;
	case 2:
		// Solo SD
		
		break;
	case 3:
		// HD y SD disponibles
		
		break;
	}

}