var courseResizeOldOnLoad;
var originalBrowserWidth;
var originalContentHeight;
var nonContentHeight;
var browserIsInternetExplorer = navigator.appVersion.toLowerCase().indexOf('msie') > -1;

function resizeBeginBodyInit() {
    document.body.scroll = 'no'; // Turn on scroll bar by default
    
    // Register the on-load function
    courseResizeOldOnLoad = window.onload;
    window.onload=initialResize;
}

function ensureTaller(sourceName, targetName) {
    sourceSize = document.getElementById(sourceName).offsetHeight + 2;
    if (sourceSize > 2 && sourceSize > document.getElementById(targetName).offsetHeight) {
        document.getElementById(targetName).style.height = sourceSize+'px';
    }
}

function ensureSameSize(divAName, divBName) {
    divAsize = document.getElementById(divAName).offsetHeight;
    divBsize = document.getElementById(divBName).offsetHeight;
    
    if (divAsize > 0 || divBsize > 0) {
        if (divAsize > divBsize) {
           document.getElementById(divBName).style.height = divAsize+'px';
        } else if (divAsize < divBsize) {
           document.getElementById(divAName).style.height = divBsize+'px';
        }
    }
}

function ensureThreeSameSize(divAName, divBName, divCName) {
    elementA = document.getElementById(divAName);
    elementB = document.getElementById(divBName);
    elementC = document.getElementById(divCName);

    _divAsize = (elementA == null) ? 0 : elementA.offsetHeight;
    _divBsize = elementB.offsetHeight;
    
    divAsize = _divAsize+_divBsize;
    divCsize = elementC.offsetHeight;
    
    if (divAsize > 0 || divCsize > 0) {
        if (divAsize > divCsize) {
           elementC.style.height = divAsize+'px';
        } else if (divAsize < divCsize) {
           elementB.style.height = (divCsize-_divAsize)+'px';
        }
    }
}

function ensureFullHeight(OtherDivName, TargetDivName) {
	targetDiv = document.getElementById(TargetDivName);
    if (window.innerHeight != null) {
        browserHeight = window.innerHeight; // Netscape
    } else { 
        browserHeight = document.body.clientHeight; // IE compatability
    }
    
    extraHeight = document.getElementById(OtherDivName).offsetHeight;
    targetDiv.originalHeight = targetDiv.offsetHeight;
    targetDiv.style.height = (browserHeight - extraHeight) + 'px';
}

function fixMargins(divName, leftDivName, rightDivName) {
    // MRS 3-23-04 NS Fix - ensure margins match float widths
    fixDiv = document.getElementById(divName);
    
    if (fixDiv != null) {
        leftDiv = document.getElementById(leftDivName);
        rightDiv = document.getElementById(rightDivName);
        
        if (leftDiv != null) {
            if (browserIsInternetExplorer) {
                fixDiv.style.marginLeft = leftDiv.offsetWidth-3; // Hack around the IE 3px bug...
            } else {
                fixDiv.style.marginLeft = leftDiv.offsetWidth;
            }
        }
        if (rightDiv != null) {
            if (browserIsInternetExplorer) {
                fixDiv.style.marginRight = rightDiv.offsetWidth-3; // Hack around the IE 3px bug...
            } else {
                fixDiv.style.marginRight = rightDiv.offsetWidth;
            }
        }
        //alert(divName+' '+leftDivName+' '+rightDivName+' '+fixDiv.style.marginLeft+' '+fixDiv.style.marginRight+' '+leftDiv.offsetWidth+' '+rightDiv.clientWidth+' '+leftDiv.innerHeight);
    }
}

function initialResize() {
    if (courseResizeOldOnLoad != null) courseResizeOldOnLoad();

    // MRS 3-23-04 NS Fix - ensure margins match float widths
    fixMargins('CoursePageTitle','CoursePageLogo','CoursePageActions');
    fixMargins('CoursePageBanner','CoursePageLogo','CoursePageActions');
    fixMargins('ResizableBlock','WestMenu','EastMenu');

    // Confirm region sizing...NS can't dynamically set this as we go...
    ensureSameSize('CoursePageActions', 'CoursePageLogo');
    ensureThreeSameSize('CoursePageTitle', 'CoursePageBanner', 'CoursePageLogo');
    ensureSameSize('CoursePageActions', 'CoursePageLogo');
    
    // Call on load to setup dynamic sizing of floats
    // MRS 12-24-03 Generated courses now set this var by default...
    if (document.targetWidth != null) originalBrowserWidth = document.targetWidth; 
    else if (document.body.clientWidth != null) originalBrowserWidth = document.body.clientWidth; // IE compatability
    else originalBrowserWidth = window.innerWidth; // Netscape
    
    // Note the size of the non-content divs
    nonContentHeight = document.getElementById('CoursePageHeader').offsetHeight;
    pageMenu = document.getElementById('NorthMenu');
    if (pageMenu == null) pageMenu = document.getElementById('SouthMenu');
    if (pageMenu != null) nonContentHeight += pageMenu.offsetHeight; 
    
    // Note the size of the content box
    originalContentHeight = 30; // A small default height for the content
    
    if (document.getElementById('ContentIframe') == null) {
        // The resizable element is not an iframe so we can use it's height
        originalContentHeight = document.getElementById('ResizableBlock').offsetHeight;
    }
    
    // If there is a east or west menu, see if they are larger than the content...
    pageMenu = document.getElementById('EastMenu');
    if (pageMenu == null) pageMenu = document.getElementById('WestMenu');
    if (pageMenu != null) {
       menuHeight = pageMenu.offsetHeight;
       if (pageMenu.originalHeight != null) {
           menuHeight = pageMenu.originalHeight;
       } 
       if (menuHeight > originalContentHeight) {
           // Menu larger than the content - use the menu size
           originalContentHeight = menuHeight;
       }
    }

    // Call the normal resize logic
    resize();
    
    // And register the notification callbacks
    window.onresize=resize; // IE
    window.onsize=resize;   // NS/Moz
}

function resize() {
    // Call when the user resizes to setup the browser...
    scrollEnabled = "no";

    // Get the screen width and see if we need a scrollbar
    if (document.body.clientWidth != null) browserWidth = document.body.clientWidth; // IE compatability
    else browserWidth = window.innerWidth; // Netscape
    if (browserWidth < originalBrowserWidth) {
        scrollEnabled = "yes";
        browserWidth = originalBrowserWidth;
    }

    // Note the new browser height and current content height
    if (window.innerHeight != null) {
        browserHeight = window.innerHeight; // Netscape
    } else { 
        browserHeight = document.body.clientHeight; // IE compatability
    }

    // Calculate the target size - size of the browser minus all the decorations on the content
    contentHeight = browserHeight - nonContentHeight;
    //alert(contentHeight+" "+browserHeight+" "+nonContentHeight+" "+originalContentHeight);
    
    // If the target is smaller than the original, use the original 
    // (otherwise we lose content)

    if (originalContentHeight > contentHeight) {
        contentHeight = originalContentHeight;
        //alert("Height constrained "+contentHeight);
        scrollEnabled="yes";
    }
    
    oldScrollEnabled = document.body.scroll;

    if (oldScrollEnabled != scrollEnabled) {
        document.body.scroll = scrollEnabled;
        
        // Adding/Removing scroll bars change the screen width...refetch this info
        if (document.body.clientWidth != null) browserWidth = document.body.clientWidth; // IE compatability
        else browserWidth = window.innerWidth; // Netscape
        if (browserWidth < originalBrowserWidth) {
            browserWidth = originalBrowserWidth;
        }
    }
    document.getElementById('CoursePage').style.width = browserWidth + 'px';
    
    // Content width is browser width - W/E menu width
    document.getElementById('ResizableBlock').style.height = contentHeight+'px';
    
    // If there are side menus, note the delta and adjust the content 
    // frame and any floating menus
    pageMenu = document.getElementById('EastMenu');
    if (pageMenu == null) pageMenu = document.getElementById('WestMenu');
    if (pageMenu != null) {
        pageMenu.style.height = contentHeight+'px';
        //alert(pageMenu.offsetHeight+' '+document.getElementById('ResizableBlock').offsetHeight);
    }
}

// BRG Bug 1852 - Trick the Apple Safari browser to think that documents share the same domain
//                i.e. - server1: dev.reston.vcampus.com
//                       server2: vdiscussion.reston.vcampus.com
// This function will remove the subdomain and replace it like the following:
//                document.domain=reston.vcampus.com 
function  setDocumentDomain() {
    if (navigator.appVersion.toLowerCase().indexOf("safari") > -1) {
        var start = document.domain.indexOf('.') + 1;  // remove sub-domain
        var end = document.domain.length; 
        document.domain = document.domain.substring(start, end);
    }
}

// HS This hides the Large Course Reports link from students
// Not really a course resize function - just here for convenience! 
function hideReports(isinstructor){
	if(!isinstructor){ // do not display to students
		var reportsMenuItem = document.getElementById('reportsMenuItem');
		if(reportsMenuItem){
			reportsMenuItem.style.display = 'none';
		}	
		
		var other = document.getElementById('MenuOtherSelect');
		if(other){
			var num = other.options.length;
			if(num !=0) {
				for(i=0; i<num; i++){
					current = other.options[i].value;
					if(current == 'reports'){
						other.options[i] = null;
					}
				}
				if(others.options.length == 1){
					other.style.display = 'none';
				}
			}
		}
	}	
} 

