var oWidth = null;
var oHeight = null;

var oScale = readCookie( "S" );
if( oScale == null || oScale > 0 ) { oScale = 1; } else { oScale = 0; }

var oSrc = getQueryVariable('i');
if( oSrc == 'error' ) { oSrc = getQueryVariable('path'); }
if( oSrc == 'error' ) { oSrc = '/indeximages/2007/December/Michelle_Trachtenberg_04.jpg'; }

if( oSrc.substring(1,1) != '/' ) { oSrc = unescape( oSrc ); }

setup();

function setup()
{
	document.title = document.title + ' - ' + oSrc.substring( oSrc.lastIndexOf( '/' ) + 1 );
	var c = document.getElementById( 'usemyimage' );
	if( c == null ) return;
	var i = document.createElement( 'img' );
	if( i != null )
	{
		i.setAttribute( 'src', oSrc );
		i.setAttribute( 'id', 'i' );
		c.appendChild( i );
		i.setAttribute( 'onload', resizeImage ); // works in IE
		// i.setAttribute( 'onload', 'resizeImage()' ); // works in FF, Opera, Safari
	}

	// should cover DOM errors

	var i = document.getElementById( 'i' );
	i.onload = resizeImage;
}

function resizeImage()
{
	if( 0 == oScale ) { return; }
	var image = document.getElementById( 'i' );
	if( oWidth == null )
	{
		oWidth = image.width;
		oHeight = image.height;
		window.onresize = resizeImage;
	}

	if( oWidth < 1 || oHeight < 1 ) return;

	ww = windowWidth() - 25 ;

	if( ww < 1043 ) ww = 1043 - 25;

	if( oWidth > ww )
	{
		image.width = ww;
		image.height = oHeight / oWidth * ww;

		if (document.layers)
			document.layers['sw'].display = 'block';
	
		else if (document.all)
			document.all['sw'].style.display = 'block';

		else if (document.getElementById)
			document.getElementById('sw').style.display = 'block';
	}

	else
	{
		image.width = oWidth;
		image.height = oHeight;

		if (document.layers)
			document.layers['sw'].display = 'none';
	
		else if (document.all)
			document.all['sw'].style.display = 'none';

		else if (document.getElementById)
			document.getElementById('sw').style.display = 'none';
	}
}

function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var j=0; j<vars.length; j++)
	{
		var pair = vars[j].split("=");
		if (pair[0] == variable) { return pair[1]; }
	}
	return 'error';
}

function windowWidthOld()
{
	if ( navigator.appName == "Netscape" )
		return window.innerWidth;
	return document.body.offsetWidth;
}

function windowWidth()
{
	// all except Explorer?
	if( self.innerHeight || navigator.appName == "Netscape")
		return self.innerWidth;

	// Explorer 6 Strict Mode
	else if( document.documentElement && document.documentElement.clientHeight )
		return document.documentElement.clientWidth;

	// All other Explorer
	else if( document.body.clientHeight )
		return document.body.clientWidth;

	// hopefully everyone else
	else if( document.body.offsetHeight )
		return document.body.offsetWidth;
}

function createCookie(name,value,days) {
	if ( days ) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function defaultScaling() { eraseCookie( "S" ); }
function enableScaling() { createCookie( "S", 1, 28 ); } //  window.location.reload(true); }
function disableScaling() { createCookie( "S", 0, 28 ); }


