
function setCarouselBtnImg(prevHover, prev, nextHover, next){

  $(".jcarousel-prev").css("background", "transparent url("+prev+") no-repeat 0 0");
  $(".jcarousel-next").css("background", "transparent url("+next+") no-repeat 0 0");

  $(".jcarousel-prev").hover(function(){
      $(this).css("background", "transparent url("+prevHover+") no-repeat 0 0");
      }, function(){
      $(this).css("background", "transparent url("+prev+") no-repeat 0 0");
      });

  $(".jcarousel-next").hover(function(){
      $(this).css("background", "transparent url("+nextHover+") no-repeat 0 0");
      }, function(){
      $(this).css("background", "transparent url("+next+") no-repeat 0 0");
      });
}

function initGlobalObjects() {
  if (typeof($img_gallery) == 'undefined') {
    $img_gallery = $("img#imagem-galeria");
    $setas_navegacao = $(".setas-navegacao");
    $btn_img_previous = $("#btnImgPrev");
    $btn_img_next = $("#btnImgNext");
  }
}

function initCurrentImageProperties() {

  initGlobalObjects();

  // Limite do tamanho da imagem para até 615px de largura e ou 415 de altura
  if($img_gallery.width() > 600) {
    //$(this).css("width", 600);
  }
  if($img_gallery.height() > 470) {
    $(this).css("height", 470);
  }

  var width = 600 - $img_gallery.width();
  if (width >= 0) {
    $img_gallery.parent().css("margin-left", (width/2));
  }

  //Alinhamento do autor em relação a margem direita da foto para imagem vertical/horizontal
  widthAuthor = (600 - $img_gallery.width())/2;
  $('.author-picture ul').css("padding-right", widthAuthor + 23)

  $btn_img_previous.css("width", $img_gallery.width()/2);
  $btn_img_next.css("width", $img_gallery.width()/2);

  $btn_img_previous.css("height", $img_gallery.height());
  $btn_img_next.css("height", $img_gallery.height());

  $setas_navegacao.css("margin-top", - $img_gallery.height());
  $('#box-foto-selecionada').css("height", $img_gallery.height());

  if($("#foto-legenda:has(h1#notice)").length == 1){
    $('#foto-legenda').css("display", "block");
    $('div.box-galeria').css("height", $img_gallery.height() + $('#notice').height() + 15);
  }else{
    $('div.box-galeria').css("height", $img_gallery.height() + $('#notice').height());
  }
}

$(document).ready(function(){

    //Seleciona a aba correspondente no header
    setHeaderNavigation();

	var gallery_max_width = $("#galeriaFoto").width();

	// Triggers the selection of a gallery item
	function selectGalleryImg(item) {
		var selected_item = $("#galeriaNav #thumbsWrapper ul li:eq("+item+")");
		var carousel = $("#thumbsWrapper").siblings(".carouselNav");
		var legend = $(selected_item).find("a img").attr("longdesc").split('|');
		var legend_text = legend[0];
		var legend_date = legend[1];
    var link_legend = $(selected_item).find("a img").attr("linkdesc");
		var credit = $(selected_item).find("p.thumb-credito").text();
		var src = $(selected_item).find("a").attr("href");

		$(selected_item)
			.addClass("active")
			.siblings()
				.removeClass("active");

		$(carousel).find("li").removeClass("disabled");
		$("#galeriaHoverNav").find("li").removeClass("disabled");

		if(item == 0) {
			$(carousel).find("li.ant").addClass("disabled");
			$("#galeriaHoverNav").find("li.ant").addClass("disabled");
		}
		if (item == $(carousel).data("carousel_length")-1) {
			$(carousel).find("li.prox").addClass("disabled");
			$("#galeriaHoverNav").find("li.prox").addClass("disabled");
		}

		$(".galeriaCredito").text(credit);
		$("#galeriaFoto .legenda").text(legend_text);
		$("#galeriaFoto .date").text(legend_date);
    	$("#galeriaFoto .link_legenda").attr('href',link_legend);
		$("#galeriaInterval .num").text(item+1);

		loadGalleryImg(src);
	}

	// Loads image on gallery container
	function loadGalleryImg(src) {
		var img = new Image();

		$("#galeriaImg")
			.addClass("carregando animating");
		$("#galeriaImg img")
			.remove();

		$(img)
			.load(function(){
				$(this).css("display", "none");

				$("#galeriaImg").append(this);
				if($(this).width() > gallery_max_width) { // resizes image if it's too big to fit the container
					$(this).css("width", gallery_max_width);
				}


		// Limite do tamanho da imagem para até 615px de largura e ou 415 de altura
        if($("#galeriaImg img").width() > 615) {
			$(this).css("width", 615);
		}
        if($("#galeriaImg img").height() > 410) {
			$(this).css("height", 410);
		}

		// Centralização da imagem, quando a mesma for menor
        var tamanho =  $("#galeriaImg img").width();
        if($(this).width() < 615) {
          $("#galeriaImg").css("margin-left", (615-tamanho)/2);
		}

				$("#galeriaImg")
					.removeClass("carregando")
					.animate({
						height: $(this).height(),
						width: $(this).width()
					}, function(){
						$("#galeriaImg").removeClass("animating");
					});
				$("#galeriaFoto .credito").css("width", $(this).width());

				$(this).fadeIn();
			})
			.error(function(){
				alert("Erro ao carregar imagem");
			})
			.attr("src", src);
	}

	function scrollCarousel(c, direction) {
		var carousel = $(c);
		var length = $(carousel).data("carousel_length");
		var offset = $(carousel).data("carousel_offset");
		var visible_items = $(carousel).data("carousel_visible_items");
		var item_width = $(carousel).data("item_width");

		// fix to melhor da semna componente
		if ($(carousel).parent(".carousel").is("#melhorSemanaNav")) {
			length = length + 1;
		}
		// fix to guia 10 mais left component
		if($(".categoriasWrapper").width() <= 210) {
			visible_items = visible_items - 1
			if(offset == (length - offset) -1) {
				item_width = item_width + 4;
			}
		}

		if(direction == "right") {
			if(length - offset <= visible_items) { // end of carousel

			} else {
				$(carousel).data("is_animating", true);
				$(carousel).data("carousel_offset", offset+1);
				$(carousel).siblings(".carouselItems").find("ul")
					.css("position", "absolute")
					.animate({ left : (offset+1)*item_width*-1}, "normal", null, function(){
						$(carousel).data("is_animating", false);
					});
			}
		} else {
			if(offset == 0) { // end of carousel

			} else {
				$(carousel).data("is_animating", true);
				$(carousel).data("carousel_offset", offset-1);
				$(carousel).siblings(".carouselItems").find("ul")
					.css("position", "absolute")
					.animate({ left : (offset-1)*item_width*-1}, "normal", null, function(){
						$(carousel).data("is_animating", false);
					});
			}
		}
	}

	// Loads carousel data and stores on each object
	$("ul.carouselNav").each(function(){
		var carousel_items = $(this).siblings(".carouselItems");

		var item_width = $(carousel_items).find("li:first").outerWidth(true);
		var carousel_width = $(carousel_items).width();

		var carousel_length = $(carousel_items).find("li").length;
		var carousel_visible_items = Math.ceil(carousel_width / item_width);

		$(this)
			.data("is_animating", false)
			.data("item_width", item_width)
			.data("carousel_offset", 0)
			.data("carousel_length", carousel_length)
			.data("carousel_visible_items", carousel_visible_items);
	});

	// Tries to load first gallery image
	if ($("#galeriaNav #thumbsWrapper li:first").length > 0) {
		arr = window.location.href.split('#');
		arr[1] = arr[1] || 1;
		sel_index = (arr[1] - 1);

		selectGalleryImg(sel_index);
	}

	// Handles clicks on gallery items
	$("#galeriaNav #thumbsWrapper ul a").click(function(){
		var index = $("#galeriaNav #thumbsWrapper li").index($(this).parents("li"));
		selectGalleryImg(index);
		return false;
	});

	// Handles clicks on carousel nav
	$("#galeriaNav .carouselNav li a").click(function(){
		if(!$(this).parents(".carouselNav").data("is_animating")) {

			var selected = $(this).parents(".carouselNav").siblings(".carouselItems").find("li").index($(".active"));

			if($(this).parents("li").is(".ant") && !$(this).parents("li").is(".disabled")){
				if(selected > 0) {
					//selectGalleryImg(selected-1);
				}
				scrollCarousel($(this).parents(".carouselNav"), "left");
			} else if (!$(this).parents("li").is(".disabled")) {
				if(selected < $(this).parents(".carouselNav").data("carousel_length")-1) {
					//selectGalleryImg(selected+1);
					//$($("#thumbsWrapper").siblings(".carouselNav")).find("li").removeClass("disabled");
				}
				scrollCarousel($(this).parents(".carouselNav"), "right");
			}
		}

		return false;
	});

	// Handles clicks on carousel hover nav
	$("#galeriaHoverNav a").click(function(){
		var carousel = $("#galeriaNav .carouselNav");

		if(!$(carousel).data("is_animating")) {
			var selected = $(carousel).siblings(".carouselItems").find("li").index($(".active"));

			if($(this).parents("li").is(".ant") && !$(this).parents("li").is(".disabled")){
				if(selected > 0) {
					selectGalleryImg(selected-1);
				}
				scrollCarousel($(carousel), "left");
			} else if (!$(this).parents("li").is(".disabled")) {
				if(selected < $(carousel).data("carousel_length")-1) {
					selectGalleryImg(selected+1);
				}
				scrollCarousel($(carousel), "right");
			}
		}

		return false;
	});
});

function setHeaderNavigation(){
    var windowHref = window.location.href

    $("#nav li").each(function(){
      href = $(this).find("a").attr("href");

      if(href == "/"){
        if(windowHref.replace(site_host, "") == href){
          $(this).find("a").addClass("selected");
        }
      } else {
        if(windowHref.replace(site_host, "").substring(0, href.length).replace(/\//g, "") == href.replace(/\//g, "")) {
          $(this).find("a").addClass("selected");
          return
        }
      }
    });
}


