// JavaScript Document
/* functions for menus */

$(document).ready(function(){
	var resizeTimer = 0;
	var h_last_viewport = 0;
	var w_last_viewport = 0;
	var doReload = false;
	
	// log function, can be used in jquery chain
	jQuery.fn.log = function (msg) {
	  //console.log("%s: %o", msg, this);
	  return this;
	};

	
	function embedpdf() {
		// do a delayed embed of pdf so that page can display correctly first
		//NOTYET
	}
	
	
	// resize pdf object to browser width, do this on resize also
	function resizepdf( e ) {
		// DOESN'T REALLY HELP MUCH, $('#leftnavtd').width(208);	// force width in ie
		// could also set width of right pane here too...quicker than loading pdf...
		
		doReload = false;
		
		var h = $('body').height();	
		var hwin = $(window).height();	// viewport height, matches h when no scrolling
		var wwin = $(window).width();	// viewport width
		
		//var h_pane = h - 109 - 2; // whole table less top row less a few pixels
		
		//h_pane = h - 2; // BTR - TESTING ONLY, MOVE MAINTABLE DOWN
		// without height 100%, make height match viewport
		h_pane = hwin - 109 - 2 - 2;
		//console.log("h: %d, h_pane: %d, hwin: %d, h_lst_vp: %d, h_old: %d",h,h_pane,hwin,h_last_viewport,h_old);
		
		if ((hwin > h_last_viewport + 3) || (wwin > w_last_viewport + 3)) {
			// resized larger, at least 3 pixels 
			if ($('#mainpane object')) {
				$('#mainpane object').height(h_pane);
			}
			$('#partsmenu').height(h_pane-53);			// image above partsmenu and scrollbar
			$('#leftnavtd').height(h_pane);
			$('#mainpane').height(h_pane);
		} else if (resizeTimer && ((hwin < h_last_viewport + 3) || (wwin < w_last_viewport + 3))) {
			// resized smaller, must be resized enough to matter
			// can't set it because contents have already made table it's default size...unless I resized entire table too...
			// must resize to viewport not table height then...
			// just refresh now.
			if (resizeTimer) {
				clearTimeout(resizeTimer);
				resizeTimer = false;
			}
			//doReload = true;

			if ($('#mainpane object')) {
				$('#mainpane object').height(h_pane);
			}
			$('#partsmenu').height(h_pane-53);			// image above partsmenu and scrollbar
			$('#leftnavtd').height(h_pane);
			$('#mainpane').height(h_pane);

			
		}
		// save last resize
		h_last_viewport = hwin;
		w_last_viewport = wwin;
		if (doReload) {
			doReload = false;
			window.location.reload();
		}
	}
	

	if(! ($.browser.msie && $.browser.version < 7)) {
		// if not ie6, resize will resize items
		$(window).resize( function() {
			if (resizeTimer) clearTimeout(resizeTimer);
			resizeTimer = setTimeout(resizepdf, 200);	
		});
	}
	
	resizepdf(1);

	
/*	
uses resizeable
    var obj = $('#docFrame');
    $('#doc').resizable(
    { 
        handles: 'all', 
        resize: function(e, ui) {
            $('#data').html(ui.size.width + 'x' + ui.size.height);
            obj.attr({ width: ui.size.width, height: ui.size.height });
        },
        start: function(e, ui) { $('#docFrame').hide(); },
        stop: function(e, ui) { $('#docFrame').show(); }
    });
*/


	
});
