isBigImageOpen = false;
largeImgIndex = 0;
$(document).ready(function(){
/*
	$("#thumbnail li a").click(function(){
		var large_path = $(this).attr('href');
		var large_alt = $(this).attr('title');
		var large_img_index = $("> #ident", this).html() - 1;
		photo_number = large_img_index;
		var img = document.createElement('img');
		
		crossFade(large_path, large_img_index );
		return false;
	});

	$("#large>img").load(function(){$("#large>img:hidden").fadeIn("slow")});
*/	
	$("#change_to_previous").click(function(){
		change_picture('prev');
	});
	$("#change_to_next").click(function(){
		change_picture('next');
	});

	$('#thumbnail li img').hoverIntent(function(e){
		var large_img_index = $("+ #ident", this).html() - 1;
		//photo_number = large_img_index;
		showBigImage(e, large_img_index);
		return false;
	}, 
	function() {
		hideBigImage();
	}
	);

});

function showBigImage(e, image_index) {

	//if(isBigImageOpen != false) 
	//hideBigImage();

	new_src = getImegeSrc(image_index);
	var offset = (window.pageYOffset) ? window.pageYOffset : document.body.scrollTop;
	// get image's size
	image_width = photos_info[image_index][6];
	image_height = photos_info[image_index][7];



	var pos_x = e.clientX;
	var pos_y = e.clientY + offset;
    var win_width = document.body.clientWidth + document.body.scrollLeft;
    var win_height = document.body.clientHeight + document.body.scrollTop;	
	
	box_width = image_width + 10;
	box_height = image_height;
	
	if ((pos_x + box_width) > win_width )
		pos_x = pos_x - box_width - 10;
	else
		pos_x = pos_x + 10;
	if ((pos_y + box_height) > win_height )
		pos_y = pos_y - box_height - 10;
	else
		pos_y = pos_y + 10;	
	
	$('#preview_image_box').fadeOut(function(){
		// set new img src
		$('#preview_image').attr({"src": src_img});
		$('#preview_image_box').css({'top': pos_y, 'left': pos_x, 'z-index': '9999999'}).fadeIn();
	});
	isBigImageOpen = true;
}

function hideBigImage() {
	$('#preview_image_box').fadeOut();
}

function getImegeSrc(index) {
	src_img = url_prefix+'Upload/'+photos_info[index][1]+'/'+photos_info[index][2]+'/Photo/'+photos_info[index][3];
	return src_img;
}

function change_picture(way) {
	if(way == 'prev') {
		if(photo_number == 0)
			photo_number = photos_num;
		else
			photo_number = photo_number - 1;
		}
		else if(way == 'next') {
			if(photo_number == photos_num)
				photo_number = 0;
			else
				photo_number = photo_number + 1;
		}
		var src_img = url_prefix+'Upload/'+photos_info[photo_number][1]+'/'+photos_info[photo_number][2]+'/Photo/'+photos_info[photo_number][3];
		crossFade(src_img, photo_number);

		return false;
}

function crossFade(src_img, photo_number) {
		var img = document.createElement('img');
		$(img).attr({"src": src_img, "width": photos_info[photo_number][4],"height": photos_info[photo_number][5]});
		$(img).addClass('details_absolute');
		var left_align = (250 - photos_info[photo_number][4]) / 2;
		$(img).css({"left": left_align });
		$("#large img").fadeOut("2000", function(){
			$("#large img:first").remove();
		});
		$(img).appendTo("#large");
		$("#large>img").fadeIn("2000");
		
		$("#number_of_photo").text(photo_number + 1);
		largeImgIndex = photo_number;
}