var MISC_DEBUG = false;

function load_image_from_document_url()
{
	var image_shown = false;
	var document_url = "" + document.URL;
	if (document_url.indexOf("?") != -1)
	{
		//document url has an argument on the end of it..
		var index = document_url.indexOf("?");
		var arg = document_url.substring(index);

		arg = arg.replace(/%20/g, " ");
		//alert("arg: " + arg);

		//make sure the arg isn't simply "?"..
		if (arg.length > 1)
		{		
			arg = arg.substring(1);
			for (i = 0; i < photos_array.length; i++)
			{

				if (photos_array[i].indexOf(arg) != -1)
				{
					esoteric_show_image(photos_array[i]);
					reset_thumbs_page(i);
					image_shown = true;
					return;
				}
			}
		}
	}

	if (image_shown == false)
	{
		esoteric_show_image(photos_array[0]);
	}
}

function esoteric_show_image(image_src)
{
	galleryShowImage(image_src);
	
}

function replace_direct_link(image_src)
{
	var document_url = "" + document.URL;
	if (document_url.indexOf("?") != -1)
	{
		document_url = document_url.substring(0, document_url.indexOf("?"));
	}
	document_url += "?" + extract_filename(image_src);	
	document.getElementById('imagelink').href = document_url;
}

function extract_filename(filename)
{
	var win_index = filename.lastIndexOf("\\");
	var unix_index = filename.lastIndexOf("/");
	var index = Math.max(win_index, unix_index);
	if (index == -1 || index == filename.length - 1)
	{
		//there isnt a forward slash or backward slash in there..
		return filename;
	}

	return filename.substring(index + 1);

}

function load_css_size_overrides()
{
	var client_x = false;
	var client_y = false;

	if (navigator.appName == "Netscape")
	{
		client_x = window.innerWidth;
		client_y = window.innerHeight;
	}
	else //assume IE
	{
		client_x = document.documentElement.clientWidth;
		client_y = document.documentElement.clientHeight;
	}


	var height = null;
	var width = null;
	var header_font_size = null;
	
	
	if (client_x > 1500 && client_y > 850)
	{
		//big resolution, 1920x1200 or greater, lucky bastards..
		height = 850;
		width = 1500;
		header_font_size = 24;
		
	}
	else if (client_x > 1000 && client_y > 600)
	{
		//client's probably in 1024x768
		height = 600;
		width = 1000;
		header_font_size = 24;
	}
	else if (client_x > 750 && client_y > 475)
	{
		//client is probably running in 800x640 
		height = 475;
		width = 750;
		header_font_size = 18;
	}
	else
	{
		//client is probably running in 640x480 or worse..
		height = 350;
		width = 500;
		header_font_size = 14;
	}		

	newCss = create_custom_sized_content_stuff(height, width, header_font_size);

	if (newCss != false)
	{
		document.writeln(newCss);
	}
	
	if (document.getElementById('imagecontent') != null)
	{
	
		_imageScroller = new ImageScroller('_imageScroller', 'imagecontent', 300, 10, height - 100, height - 100);
		_imageScroller.init();
	}
		
	centerContent('theeverythingbox', height, width);
	


	if (MISC_DEBUG != false)
	{
		var message = "[load_css_size_overrides() info]\n";
		message += "client x,y is " + client_x + "," + client_y + "\n";
		if(newCss != false)
		{
			message += "loading css override file \"" + newCss + "\"\n";
		}
		else
		{
			message += "using default implementation.\n";
		}
		alert(message);
	}
}

function create_custom_sized_content_stuff(content_height, content_width, header_font_size)
{
	var content_section = content_height - 100;
	var thumbs_dimension = content_section / 5;
	
	var content = "<style>\n";
	content += "div#content { width: " + content_width + "px; }\n";	
	content += "div#header { width: " + content_width + "px; }\n";
	
	
	content += "div#thumbs { width: " + content_section + "px; height: " + content_section + "px; }\n";
	content += "div#thumbscontent { width: " + content_section + "px; height: " + content_section + "px; }\n";
	content += "div#thumbs IMG.thumbnail { width: " + thumbs_dimension + "px; height: " + thumbs_dimension + "px; }\n";
	content += "div#thumbcontrols { height: " + (content_section - 24) + "px; }\n";
	
	content += "div#image { width: " + content_section + "px; height: " + content_section + "px; }\n";	
	content += "div#imagecontent { width: " + content_section + "px; height: " + content_section + "px; }\n";	
	content += "div#imagecontrols { width: " + (content_section - 24) + "px; }\n";
	
	content += "div#frontpagecontent { width: " + content_width + "px; height: " + content_section + "px; }\n";
	content += "IMG.frontpageimage { width: " + thumbs_dimension + "px; height: " + thumbs_dimension + "px; }\n";
		
	content += "div#about { width: " + (content_width - 40) + "px; }\n";
	content += "DIV#about H1 { width: " + (content_width - 40) + "px; }\n";
	
	content += "DIV#headertitle { font-size: " + header_font_size + "px; }\n";
	
	var nav_image_size = Math.min(40, (content_width / 30));
	
	content += "div#nav IMG { width: " + nav_image_size + "px; height: " + nav_image_size + "px; }\n";
	
	content += "</script>"
	
	return content;
}


function centerContent(contentDivId, divHeight, divWidth)
{
	var clientHeight = false;
	var clientWidth = false;
	if (navigator.appName == "Netscape")
	{
		clientWidth = window.innerWidth;
		clientHeight = window.innerHeight;
	}
	else //assume IE
	{
		clientWidth = document.documentElement.clientWidth;
		clientHeight = document.documentElement.clientHeight;
	}

	//var clientHeight = document.body.clientHeight;
	//var clientWidth = document.body.clientWidth;

	var div = document.getElementById(contentDivId);	
	
	//alert(clientHeight + " " + divHeight);

	if (clientHeight > divHeight)
	{
		div.style.marginTop = Math.max(0, Math.floor((clientHeight - divHeight) / 2)) + 'px';
	}
	else
	{
		div.style.marginTop = "0px";
	}
}

