/*
 * picsearchImage.js
 *
 * Author: Jon Eriksmo
 * Email: jon@itano-systems.com
 * Date: 2009-07-12
 *
 */

function itanoObjectImage(data) {
	this.numResults  = 20;
	this.boxWidth    = data[0];
	this.extraWidth  = data[1];
	this.extraSpace  = data[2];
	this.minCols     = data[3];
	
	/*
	 * Returns the width of the window.
	 */
	this.getWindowWidth = function() {
		if (!(document.documentElement.clientWidth == 0)) {
			return document.documentElement.clientWidth;
		}
		else {
			return document.body.clientWidth;
		}
	}

	/*
	 * Updates the container for the search result.
	 * 
	 * This functions checks the current width of the window,
	 * and sets the number of columns in the result container
	 * accordingly (10, 9, 6, 5, 4, 3 or 2 columns).
	 * 
	 */
	this.resizeResult = function() {
		available = this.getWindowWidth() - this.extraSpace;

		if (available <= 504) {
			available = 504;
		}
		else {
			for (var i = 6; i >= this.minCols; i--) {
				resultSize = i * this.boxWidth + this.extraWidth;
				if (resultSize <= available || i == this.minCols) {
					padding = (available - resultSize) / 2;
					size = available - (padding * 2);
					break;
				}
			}
		}
		
		document.getElementById('cols').value = i;
		document.getElementById('width').value = available;
		
		$('a.linkSelector').each(function() {
	        this.href = this.href + "&cols=" + i + "&width=" + available;
	    });
	}
}

/*
 * Set all the variables, and create the itano object.
 * 
 * boxWidth:		this is the width of the image box (including margins)
 * extraWidth:		this is the extra width of the container box (margin, padding and borders)
 * extraSpace:		this is the extra space outside the container box that we want to use for other content
 * minCols:			this is the minimum number of columns we want to display
 */
var itanoData = new Array();
itanoData[0] = 168;					/* boxWidth */
itanoData[1] = 2;					/* extraWidth */
itanoData[2] = 280;					/* extraSpace */
itanoData[3] = 3;					/* minCols */
itanoImage = new itanoObjectImage(itanoData);

/*
 * Register the event handlers
 */
$(document).ready(function () { itanoImageWrapper(); });

/*
 * We need this wrapper function because we cannot register an object
 * member function for the onload and onresize event handlers.
 */
function itanoImageWrapper() {
	itanoImage.resizeResult();
}