var i;
var actividad = false;
var seccionActive = false;
var html5Video = false;

function isVideoCompatible(){
	var v = document.createElement("video"); 	
	if( !v.play ) { 
			html5Video = false;
	}else{
			html5Video = true;
		}
}


//cufon
Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});

$(document).ready(function(){
		
		
	isVideoCompatible();
	
	runActividad();
	
	// navegacion -- carga html						   
   $("nav a").each(function(e){
      var href = $(this).attr("href");
      $(this).click(function(){ 
		clearInterval(i);							 
		// si esta cargado fuera
		if($(this).parent().attr("class")=="sel")return false;
		//color elemento seleccionado
		aa= $(this).parent().parent().find("li").each(function(){
			$(this).removeClass();
		});
		
		$(this).parent().addClass("sel");		
		//desactiva animacion home
		seccionActive=true;
		
//		$("#menu-home").fadeOut("slow");
		$("#optionSel1").removeClass("resaltado");
		Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});
		
		//configura contenedor segun seccion
		sClass= "section"+e;
		$("#show").hide().removeClass().addClass(sClass);
		$("#show").load(href,function(){showSection(e);});	
		Cufon.replace('#nav-replace', {hover: true, hoverables: {li: true, a:true}});
		return false;
      });
	  
 
	  $(this).hover(				
		function(){
			if(!seccionActive){

				txtHref= $(this).attr("href");								
				if(txtHref=="en/what.html" || txtHref=="es/what.html"){
					actividad = true;
					clearInterval(i);				
					//$("#menu-home").stop(true,true).fadeIn("slow");
					$("#menu-home").stop(true,true);
					//$("#optionSel1").addClass("resaltado");
					//Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});
				}else{

					$("#optionSel1").removeClass("resaltado");
					Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});
					}
			}	
		},			
		function(){
			if(!seccionActive){	
			txtHref= $(this).attr("href");			
			if(txtHref=="en/what.html" || txtHref=="es/what.html"){
				
				//$("#menu-home").stop(true,true).fadeOut("slow",function(){runActividad();actividad=false;});
				$("#menu-home").stop(true,true)
				$("#optionSel1").removeClass("resaltado");
				Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});
				runActividad();actividad=false;
			}else{
				//activar de nuevo control
				runActividad();
				}
		}
			
		}	
					
		);
	  
	  
   });

});




function runActividad(){
	if(!seccionActive){
	clearInterval(i);
	i =  setInterval("checkActividad()",4000);
	}
}


function checkActividad(){
	if(!actividad){
		//$("#menu-home").stop(true, true).fadeIn("slow", function(){actividad=true;});
		$("#menu-home").stop(true, true)
		$("#optionSel1").addClass("resaltado");
		Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});		
		actividad=true;
		clearInterval(i);
	}else{
		
		//$("#menu-home").stop(true, true).fadeOut("slow", function(){actividad=false;});
		$("#menu-home").stop(true, true)
		$("#optionSel1").removeClass("resaltado");
		Cufon.replace('#nav-replace, footer, #menu-home', {hover: true, hoverables: {li: true, a:true}});		
		actividad=false;
	}
}


//animacion despliegue tras carga
function showSection(e){	

	$(".reel-gallery ul").removeClass("disabled");
	
	//cufon sobre html cargado
	Cufon.replace("section h2");
	//anima capa
	$("#show").stop(true,true).css("display","none").animate({"height": "toggle", "opacity": "toggle"}, "slow");		 	
	//si estoy en videos lanza galeria
	if(e==4){
		launchGallery();
		}

}



//carrusel videos
function launchGallery(){
	//factiva galeria
	$(function() {
		$(".reel-gallery").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev"
		});
	});
	//funcionalidad botones galeria
	$(".reel-gallery li a").each(function(e){
		$(this).click(function(){
			setVideoInfo($(this).attr("data-video"),$(this).attr("data-url-vimeo"));
			return false;
		})			  
	})
	
	//mostrar reel
	$("#play-reel").click(function(){
		$("#video-player").css("display","none");
		$("#controls").css("display","none");				
		$("#video-player .txt-info-video").empty();
		$("#video-player #player").empty();			
		
		$(".reel-gallery").css("display","none");	
		$("#wrap-reel-player").css("display","block");
		loadReel($(this).attr("data-url-ogg"),$(this).attr("data-url-mp4"));
	})

	// cerrar reel
	$("#close-reel").click(function(){
		$(".reel-gallery ul").removeClass("disabled");									
		$("#wrap-reel-player").css("display","none");									
		$("#video-player").css("display","block");
		$("#controls").css("display","block");		
		$(".reel-gallery").css("display","block");	
	});	
}

function setVideoInfo(selector,urlVimeo){

	$(".reel-gallery ul").addClass("disabled");
	
	$("#video-player").css("display","block");	
	
	//muestra info del video

	loadVideo(urlVimeo);	
	var divData = $(selector).clone();
	$("#info-video .txt-info-video").css("display","none");
	$("#info-video .txt-info-video").empty().append(divData)
	$("#info-video .txt-info-video").fadeIn("slow");	
	return false;	
}


function loadVideo(urlVimeo){


	sVideo = '<object width="455" height="256">';
	sVideo += '<param name="allowfullscreen" value="true" />';
	sVideo += '<param name="allowscriptaccess" value="always" />';
	sVideo += '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+urlVimeo+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1" />';
	sVideo += '<embed src="http://vimeo.com/moogaloop.swf?clip_id='+urlVimeo+'&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="455" height="256">';
	sVideo += '</embed>';
	sVideo += '</object>';

	$("#player").html(sVideo);
	
	/*
	if(html5Video){
		var sVideo = '';
//        sVideo += '<video id="demo-video"  autobuffer controls width="450" height="225">';
        sVideo += '<video id="demo-video"  autobuffer controls width="455" height="256">';
        sVideo += '<source src="'+urlMP4+'" type="video/mp4" />';
        sVideo += '<source src="'+urlOGG+'" type="video/ogg" />';
        sVideo += '</video>';		
		$("#player").html(sVideo);		
		}else{
			swfobject.embedSWF("swf/flowplayer-3.2.0.swf", "player", "455", "256", "9.0.0", null, {  
				config: "{'clip': '"+urlMP4+"', 'autoPlay': 'false', 'autoBuffering': 'true'}"
				} 
			);

			}	
	*/
}

function loadReel(urlOGG,urlMP4){
	if(html5Video){
		var sVideo = '';
        sVideo += '<video id="demo-video"  autobuffer controls width="600" height="300">';
        sVideo += '<source src="'+urlMP4+'" type="video/mp4" />';
        sVideo += '<source src="'+urlOGG+'" type="video/ogg" />';
        sVideo += '</video>';		
		$("#reel-player").html(sVideo);		
		}else{
			swfobject.embedSWF("http://releases.flowplayer.org/swf/flowplayer-3.1.5.swf", "reel-player", "600", "300", "9.0.0", null, {  
				config: "{'clip': '"+urlMP4+"' }}" 
				} 
			);
			}	
}
