/**
 * Randomly reorder child elements.
 * The optional *callback* is called with each child and its new (deep) clone, allowing you to
 * copy data over or anything else you may require.
 *
 * Example usage:
 * controls.songs.reorder(function(child, clone) {
 * clone.data('info', child.data('info'));
 * var id = child.data('info').id;
 * if (self.current != undefined && self.current.data('info').id == id) self.current = clone;
 * if (self.next != undefined && self.next.data('info').id == id) self.next = clone;
 * });
 *
 * @see http://blog.rebeccamurphey.com/2007/12/11/jquery-plugin-randomly-reorder-children-elements/
 */
$.fn.reorder = function(callback) {
 
  // random array sort @see http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }
 
  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;

    if (childCount > 1) {
      $children.hide();
 
      var indices = new Array();
      for (i=0;i<childCount;i++) { indices[indices.length] = i; }
      indices = indices.sort(randOrd);
      $.each(indices,function(j,k) {
        var $child = $children.eq(k);
        var $clone = $child.clone(true);
        $clone.show().appendTo($this);
        if (callback != undefined) {
          callback($child, $clone);
        }
        $child.remove();
      });
    }
  }));
}

/**
 * Desenvolvidor linha a linha por: Thiago Prazeres - thiagoprazeres@gmail.com
*/
var url_site = "http://www.aope.org.br/";
/*
* Efeito de rolagem
*/
var x = 0;
function rola_noticia(){
	var altura = $("#topo div.noticias div").height();
	noticia_rolando = setTimeout(function(){
	var margem = -x;
	$("#topo div.noticias div").css({marginTop: (margem)});
	x++;
	if((altura*-1)>margem){ x = -90; }
	rola_noticia();
	}, 100);
}
//Função para o formulário de contato.
function callback(){
	$.post("persistencia/contato.php", { nome: $("#nome").attr("value"), email2: $("#email2").attr("value"), telefone: $("#telefone").attr("value"), assunto: $("#assunto").attr("value"), mensagem: $("#mensagem").attr("value") },
	function(data){
		$("#respostaemail").fadeIn().html(data);
	});
	return false;
}

$(document).ready(function(){
	rola_noticia();
	$("#topo div.noticias").mouseover(function(){clearTimeout(noticia_rolando);});
	$("#topo div.noticias").mouseout(rola_noticia);
	//Habilita target _blank para todos os links de notícias.
	$("div.noticias div a, #artigos a, #serrambi").attr("target", "_blank");
	//Bota pena em todos os t�tulos
	$("#conteudo #principal h2 span, #conteudo #principal h1 span").append(' <img src="'+url_site+'img/pena.gif" width="21" height="24" alt="Pena">');
	//Habilita o plug do light box na imagens da galeria de fotos
	if($("#galeria a").length>0){
		$("#galeria a").lightbox();
	}
	if($("#salute").length>0){
		$("#salute").lightbox();
	}
	/*
	 * Idéias de André.
	 * Exibir o galo de campina apenas quando a p�gina for grande o suficiente para ele
	 * aparecer por inteiro. Caso contr�rio oculta.
	*/
	if($("#geral").height()>960){
		$("#geral").addClass("com_fundo");
	}
	//Rodap� um pouco transparente
	$("#rodape").css("opacity", "0.7");
	/*
	 * P�gina de artigos
	*/
	$("#lista_artigos a, p.adobe_reader a, .lista_links li a").attr("target", "_blank");
	//Efeitoa de passar o mouse e ficar mais transparente.
	$("#lista_galeria a").hover(function(){
		$(this).fadeTo("slow", 0.5);
	}, function(){
		$(this).fadeTo("slow", 1);
	});
	//Habilita o plug do light box na imagens da galeria de fotos
	if($("#lista_fotos a").length>0){
		$("#lista_fotos a").lightbox();
	}
	//Corrige tamanho da div galeria.
	var altura_lista_fotos = 0;
	//Quantidade de imagens
	var qt_img = Math.round($("#lista_fotos li a").length/4)+1;
	if(qt_img>4){
		var altura_lista_fotos = (($("#lista_fotos li a").height()+8)*qt_img)+64;
		$("#conteudo").height(altura_lista_fotos);
	}
	//Alinha imagem na tabela ao topo
	$("#lista_fotos").attr("valign", "top");
	$("#anuncios ul:eq(0) li:eq(2) a, #anuncios ul:eq(0) li:eq(1) a, #anuncios ul:eq(0) li:eq(3) a").attr("target", "_black");
	$(".criadores, #rodape a").attr("target", "_black");
	
	//Reordena banner
	$("#anuncios ul").reorder();
});

/*
 * Controla rolagem da p�gina afim de fixar fundo.
*/
$(window).scroll(function () {
	var altura_html = $(window).height();
	var rolagem_vertical = $("html").attr("scrollTop");
	if((rolagem_vertical+altura_html)>1068){
		$("body, #geral").css({"background-attachment": "fixed", "background-position": "bottom"});
	}else {
		$("body").css({"background-attachment": "scroll", "background-position": "top"});
		$("#geral").css({"background-attachment": "scroll", "background-position": "50% 655px"});
	}
});