var objTop;

$(document).ready(function(){
	$(".accordion_c a").click(SlideDownUp);	
	$(".accordion_a a").click(SlideDownUp);	
	
	//Scroll Cartelera	
	var interval;
	objTop = $("#tableScroll").height()-365;
	interval = scrollObject("#tableScroll");	
	
	$(".scrollDown").click(function(){
		return false;								
	});	
	$(".scrollUp").click(function(){
		return false;								
	});	
	
	$(".get-cartelera-pdf").click(function(){
		var numero  = $("#diasCarteleraTv a.active").attr("href");
		numero = numero.split("#");
		numero = numero[1];
		var href = "http://www.juventudrebelde.cu/get/cartelera-tv-pdf/"+$("#diasCarteleraTv a.active").attr("rel")+"/"+numero;
		document.location = href;
		return false;									   
	});
	
	//Interactividad el tiempo
	$(".weather-nav a").click(weatherNav);
	$(".locationLink").click(getWeatherLargeWidget);
	$("#sateliteLink").click(function(){
		setInterval("slideShowSatelite()", 150);							  
	});
	
	//Interactividad cartelera TV
	$(".carteleraTV").click(getDayCarteleraTv);
	$(".canales").click(getCanalCartelera);
	

	//igualar columnas
	//equalHeight(".equalH");
	
	//Interactividad tintero
	$(".tintero-link").click(function(){
		var obj = $(this).attr("rel");
		$("#tintero-widget").find(".show").removeClass("show").addClass("hide");
		$(obj).removeClass("hide");
		$(obj).addClass("show");
		return false;
	});
	
	//Interactividad 'area Fidel Castro
	$(".fLink").click(function(){
		var obj = $(this).attr("rel");
		$("#dataArea").find(".show").removeClass("show").addClass("hide");
		$(obj).removeClass("hide");
		$(obj).addClass("show");
		$("#dataArea").removeClass("reflexiones_active");
		$("#dataArea").removeClass("imagenes_active");
		$("#dataArea").removeClass("videos_active");
		if(obj=="#reflexiones"){
			$("#dataArea").addClass("reflexiones_active");	
			$(this).parent("li").addClass("reflexiones_active");
			$("li.imagenes_active").removeClass("imagenes_active");
			$("li.videos_active").removeClass("videos_active");
		}
		if(obj=="#videos"){
			$("#dataArea").addClass("videos_active");
			$(this).parent("li").addClass("videos_active");
			$("li.imagenes_active").removeClass("imagenes_active");
			$("li.reflexiones_active").removeClass("reflexiones_active");
		}
		if(obj=="#imagenes"){
			$("#dataArea").addClass("imagenes_active");	
			$(this).parent("li").addClass("imagenes_active");
			$("li.reflexiones_active").removeClass("reflexiones_active");
			$("li.videos_active").removeClass("videos_active");
		}
		return false;
	});
	
	$("div#calendario li a").click(function (){$(this).html("<img src='/img/14.gif' />")});
	$(".day-active").click(function(){
		$.get("/ajax/", {request: "getPritedDay", date:$(this).attr("rel")})								
	});
	
	//Obtener publicidad
	$.get("/ajax/", {request:"getRandomAds"}, function(data){
		$("#imagen-aleatoria").append(data);												   
	});
	
	//Mas autores
	$(".author-list-btn").click(showAuthorList);
	$(".author-list-btn").fadeIn();
	
	//COmpartir
	$(".sendByEmail").click(function(){
		$(".send_friend").slideDown();								 
	});
	$("form#sendEmailFriend").validate();
	
});

function showAuthorList(){
	var attr = $(this).attr("rel");
	if(attr=="non-active"){
		$(".author-list").slideDown();
		$(".no-active").removeClass("no-active");
		$(".author-list-btn").addClass("active");
		$(this).attr("rel", "active");
	} else {
		$(".author-list").slideUp(function(){
			$(".active").removeClass("active");
			$(".author-list-btn").addClass("no-active");								   
		});			
		$(this).attr("rel", "non-active");
	}
	return false;	
}

// prepare the form when the DOM is ready 
$(document).ready(function() { 
    var options = { 
        target:        '#subscriptionResponse',   
		// target element(s) to be updated with server response 
		clearForm: true,  			 // clear all form fields after successful submit 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
              
        //resetForm: true
		beforeSubmit:  function(){
			$('#subscriptionResponse').fadeIn();	
			$('#subscriptionResponse').html('<br /><center><img src="/img/35.gif" /></center>');			
		},  // pre-submit callback 
        success: function() { 
        	//$('#subscriptionResponse').slideDown(); 
        } 
    }; 
 
    // bind to the form's submit event 
    $('#subscriptionForm').submit(function() { 

        $(this).ajaxSubmit(options); 

        return false; 
    }); 
	
}); 

$(document).ready(function() { 
    var options = { 
        target:        '#contactResponse',   
		// target element(s) to be updated with server response 
		clearForm: true,  			 // clear all form fields after successful submit 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
              
        //resetForm: true
		beforeSubmit:  function(){
			$('#contactResponse').html('<br /><center><img src="/img/35.gif" /></center>');			
		},  // pre-submit callback 
        success: function() { 
        	//$('#subscriptionResponse').slideDown(); 
        } 
    }; 
 
    // bind to the form's submit event 
    $('#contactForm').submit(function() { 

        $(this).ajaxSubmit(options); 

        return false; 
    }); 
	
});

$(document).ready(function() { 
    var formShare = { 
        target:        '.send_explication',   
		// target element(s) to be updated with server response 
		clearForm: true,  			
		beforeSubmit:  function(){
			$('.send_explication').html('<br /><img src="/img/loading.gif" />');			
		}, 
        success: function() { 
			$('.send_explication').html('<br />El articulo ha sido enviado correctamente');
		} 
    }; 
 
    // bind to the form's submit event 
    $('#sendEmailFriend').submit(function() { 

        $(this).ajaxSubmit(formShare); 

        return false; 
    }); 
	
});

function SlideDownUp(){
	var $action = $(this).attr("rel");
	var $link = $(this).parent();
	var $obj = $(this).parent().parent().find(".accordion");
	if($action=="up"){
		$obj.slideUp();	
		$link.removeClass("accordion_a");
		$link.addClass("accordion_c");
		$(".accordion_c a").click(SlideDownUp);
		$(this).attr("rel", "down");
	} else {
		$obj.slideDown();	
		$link.removeClass("accordion_c");
		$link.addClass("accordion_a");
		$(".accordion_c a").click(SlideDownUp);
		$(this).attr("rel", "up");	
	}
	return false;	
}


function scrollObject(obj){	

	var interval;	
	$(".scrollUp").mousedown(function(){
		interval = setInterval('scrollUp("'+obj+'", "'+objTop+'")',10);		
	});
	$(".scrollDown").mousedown(function(){			
		interval = setInterval('scrollDown("'+obj+'", "'+objTop+'")', 10);		
	});	
	
	$(".scrollDown").mouseup(function(){clearInterval(interval)});
	$(".scrollUp").mouseup(function(){clearInterval(interval)});
	return interval;
}
function scrollDown(obj){
	var top = $(obj).css("top");
	if( top=="auto" ){ top="0"; }
	top = top.split("px");		
	top = top[0]*(-1);
	var topNumber = top;
	if(topNumber<objTop){
		top = top+4;
		top = "-"+top+"px";
		$("#tableScroll").css("top", top);
	}	
}
function scrollUp(obj){
	var top = $(obj).css("top");
	if( top=="auto" ){ top="0"; }
	top = top.split("px");		
	top = top[0]*(-1);
	var topNumber = top;
	if(topNumber<=objTop || topNumber==objTop || topNumber>objTop){
		top = top-4;
		top = "-"+top+"px";
		$(obj).css("top", top);
	}
}

function weatherNav(){
	$(this).parent().parent().find("li.current").removeClass("current");
	if($(this).attr("href")=="/el-tiempo/#satelite"){
		$("#mapa").hide();
		$("#pronostico").hide();
		$("#satelite").show();
		$(this).parent().addClass("current");
	}
	if($(this).attr("href")=="/el-tiempo/#mapa"){
		$("#satelite").hide();
		$("#pronostico").hide();
		$("#mapa").show();
		$(this).parent().addClass("current");
	}
	if($(this).attr("href")=="/el-tiempo/#pronostico"){
		$("#mapa").hide();
		$("#satelite").hide();
		$("#pronostico").show();
		$(this).parent().addClass("current");
	}
	return false;	
}

function getWeatherLargeWidget(){
	var file = $(this).attr("rel");
	$.get("/cache/"+file+".cache",
	  function(data){
		$(".weather-container").html(data);
	  });
	return false;
}

function getDayCarteleraTv(){
	var canalTV = $(this).attr("rel");
	var diaSemana = $(this).attr("href");
	diaSemana = diaSemana.split("#");
	diaSemana = diaSemana[1];
	if(diaSemana==7){
		diaSemana=0;	
	}
	$(this).parent().parent().find(".active").removeClass("active");
	$(this).addClass("active");
	$("#tableContent div.loading").fadeIn();
	$.post("/ajax/", {request:"getDayTelevision", tvChannel:canalTV, weekDay: diaSemana}, function(data){		
		$("#tableScroll").html(data);	
		$("#tableContent div.loading").hide();		
		$("#tableScroll").css("top", "0px");
		$("#tableScroll").css("height", $("#tableScroll table").css("height"));
		objTop = $("#tableScroll").height()-365;
		clearInterval(interval);		
		interval = scrollObject("#tableScroll");
	});
	return false;	
}

function getCanalCartelera(){	

	$(this).parent().parent().find("li.active").removeClass("active");
	$(this).parent("li").addClass("active");

	var canalTV = $(this).attr("href");
	canalTV = canalTV.split("#");
	canalTV = canalTV[1];
	var listLink = $("#diasCarteleraTv li a").get();	
	$.each(listLink, function(){
		 $(this).attr("rel", canalTV);						  
	});
	var diaSemana = $(this).attr("rel");
	if(diaSemana==7){
		diaSemana=6;	
	}
	$("#diasCarteleraTv").find(".active").removeClass("active");
	$("#cartelera-"+diaSemana).addClass("active");
	$("#tableContent .loading").fadeIn();
	$.post("/ajax/", {request:"getDayTelevision", tvChannel:canalTV, weekDay: diaSemana}, function(data){		
		$("#tableScroll").html(data);	
		$("#tableContent div.loading").hide();
		$("#tableScroll").css("top", "0px");
		$("#tableScroll").css("height", $("#tableScroll table").css("height"));
		objTop = $("#tableScroll").height()-365;	
		clearInterval(interval);		
		interval = scrollObject("#tableScroll");	
	});
	return false;	
}

function equalHeight(group) {
	tallest = 0;
	$(group).each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$(group).height(tallest);
}

//Texto rezize e imprimir

$(document).ready(function(){
  // Reset Font Size
  var originalFontSize = $('html').css('font-size');
    $(".resetFont").click(function(){
    $('html').css('font-size', originalFontSize);
  });
  // Increase Font Size
  $(".fontUp").click(function(){
    var currentFontSize = $('.read p').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum+2;
	if(newFontSize<30){
    	$('.read p').css('font-size', newFontSize);
	}
    return false;
  });
  // Decrease Font Size
  $(".fontDown").click(function(){
    var currentFontSize = $('.read p').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum-2;
	if(newFontSize>=14){
    	$('.read p').css('font-size', newFontSize);
	}
    return false;
  });
  
  $(".printNews").click(function(){print(); return false;});
});

//Comportamiento multimedia

$(document).ready(function(){
	/*$(".multimedia-cont img").mouseover(function(){
		var obj = $(this);
		var placeholder = $("#multimedia-cont-"+$(obj).attr("rel"));
		$(placeholder).append("<p>"+($(obj).attr("alt"))+"</p>");											 
	});	
	$(".multimedia-cont img").mouseout(function(){
		var obj = $(this);
		var placeholder = $("#multimedia-cont-"+$(obj).attr("rel")+" p");
		$(placeholder).remove();											 
	});	*/
});
