//Script de acoes, auxilio em posicionamento, slideshow
//Site: eusou.jp
//Autor do script: Rochester Oliveira (rochesterj [at] gmail.com)
//Layout e planejamento da interacao: Joao Paulo Teixeira (joaopaulots [at] gmail.com)
//Ultima revisao: 23/08/2010

//aplica o masonry a div principal do site
function organizaContent() {
	var teste = teste;
//	alert("oi");
/*
	$('#content').masonry({
		columnWidth: 10,
		// Width in pixels of 1 column of your grid.
	    itemSelector: '.box:visible' ,
		animate: true,
		saveOptions: true
	}); */
}
function mudaTamanho(box) {
	var seletor = ".box" + box;
	var boxG = "G" + seletor;
	var galeria = "#galleria" + box;
	var operacao = $.cookie(seletor);
	var antes = seletor + " .antes";
	var depois = seletor + " .depois";
	var pai = $(seletor).parent().attr("class");
	var width = 0;
	var height = 0;
	//prepara scroll
	var offset = $(seletor).offset();
	var left = offset.left;
	var top = offset.top;
	var viewportH = $(window).height();
	var viewportW = $(window).width();
	//pra verificar se nao estamos no showall
	var todos = $.cookie("todos");
	if (operacao != null) {
		//ja existe, logo vamos DIMINUIR o box, desaparecer com o .depois e desmarcar o cookie
		//pegando possiveis widths
		switch(box) {
			case 1:
			case 2:
			case 6:
			case 7:
			case 9:
			case 11:
			case 13:
				width = 150;
				break;
			case 3:
			case 5:
			case 12:
				width = 230;
				break;
			case 4:
			case 8:
				width = 300;
				break;
			case 10:
				width = 200;
				break;
			default:
				width = "0";
		}
		//pegando possiveis heights
		switch(pai) {
			case 'linha1':
			case 'linha4':
				height = 150;
				break;
			case 'linha2':
			case 'linha3':
				height = 200;
				break;
			default:
				height = "0";
		}
		if (box == 1 || box == 2 || box == 3) {
		  $(seletor).css('marginTop','auto');
		}
		
		$(seletor).animate({ width: width, height: height}, function() {
																			$('body').scrollTo( 0, 500, { easing:'swing', queue:true, axis:'xy' } );
																			$("#content").css('width', function(index) { return $(this).outerWidth() - 700;});
																			$(".blocos").css('width', function(index) { return $(this).outerWidth() - 700;});
																			}); //seta tudo
		$(depois).animate({ opacity: 'hide'}, function() { $(antes).animate({ opacity: 'show'}); }); //esconde os itens de "depois" e mostra os itens de "antes"
		//justando a content e blocos
		
		
		
		$.cookie(seletor, null);
	}
	else {
		//ainda nao existe, logo vamos AUMENTAR o box, dar scroll, mostrar o .depois e setar um cookie
		//justando a content e blocos
		$("#content").css('width', function(index) { return $(this).outerWidth() + 700;});
		$(".blocos").css('width', function(index) { return $(this).outerWidth() + 700;});
		//setando possiveis widths
		switch(box) {
			case 1:
			case 2:
			case 6:
				width = 700;
				break;
			case 3:
			case 4:
				width = 850;
				break;
			case 5:
				width = 780;
				break;
			case 7:
				width = 650;
				break;
			case 8:
			case 12:
				width = 800;
				break;
			case 9:
			case 13:
				width = 750;
				break;
			case 10:
				width = 310;
				break;
			case 11:
				width = 305;
				break;
			default:
				width = "0";
		}
		
		//pegando possiveis heights
		switch(pai) {
			case 'linha1':
			case 'linha2':
			case 'linha3':
			case 'linha4':
				height = 560;
				break;
			default:
				height = "0";
		}
		//seta a galeria
    if ($.cookie(boxG) == null) { 
    
      $(galeria).galleria({
  			//thumb_crop: true, // crop all thumbnails to fit
  			transition: 'fade', // crossfade photos
  			transition_speed: 700, // slow down the crossfade
  			autoplay: false,
  			height: 560,
  			show_info: false,
  			show_counter: false
          });
      $.cookie(boxG, '1');
      
    }
		
		//calcula scroll vertical
		viewportH = viewportH - height;
		if (viewportH > 1 ) {
			//o box nao eh maior que o view, ai posso centralizar ele
			viewportH = viewportH/2;
			top = top - viewportH;
			if (top < 0){
				top = 0;
			}
		}
		//calcula scroll horizontal
		viewportW = viewportW - width;
		if (viewportW > 1 ) {
			//o box nao eh maior que o view, ai posso centralizar ele
			viewportW = viewportW/2;
			left = left - viewportW;
			if (left < 0){
				left = 0;
			}
		}
		
		$(seletor).animate({ width: width, height: height}, function() { if (todos != 1) { $.scrollTo( {top: top, left: left}, 500); }}); //seta tudo
		
		$(antes).animate({ opacity: 'hide'}, function() { $(depois).animate({ opacity: 'show'}); }); //esconde os itens de "antes" e mostra os itens de "depois"
		
		$.cookie(seletor, "1"); //seta cookie
	}

}
function showAll() {
	var todos = $.cookie("todos");
//testa se ja estamos no show all, se tiver so da reload, se nao ai executa tudo
	if (todos != 1) {
		var i = 1;
		$(".linha2").css('clear', 'none');
		$(".linha3").css('clear', 'none');
		$(".linha4").css('clear', 'none');
		while (i < 14) {
			$.cookie("todos", 1);
			mudaTamanho(i);
			i++;
		}
		$("#showAll").text("Hide All");
	}
	else {
		$.cookie("todos", null);
		window.location.reload();
	}
}

$(document).ready(function() {
	organizaContent();
	//zerar os cookies no load
	var i = 1;
	var box = "";
	var boxG = "";
	while (i < 14) {
		box = ".box"+i;
    boxG = "G" + box;
		$.cookie(box, null);
		$.cookie(boxG, null);
		i++;
	}
	$.cookie("todos", null);
});
