function photopane(contsel,photosel){
	this.contselector=contsel;
	this.photoselector=photosel;

	$(this.contselector).css('position','relative');
	$(window).resize($.proxy(this.process,this));
	this.process();
}
photopane.prototype.process=function(){
		this.left=0;
		this.col=0;
		this.top=[];
		this.maxheight=0;
		
		$(this.photoselector).each($.proxy(function(i,photo){
			if(this.left>=$(this.contselector).innerWidth()-$(photo).outerWidth(true)){
				this.left=0;
				this.col=0;
			}
			$(photo).css({
				"position":"absolute",
				"top":this.top[this.col]+"px",
				"left":this.left+"px"
			});

			if(!this.top[this.col]){
				this.top[this.col]=0;
			}

			this.top[this.col]+=$(photo).outerHeight(true);
			
			if(this.top[this.col]>this.maxheight){
				this.maxheight=this.top[this.col];
				$(this.contselector).css('height',this.maxheight);
			}
			if(this.left>=$(this.contselector).innerWidth()-$(photo).outerWidth(true)){
				this.left=0;
				this.col=0;
			}else{
				this.left+=$(photo).outerWidth(true);
				this.col++;
				
			}

	},this));
};

$(function (){
	users=new user();
	users.login_submit($('#loginform'));

	$('#btn-register').fancybox({
		onComplete:function (){
			users.reg_submit($('#registerform'));
		}
	});
	$('#logout').click(users.logout);

	profile=new profile();
	$('#album-add').fancybox({
		onComplete:function (){
			profile.album_add_submit($('#albumaddform'));
		}
	});
	
	$('#photo-add').fancybox({onComplete:function (){$.fancybox.resize();}});
	$('.photo-edit').fancybox({onComplete:function (){$.fancybox.resize();}});

	$('#fav-add').fancybox({
		onComplete:function (){
			profile.fav_add_submit($('#favaddform'));
		}
	});
	$("#filesurl").change(function() {
		$("#pseudfile").val($("#filesurl").val());
	})
	new photopane('#new_photos','#new_photos .photo');
	new photopane('#top_photos','#top_photos .photo');

	$('#button-add-friend').click(function(e){
		$.get(e.href);
		$('#button-add-friend').hide();
		$('#friend-already').show();
		$('#button-del-friend').show();
		return false;
	});
	$('#button-del-friend').click(function(e){
		$.get(e.href);
		$('#button-add-friend').show();
		$('#friend-already').hide();
		$('#button-del-friend').hide();
		return false;
	});

	dragon=false;

	$('#vote-slider').css('cursor','pointer');
	$(window).mousemove(function(event){
		if(dragon){
			var x=event.clientX;
			var offset=90;
			var max=10;
			var sliderWidth=$('#vote-slider').width();
			var wholeWidth=$('#photovote').width();

			if((x>offset-Math.floor(sliderWidth/2)) && (x<offset+wholeWidth-Math.floor(sliderWidth/2))){
				$('#vote-slider').css('left',((x-offset))+'px');
				$('#vote-current').css('width',x-offset+Math.floor(sliderWidth/2)+'px');
			
				voteresult=((x-offset+Math.floor(sliderWidth/2))/wholeWidth)*max;
				voteresult=Math.floor(voteresult*10)/10;
				$('#vote-slider-indicator').html(voteresult);
			}
		}
	});
	$('#vote-slider').mousedown(function(e){
		dragon=true;
		return false;
	});
	$(window).mouseup(function(){
		if(dragon){
			$.post('/user/vote',{rate:voteresult}, function(){},'json');
		}
		dragon=false;
		
	});
});

jQuery.postJSON=function(url,data,success,context){
	$.ajax({
	type: 'POST',
	url: url,
	data: data,
	success: success,
	dataType: 'json',
	context: context
	});
};




