$jq.noConflict();

var ieversao = 20;
//Testando Navegador para anular algumas funcionalidades
var ieversao = 20;
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
   ieversao=new Number(RegExp.$1)
}

var chave = "3c195afc177e6583bd7a072167f6efc9";
var segredo = "5f6ad7669d74792c";
var url = "http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=3c195afc177e6583bd7a072167f6efc9&user_id=49407863%40N08&format=json&jsoncallback=?";

$jq(document).ready(function(){
	
	//Fechar flutuante
	document.onkeyup = function(e){ 	
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		if(keycode == 27){ // close
			$jq('#flutuante').slideUp('slow');
		}	
	};

	$jq('#flutuante #bot_fechar').click(function(event){
		$jq('#flutuante').slideUp('slow');
		event.preventDefault();
	});
	
	var total_de_imagens;
	$jq.getJSON(url, function(data){
		//Removendo Animação Gif de carregamento
		$jq("#lista_fotos_flickr #loading_flickr").remove();
		$jq(data.photosets.photoset).each(function(index, elemento){
			//Preparando Imagens
			var imagem 			= $jq("<img />");
			var farm 			= elemento.farm;
			var server 			= elemento.server;
			var primary 		= elemento.primary;
			var secret 			= elemento.secret;
			var id 				= elemento.id;
			var title 			= elemento.title._content;
			var url_montada 	= "http://farm"+farm+".static.flickr.com/"+server+"/"+primary+"_"+secret+"_m.jpg";
			$jq(imagem).attr("src", url_montada);
			$jq(imagem).attr("alt", title);
			//Testando se a imagem é horizontal, se for, definir os tamanhos
			if($jq(imagem).attr('height') < $jq(imagem).attr('width')){
				$jq(imagem).attr("width", "198");
				$jq(imagem).attr("height", "131");
			};
			//$jq(imagem).attr("height", "131");
			//Preparando Link
			var a_tag 			= $jq("<a></a>");
			$jq(a_tag).attr("href", "http://www.flickr.com/photos/integrativa/sets/"+elemento.id+"/");
			$jq(a_tag).attr("target", "_blank");
			$jq(a_tag).attr("title", title);
			$jq(a_tag).append(imagem);
			//Adicionando ação de abrir flutuante com slideshow do album
			if(ieversao>7){
				var div_slide = $jq('<div id="slide_show_album" style="position:fixed; top:0; width:100%; height:100%; background-color:#000"><a href="#" style="color:#FFF; background-color:#535353; padding:2px 10px">Fechar</a><br clear="all"/><iframe align=center src=http://www.flickr.com/slideShow/index.gne?set_id='+id+'&tags=foo frameBorder=0 width=600 scrolling=no height=100%></iframe></div>');
				$jq(a_tag).click(function(event){
					event.preventDefault();
					$jq('body').append(div_slide);
					$jq('#slide_show_album').hide();
					$jq('#slide_show_album').fadeIn();
					$jq('#slide_show_album a').click(function(event){
						event.preventDefault();
						$jq('#slide_show_album').fadeOut('', function(){
							$jq('#slide_show_album').remove();
						});
					});
				});
			}
			var lista 			= $jq('<li></li>').append(a_tag);
			$jq(lista).append('<div class="titulo">'+title+'</div>');
			//Adicionando ao HTML
			$jq("#fotos #lista_fotos").append(lista);
			$jq(lista).fadeIn(2000);
		});
		//$jq("#fotos #lista_fotos").css({'width': data.photos.photo.length*157+'px'});
		total_de_imagens = data.photosets.photoset.length;
	});
	
	var pos_atual = 0;
	$jq('#fotos div.proxima').click(function(event){
		event.preventDefault();
		if(pos_atual <= total_de_imagens-3){
			pos_atual++;
		}
		$jq("#fotos #lista_fotos").animate({'left': '-'+pos_atual*202+'px'});
	});
	$jq('#fotos div.anterior').click(function(event){
		event.preventDefault();
		if(pos_atual > 0){
			pos_atual--;
		}
		$jq("#fotos #lista_fotos").animate({'left': '-'+pos_atual*202+'px'});
	});
});
