HOST_NAME = 'http://' + window.location.hostname + '/';
/* preload images begin */
o_image = new Array();
i_src = new Array();
i_src[0] = HOST_NAME + 'moduls/gallery/images/loader.gif';
i_src[1] = HOST_NAME + 'moduls/gallery/images/back.jpg';
i_src[2] = HOST_NAME + 'moduls/gallery/images/forward.jpg';
i_src[3] = HOST_NAME + 'moduls/gallery/images/close.jpg';
for (i = 0; i < 4; i++) {
    o_image[i] = new Image();
	o_image[i].src = i_src[i];
}
/* preload images end */

$(function () {
	$("body").append(
        '<div id="gallery"></div>' +
        '<div id="loading">' +
			'<a href="javascript:close();">' +
				'<img src="' + HOST_NAME + 'moduls/gallery/images/loader.gif" alt="loading..." border="0" />' +
			'</a>' +
		'</div>' +
		'<div id="shadow"></div>'
	);
	$("#shadow").click(close);
});

var time_int;

function gallery(image, id)
{
	$("#gallery").hide();
	$("object").css("visibility", "hidden");
	/* show shadow begin*/
	shadowHeight = $(document).height();
	if (shadowHeight < $(window).height())
	    shadowHeight = $(window).height();
	$("#shadow").css("height", shadowHeight + "px");
	$("#shadow").show();
	/* show shadow end*/
	/* show loader begin*/
	$("#loading").css("top", $(window).height() / 2 + $(document).scrollTop() - 8 + 'px');
	$("#loading").fadeIn("slow");
	/* show loader end*/
	/* load image begin */
	img = new Image();
 	img.src = image;
 	$(img).load(function () {
		ShowImage(img, id);
	});
	/* load image end */
}

function ShowImage(img, id)
{
	var ImgWidth  = img.width;
	var ImgHeight = img.height;
	var ImgBorder = 25;
	if (ImgHeight > $(window).height() - 60) {
		ImgWidth = (($(window).height() - 60) / ImgHeight) * ImgWidth;
	    ImgHeight = $(window).height() - 60;
	}
	if (ImgWidth > $(window).width() - 10) {
		ImgHeight = (($(window).width() - 10) / ImgWidth) * ImgHeight;
		ImgWidth = $(window).width() - 10;
	}
	// begin next image and previous image
	is_next_image = false;
	is_prev_image = false;
	if (id != 0) {
		try {
			var next_element = document.getElementById('image' + (id + 1));
			next_image = next_element.getAttribute("src");
			next_image = next_image.replace('thumbs', 'images');
			is_next_image = true;
		} catch (e) { }

		try {
			var prev_element = document.getElementById('image' + (id - 1));
			prev_image = prev_element.getAttribute("src");
			prev_image = prev_image.replace('thumbs', 'images');
			is_prev_image = true;
		} catch (e) { }
	}
	// end next and previous image

	if ($("#gallery_checkbox").is(":checked"))
	    checked = true;
	else
	    checked = false;
	    
	ImgWidth_half  = Math.round(ImgWidth / 2);
	ImgHeight_half = Math.round(ImgHeight / 2);

	$("#gallery").css("top", $(window).height() / 2 + $(document).scrollTop() - ImgHeight_half - ImgBorder + 'px');
	$("#gallery").css("margin-left", '-'+ImgWidth_half+'px');
	
	$("#gallery").html('<img class="g_image" src="'+img.src+'" width="' + ImgWidth + '" height="' + ImgHeight + '" />');
	$("#gallery").append('<div id="gallery_auto_list"><input id="gallery_checkbox" type="checkbox" /><label for="gallery_checkbox">AutoList</label></div>');
	if (checked)
		$("#gallery_checkbox").attr("checked", true);
	$("#gallery").append(
		'<a class="gallery_close" href="javascript:close();">' +
			'<img border="0" src="' + HOST_NAME + 'moduls/gallery/images/close.jpg" alt="close" />' +
		'</a>');
	if (is_next_image) {
		$("#gallery").append(
			'<a class="gallery_button2" href="javascript:gallery(\'' + next_image + '\', ' + (id + 1) + ');">' +
				'<img src="' + HOST_NAME + 'moduls/gallery/images/forward.jpg" alt="back" border="0" />' +
			'</a>');
	}
	if (is_prev_image) {
		$("#gallery").append(
			'<a class="gallery_button1" href="javascript:gallery(\'' + prev_image + '\', ' + (id - 1) + ');">' +
				'<img src="' + HOST_NAME + 'moduls/gallery/images/back.jpg" alt="forward" border="0" />' +
			'</a>');
	}
	// set timeout begin
	if (checked == true && is_next_image == true) {
		clearTimeout(time_int);
	    time_int = setTimeout("gallery('" + next_image + "', " + (id + 1) + ")", 5000);
	}
	
	$("#gallery_checkbox").change( function () {
		if ($("#gallery_checkbox").is(":checked") && is_next_image == true) {
			clearTimeout(time_int);
			time_int = setTimeout("gallery('" + next_image + "', " + (id + 1) + ")", 5000);
		}
		else {
			clearTimeout(time_int);
		}
	});
	// set timeout end
	
	$("#loading").fadeOut("slow", function () {
		$("#gallery").fadeIn("slow");
	});
}

function close()
{
	clearTimeout(time_int);
	$("#gallery").empty();
	$("#gallery").hide();
	$("#loading").hide();
	$("#shadow").hide();
	$("object").css("visibility", "visible");
}
