$(document).ready(function() {

	var newsIndex = 0;
	$("#featured .news .next").click(function(){
		newsIndex++;
		if(newsIndex == $("#featured .news li").length) newsIndex = 0;
		$("#featured .news li").hide();
		$("#featured .news li").eq(newsIndex).show();
		return false;
	});
	$("#featured .news .prev").click(function(){
		if(newsIndex == 0) newsIndex = 2;
		else  newsIndex--;
		$("#featured .news li").hide();
		$("#featured .news li").eq(newsIndex).show();
		return false;
	});
	
	$("#slides li a").click(function(){
		clearInterval(slides);
		slide($(this).parent().attr("id").replace('s-',''));
		return false;
	});
	
	$("a.add-note").click(function(){
		var offset = $(this).offset();
		var top = offset.top - 5;
		var left = offset.left - 480;
		if($(this).parent().attr("id")=="notes"){
			left = left + 15;
		}
		$("#add-note").css({'top':top+'px', 'left':left+'px'}).fadeIn();
		return false;
	});
	$("#add-note").submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), function(data){
			$("textarea[name='testimonio']").val("");
		});
		$(this).fadeOut();
		return false;
	});
	
	$("#nav a[href='contacto']").click(function(){
		var offset = $(this).offset();
		var top = offset.top + 25;
		var left = offset.left - 590;
		$("#contact").css({'top':top+'px', 'left':left+'px'}).fadeIn();
		return false;
	});
	$("#contact").submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), function(data){ alert("Tu mensaje ha sido enviado, muchas gracias"); });
		$(this).fadeOut();
		return false;
	});
	
	$("a.add-comment").click(function(){
		var offset = $(this).offset();
		var top = offset.top - 5;
		var left = offset.left - 465;
		$("#add-comment").css({'top':top+'px', 'left':left+'px'}).fadeIn();
		return false;
	});
	$("#add-comment").submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), function(data){
			$("input[name='nombre']").val("");
			$("input[name='correo']").val("");
			$("textarea[name='comentario']").val("");
		});
		$(this).fadeOut();
		return false;
	});
	
	$("a.add-reply").click(function(){
		$("#add-reply").fadeIn();
		$("input[name='respuesta']").val($(this).attr("rel"));
		return false;
	});
	$("#add-reply").submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), function(data){
			$("input[name='nombre']").val("");
			$("input[name='correo']").val("");
			$("textarea[name='comentario']").val("");
		});
		$(this).fadeOut();
		return false;
	});
	
	$("form img.close").click(function(){
		$(this).parent().fadeOut();
	});
	
	$("#featured .gallery .file").click(function(){
		if($(this).hasClass("youtube")){
			$("#prettyPhoto").attr("href", $(this).attr("href")).trigger('click');
		} else {
			window.open($(this).attr("href"));
		}
		return false;
	});
	
	$("input[name='search']").focus(function(){
		$(this).val("");
	});
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	if($("#submenu li:first").hasClass("active")){
		$("#submenu").addClass("first");
	}
	
	if($("#slide").attr("alt")!=""){
		$("#slide").css({"cursor":"pointer"});
	} else {
		$("#slide").css({"cursor":"default"});
	}
	
	$("#pp").attr("href","http://www.youtube.com/watch?v="+$("#slides li").eq(0).find("a").attr("rel"));
	$("#slide").click(function(){
		if($(this).attr("alt")!="")
			$("#pp").trigger("click");
	});
	
	$("a.replies").click(function(){
		var a = $(this);
		if($('#r-'+a.attr("rel")).text()==""){
			$('#r-'+a.attr("rel")).load('respuestas/'+a.attr("rel"), function(){
				$('#r-'+a.attr("rel")).slideDown(function(){
					a.hide();
					a.next().show();
				});
			});
		} else {
			$('#r-'+a.attr("rel")).slideDown(function(){
				a.hide();
				a.next().show();
			});
		}
		return false;
	});
	
	$("a.collapse").click(function(){
		var a = $(this);
		$('#r-'+a.attr("rel")).slideUp(function(){
			a.prev().show();
			a.hide();
		});
		return false;
	});
	
	$("#comments li a.vote").click(function(){
		var count = parseInt($(this).text().replace("(",""))+1;
		var el = $(this);
		var url = el.attr("href").split("/");
		var ajax = true;
		
		if($.cookie("comentarios")!=null){
			var com = $.cookie("comentarios").split(",");
			for(i=0; i<com.length; i++){
				if(parseInt(com[i])==parseInt(url[2])) ajax = false;
			}
		}
		if(ajax) {
			$.post(el.attr("href"), { }, function(data){
				if(data.respuesta=="OK") {
					el.text("("+count+")");
					if($.cookie("comentarios")!=null) 
						$.cookie("comentarios", $.cookie("comentarios")+','+url[2], { path: '/', expires: 30 });
					else 
						$.cookie("comentarios", url[2], { path: '/', expires: 30 });
				}
			}, "json");
		}
		return false;
	});
	
	$("#signup").submit(function(){
		$.post($(this).attr("action"), $(this).serialize(), function(data){
			alert("Sus datos han sido enviados, ya puede descargar los materiales de la campaņa.");
			$("input, textarea").val("");
			$("#downloads").attr("title","").removeClass("dis");
			$.cookie("descargas", 1, { path: '/', expires: 300 });
		});
		return false;
	});
	
	$("#downloads a").click(function(){
		if($(this).parent().hasClass("dis")){
			alert($(this).parent().attr("title"));
			return false;
		}
	});
	
	if($.cookie("descargas")!=null){
		$("#downloads").attr("title","").removeClass("dis");
	}
	
	if($("#submenu").height()>$("#content").height()) $("#content").height($("#submenu").height()+15);
	
});

var ticker = setInterval('showTicker()', 4000);
var items = 3;
function showTicker(){
	if(items>=50) {$(".testimonies ul").animate({"top": "0px"}, "fast"); items = 2;}
	else {$(".testimonies ul").animate({"top": "-=64px"}, "slow");}
	items++;
}

var banners = setInterval('showBanners()', 5000);
var b = 1;
function showBanners(){
	var len = $("#banners li").length;
	if(b>=len) {$("#banners ul").animate({"top": "0px"}, "fast"); b = 0;}
	else {$("#banners ul").animate({"top": "-=60px"}, "slow");}
	b++;
}

function slideshow(){
	if($("#slides li.active a").length) {
		var max  = $("#slides li").length;
		var curr = $("#slides li.active").attr("id").replace('s-','');
		var next = (parseInt(curr) >= max) ? 1 : parseInt(curr)+1 ;
		slide(next);
	}
}

function slide(index){
	var cursor = "pointer";
	if($("#slides li").eq(index-1).find("a").attr("rel")=="") cursor = "default";
	$("#slide").attr("src",$("#slides li").eq(index-1).find("a").attr("href")).attr("alt",$("#slides li").eq(index-1).find("a").attr("rel")).css({"cursor":cursor});
	$("#pp").attr("href","http://www.youtube.com/watch?v="+$("#slides li").eq(index-1).find("a").attr("rel"));
	$("#slides li").removeClass("active");
	$("#slides li").eq(index-1).addClass("active");
}

var slides = setInterval("slideshow()", 7000);
