//bgAdjust
//D.V. O'Donnell, March 2010
//jQuery 'background' image resize script

var origWidth;
var origHidth;


		$(document).ready(function() {
//alert($('#docBox').width()+'x'+$('#docBox').height()+' '+$(window).width()+'x'+$(window).height());

		//this method required to account for webkit browsers (safari/chrome)
		$('#bgImg').load(function() {
		    // Remove attributes in case img-element has set width and height
		    $(this).removeAttr("width")
		           .removeAttr("height")
		           .css({ width: "", height: "" }); // Remove css dimensions as well
		    origWidth = this.width;
		    origHeight = this.height;
			doResize();
		})
		.each(function(){
			if(this.complete) $(this).trigger("load");
			});
		
		});

		

		$(window).bind('resize', function() {
					doResize();
		});

		function doResize() {

        		var arrayPageSize = getPageSize();
        		//use full document height, wrapped in docBox, to account for scrolling
						var window_height=arrayPageSize[1];
        		arrayPageSize[1]=$('#docBox').height();
        		//alert(origWidth+'x'+origHeight+' '+$('#docBox').width()+'x'+arrayPageSize[1]+' '+$(window).width()+'x'+$(window).height());
        		var set_height=origHeight;
        		var set_width=origWidth;
        		var w_diff=origWidth-arrayPageSize[0];
        		var h_diff=origHeight-arrayPageSize[1];
        		if (w_diff < h_diff) {
							if (set_width < arrayPageSize[0]) {
								if (arrayPageSize[1] < origHeight) {
									set_width=(arrayPageSize[0])+50;								
								}
								else {
								set_width=(arrayPageSize[0]);
								}
								set_height=(set_width/origWidth)*set_height;
							}
						} else if (h_diff < w_diff) {
							if (set_height < arrayPageSize[1]) {
									set_height=arrayPageSize[1];
								set_width=(set_height/origHeight)*set_width;
							}	
						}
						$('#bgImg').attr('width',set_width);
						$('#bgImg').attr('height',set_height);
								if (arrayPageSize[1] < origHeight) {
									if (arrayPageSize[1] > $(window).height()) {
										//alert($('#bgBox').height());
										$('#bgBox').height(arrayPageSize[1]);
										//alert($('#bgBox').height());
									}
									else {
										$('#bgBox').height($(window).height());
									}								
								}
		}

		function getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		}