// manipulação das galerias e outros eventos javascript
var width_gallery = 0;
$(document).ready(function(){ 
	$(window).load(function () {
		$('#loading').fadeOut('normal');
	})
	if ($(".gallery-preview")[0] != null) {
		$(".gallery-preview_parent").jScrollPane({
			autoReinitialise: true,
		});
		$('.jspContainer').height(550);
		$jspane = $(".gallery-preview_parent").data('jsp');
		setInterval(function () {
			$jspane.setContentWidth($(".gallery-preview").width()-917);
			$(".gallery-preview").width(width_gallery);
		}, 1900);
	}
	
    url_base = "http://www.brunodias.com.br";
    if($(".gallery-source")[0]) {
        $(".gallery-source a:first").addClass("active");
        if ( $(".gallery-source a:first").attr("id") ) {
            var id = $(".gallery-source a:first").attr("id").replace(/_/g,"");
        }
        var page = $(".gallery-source ul").attr("id");

        $.post(url_base+"/pop-gallery.php",
            {id_gallery: id, page_show: page},
            function(valor){
				$jspane.scrollTo(0);
                $(".gallery-preview").html(valor);
				$(".gallery-preview").width(917);
				width_gallery = 917;
				$first_each = true;
				$(".gallery-preview img").each(function () {
					$(this).load(function () {
						if ($first_each) {
							width_gallery = $(this).width()+3;
							$first_each = false;
						}else{
							width_gallery += $(this).width()+3;	
						}
					})
				});
            }
        )

        //pega id da galeria para buscar imagens
        $(".gallery-source a").click(function(){
			$('#loading').fadeIn('normal');
            $(".gallery-source a").removeClass("active");

            var id = $(this).attr("id").replace(/_/g,"");
            var page = $(".gallery-source ul").attr("id");

            $(this).addClass("active");

            $.post(url_base+"/pop-gallery.php",
                {id_gallery: id, page_show: page},
                function(valor){
					$jspane.scrollTo(0);
					$(".gallery-preview").html(valor);
					$(".gallery-preview").width(917);
					width_gallery = 0;
					$first_each = true;
					qtd_img = $(".gallery-preview img").size();
					$(".gallery-preview img").each(function (i) {
						$(this).load(function () {
							if ($first_each) {
								width_gallery = $(this).width()+3;
								$first_each = false;
							}else{
								width_gallery += $(this).width()+3;	
							}
						});
						if (i == (qtd_img-1)) {
							setTimeout(function () {$('#loading').fadeOut('normal');}, 1900);
						}
					});
                }
            )
        });

        //pega altura do box com galeria, com overflow:hidden
        var height_gallery_source = $(".gallery-source").height();
        //pega a altura da listagem de galerias, sem o overflow:hidden
        var height_ul = $(".gallery-source ul").height();

        //inclui os links de controle da galeria
        if(height_gallery_source == 60) {
            $('<div class="gallery-control"><span class="gallery-return">return</span> | <span class="gallery-next"><a class="active" href="#">next</a></span></div>').insertAfter(".gallery-source");
        }

        i = 1;
        count_scrollHeight = 0;
        //eventos do link next
        $(".gallery-next a").live("click", function(){

            if(i == 0) i = 1;

            count_scrollHeight = 60 * i;

            $(".gallery-source").scrollTop(count_scrollHeight);

            if (height_gallery_source == 60) {
                $(".gallery-return").html('<a class="gallery-return active" href="#">return</a>');
            }

            if(count_scrollHeight >= (Math.floor(height_ul / 60) * 60)) {
                $(".gallery-next").html('next');
            }

            i++;
            return false;

        });

        //eventos do link return
        $(".gallery-return a").live("click", function(){

            if(count_scrollHeight == 60 || i == 2) { i = 0; } else { i--; };

             if(count_scrollHeight == 60) {
                $(".gallery-return").html('return');
             }

             if(count_scrollHeight > 0) {
                count_scrollHeight = count_scrollHeight / i;
             }

            $(".gallery-source").scrollTop(count_scrollHeight);
            if(count_scrollHeight < height_ul) {
                $(".gallery-next").html('<a class="active" href="#">next</a>');
            }

            return false;
        });
    }

    $(".menu li.one").hover(

        function () {

           $(this).children("ul").fadeIn("slow");
        },
        function () {
            $(".menu li ul").hide();
        }
       
    );

    $("form[name=contact]").validate({
            rules: {
                    ctt_email: {
                            required: true,
                            email: true
                    },
                    ctt_nome: "required",
                    ctt_mensagem: "required"
            }
    });
	
	if ($('#banner_home')[0] != null) {
		$('#banner_home_content').cycle({
			prev: $('#banner_home_seta_left a'),
			next: $('#banner_home_seta_right a'),
			
		});	
		
	}
});



