﻿/**
 * Rivet Software Inc.
 * ©Copyright 2006-2009 Rivet Software, Inc. All rights reserved.
 **/

RS.Window = {};

RS.Window.OnResizeAttach = function() {
	RS.Window.SubtractHeight = 0;
	RS.Window.SubtractHeight += document.getElementById('header').offsetHeight;
	RS.Window.SubtractHeight += document.getElementById('footer').offsetHeight;

	//For the surrounding elements
	//43 = arbitrary height of inner elements
	RS.Window.SubtractHeight += 43;

	RS.Window.CompanySearch = document.getElementById('CompanySearch');
	RS.Window.CompanySearch_Results = document.getElementById( 'CompanySearch_Results' );
	RS.Window.TabContents = document.getElementById('TabContents');

	RS.Window.OnResize();
	var e = new HD.Event(window, 'onresize', RS.Window.OnResize);
	e.Attach();
}

RS.Window.OnResize = function() {
	RS.Window.Resize();
	//A second call removed the scroll bar
	RS.Window.Resize();
}

RS.Window.Resize = function(){
	var rects = getWindowRects();
	var suggHeight = rects.Height - RS.Window.SubtractHeight;
	suggHeight = Math.max(suggHeight, 450 );//RS.Window.CompanySearch.offsetHeight);

	RS.Window.CompanySearch_Results.style.height = ( suggHeight - 45 ) +'px';
	RS.Window.CompanySearch.style.height = suggHeight +'px';
	RS.Window.TabContents.style.height   = suggHeight +'px';
}

/**
* Attempts to get the measurements for the window provided or the current window.
* @param {Window} domWin [Optional]	The window to get the measurements for
* @return {Object}	The window's measurements
*/
function getWindowRects(domWin) {
	var coords = {};
	if (!domWin) {
		domWin = window;
	}

	var domDoc = domWin.document;

	if (domWin.innerHeight) {
		coords['Height'] = domWin.innerHeight;
		coords['Width'] = domWin.innerWidth;
	} else if (domDoc.documentElement && domDoc.documentElement.clientHeight) {
		coords['Height'] = domDoc.documentElement.clientHeight;
		coords['Width'] = domDoc.documentElement.clientWidth;
	} else if (domDoc.body) {
		coords['Height'] = domDoc.body.clientHeight;
		coords['Width'] = domDoc.body.clientWidth;
	}

	if (domWin.pageYOffset) {
		coords['Top'] = domWin.pageYOffset;
		coords['Left'] = domWin.pageXOffset;
	} else if (domDoc.documentElement && domDoc.documentElement.scrollTop) {
		coords['Top'] = domDoc.documentElement.scrollTop;
		coords['Left'] = domDoc.documentElement.scrollLeft;
	} else if (domDoc.body) {
		coords['Top'] = domDoc.body.scrollTop;
		coords['Left'] = domDoc.body.scrollLeft;
	}

	fullHeight1 = domDoc.body.scrollHeight;
	fullHeight2 = domDoc.body.offsetHeight
	if (fullHeight1 > fullHeight2) {
		coords['fullHeight'] = domDoc.body.scrollHeight;
		coords['fullWidth'] = domDoc.body.scrollWidth;
	} else {
		coords['fullHeight'] = domDoc.body.offsetHeight;
		coords['fullWidth'] = domDoc.body.offsetWidth;
	}

	coords['Right'] = coords['Left'] + coords['Width'];
	coords['Bottom'] = coords['Top'] + coords['Height'];

	return coords;
}