//Define Global vars for Slide
i = 0;
k = 0;
j = 0;
h = 0;
time = 10000;

element = '#slide .rotation';
element2 = '#slide-1 .rotation-1';
element3 = '#slide-2 .rotation-2';
element4 = '#slide-3 .rotation-3';


var actSlideCine;
var actSlide;
var actSlideTv;
var actSlideNatura;
//End Define Global vars for Slide

$(document).ready(function(){


	// Initialize Minimal Galleriffic Gallery
	/*$('#thumbs').galleriffic({
		imageContainerSel:      '#slideshow',
		controlsContainerSel:   '#controls',
		numThumbs: 16,
		renderSSControls: false,
		captionContainerSel:       '#caption'
	});*/
	
	// End Minimal Galleriffic Gallery


 $('input.textoABuscar').alphanumeric({allow:"., "});


/*********************************	SLIDE HOME	********************************/
	$(element).eq(0).css("display","block");
	$(element2).eq(0).css("display","block");
	$(element3).eq(0).css("display","block");
	$(element4).eq(0).css("display","block");
	$("#filmCineNav li a:first").addClass("active");
	$("#filmTvNav li a:first").addClass("active");
	$("#filmNaturaNav li a:first").addClass("active");
	//console.log($(element).eq(i));
	//actSlide = setTimeout(nextFilm,time);
	//actSlideCine = setTimeout(nextFilmCine,time);
	//actSlideTv = setTimeout(nextFilmTv,time);
	//actSlideNatura = setTimeout(nextFilmNatura,time);
	
	$("#filmNav li a").hover(function(){
		clearTimeout(actSlide);
		var film = parseInt($(this).attr("id").substring(5,6));

		if(i != film){
			jQuery(element).eq(i).fadeOut(100);
			jQuery(element).eq(film).fadeIn(100);
			i = film;
			$('#filmNav a').removeClass('active');
			$('#filmNav a#film_'+ i ).addClass('active');
		}

	},function(){

		if($(".mas_vistos").css("display") == "block") actSlide = setTimeout(nextFilm,time);

	});
	
	$("#filmCineNav li a").hover(function(){
		//clearTimeout(actSlideCine);
		var film = parseInt($(this).attr("id").substring(10,11));

		if(k != film){
			
			jQuery(element2).eq(k).fadeOut(100);
			jQuery(element2).eq(film).fadeIn(100);
			k = film;
			$('#filmCineNav a').removeClass('active');
			$('#filmCineNav a#film_cine_'+ k ).addClass('active');
		}
	},function(){
		//actSlideCine = setTimeout(nextFilmCine,time2);
	});
	/*-----------------Para Slide Tv hover----------------*/
	$("#filmTvNav li a").hover(function(){
		//clearTimeout(actSlideTv);
		var film = parseInt($(this).attr("id").substring(8,9));

		if(j != film){
			
			jQuery(element3).eq(j).fadeOut(100);
			jQuery(element3).eq(film).fadeIn(100);
			j = film;
			$('#filmTvNav a').removeClass('active');
			$('#filmTvNav a#film_tv_'+ j ).addClass('active');
		}
	},function(){
		//actSlideTv = setTimeout(nextFilmTv,time);
	});
	/*-------------------fin-------------------------------*/
	/*-----------------Para Slide Natura hover----------------*/
	$("#filmNaturaNav li a").hover(function(){
		//clearTimeout(actSlideNatura);
		var film = parseInt($(this).attr("id").substring(12,13));

		if(h != film){
			//alert($(this).attr("id").substring(12,13));
			jQuery(element4).eq(h).fadeOut(100);
			jQuery(element4).eq(film).fadeIn(100);
			h = film;
			$('#filmNaturaNav a').removeClass('active');
			$('#filmNaturaNav a#film_natura_'+ h ).addClass('active');
		}
	},function(){
		//actSlideNatura = setTimeout(nextFilmNatura,time);
	});
	/*-------------------fin-------------------------------*/
	//Evento click en el menu muestra el detalle
	$("#filmNav li a").not(".link").click(function(){
		clearTimeout(actSlide);//pause slide
		$(".mas_vistos .box_select").hide();

		var detail = $(this).attr("href");
		var detailArray = detail.split(",");
		if(detailArray.length > 1){
			var t = pintaTrailer(detailArray[1]);

			$(detailArray[0]+" .trailerContent").html(t);
		}
		$("#masVistos").addClass('box_topBig');
		$(detailArray[0]).show();

		return false;
	});

	//Evento click en la imagen muestra el detalle
	$("#slide .rotation a.hide").not(".link").click(function(){
		clearTimeout(actSlide);//pause slide
		$(".mas_vistos .box_select").hide();

		var detail = $(this).attr("href");

		var detailArray = detail.split(",");
		$("#masVistos").addClass('box_topBig');
		if(detailArray.length > 1){
			var t = pintaTrailer(detailArray[1]);

			$(detailArray[0]+" .trailerContent").html(t);
		}

		$(detailArray[0]).show();
		return false;
	});

	//Evento click para volver al slide
	$("a.btnVolver").click(function(){
		$(".box_trailer_home").hide();
		$(".box_trailer_home .trailerContent .trailer").empty();
		$("#masVistos").removeClass('box_topBig');
		$(".mas_vistos .box_select").show();
		actSlide = setTimeout(nextFilm,time);//play slide
		return false;

	});

	//hover sobre la imagen detiene el slide
	$(".rotation a img").hover(function(){
		clearTimeout(actSlide);
	},function(){
		actSlide = setTimeout(nextFilm,time);
	});
	/*$(".rotation-1 a img").hover(function(){
		clearTimeout(actSlideCine);
	},function(){
		actSlideCine = setTimeout(nextFilmCine,time2);
	});
	$(".rotation-2 a img").hover(function(){
		clearTimeout(actSlideTv);
	},function(){
		actSlideTv = setTimeout(nextFilmTv,time);
	});
	$(".rotation-3 a img").hover(function(){
		clearTimeout(actSlideNatura);
	},function(){
		actSlideNatura = setTimeout(nextFilmNatura,time);
	});*/

/****************************	END SLIDE HOME	************************************/

/***************************	TABS	*************************************************/


	$('#box_tabs .box_select_tab ul li').children('a').click(function(){

		$('#box_tabs .box_select_tab ul li a.link').removeClass('active');

		$(this).addClass('active');

		$(".box_tabs_cont .all_box").fadeOut(50);

		var href = $(this).attr("href");

		$(".box_tabs_cont").children('div'+href).fadeIn(200);

		return false;
	});

//Auto height tab
var max_height = 0;
$("#box_tabs .box_tabs_cont .all_box").each(function(){

	if($(this).height() > max_height) max_height = $(this).height();

});
$(".box_tabs_cont").height(max_height);


/******************************	END TABS	*****************************************************/


/********************HOME HOVER'S CAROUSEL***************************/

$('#carousel_home .panel').hover(function(){

		$(this).addClass('panel_hover');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('p').children('a').addClass('color');
		$('.panel_hover').children('table').children('tbody').children('.tr_img').children('td').children('.img').addClass('brdr');

	},function(){

		$(this).removeClass('panel_hover');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('p').children('a').removeClass('color');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('p').children('a').removeClass('color');
		$('.panel_hover').children('table').children('tbody').children('.tr_img').children('td').children('.img').removeClass('brdr');

	});


$('ul.jcarousel-skin-tango li').hover(function(){

		$(this).addClass('panel_hover');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('p').children('a').addClass('color');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('table').children('tbody').children('tr').children('td').children('p').children('a').addClass('color');
		$('.panel_hover').children('table').children('tbody').children('.tr_img').children('td').children('.img').addClass('brdr');

	},function(){

		$(this).removeClass('panel_hover');
		$('.panel_hover').children('table').children('tbody').children('tr').children('.td_text').children('div').children('p').children('a').removeClass('color');
		$('.panel_hover').children('table').children('tbody').children('.tr_img').children('td').children('.img').removeClass('brdr');

	});

/********************END HOME HOVER'S CAROUSEL***************************/

//setTimeout(ajustarWindowHeight,500);

});




/****************************	FUNCTIONS SLIDE HOME	************************************/
function nextFilm()
{
    var amount = jQuery(element).length;
    clearTimeout(actSlide);

	if(amount > i+1){
		jQuery(element).eq(i).fadeOut(1000);
	    jQuery(element).eq(i+1).fadeIn(1000);
		i++;
	}
	else{
		jQuery(element).eq(i).fadeOut(1000);
	    jQuery(element).eq(0).fadeIn(1000);
		i = 0;
	}

	$('#filmNav a').removeClass('active');
	$('#filmNav a#film_'+ i ).addClass('active');

  if(time != 0) actSlide = setTimeout(nextFilm,time);
}
/*Slide Home produccion cine*/
function nextFilmCine()
{
    var amount = jQuery(element2).length;
    //clearTimeout(actSlideCine);

	if(amount > k+1){
		jQuery(element2).eq(k).fadeOut(1000);
	    jQuery(element2).eq(k+1).fadeIn(1000);
		k++;
	}
	else{
		jQuery(element2).eq(k).fadeOut(1000);
	    jQuery(element2).eq(0).fadeIn(1000);
		k = 0;
	}

	$('#filmCineNav a').removeClass('active');
	$('#filmCineNav a#film_cine_'+ k ).addClass('active');

  //if(time != 0) actSlideCine = setTimeout(nextFilmCine,time);
}
/*fin slide home produccion cine*/
/*Slide Home produccion tv*/
function nextFilmTv()
{
    var amount = jQuery(element3).length;
    //clearTimeout(actSlideTv);

	if(amount > j+1){
		jQuery(element3).eq(j).fadeOut(1000);
	    jQuery(element3).eq(j+1).fadeIn(1000);
		j++;
	}
	else{
		jQuery(element3).eq(j).fadeOut(1000);
	    jQuery(element3).eq(0).fadeIn(1000);
		j = 0;
	}

	$('#filmTvNav a').removeClass('active');
	$('#filmTvNav a#film_tv_'+ j ).addClass('active');

  //if(time != 0) actSlideTv = setTimeout(nextFilmTv,time);
}
/*fin slide home produccion cine*/
/*Slide Home produccion cine*/
function nextFilmNatura()
{
    var amount = jQuery(element4).length;
    //clearTimeout(actSlideNatura);

	if(amount > h+1){
		jQuery(element4).eq(h).fadeOut(1000);
	    jQuery(element4).eq(h+1).fadeIn(1000);
		h++;
	}
	else{
		jQuery(element4).eq(h).fadeOut(1000);
	    jQuery(element4).eq(0).fadeIn(1000);
		h = 0;
	}

	$('#filmNaturaNav a').removeClass('active');
	$('#filmNaturaNav a#film_natura_'+ h ).addClass('active');

  //if(time != 0) actSlideNatura = setTimeout(nextFilmNatura,time);
}
/*fin slide home produccion cine*/

/***********************SLIDE PRESS ROOM********************************/

function slidePreesRoom(){

	if($('#press_toggle').css('display') == 'block'){
   $('a.btn_press').addClass('down');
	 $('a.btn_press').removeClass('up');
	 $('#press_toggle').slideUp('normal');
   var url = globalURL + "site/pressRoomSlideUp";
	 $.get(url);
	}else{
   $('a.btn_press').addClass('up');
	 $('a.btn_press').removeClass('down');
	 $('#press_toggle').slideDown('normal');
	 var url = globalURL + "site/pressRoomSlideDown";
	 $.get(url);
	}
	return false;
}

/*********************END SLIDE PRESS ROOM******************************/



/*function loginPrensa(){
		if(document.login.password.value=="" || document.login.password.value!="vision"){
			alert("La contraseña es incorrecta"); 	
		}else{
			document.location="http://prensa.wandavision.com/index2.php?pass=vision";
		}
		return false;
	}
	
	
function recuperaPass(){
	//alert("HOLA");
	if(document.recPass.correoRecover.value==""){
		alert("El email no puede estar vacío"); 	
	}else{
		document.recPass.submit();
		//alert(document.recPass.correoRecover.value);
	}
	return false;
}



function enviaRegistro(){
		//alert("HOLA");
		var error = false;
		
		if(document.registro.data[Periodist][email].value==""){
			error = true;
		}else if(document.registro.nombre.value==""){	
			 error = true;	
		}else if(document.registro.apellidos.value==""){	
			 error = true;	
		}else if(document.registro.medio.value==""){	
			 error = true;	
		}else if(document.registro.provincia.value==""){	
			 error = true;	
		}else if(document.registro.telefono.value==""){	
			 error = true;	
		}else if(document.registro.tipomedio.value==""){	
			 error = true;	
		}
		if(error){
			alert("Tiene que rellenar los campos obligatorios");
		}else{
			document.registro.submit();
		}
		
		
		return false;
	}*/


/*Validaci�n del formulario de contactos*/

function enviaRegistro(formId)
{
	//var form = document.form;
	var ok = true;

	$("form#"+formId).find(".required").not(".email").each(function(){
		if(IsBlank($(this).val())) // si el valor del input est� vac�o�
		{
			ok = false;
			if($(this).siblings("span.error").length == 0 ){
				$(this).siblings("span.error").remove();
				//$(this).css("border","1px solid #f00");
				$(this).after('<span class="error">This is a required field.</span>');
				$(this).siblings("span.error").fadeIn('normal');
			}
		}
		else    // si no est� vac�o el campo . . .
		{
			//$(this).css("border","1px solid #E6E6E6");
			$(this).siblings("span.error").remove();
		}
	});


	$("form#"+formId).find(".email").filter(".required").each(function(){
		if(!isValidEmail($(this).val())){ // este input es pasado por la funci�n �valid_email()� � si no es correcto�
			ok = false; // el ok sera �false�
			if($(this).siblings("span.error").length == 0 ){
				$(this).siblings("span.error").remove();
				//$(this).css("border","1px solid #f00");
				$(this).after('<span class="error">Email is invalid.</span>');
				$(this).siblings("span.error").fadeIn('normal');
			}

		}else{

			//$(this).css("border","1px solid #E6E6E6");
			$(this).siblings("span.error").remove();

		}
	});

	return ok;
}





/*Validaci�n del formulario de contactos*/

function validate(formId)
{
	//var form = document.form;
	var ok = true;

	$("form#"+formId).find(".required").not(".email").each(function(){
		if(IsBlank($(this).val())) // si el valor del input est� vac�o�
		{
			ok = false;
			if($(this).siblings("span.error").length == 0 ){
				$(this).siblings("span.error").remove();
				//$(this).css("border","1px solid #f00");
				$(this).after('<span class="error">This is a required field.</span>');
				$(this).siblings("span.error").fadeIn('normal');
			}
		}
		else    // si no est� vac�o el campo . . .
		{
			//$(this).css("border","1px solid #E6E6E6");
			$(this).siblings("span.error").remove();
		}
	});


	$("form#"+formId).find(".email").filter(".required").each(function(){
		if(!isValidEmail($(this).val())){ // este input es pasado por la funci�n �valid_email()� � si no es correcto�
			ok = false; // el ok sera �false�
			if($(this).siblings("span.error").length == 0 ){
				$(this).siblings("span.error").remove();
				//$(this).css("border","1px solid #f00");
				$(this).after('<span class="error">Email is invalid.</span>');
				$(this).siblings("span.error").fadeIn('normal');
			}

		}else{

			//$(this).css("border","1px solid #E6E6E6");
			$(this).siblings("span.error").remove();

		}
	});

	return ok;
}

function validateFormForgotPassword(formId)
{
	var ok = true;

	$("form#"+formId).find(".email").filter(".required").each(function(){
		if(!isValidEmail($(this).val())){
			ok = false;
			if($(this).siblings("span.error").length == 0 ){
				$(this).siblings("span.error").remove();
				$(this).after('<span class="error">Email is invalid.</span>');
				$(this).siblings("span.error").fadeIn('normal');
			}
		}else{
			$(this).siblings("span.error").remove();
		}
	});

	return ok;
}

function validnum(s)
{
     // Check for number
	 num = new RegExp(/^(?:\+|-)?\d+$/);
     if (!num.test(s)) {
          return false;
     }
	return true;
}

function IsBlank (strString)
{
	if (strString.length == 0)
		return true;

	for (i = 0; i < strString.length; i++)
	{
		strChar = strString.charAt(i);
		if (strChar != " ")
			return false;
	}
	return true;
}

function isValidEmail(email){

	if (email.length < 5)
		return false;

	subEmail=email.split('@'); //subEmail is a string array (contains strings splitted by '@')
	if (subEmail.length != 2)
		return false;

	dotStr=subEmail[1].split('.'); //dotStr is a string array (contains strings splitted by '.')

	if(dotStr.length<2)
		return false;

	for(i=1;i<dotStr.length;i++){
		if((dotStr[i].length!=2)&&(dotStr[i].length!=3))
			return false;
	}
return true;
}// End isValidEmail

function pintaTrailer(video){
      var trailer = "";

	  var tipo = video.indexOf(".mp4");



      if(tipo!=-1){
		trailer += '<div class="trailer"><video id="videoFicha" class="video-js" width="640" height="400" poster="img/previoTrailerHome.jpg" controls preload>';
		trailer += '<source src="'+video+'" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'>';
		trailer += '</video></div>';
	  }else{
		trailer += '<div class="trailer"><object class="vjs-flash-fallback" width="640" height="400" type="application/x-shockwave-flash" data="ComplotVideoPlayer-16-9-640.swf">';
		trailer += '<param name="movie" value="ComplotVideoPlayer-16-9-640.swf" />';
		trailer += '<param name="quality" value="high" />';
		trailer += '<param name="bgcolor" value="#000000" />';
		trailer += '<param name="play" value="true" />'
		trailer += '<param name="loop" value="true" />';
		trailer += '<param name="wmode" value="window" />';
		trailer += '<param name="scale" value="showall" />';
		trailer += '<param name="menu" value="true" />';
		trailer += '<param name="devicefont" value="false" />';
		trailer += '<param name="salign" value="" />';
		trailer += '<param name="allowScriptAccess" value="sameDomain" />';
		trailer += '<param name="allowFullScreen" value="true" />';
		trailer += '<param name="FlashVars" value="videosource='+video+'" />';
		trailer += '</object></div>';
	  }


	return trailer;
}

function pintaTrailer2(video) {

    var trailer = "";

	  var tipo = video.indexOf(".mp4");

    if(tipo!=-1){

  		trailer += '<div class="trailer"><video id="videoFicha" class="video-js" width="414" height="264" poster="' + globalURL + 'img/previoTrailerHome.jpg" controls preload>';
  		trailer += '<source src="' + video + '" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'>';
  		trailer += '</video></div>';

	  } else {

  		trailer += '<div class="trailer"><object class="vjs-flash-fallback" width="414" height="264" type="application/x-shockwave-flash" data="' + globalURL + 'ComplotVideoPlayer-16-9-640.swf">';
  		trailer += '<param name="movie" value="' + globalURL + 'ComplotVideoPlayer-16-9-640.swf" />';
  		trailer += '<param name="quality" value="high" />';
  		trailer += '<param name="bgcolor" value="#000000" />';
  		trailer += '<param name="play" value="true" />'
  		trailer += '<param name="loop" value="true" />';
  		trailer += '<param name="wmode" value="window" />';
  		trailer += '<param name="scale" value="showall" />';
  		trailer += '<param name="menu" value="true" />';
  		trailer += '<param name="devicefont" value="false" />';
  		trailer += '<param name="salign" value="" />';
  		trailer += '<param name="allowScriptAccess" value="sameDomain" />';
  		trailer += '<param name="allowFullScreen" value="true" />';
  		trailer += '<param name="FlashVars" value="videosource=' + video + '" />';
  		trailer += '</object></div>';

	  }

	return trailer;

}

function mostrarTrailer(urlVideo, iframe) {
   $('#iframe_' + iframe + ' .box_video').html(pintaTrailer2(urlVideo));
}

function ajustarWindowHeight(){

	//var header = $(".header").height();
	//var footer = $(".footer").height();
	var main = $(".main").height();
	var body = $("body").height();
	var html = $("html").height();

	if(html > body){

		var dif = html - body;

		$(".main").css("min-height",main+dif);

	}

}




function buscar(){
	var verifSearch=true;
	if($('#BuscarBuscar').val() == "Buscar" || $('#BuscarBuscar').val() == ""){
		verifSearch=false;
					
	}
	return verifSearch;
}


