function setScrollbar() {
	$("#mcs5_container").mCustomScrollbar("horizontal",500,"easeOutCirc",0,"fixed","yes");
}

var deduction = 40;

var this_gallery;
var gallery = $("#gallery").find('.content p');
var count_imgLoaded = 0;
var total_img = 0;
var maxContainerWidth = 0;

$("#address").find("div").css('opacity', '0');

$("#address").hover(function() {
	$("#address").find("div").animate({'opacity': '1'}, 300);
}, function() {
	$("#address").find("div").animate({'opacity': '0'}, 300);
});

var get;
function getGallery(portfolio) {
	if (typeof get == 'object') {
		get.abort();
	}

	if (typeof portfolio == 'undefined') {
		portfolio = location.hash.replace('#!/', '');

		if (portfolio == '') {
			portfolio = '1';
		}
	}

	$("#portfolio").find('li img').each(function() {
		var $this = $(this);
		var this_src = $this.attr('src');
		if (this_src.indexOf('_on.jpg') != -1) {
			$this.attr('src', this_src.replace('_on.jpg', '_off.jpg'));
		}

		if (this_src == 'images/loading.gif') {
			$this.attr('src', 'images/nav/'+$this.parent().attr('href').replace('#!/', '')+'_off.jpg');
		}
	});

	var maxHeight = screen.availHeight;
	var deductHeight = $("#top").height() + $("#category").height() + $("#copyright").height();
	var max_height = Math.floor(maxHeight - deductHeight);
	var screenHeight = $(window).height() - deduction;
	var actual_height = Math.floor(screenHeight - deductHeight);

	$("#nav_"+portfolio).attr('src', 'images/loading.gif');

	get = $.get('php-bin/get_album.php', {
		folder: portfolio,
		height: max_height,
		actual_height: actual_height
	}, function (data) {
		gallery.html('');
		this_gallery = data;
		count_imgLoaded = 0;
		total_img = this_gallery.length-1;
		maxContainerWidth = data[total_img].total_width;

		$("#gallery").find(".dragger").css('left', '0');
		$("#gallery").find(".container").css('left', '0').width(maxContainerWidth+'px');
		setScrollbar();
		load_img();
	}, 'json');
}

function load_img() {
	if (count_imgLoaded != this_gallery.length-1) {
		if (typeof this_gallery[count_imgLoaded] != 'undefined') {
			count_imgLoaded = count_imgLoaded + 1;

			gallery.append('<div style="'+this_gallery[count_imgLoaded-1].size+';display:table-cell;vertical-align:middle;border:1px dashed #ccc;margin-left:5px" id="img-'+count_imgLoaded+'"><img src="images/loading.gif" style="width:32px;height:26px;margin:0 auto;display:block;"/></div>')
			$("#img_loading").html('<img src="'+this_gallery[count_imgLoaded-1].src+'" style="'+this_gallery[count_imgLoaded-1].size+'" onload=\'img_loaded('+count_imgLoaded+', "'+this_gallery[count_imgLoaded-1].src+'", "'+this_gallery[count_imgLoaded-1].size+'");\' />');	
		}
	}
}

var finishLoaded = false;
function img_loaded(count, src, size) {
	$("div#img-"+count).fadeOut('fast', function() {
		$(this).replaceWith('<img src="'+src+'" style="'+size+'" id="img-'+count_imgLoaded+'"/>').fadeIn('slow');
		lastCount = parseInt(count_imgLoaded)-1;
		$("#img-"+lastCount).width('auto');

		if (count_imgLoaded == total_img) {
			finishLoaded = true;
			$("#img-"+count_imgLoaded).addClass('last').width('auto');

			var portfolio = location.hash.replace('#!/', '');
			if (portfolio == '') {
				portfolio = '1';
			}

			$("#nav_"+portfolio).attr('src', 'images/nav/'+portfolio+'_on.jpg');

			var containerWidth = 0;
			$("#gallery").find("img").each(function() {
				containerWidth = containerWidth + $(this).width() + 5;
			});
			$("#gallery").find('.container').width(containerWidth+'px');
			setScrollbar();
		}

		load_img();
	});
}

function resizeGallery() {
	var deductHeight = $("#top").height() + $("#category").height() + $("#copyright").height();
	var screenHeight = $(window).height() - deduction;
	var galleryNewHeight = screenHeight - deductHeight;
	var galleryHeight = $("#gallery").height();

	//$("#gallery").height(galleryNewHeight+'px');
	$("#gallery img").height(galleryNewHeight+'px');

	if (finishLoaded) {
		var containerWidth = 0;
		$("#gallery").find("img").each(function() {
			containerWidth = containerWidth + $(this).width() + 5;
		});

		//containerWidth = containerWidth - 5;

		/*if (containerWidth > maxContainerWidth) {
			containerWidth = maxContainerWidth;
		}*/

		$("#gallery").find(".container").height(galleryNewHeight+'px');
		$("#gallery").find('.container').width(containerWidth+'px');
		setScrollbar();
	}
}

$(document).ready(function() {

var screenHeight = $(window).height() - deduction;
var deductHeight = $("#top").height() + $("#category").height() + $("#copyright").height();
var actual_height = Math.floor(screenHeight - deductHeight);
$("#gallery").find(".container").height(actual_height+'px');

resizeGallery();
$(window).resize(resizeGallery);

getGallery();
$("#category a").click(function() {
	var $this = $(this);

	if (location.hash != $this.attr('href')) {
		getGallery($this.attr('href').substring(3));
	}
});

});

$(window).load(function() {
//setScrollbar();
//$("#gallery").height($(window).height()-100);
//var container_top = parseInt(($("#gallery").height()-470)/2);
//$("#mcs5_container").css('top', container_top+'px');

//$("#gallery img").width('auto');
});
