var full_img_counter = 0;
var thumb_counter = 0;
var img_index = 0;
var time; //ms

$(function(){
	$('.thumbnails img').mouseover(function(){
		$(this).fadeTo('fast', 0.5);
		//$(this).css({ 'text-align': 'center' });
	}).mouseout(function(){
		$(this).fadeTo('fast', 1);
		//$(this).css({ 'text-align': 'center' });
	});
	
	$('.thumb_page_no').mouseover(function(){
		$(this).css('background', 'white');
	}).mouseout(function(){
		$(this).css('background', '#222');
	}).click(function(){
		$('img.'+ $(this).value() - 1).hide('slow');
		$('img.'+ $(this).value()).show('slow');
	});
		
});
	
	var img_no = new Array();
	img_no[0] = 1; //First image loaded.
	
	var thumb_srcs = new Array();
	function collectThumbSrc(thumb_src) {
		thumb_srcs.push(thumb_src);
		//thumb_srcs = phpthumb_srcs;
	}
	function preloadThumbnail() {
		loading();
		for(var j = 0; j < thumb_srcs.length; j++) {
			var thumb = new Image();
			thumb.src = thumb_srcs[j];
			thumb.onload = function() { //when img loading complete
				thumb_counter++;
				if(thumb_counter == thumb_srcs.length && full_img_counter == full_img_srcs.length) {
					loaded();
				}
			}
			
		}

	}
	
	var full_img_srcs = new Array();
	function collectFullImgSrc(src) {
		full_img_srcs.push(src);
	}
	
	function preloadFullImg(full_img_src) {
		var full_img = new Image();
		full_img.onload = function() {
			full_img_counter++;
			$('#modal td#img_no').html(full_img_counter + '/' +full_img_srcs.length);
			if (full_img_counter == full_img_srcs.length) {
				loaded();
			}
		};
		full_img.src = full_img_src;
		
	}
	function preloadFullImgs() {
		loading();
		for(var i = 0; i < full_img_srcs.length; i++) {
			preloadFullImg(full_img_srcs[i]);
		}
	}

	function preloadAllImg() {
		//preloadThumbnail();
		preloadFullImgs();
	}
	
	function onOverAlbumLink() {
		$('a#album_link').css({ color: '#555' });
	}
	function onOutAlbumLink() {
		$('a#album_link').css({ color: '#fff' });
	}
	function loading() {
		$('html').css({ overflow: 'hidden'});
		$('#overlay, #modal').show();
	}
	function loaded() {
		$('html').css({ overflow: 'auto'});
		$('#overlay, #modal').hide();
		runSlideImgs(8000); //default time of slide
	}
	
	function showImg(full_img_src, img_arr_index) {
		if(img_arr_index == undefined) { //show by slide
			if($('img#full_img').attr('src') == undefined) { //full_img is no src
				$('img#full_img').attr('src', full_img_src).fadeIn(3000);
			} else {
				
				$('img#full_img').fadeOut(3000, function(){ //fade out old img
					$(this).attr('src',full_img_src).fadeIn(3000);
				});
			}
		} else { //show by click
			clearTimeout(time);
			img_index = img_arr_index; //index of image click
			runSlideImgs(10000); //more delay on clicked img.
			
		}
		//loaded();
		//$('#thumb_no').html('image '+ album_img_i+ ' / '+ album_img_num);
	}
	
	//slide cus photos
	var flag = 0; //for delay clicked img.
	function runSlideImgs(ms) {
		if (flag == 1) { ms = 8000; flag = 0; }
		if (img_index < full_img_srcs.length) {
			showImg(full_img_srcs[img_index]);
			img_index++;
			if (ms == 10000) flag = 1;
			
			time = setTimeout('runSlideImgs('+ ms +')',ms);
		} else { //reload slide
			img_index = 0;
			if (ms == 10000) flag = 1;
			time = setTimeout('runSlideImgs('+ ms +')',ms);
		}
	}
