var $currentPhoto;
var $currentPage;
var $totalPages;
var $slideShow = false;
$(window).ready(function (){
	$("#comment-input").focus( function() { 
		if($(this).val() == "Digite aqui o seu produtivo comentário."){
			$(this).val('');
			$(this).css('color','#000');
		}
	} ).blur( function() {
		if($(this).val() == ""){
			$(this).val('Digite aqui o seu produtivo comentário.');
			$(this).css('color','#888');
		}
	});
	$('#submit-comment').click(function(){
		if($logado) {
			if($('#comment-input').val() != '' && $('#comment-input').val() !== 'Digite aqui o seu produtivo comentário.'){
				$('#loader-post').show();
				$.post("post-comment.php",{texto : $('#comment-input').val(), fotoId : $currentPhoto.attr('rel')},function(){
					$('#comments-container').load('get-comments.php?id='+$currentPhoto.attr('rel'));
					$("#comment-input").val('Digite aqui o seu produtivo comentário.');
					$("#comment-input").css('color','#888');
					$('#loader-post').hide();
				});
			}
		} else {
			alert('Você precisa estar logado para fazer isto.');
		}
	});
	shortcut.add("Ctrl+Right",function() {
		loadThumbs($currentPage+1);
	},{disable_in_input : true});
	shortcut.add("Ctrl+Left",function() {
		loadThumbs($currentPage-1);
	},{disable_in_input : true});
	shortcut.add("Right",function() {
		nextFoto();
	},{disable_in_input : true});
	shortcut.add("Left",function() {
		prevFoto();
	},{disable_in_input : true});
	shortcut.add("Space",function() {
		toggleSlideShow();
	},{disable_in_input : true});
	
	

});
function drag(){
	var $gallery = $('#box-thumbs');
	$('a',$gallery).draggable({
		cancel: 'a.ui-icon',// clicking an icon won't initiate dragging
		revert: 'invalid', // when not dropped, the item will revert back to its initial position
		containment: $('#demo-frame').length ? '#demo-frame' : 'document', // stick to demo-frame if present
		helper: 'clone',
		cursor: 'move'
	});
	
	$('#box-pack').droppable({
		accept: '#box-thumbs a',
		drop: function(ev, ui) {
		 $.get('addzip.php',{item:ui.draggable.attr('rel')},function(data){
			 if(data.added == 1){
				 $('.empty-pack').hide();
				 $('#hide-pack').show();
				 $('#box-pack-list').append('<a href="Album/'+data.url+'/'+data.nome_arquivo+'" rel="'+data.id+'" ><img width="56" height="56" src="DJCrop.php?w=56&h=56&i=gals/'+data.pasta+'/'+data.nome_arquivo+'"></a>');
			 }
         },'json');
		}
	});

	
}
function hidezip(){
	$('.empty-pack').show();
	$('#box-pack-list').html('');
	$('#hide-pack').hide();
}
function loadThumbs(page){
	$('#box-thumbs').load('getThumbs.php?id='+$galleryId+'&pag='+page,function(){
		drag();
		getFoto($('#box-thumbs-list a').attr('href'),$('#box-thumbs-list a').attr('rel'));
		$('#box-thumbs-list a').click(function() {
			getFoto($(this).attr('href'),$(this).attr('rel'));
		});
	});
}

function loadThumbsPermalink(page){
	$('#box-thumbs').load('getThumbs.php?id='+$galleryId+'&pag='+page,function(){
		drag();
		$('#box-thumbs-list a').click(function() {
			getFoto($(this).attr('href'),$(this).attr('rel'));
		});
		afterLoadPermalink();
	});
}

function loadThumbsLastPhoto(page){
	$('#box-thumbs').load('getThumbs.php?id='+$galleryId+'&pag='+page,function(){
		getFoto($('#box-thumbs-list a:last-child').attr('href'),$('#box-thumbs-list a:last-child').attr('rel'));
		$('#box-thumbs-list a').click(function() {
			getFoto($(this).attr('href'),$(this).attr('rel'));
		});
	});
}

function getFoto(src,id){
	$("#bar-slideshow").stop();
	$("#bar-slideshow").css('width','0px');
	if($('#foto').attr('src') != src){
		$img = $('<img>');
		$("#canvas").hide();
		$('#foto-case').css('background-image','url(img/loader-foto.gif)');				
		$img.load( function(){
			$('#canvas').remove();
			//$('#toAnnotate').attr('src', this.src);
			$("#show-case-foto").annotateImage({
				getUrl: "jia/get.php?id="+id,
				saveUrl: "jia/save.php?fotoId="+id,
				deleteUrl: "jia/delete.php",
				editable: $editable,
				width : this.width,
				height : this.height
			});
			$currentPhoto = $('#box-thumbs-list a[href="'+src+'"]');
			if($slideShow){
				playSlideShow();
			}
			$split = src.split('/');
			$('#permalink-input').val('http://vibecapixaba.com.br/v2/Album/'+$galleryURL+'/'+$split[$split.length-1]);
			$('#foto').attr('src', src);
			$('#foto-case').css('background-image','');
			$('#foto').fadeIn();
			$('#comments-container').html('<br><center><img src="img/loader-comments.gif"></center><br>');
			$('#comments-container').load('get-comments.php?id='+id);
			generateRating();
		})
		$img.attr('src',src);
	}				
}
function nextFoto(){
	if($('#box-thumbs-list a:last-child').attr('href') == $currentPhoto.attr('href')){
		if($currentPage != $totalPages){
			loadThumbs($currentPage+1);
		}
	} else {
		$currentPhoto = $currentPhoto.next();
		getFoto($currentPhoto.attr('href'),$currentPhoto.attr('rel'));
	}
}
function prevFoto(){
	if($('#box-thumbs-list a:first-child').attr('href') == $currentPhoto.attr('href')){
		if($currentPage != 1){
			loadThumbsLastPhoto($currentPage-1);
		}
	} else {
		$currentPhoto = $currentPhoto.prev();
		getFoto($currentPhoto.attr('href'),$currentPhoto.attr('rel'));
	}
	
}
function toggleSlideShow(){
	if(!$slideShow){
		playSlideShow();
	} else {
		stopSlideShow();
	}
}
function playSlideShow(){
		$slideShow = true;
		$("#bar-slideshow").animate({width: '634px'}, 5000,'',function(){
			nextFoto();
		});
}
function stopSlideShow(){
	$("#bar-slideshow").stop();
	$("#bar-slideshow").css('width','0px');
	$slideShow = false;
}
function closeHelp(){
	$('.help-bar').hide('slow');
}
function toggleHelp(){
	$('.help-bar').toggle('slow');
}
function refreshComments(){
	$('#comments-container').html('<br><center><img src="img/loader-comments.gif"></center><br>');
	$('#comments-container').load('get-comments.php?id='+$currentPhoto.attr('rel'));
}
function generateRating(){
	$("#rat").stars({
		inputType: "select",
		oneVoteOnly: true,
		callback: function(ui, type, value)
		{
			if($logado){
				$.post("vote.php", {rate: value,id:$currentPhoto.attr('rel') }, function(json)
				{
					ui.select(Math.round(json.rating));
	
				}, "json");
			} else {
				alert('Você precisa estar logado para fazer isto.');
				$pontos = 0;
				$.get('getRating.php',{id:$currentPhoto.attr('rel')},function(json){
					$pontos = json.rating;
					$("#rat").stars("select", $pontos);
					$("#rat").stars('enable');
				},'json');
			}
		}
	});
	$pontos = 0;
	$.get('getRating.php',{id:$currentPhoto.attr('rel')},function(json){
		$pontos = json.rating;
		$("#rat").stars("select", $pontos);
		$("#rat").stars('enable');
	},'json');
	
	
}