/////////////////////////// QUICK HELPER FUNCTIONS ///////////////////////////////

// shortcut to getElementById
function $(element) { return document.getElementById(element); }


// quick IE browser detection
function isMSIE() {
	
	return (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
}


// get a reference to a flash movie
function getFlashMovie(id) {

	var movie = null;
	
	if (isMSIE()) {
		movie = window[id];
	} else {
		movie = document[id];
	}
	
	return movie;
};






/////////////////////////////// OPEN WINDOW ///////////////////////////////

function popWin(url, features) {

    // this is the format for possible features: toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=500,height=500
	
	var didWindowOpen = false;
	
	if (features == null || features == "") {
		didWindowOpen = window.open(url, '_blank');
	} else {
		didWindowOpen = window.open(url, '_blank', features);
	}
	
	
	if (!didWindowOpen) {
    	alert("It looks like this window couldn't open. Please disable your popup blocker and try again.");
    }

}


function popWinSimple(url, width, height) {
	
	// detect google chrome and add 50px of height (because Chrome mistakenly counts height as outerHeight)
	if (Boolean(window.chrome)) height += 50;
	
	var didWindowOpen = window.open(url, '_blank', 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
	
    if (!didWindowOpen) {
    	alert("It looks like this window couldn't open. Please disable your popup blocker and try again.");
    }
}






///////////////////////////////// VIDEO /////////////////////////////////


var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

function showVideo(html, width, height, downloadLink, isInDemo, isInGreenhouse) {
    
    if (downloadLink.length > 0) height -= 15;
    
	resizeVideoBox(width, height);
	centerVideoBox(width + 20, height + 20); // add 20px because the real width and height is 20px larger (counting the padding)
	
    if (IE6) $('videoBox').className = 'videoBox-ie6';
    
	$('videoBox').style.visibility = 'visible';
	
    $('videoBox').innerHTML += '<a id="close-video-btn" href="javascript:void(0);" class="close" onClick="closeVideoScreen(); return false;"></a>';
	$('videoBox').innerHTML += html;
	
	if (downloadLink.length > 0) {
		
		var additionalPath = isInDemo ? "../../" : (isInGreenhouse ? "../" : "");
		
		$('videoBox').innerHTML += '\
        <div class="footer"> \
            <a href="'+downloadLink+'" class="download"> \
                <img src="' + additionalPath + 'images/download_video_button.gif" title="download" class="download_image" /> \
           </a> \
        </div>';
    }
	
	return false;
}

function hideVideo() {
	$('videoBox').style.visibility = 'hidden';
	$('videoBox').innerHTML = '';
}

function closeVideoScreen() {
	getFlashMovie('main_flash').closeVideoScreen(); // call the function in flash to remove and hide the video
}

function resizeVideoBox(width, height) {
	$('videoBox').style.width = width + "px";
	$('videoBox').style.height = height + "px";
}

function centerVideoBox(width, height) {
	$('videoBox').style.marginLeft = (-1 * width / 2) + "px";
	$('videoBox').style.marginTop = (-1 * height / 2) + "px";
}



function showMediaPlayerEmbedVideo(html, width, height, localIsInGreenhouse) {
	
	isInGreenhouse = localIsInGreenhouse;
	
	$('mediaPlayerContainer').innerHTML = '\
        <div id="mediaPlayer"> \
        	'+html+'\
        </div>';
	
	// resize box
	$('mediaPlayer').style.width = width + "px";
	$('mediaPlayer').style.height = height + "px";
		
	// if our video is smaller than our media player canvas we need 
	// to adjust the margin further to center it in the canvas
	if (width < 500) $('mediaPlayer').style.paddingLeft = ((500 - width) / 2) + "px";
	if (height < 282) $('mediaPlayer').style.paddingTop = ((282 - height) / 2) + "px";
	
	// show box
	$('mediaPlayerContainer').style.visibility = 'visible';
	
	// set variable to true so our handleResize function positions our box properly
	isMediaPlayerEmbedVisible = true;
	
	return false;
}

function hideMediaPlayerEmbedVideo() {
	$('mediaPlayerContainer').style.visibility = 'hidden';
	$('mediaPlayerContainer').innerHTML = '';
	
	// set variable to false so our handleResize function won't touch the box
	isMediaPlayerEmbedVisible = false;
	
}

function handleMediaPlayerEmbedScroll(hasHorizontalScrollBar) {
	
	var isMediaPlayerEmbedFixed = ($('mediaPlayerContainer').style.marginLeft == "0px") ? true : false;
	
	if (isMediaPlayerEmbedFixed && !hasHorizontalScrollBar) {
		
		// un-fix the position
		$('mediaPlayerContainer').style.left = "50%";
		$('mediaPlayerContainer').style.marginLeft = "-430px";
				
	} else if (!isMediaPlayerEmbedFixed && hasHorizontalScrollBar) {
		
		// fix the position
		$('mediaPlayerContainer').style.left = "150px";
		$('mediaPlayerContainer').style.marginLeft = "0px";
		
	}
	
}



////////////////////////////// BROWSER WINDOW //////////////////////////////

// set the initial minimum height and width for flash. whenever the window is smaller than this, we'll add scrollbars. flash will change this value as needed to fit its content.
var flashWidth = 0; 
var flashHeight = 0;
var hasHorizontalScrollBar = false;
var hasVerticalScrollBar = false;
var scrollTimeoutID = 0;
var zoomScale = 1;

// global variables used for the media player embed video & browser window
var isInGreenhouse = false;
var isMediaPlayerEmbedVisible = false;



// return a value for how tall the horizontal scroll bar is, so we can compensate for it when we try to attach things to the top and bottom (only do this for non-IE browsers)
function getBrowserScrollBarSize() {
    
    if (typeof(window.innerHeight) != "number") return 0; // for ie
    
    if (navigator.userAgent.indexOf("Safari")) {
        return 15;
    } else {
        return 17;
    }
}



// return an object that has the width 
function getWindowSize() {

  var windowHeight = 0;
  var windowWidth = 0;
  
  if (typeof(window.innerHeight) == "number") {
  
    //Non-IE
    windowHeight = window.innerHeight;
    windowWidth = window.innerWidth;
    
    // compensate for the horizontal in non-IE browsers
    if (hasHorizontalScrollBar) windowHeight -= getBrowserScrollBarSize();
    
    
  } else if (document.documentElement && document.documentElement.clientHeight) {
  
    //IE 6+ in 'standards compliant mode'
    windowHeight = document.documentElement.clientHeight;
    windowWidth = document.documentElement.clientWidth;
    
  } else if (document.body && document.body.clientHeight) {
  
    //IE 4+ in quirks mode
    windowHeight = document.body.clientHeight;
    windowWidth = document.body.clientWidth;
  }
  
  return {width: windowWidth, height: windowHeight};
}



// return an object that knows how far we've scrolled horizontally and vertically
function getScrollPosition() {

  var scrollY = 0;
  var scrollX = 0;
  
  if (typeof(window.pageYOffset) == 'number') {
  
    // Netscape compliant
    scrollY = window.pageYOffset;
    scrollX = window.pageXOffset;
    
  } else if (document.documentElement && document.documentElement.scrollTop != undefined) {
    
    // IE6+ standards compliant mode
    scrollY = document.documentElement.scrollTop;
    scrollX = document.documentElement.scrollLeft;
    
  } else if (document.body && document.body.scrollTop != undefined) {
  
    // DOM compliant
    scrollY = document.body.scrollTop;
    scrollX = document.body.scrollLeft;
    
  }
  
  return {x: scrollX, y: scrollY};
}



// change the size of the flash based on the window size.
// also, set a timer to call handleScroll a moment after we've stopped resizing (because some browsers 
// don't throw a scroll event when the scrollbars pop in quickly)
function handleResize() {
    
    var windowSize = getWindowSize();
    var windowWidth = windowSize.width;
    var windowHeight = windowSize.height;
    
    hasHorizontalScrollBar = (windowWidth < flashWidth);
    hasVerticalScrollBar = (windowHeight < flashHeight);
	
    var newFlashWidth = (windowWidth < flashWidth) ? flashWidth : "100%";
    var newFlashHeight = (windowHeight < flashHeight) ? flashHeight : "100%";

    if (typeof(newFlashWidth) == "number") newFlashWidth = newFlashWidth + "px";
    if (typeof(newFlashHeight) == "number") newFlashHeight = newFlashHeight + "px"; //either add the px for pixels or leave as % sign
	
    $("flashcontentholder").style.width = newFlashWidth;
    $("flashcontentholder").style.height = newFlashHeight;
    
    handleScroll();
    
    if (isMediaPlayerEmbedVisible && isInGreenhouse) handleMediaPlayerEmbedScroll(hasHorizontalScrollBar);
    
    
    // set a timer to update once we've stopped moving
    clearTimeout(scrollTimeoutID);
    
    scrollTimeoutID = setTimeout("handleScroll()", 200);
}



// when we scroll, tell flash where the top, right, bottom and left of the window is (in flash's coordinate system).
function handleScroll() {
    
    var flashMovie = getFlashMovie("main_flash");
    
    if (flashMovie != null && flashMovie.updateBrowserWindow != null && flashMovie.updateBrowserWindow != undefined) {
        
        var scrollPosition = getScrollPosition();
        var windowSize = getWindowSize();
        
        var top = scrollPosition.y;
        var bottom = top + windowSize.height;
        var left = scrollPosition.x;
        var right = left + windowSize.width;
        
        
        // compensate for the vertical scroll bar in non-ie browsers
        if (hasVerticalScrollBar) right -= getBrowserScrollBarSize();
    
        flashMovie.updateBrowserWindow(top, right, bottom, left);
    }
}


    
// allow flash to update the size of the flash movie (this can be called from flash or from javascript set by the php)
function setFlashSize(newWidth, newHeight) {
	
    flashWidth = newWidth;
    flashHeight = newHeight;
    handleResize();
}


// called from flash passing in the StageWidth
function warnIfGreenhouseZoomed(flashMovieWidth, greenhouseOrDemo) {
	
	var windowSize = getWindowSize();
    var windowWidth = windowSize.width;
	
	zoomScale = flashMovieWidth / windowWidth;
	//alert("zoom scale, flashMovieWidth and windowWidth = " + zoomScale + " " + flashMovieWidth + " " + windowWidth);
	// if the zoom is not 100% (or close), show a warning
	if (zoomScale < .95) {
		
		alert("It looks like your browser is zoomed out.\n\nThe " + greenhouseOrDemo + " will probably not look right until you adjust back to the actual size (try the \"View\" setting on your browser).");
		
	} else if (zoomScale > 1.05) {
		
		alert("It looks like your browser is zoomed in.\n\nThe " + greenhouseOrDemo + " will probably not look right until you adjust back to the actual size (try the \"View\" setting on your browser).");
	}
}

// set up handlers to listen for the window resizing and scrolling
window.onresize = handleResize;
window.onscroll = handleScroll;
