var loadedimages = new Array();
var imageorder = new Array();
var currentgallery = 0;
var currentimage = 0;

function show_gallery(gallery_id, image_id)
{
	currentgallery = gallery_id;
	currentimage = image_id;
	
	if(currentgallery && currentimage)
	{
		
		//=====================================================
		// CONFIGURATION
		//=====================================================
		var maxwidth = 800;
		var maxheight = 600;
		var minwidth = 150;
		var minheight = 120;
		var bordercolor = "white";
		var borderwidth = 8;
		var background_color = "black";
		var background_opacity = 10;
		var image_background_color = "white";
		var is_infobar = 1;
		var infobar_color = "white";
		var infobar_height = 24;
		var infobar_padding = 8;
		var font_size = "11px";
		var font_color = "#4AABDD";
		var button_color = "transparent";
		var button_opacity = 60;
		var button_font_size = "44px";
		var button_font_color = "#4c4c4c";
		var button_width = 40;
		var button_height = 60;
		var button_margin = 0;
		var button_prev_html = '<img src="files/button40x60-white-prev.png" alt="prev" border="0">';
		var button_next_html = '<img src="files/button40x60-white-next.png" alt="next" border="0">';
		var button_close_html = '<img src="files/button3-20-black-close.png" alt="close" border="0">';
		/*var button_prev_html = '<div style="text-decoration: none;"><</div>';
		var button_next_html = '<div style="text-decoration: none;">></div>';
		var button_close_html = '<div style="text-decoration: none;">x</div>';*/
		var button_close_opacity = 100;
		var button_close_font_size = "18px";
		var button_close_font_color = "#4c4c4c";
		var button_close_color = "white";
		var button_close_width = 20;
		var button_close_height = 20;
		var button_close_margin = -8;
		//=====================================================
		
		
		var g_id = "galleryimage[" + gallery_id + "]";
		var i_id = g_id + "[" + image_id + "]";
		var x, y, thiswidth, thisheight, previmage, nextimage;
		var prevlink = "";
		var nextlink = "";
		var prev = 0;
		var next = 0;
		var last_id = 0;
		var imagecounter = 0;
		var images_length = 0;
		var counter_stop = 0;
		
		
		
		
		if(self.innerHeight)
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
	 	else if(document.documentElement && document.documentElement.clientHeight)
	 	{
		 	x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if(document.body)
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		if(maxwidth == 0)
		{
			if(x - 100 > minwidth)
				maxwidth = x - 100;
			else
				maxwidth = minwidth;
		}		
		if(maxheight == 0)
		{
			if(y - 100 > minheight)
				maxheight = y - 100;
			else
				maxheight = minheight;
		}
			
			
			
		
	
		for(var i in imageorder[gallery_id])
		{
	    if(!counter_stop)
	    	imagecounter ++;
			if(prev != 0 && !next)
				next = imageorder[gallery_id][i];
			if(imageorder[gallery_id][i] == image_id)
			{
				if(last_id)
					prev = last_id;
				else
					prev = -1;
				counter_stop = 1;
			}
	    last_id = imageorder[gallery_id][i];
	    images_length ++;
		}
		if(prev == -1)
			prev = 0;	
		
		if(prev)
			previmage = loadedimages[gallery_id][prev];
		if(next)
			nextimage = loadedimages[gallery_id][next];	
		
		var thisimage = loadedimages[gallery_id][image_id];
		thisimage.src = document.getElementById(i_id).src.replace("/t3/", "/");
		var thisimagewidth;
		var thisimageheight;
		
		
		
		
		
		if(thisimage.naturalWidth)
		{
			thisimagewidth = thisimage.naturalWidth;
			thisimageheight = thisimage.naturalHeight
		}
		else
		{
			thisimagewidth = thisimage.width;
			thisimageheight = thisimage.height
		}
		
		if(thisimagewidth >= thisimageheight)
		{
			if(thisimagewidth > maxwidth)
				thiswidth = maxwidth;
			else if(thisimagewidth < minwidth)
				thiswidth = minwidth;
			else
				thiswidth = thisimagewidth;
				
			thisheight = thisimageheight * thiswidth / thisimagewidth;
			
			if(thisheight > maxheight)
			{
				thiswidth = thiswidth * maxheight / thisheight;
				thisheight = maxheight;
			}
		}
		
		else
		{
			if(thisimageheight > maxheight)
				thisheight = maxheight;
			else if(thisimageheight < minheight)
				thisheight = minheight;
			else
				thisheight = thisimageheight;
				
			thiswidth = thisimagewidth * thisheight / thisimageheight;
			
			if(thiswidth > maxwidth)
			{
				thisheight = thisheight * maxwidth / thiswidth
				thiswidth = maxwidth;
			}
		}
			
		thiswidth = Math.round(thiswidth);
		thisheight = Math.round(thisheight);
				
		
		
		document.getElementById("galleryback").style.visibility = "visible";
		document.getElementById("galleryback").style.display = "block";
		document.getElementById("galleryback").style.width = "100%";
		document.getElementById("galleryback").style.height = "100%";
		document.getElementById("galleryback").style.position = "fixed";
		document.getElementById("galleryback").style.top = "0px";
		document.getElementById("galleryback").style.left = "0px";
		document.getElementById("galleryback").style.backgroundColor = background_color;
		document.getElementById("galleryback").style.opacity = "0." + background_opacity;
		document.getElementById("galleryback").style.filter = "alpha(opacity=" + background_opacity + ")";
		
		document.getElementById("galleryarea").style.visibility = "visible";
		document.getElementById("galleryarea").style.display = "block";
		document.getElementById("galleryarea").style.width = thiswidth + "px";
		document.getElementById("galleryarea").style.height = thisheight + "px";
		document.getElementById("galleryarea").style.position = "fixed";
		document.getElementById("galleryarea").style.top = Math.round(y / 2 - thisheight / 2 - infobar_height - infobar_padding) + "px";
		document.getElementById("galleryarea").style.left = Math.round(x / 2 - thiswidth / 2) + "px";
		document.getElementById("galleryarea").style.fontSize = font_size;
		document.getElementById("galleryarea").style.color = font_color;
		document.getElementById("galleryarea").style.border = borderwidth + "px solid " + bordercolor;
		document.getElementById("galleryarea").style.backgroundColor = image_background_color;
		
		
		var opacity = "";
		var styles = new Array();
		if(button_opacity < 100)
			opacity = " opacity: " + (button_opacity / 100) + "; filter: alpha(opacity=" + button_opacity + ");";
		styles[0] = "z-index: 1005; visibility: hidden; cursor: pointer; " + opacity + " position: relative; height: " + button_height + "px; width: " + button_width + "px; color: " + button_font_color + "; background: " + button_color + "; font-size: " + button_font_size + "; white-space: nowrap; text-align: center;";
		styles[1] = "float: left; left: " + button_margin + "px;";
		styles[2] = "float: right; right: " + (button_margin - button_close_width) + "px;";
		
		styles[3] = "z-index: 1002; float: top; position: relative; top: " + (0 - button_height) + "px; right: 0px; height: " + thisheight + "px; width: " + thiswidth + "px; border: none;";
		styles[4] = "z-index: 1002; position: relative; top: " + (0 - button_height) + "px; left: " + (0 - borderwidth) + "px; float: bottom; height: " + infobar_height + "px; width: " + (thiswidth - 2 * infobar_padding) + "px; color: " + font_color + "; text-align: center; background: " + infobar_color + "; border: " + borderwidth + "px solid " + bordercolor + "; border-top: none; padding: " + infobar_padding + "px;";
		
		opacity = "";
		if(button_close_opacity < 100)
			opacity = " opacity: " + (button_close_opacity / 100) + "; filter: alpha(opacity=" + button_close_opacity + ");";
		styles[5] = "z-index: 1004; cursor: pointer; " + opacity + " position: relative; height: " + button_close_height + "px; width: " + button_close_width + "px; color: " + button_close_font_color + "; background: " + button_close_color + "; font-size: " + button_close_font_size + "; white-space: nowrap; float: right; top: " + button_close_margin + "px; right: " + button_close_margin + "px;";
		
		if(is_infobar)
			styles[6] = "background: black; opacity: 0.0; filter: alpha(opacity=0); z-index: 1003; position: relative; top: " + (0 - thisheight - borderwidth - button_height - infobar_height - 2 * infobar_padding) + "px; width: 45%; height: 100%;";
		else
			styles[6] = "background: black; opacity: 0.0; filter: alpha(opacity=0); z-index: 1003; position: relative; top: " + (0 - thisheight - button_height) + "px; width: 45%; height: 100%;";
		styles[7] = "float: left;";
		styles[8] = "float: right;";
		styles[9] = "float: left;";
		
		var leftarea = "";
		var rightarea = "";
		
		if(prev)
		{
			previmage.src = document.getElementById(g_id + "[" + prev + "]").src.replace("/t3/", "/");
			prevlink = "<div onmouseover='javascript:show_element(\"prev\");' onclick='javascript:show_gallery(" + gallery_id + ", " + prev + ");' id='prev' style='" + styles[0] + " " + styles[1] + "'><a style='color: " + button_font_color + ";' href='javascript:show_gallery(" + gallery_id + ", " + prev + ");'>" + button_prev_html + "</a></div>";	
			leftarea = "<div onmouseover='javascript:show_element(\"prev\");' onmouseout='javascript:hide_element(\"prev\");' id='gallery_leftarea' style='" + styles[6] + " " + styles[7] + "'></div>";
		}
		if(next)
		{
			nextimage.src = document.getElementById(g_id + "[" + next + "]").src.replace("/t3/", "/");
			nextlink = "<div onmouseover='javascript:show_element(\"next\");' onclick='javascript:show_gallery(" + gallery_id + ", " + next + ");' id='next' style='" + styles[0] + " " + styles[2] + "'><a style='color: " + button_font_color + ";' href='javascript:show_gallery(" + gallery_id + ", " + next + ");'>" + button_next_html + "</a></div>";
			rightarea = "<div onmouseover='javascript:show_element(\"next\");' onmouseout='javascript:hide_element(\"next\");' id='gallery_rightarea' style='" + styles[6] + " " + styles[8] + "'></div>";	
		}
		if(!prev && !next)
		{
			leftarea = "<div style='" + styles[6] + " " + styles[7] + "'></div>";
			rightarea = "<div style='" + styles[6] + " " + styles[8] + "'></div>";
			prevlink = "<div style='" + styles[0] + " " + styles[1] + "'></div>";
			nextlink = "<div style='" + styles[0] + " " + styles[2] + "'></div>";
			
		}
		
		var button_close = "<div onclick='javascript:hide_gallery();' id='close' style='" + styles[5] + "'><a style='color: " + button_close_font_color + ";' href='javascript:hide_gallery();'>" + button_close_html + "</a></div>";
		
		var galleryhtml = button_close + prevlink + nextlink + "<img style='" + styles[3] + "' src='" + thisimage.src + "' alt=''>";
		
		if(is_infobar)
		{
			var counter_text = '<div id="imagecounter" style="' + styles[9] + '">' + imagecounter + "/" + images_length + '</div>';
			var infobar = '<div id="infobar" style="' + styles[4] + '">' + counter_text + document.getElementById(i_id).alt + '</div>';
			galleryhtml = galleryhtml + infobar;
		}
		
		galleryhtml = galleryhtml + leftarea + rightarea;
		
		document.getElementById("galleryarea").innerHTML = galleryhtml;
		
		if(prev)
			document.getElementById("prev").style.top = (thisheight / 2 - button_height / 2) + "px";
		if(next)
			document.getElementById("next").style.top = (thisheight / 2 - button_height / 2) + "px";
		
	//	var scrollbar_position = document.body.scrollTop;	
	//	if (document.all)
	//		scrollbar_position = document.body.scrollTop;
	//	else 
	//		scrollbar_position = window.pageYOffset;
	//	window.onscroll = window.scroll(scrollbar_position, scrollbar_position);
	//		document.body.style.overflow = "hidden";

	} // currentgallery, currentimage

}



function hide_gallery()
{
	
	document.getElementById("galleryback").style.width = "0px";
	document.getElementById("galleryback").style.height = "0px";
	document.getElementById("galleryback").style.visibility = "hidden";
	document.getElementById("galleryback").style.display = "none";
	
	document.getElementById("galleryarea").style.width = "0px";
	document.getElementById("galleryarea").style.height = "0px";
	document.getElementById("galleryarea").style.visibility = "hidden";
	document.getElementById("galleryarea").style.display = "none";
	
	currentgallery = 0;
	currentimage = 0;
}

function show_element(id)
{
	if(document.getElementById(id))
		document.getElementById(id).style.visibility = "visible";
}

function hide_element(id)
{
	if(document.getElementById(id))
		document.getElementById(id).style.visibility = "hidden";
}

function load_gallery()
{
	var gallery_id;
	var counter = 0;
	for(var i = 0; i < document.getElementsByTagName("img").length; i++)
	{
		if(document.getElementsByTagName("img")[i].id.indexOf("galleryimage") != -1)
		{
			gallery_id = (document.getElementsByTagName("img")[i].id).split("][", 1);
			gallery_id = (gallery_id + "").replace("galleryimage[", "");
			element_id = ((document.getElementsByTagName("img")[i].id).replace("galleryimage[" + gallery_id + "][", "")).replace("]", "");
			if(loadedimages[gallery_id] == undefined)
			{
				loadedimages[gallery_id] = new Array();
				imageorder[gallery_id] = new Array();
			}
			if(loadedimages[gallery_id][element_id] == undefined)
			{
				loadedimages[gallery_id][element_id] = new Image;
				loadedimages[gallery_id][element_id].src = (document.getElementById("galleryimage[" + gallery_id + "][" + element_id + "]").src).replace("/t3/", "/");
				loadedimages[gallery_id][element_id].width;
				loadedimages[gallery_id][element_id].height;
				imageorder[gallery_id][counter] = element_id;
				counter ++;
			}
		}
	} // for	
}

function reload_gallery()
{
	show_gallery(currentgallery, currentimage);
}
