/* Bodaclick
// Miguel Alvarez y Kike Valdenebro - 09.10.2009
// Redbility
*/

// Libreria funciones para la dinamica de interfaz TV .  Hace uso de jquery-1.3.2.min y de flowplayer-3.1.4.min

// Inicializacion

fplayer = flowplayer ("player", {wmode:'opaque', src:'/js/flowplayer/swf/flowplayer.commercial-3.1.4.swf'}, {
	// product key 
	key: '#$755ec9cead8770d2a98',  // Licencia comercial para el dominio "www.bodaclick.com"
	// Logo
	/*logo: { 
		url: '/css/gfx/videoplayer/player-logo.png',
		bottom: 70, 
		right: 20,
		width: 115,
		height: 20,
		opacity: 1,  
		fullscreenOnly: false
	},*/
	// Boton Play
	play: { 
		url: '/css/gfx/videoplayer/btn-play-01.png',
		width: 100,
		height: 100,
		opacity: 1,
		fadeSpeed: 500
	},
	// Menu contextual
	contextMenu: [ 
		"Bodaclick  Videoplayer 1.1" 
	],
	// Configuracion del video por defecto
	clip: {
		url: '',
		baseUrl: '',
		autoPlay: false, 
		autoBuffering: true, 
		// Eventos
		onBegin: function(clip) { 
			$('#player-info').show(); 
			$('#player-related').hide(); 
		},
		onStart: function(clip) { 
			google._trackEvent("Videos", "Play", clip.url); 
			$('#player-info').show();
			$('#player-related').hide(); 
		},
		onPause: function(clip) {  
			google._trackEvent("Videos", "Pause", clip.url, parseInt(this.getTime())); 
		},  
		onResume: function(clip) { 
			$('#player-info').fadeOut(1000); 
			$('#player-related').hide(); 
		},
		onStop: function(clip) {  
			google._trackEvent("Videos", "Stop", clip.url, parseInt(this.getTime())); 
			$('#player-related').hide(); 
		},  
		onBeforeFinish: function(clip) { 
			if (this.isFullscreen()) { 
				this.toggleFullscreen();         
			}        
			$('#player-info').hide(); 
			$('#player-related').fadeIn(500); 
		},
		onFinish: function(clip) {  
			google._trackEvent("Videos", "Finish", clip.url); 
			$('#player-related').hide(); 
		}
	},
	// Configuracion de los controles
	plugins: {         
		controls: {
			licenceKey: '9fa8d52162e1744d956dd326fbdf1af7dad748b9',
			url: '/js/flowplayer/swf/controlbar.gm.bodalicious-3.1.3.1.swf',
			height: 50
		}
	}		
});


$(document).ready(function() {
									
	// Boton "Más info"
	$("a.a-player-info-abrir").click(function(e) {
		e.preventDefault();
		$('#player-info').fadeIn(500); 
	});
	$("a.a-player-info-cerrar").click(function(e) {
		e.preventDefault();
		$('#player-info').fadeOut(500); 
	});

	// Boton "HQ" (Alta calidad)
	$("a.a-player-hq").click(function(e) {
		e.preventDefault();
		if($(this).hasClass('on')) { // Ya estamos en HQ. Desactivar HQ
			$(this).removeClass('on');
			fplayer.play($('#player').attr('href'));
		} else { // Activar HQ
			$(this).addClass('on');
			fplayer.play($(this).attr('href'));
		}
	});
	
	// Boton "Insertar video"
	$("a.a-player-insertar").click(function(e) {
		e.preventDefault();
		$("#sub-player-embed").fadeIn(500);
		e.stopPropagation();
		$("#sub-player-embed").click(function(e2) {
			e2.stopPropagation();
			});
		$(document).click(function(e2) {
			e2.preventDefault();
			$("#sub-player-embed").fadeOut(500); 
			$(document).unbind('click');
		});
	});
	$("#embed-codigo").click(function(e) {
		e.preventDefault();
		$(this).select();
	});

	// Videos relacionados
	var vrNumRes = $("#player-related .listvideo").length;  // Numero de resultados en total
	var vrPosActual = 1;
	var vrAnchoMin = 112;  // Ancho de miniatura mas margen (px)
	$("a#player-related-ctrl-izq").click(function(e) {
		e.preventDefault();
		$(this).blur();
		if (vrPosActual-1 >= 1) {
			vrPosActual -= 1;
			$("#player-related-carro").animate({ 
			  left: -vrAnchoMin*(vrPosActual-1)
			}, 500 );
		}
	});
	$("a#player-related-ctrl-der").click(function(e) {
		e.preventDefault();
		$(this).blur();
		if (vrPosActual+4 <= vrNumRes) {
			vrPosActual += 1;
			$("#player-related-carro").animate({ 
			  left: -vrAnchoMin*(vrPosActual-1)
			}, 500 );
		}
	});


	// Ocultacion de elementos "hidden" (accesibilidad)
	$(".hidden").css("display", "none");
	
	// Selector mas opciones
	$(".selector a.masopciones").click(function(e) {
		e.preventDefault();
		if($(this).attr('class') == 'masopciones activo' ) {
			$(this).removeClass('activo');
			$(".selectcapa").hide();
		} else {
			$(this).addClass('activo');
			$(".selectcapa").show();
		}
	});
	
	// Submenu con capas
	$("#submenuvideoteca ul li ul").hide();
	$("#submenuvideoteca ul li").bind("mouseenter",function(){
		$(this).children("ul").show()
	}).bind("mouseleave",function(){
		$(this).children("ul").hide()
	});
	
	// Pestanas
	$(".ulpestanas li a").click(function(e) {
		e.preventDefault();
		var idpestana = $(this).attr('href');
		$('.pestanas-contenido').hide();
		$(".ulpestanas li").removeClass('activo');
		$(idpestana).show();	
		$(this).parent().addClass('activo');
	});
	
	// Efecto hover sobre miniaturas de video
	$(".listvideo").bind("mouseenter",function(){
		$(this).addClass('listvideo-hover');
	}).bind("mouseleave",function(){
		$(this).removeClass('listvideo-hover');
	});
	
	// Click sobre miniaturas de video
	$(".listvideo").click(function(e) {
		window.location = $(this).children('.texto').children('.titulo').children('a').attr('href');
	});
	
	// Resultados de busqueda

	// Inicializacion
	var rbResPorPag = 9;  // Resultados por pagina
	var rbAnchoPag = 357;  // Ancho de la pagina de resultados (px)
	var rbNumRes = $(".resultados .listvideo").length;  // Numero de resultados en total
	var rbNumPags = $(".resultados .rb-pagina").length;  // Numero de paginas en total
	var rbPagActual = 1;  // Pagina actual
	var rbControlPos = 1;  // Posicion del carro de botones-numero
	var rbAnchoControl = 19; // Ancho del boton-numero (px)
	
	$(".res-ini").text(1);
	$(".res-fin").text(rbResPorPag);
	$(".res-total").text(rbNumRes);
	$(".numeros-carro:eq(0) a:eq(0)").addClass('inactivo');
	$(".numeros-carro:eq(1) a:eq(0)").addClass('inactivo');
	$(".pag-primeros").addClass('inactivo');
	$(".pag-anteriores").addClass('inactivo');
	
	// Botones numero
	$(".numeros-carro a").click(function(e) {
		$(this).blur();
		e.preventDefault();
		rbPagActual = $(this).text();
		$(".rb-carro").animate({ 
		  left: -rbAnchoPag*(rbPagActual-1)
		}, 500 );
		$(".numeros-carro a").removeClass('inactivo');
		$(".numeros-carro:eq(0) a:eq("+Number(rbPagActual-1)+")").addClass('inactivo');
		$(".numeros-carro:eq(1) a:eq("+Number(rbPagActual-1)+")").addClass('inactivo');
		$(".res-ini").text((rbPagActual-1)*rbResPorPag+1);
		$(".res-fin").text(rbPagActual*rbResPorPag);
	});

	// Mueve el carro de botones-numero a la posicion actual rbControlPos
	function rbControlIrA() {
		$(".numeros-carro").animate({ 
		  left: -rbAnchoControl*(rbControlPos-1)
		}, 500 );
		$(".pag-primeros").removeClass('inactivo');
		$(".pag-anteriores").removeClass('inactivo');
		$(".pag-siguientes").removeClass('inactivo');
		$(".pag-ultimos").removeClass('inactivo');
		if (rbControlPos<4) {
			$(".pag-primeros").addClass('inactivo');
			$(".pag-anteriores").addClass('inactivo');
		}
		if ((rbControlPos+3)>rbNumPags) {
			$(".pag-siguientes").addClass('inactivo');
			$(".pag-ultimos").addClass('inactivo');
		}
	}

	// Boton "siguientes"
	$(".pag-siguientes").click(function(e) {
		$(this).blur();
		e.preventDefault();
		if ((rbControlPos+3) <= rbNumPags) {
			rbControlPos = rbControlPos+3;
			rbControlIrA();
		}
	});

	// Boton "anteriores"
	$(".pag-anteriores").click(function(e) {
		$(this).blur();
		e.preventDefault();
		if ((rbControlPos-3) >= 1) {
			rbControlPos = rbControlPos-3;
			rbControlIrA();
		}
	});

	// Boton "primeros"
	$(".pag-primeros").click(function(e) {
		$(this).blur();
		e.preventDefault();
		rbControlPos = 1;
		rbControlIrA();
	});

	// Boton "ultimos"
	$(".pag-ultimos").click(function(e) {
		$(this).blur();
		e.preventDefault();
		rbControlPos = rbNumPags;
		rbControlIrA();
	});
	
});


