
//code from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

f_clientHeight = function () {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

f_filterResults = function (n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


getViewPortHeight = function(){
	
	//return f_clientHeight();
	
	var viewportwidth;
	var viewportheight;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth, viewportheight = window.innerHeight;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	else 
		if (typeof document.documentElement != 'undefined' &&
		    typeof document.documentElement.clientWidth != 'undefined' &&
		    document.documentElement.clientWidth != 0) {
			   viewportwidth = document.documentElement.clientWidth, viewportheight = document.documentElement.clientHeight;
		}
		
		// older versions of IE
		
		else {
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth, viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		}
	return viewportheight;
	}


getViewPortWidth = function(){
	
	//return f_clientHeight();
	
	var viewportwidth;
	var viewportheight;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth, viewportheight = window.innerHeight;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	else 
		if (typeof document.documentElement != 'undefined' &&
		    typeof document.documentElement.clientWidth != 'undefined' &&
		    document.documentElement.clientWidth != 0) {
			   viewportwidth = document.documentElement.clientWidth, viewportheight = document.documentElement.clientHeight;
		}
		
		// older versions of IE
		
		else {
			viewportwidth = document.getElementsByTagName('body')[0].clientWidth, viewportheight = document.getElementsByTagName('body')[0].clientHeight;
		}
	return viewportwidth;
	}


scrollTop = function()
{
	var offsetY = 0;
	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
		}
	}
	//w3c
	else
	{
		offsetY = window.pageYOffset;
	}
	return offsetY
}

	
function setRightMenuPos() {
	
	var menuH
	var windowH
	var scale
	var marginTopVal
	var overflowHeight
	var documentHeight
	var menuOverflowHeight
	var topAdjust = -45;
	var bottomSpace = 100;
	var scrollAdj = 0;
	var topSpace = 100; // height of the user id area in top_menu
	var userTopSpace = 175; // height of entire _top_menu

	if ($('jsControl') ) { 
		menuH = parseInt($('right-bar').getStyle("height"));
		windowH = getViewPortHeight();
		documentHeight = parseInt($(document.body).getStyle("height"))
		menuOverflowHeight = Math.max(0, menuH-(documentHeight-bottomSpace-topSpace));
		if (menuOverflowHeight > 0) {
			overflowHeight = Math.max(0, documentHeight - windowH - scrollAdj) * 1.0
			scrollPP = overflowHeight > 0 ? Math.min(1, (scrollTop() / overflowHeight)) : 0;
			marginTopVal = (-parseInt(scrollPP * menuOverflowHeight) + topAdjust) + "px";
		} else {
			marginTopVal = (topAdjust+(userTopSpace-topSpace)+Math.min(0,(documentHeight-bottomSpace-userTopSpace)-menuH))+"px"
		}
				$('right-bar').setStyle({
					marginTop: marginTopVal
				})
	}
}

window.onscroll = setRightMenuPos;
	
// note we are cannot have mulitple handlers because sequence is important, and
// also _footer.html.erb has a call to window.onsize AFTER a partial reload, so change this with care

window.onresize = function(){
	var h = getViewPortHeight();	
	var w = getViewPortWidth();
	var windowsFound = false;
	var theWindows = UI.defaultWM ? UI.defaultWM.windows() : [];
	for (var i = 0; i<theWindows.length; i++) {
		var theWindow = theWindows[i];
		if (theWindow.visible) {
			windowsFound = true;
			setSize(w,h,theWindow);
		}
	}
	if (windowsFound) {
		$('footer_div').hide();
		return;
	}
	$('footer_div').show();

	// we don't want to take up space with the footer IF the view port height is very small on the jobs/new page
	// this is so the jsControl iframe can be a reasonable size, and the cart display fits.
	// however there is no way to separate the event handlers since we need to make sure the
	// setStyle to position fixed does not get overwritten by a later handler call, so we just
	// make sure jsControl exists and deal with it here.  Sorry for the many magic numbers!
	if ($('jsControl')) {
		//$('footer_padding').hide();
		var frameHeight = h - 168; //subtract space for Job setup buttons + header to get a starting height
		var minDesiredHeight = 468; 
		if (minDesiredHeight > frameHeight) {
			$('footer_div').setStyle({ // the entire footer wont show and still meet the minDesiredHeight so.. 
				position: "relative",
				top: "-18px"
			});
			$('footer_padding').hide();
			frameHeight = Math.min(minDesiredHeight, h-106)
			//frameHeight = Math.max((468-168)+80, h - 88); // just leave enough space to see the Job setup buttons
		} else {  // we can show the whole footer
			$('footer_div').setStyle({position: "fixed", top: (getViewPortHeight()-80) + "px"});
			$('footer_padding').show()
			frameHeight = Math.max(frameHeight-172, minDesiredHeight);
			// make sure that there is room to see all of the right bar, in customer service mode it gets very big
			frameHeight = Math.max(frameHeight, parseInt($('right-bar').getStyle("height"))-140)
			}
		$('jsControl').style.height = Math.max(frameHeight, 420) + "px";
		setRightMenuPos();
		hideTopBar();
	} else if ($('footer_div')) {
		$('footer_div').setStyle({position: "fixed", top: getViewPortHeight()-80 + "px"});
		$('footer_padding').show()		
	}
	
};

setSize = function(w,h,theWindow) {
	if (!theWindow.sizeMgr_originalSize) theWindow.sizeMgr_originalSize = theWindow.getSize();
	var theSize = theWindow.sizeMgr_originalSize;
	var width = Math.min(theSize.width, w-100);
	var height = Math.min(theSize.height, h-100);
	theWindow.setSize(width, height);
	theWindow.center();
}

keepDialogSizedRight = function(dialog, theClass, onSizeChange){
	var handleSizeChange = function(e) {
		if (e.memo.window == dialog) {
			$$(theClass).each(function(element){
				onSizeChange(element, dialog)
			})
		}
	}
	document.observe("window:size:changed", handleSizeChange);
	setSize(getViewPortWidth(), getViewPortHeight(), dialog);
	handleSizeChange({memo:{window:dialog}});
}

hideTopBar = function(){
		var h = getViewPortHeight();
		if ( h-340 < 468) {
			$('new-job-form').scrollTo();
			}
		else {

		}
};



Event.observe(window, 'load', window.onresize);

document.observe('window:destroyed', window.onresize );



