//Define Global vars for Slide
i = 0;
time = 10000;
element = '#slide .rotation';
var actSlide;
//End Define Global vars for Slide

$(document).ready(function(){


 $('input.textoABuscar').alphanumeric({allow:"., "});


/*********************************	SLIDE HOME	********************************/
	$(element).eq(0).css("display","block");
	//console.log($(element).eq(i));
	actSlide = setTimeout(nextFilm,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);

	});

	//Evento click en el menu muestra el detalle
	$("#filmNav li a").not(".link").click(function(){
		clearTimeout(actSlide);//pause slide
		$(".box_top .box_select").hide();

		var detail = $(this).attr("href");
		var detailArray = detail.split(",");
		if(detailArray.length > 1){
			var t = pintaTrailer(detailArray[1]);

			$(detailArray[0]+" .box_left").html(t);
		}

		$(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
		$(".box_top .box_select").hide();

		var detail = $(this).attr("href");

		var detailArray = detail.split(",");

		if(detailArray.length > 1){
			var t = pintaTrailer(detailArray[1]);

			$(detailArray[0]+" .box_left").html(t);
		}

		$(detailArray[0]).show();
		return false;
	});

	//Evento click para volver al slide
	$("a.btnVolver").click(function(){
		$(".box_trailer_home").hide();
		$(".box_trailer_home .box_left .trailer").empty();
		$(".box_top .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);

	});

/****************************	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 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******************************/






/*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="543" height="268" 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="543" height="268" type="application/x-shockwave-flash" data="ComplotVideoPlayer-543x268.swf">';
		trailer += '<param name="movie" value="ComplotVideoPlayer-543x268.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="432" height="238" 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="432" height="238" type="application/x-shockwave-flash" data="' + globalURL + 'ComplotVideoPlayer-543x268.swf">';
  		trailer += '<param name="movie" value="' + globalURL + 'ComplotVideoPlayer-543x268.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);

	}

}


