var filter = "";
var hideMain = true;
var hideMainClick = false;
function reloadPictures(id,start){
	$("#pictures").empty();
	$("#picture-nav").empty();
	$.getJSON(webroot_path+"home/busca_imagens_ajax/0/"+id, function(data){
		$.each(data,function(i,item){
			src = webroot_path+'files/imagens/'+item.Imagem.imagem;
			legenda = item.Imagem.legenda;
			if(item.Imagem.legenda==null)
				legenda = '';
			$("#pictures").append('<img src="'+src+'" alt="'+legenda+'" />');
		});
		
		$("#pictures").cycle({ 
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0,
		    startingSlide: start,
		    next:   '#picture-right', 
		    prev:   '#picture-left',
		    pager:  '#picture-nav'
		});
	});
	$('.check-contato').attr('src',webroot_path+'img/list.png');
	$("#contato").fadeOut();
}

function reloadCategories(filter){
	$("#gallery").empty();
	var htmlCategories = new Array();
	
	$.getJSON(webroot_path+"home/busca_imagens_ajax/"+filter, function(data){
		$.each(data,function(i,item){
			category = item.Imagem.galeria_id;
			posicao = item.Galeria.posicao;
						
			src = webroot_path+'files/imagens/thumb/'+item.Imagem.imagem;
			
			legenda = item.Imagem.legenda;
			id = item.Imagem.id;
			if(item.Imagem.legenda==null)
				legenda = '';
			
			if(!htmlCategories[category]){
				htmlCategories[category] = new Array();
				htmlCategories[category]['conteudo'] = '';
			}
			
			htmlCategories[category]['conteudo'] += '<a href="javascript:void(0)" title="'+item.Galeria.nome+'"><img src="'+src+'" id="picture-'+id+'" alt="'+legenda+'" class="picture" /></a>';
			htmlCategories[category]['posicao'] = posicao;
			htmlCategories[category]['id'] = item.Imagem.galeria_id;
		});

		
		htmlCategories.sort(function(a,b) {
			return a['posicao']-b['posicao'];
		});
		
		$.each(htmlCategories, function(category,html){
			if(html&&html['conteudo']){
				$("#gallery").append('<div class="category" id="category-'+html['id']+'"><div>'+html['conteudo']+'</div><div class="category-name"></div></div>');
			}
		});
		
		
		$(".category").hover(function(){
			$(this).animate({width:210},'fast');
			
			 $(this).children('.category-name').html($(this).children().children().eq(0).attr('title'));
			 $(this).children('.category-name').fadeIn('fast');
			 $(this).children('.category-name').css('top',$(this).position().top+25);
			
			leftAc = $(this).children().children().size()*50;
			if(leftAc>230) leftAc = 230;

			
			$(this).children('.category-name').css('left',$(this).position().left+leftAc);
			if(hideMain)
				$('#main').hide();
		},function(){
			$(this).animate({width:125},'fast');
			$(this).children('.category-name').fadeOut('fast');
			
			$('#main').show();
			hideMain = true;
		});
		
		
		$("#gallery").css('margin-top',0);
		
		reloadPictures($("#gallery div").eq(0).attr('id').replace('category-',''),0);
	});
}

$("document").ready(function(){
	reloadCategories('');
	
	$("#picture-up").click(function(){
		if(parseInt($("#gallery").css("marginTop").replace("px",''))<0)
			$("#gallery").animate({marginTop:"+=42px"},"fast");
	});
	
	$("#picture-down").click(function(){
		if($("#gallery").height()>$("#gallery-container").height()&&(-1*parseInt($("#gallery").css('margin-top').replace("px",'')))<($("#gallery").height()-$("#gallery-container").height()))
			$("#gallery").animate({marginTop:"-=42px"},"fast");	
	});
	
	$(".check-categoria").parent('li').click(function(){
		id = $(this).children('img').attr('id').replace("category-","");
		if(filter.indexOf(id)==-1){
			if(filter!='')
				filter += ',';
			filter += id;
			$(this).children('img').attr('src',webroot_path+'img/list_ck.png');
		}
		else {
				filter = filter.replace(','+id,'').replace(id,'');
				if(filter.charAt(0)==',')
					filter = filter.slice(1);
			$(this).children('img').attr('src',webroot_path+'img/list.png');
		}
		reloadCategories(filter);
	});
	
	/*$("#categories>ul li").hover(function(){
		$("#main").hide();
	});*/
	
	$(".check-contato").parent('li').click(function(){
			if($("#contato").css('display') == 'none') {
				$('.check-categoria').attr('src',webroot_path+'img/list.png');
				$(this).children('img').attr('src',webroot_path+'img/list_ck.png');
				$('#main').hide();
				$("#social-icons").hide();
				$('#contato').show();
				$('#contato').load(webroot_path+'contato/');
			}
			else {
				$('.check-contato').attr('src',webroot_path+'img/list.png');
				$("#contato").fadeOut();
			}
	});
	$(".fechar-contato").live('click',function(){
		$('.check-contato').attr('src',webroot_path+'img/list.png');
		$("#contato").fadeOut();
	});
	
	
	$(".category .picture").live('click',function(){
		$('#main').css('visibility','visible');
		$('#social-icons').css('visibility','visible');
		
		id = $(this).parent().parent().parent().attr("id").replace("category-","");
		
		start = $(this).parent().prevAll().length;
		
		reloadPictures(id,start);
		hideMain = false;
		$('#main').fadeIn();
		$('#social-icons').show();
	});
	
	/*
	$('#main').hover(function(){
		hideMainClick = false;
		$('body').click(function(){
				$('#main').show();
		})
	});
	
	$('#picture-nav').live('click hover',function(){
		hideMainClick = false;
	});	
	
	$('#main').mouseout(function(){
		hideMainClick = true;
		$('body').click(function(){
			if(hideMainClick)
				$('#main').hide();
		})
	});
	*/

});




function enviar_contato(){
	$('#table-form').hide();
	$('#table-msg').show();
	$('.msg').html('<img src="'+webroot_path+'img/ajax-loader.gif" />');
	var dados = 'nome='+$('#nome').val()+'&telefone='+$('#telefone').val()+'&email='+$('#email').val()+'&assunto='+$('#assunto').val()+'&mensagem='+$('#mensagem').val();
	$.ajax({
	  type: 'POST',
	  url: webroot_path+'contato/index',
	  data: dados,
	  success: function(data){
			$('.msg').html(data);
		}
	});
}
