/*
 * World's Worst Software Javascript Background Scaler (C) Jason Baker 2007
 *
 * A background scaler that attempts to make the background fill the width
 *   and height of the browser. This library was written for the following sites:
 *     http://www.onejasonforsale.com
 *     http://www.experimentalfutility.com
 *     http://blunx.no-ip.com
 *
 * Feel free to use this library on your own sites!
 *
 * For updated versions of the library, visit
 *	   http://www.worldsworstsoftware.com
 *
 * Updates:
 *  2007/02/28
 *   - Initial Version.
 *
 * Usage Instructions:
 *  1) Link to this library in the HEAD section of your page like so:
 *
 *     <script type="text/javascript" src="wwsbgscaler.js" />
 *
 *  2) Place your background image inside of your BODY section, outside of any DIVs
 *  3) Give your bottom-most div an id (for this example: "content")
 *  4) Code your background IMG tag to call setupImageScaler like so:
 *
 *      <img src="bg.jpg" style="position: absolute; top: 0px; left: 0px; visibility: hidden;"
 *           onLoad='setupImageScaler(this, "content", 100, true, true);'>
 *
 *      Note: 100 in the line above is the pixel offset to add to the bottom dimension
 *            of the content div when calculating the background height. This gives
 *            you the ability to give your bottom most div some space between it and the
 *            bottom of the page.
 *
 *      Note: it is a good idea to start the background image's visibility style
 *            off as "hidden" so non-javascript browsers won't show a unresized
 *            background image.
 *
 */


var imageToResize;
var scaleX;
var scaleY;
var contentDiv;
var divOffset;


function getClientHeight()
{
	return document.documentElement.clientHeight;
}

function getClientWidth()
{
	return document.documentElement.clientWidth;
}

function getDivHeight()
{
	return contentDiv.offsetHeight + (divOffset * 2);
}

function showImage()
{
	imageToResize.style.visibility="visible";
}

function resizeImage()
{
	/*alert('original_width:'+or_w+'newwidth:'+getClientWidth());*/
	new_image = document.getElementById("bgimage");
	var newImg = new Image();
	newImg.src = new_image.src;
	var or_h = newImg.height;
	var or_w = newImg.width;
	new_height = getClientHeight();
	new_width = getClientWidth();
	devide = or_w/new_width;
	
	/* calculate new width from new height */
	new_height = or_h/devide;
	
	new_image.width = new_width;
	new_image.height = new_height;
}

/*
 * image - the image object to scale
 * div id - the id for the div to calculate content height from
 * div_offset - the offset to add to the div height for total content height
 * scale_x - boolean flag specifying whether to scale the image along the x axis or not
 * scale_y - boolean flag specifying whether to scale the image along the y axis or not
 */
function setupImageScaler(image, div_id, div_offset, scale_x, scale_y, original_w, original_h)
{
	imageToResize = image;
	scaleX = scale_x;
	scaleY = scale_y;
	or_w = original_w;
	or_h = original_h;
	contentDiv = document.getElementById(div_id);
	divOffset = div_offset;
	resizeImage();
	showImage();

	onresize = function() { resizeImage(); }
	onload = function() { resizeImage(); }
}




