// This javascript code takes care of rescaling and pulling in additional items from the
// news feed

// The default screen is 596 pixels
// Each newsfeed item is 92 pixels high


function addEvent( obj, type, fn ){ 
   if (obj.addEventListener){ 
      obj.addEventListener( type, fn, false );
   }
   else if (obj.attachEvent){ 
      obj["e"+type+fn] = fn; 
      obj[type+fn] = function(){ obj["e"+type+fn]( window.event ); } 
      obj.attachEvent( "on"+type, obj[type+fn] ); 
   } 
} 

//Run dynamicLayout function when page loads and when it resizes.
addEvent(window, 'load', dynamicLayout);
addEvent(window, 'resize', dynamicLayout);

function dynamicLayout() {
	var windowHeight = getBrowserHeight();
	var heightDiff = windowHeight - 595;

	if (windowHeight <= 597) { return; }

	rescale(heightDiff);
	newsItems(heightDiff);
}

function newsItems(h) {
	// Calculate the number of news items
	var n = Math.floor((383+h)/95);
	document.getElementById("newsHolder").style.height=(3+(n*94)) + "px";
}

function rescale(h) {
	// Don't rescale if the height is right!
	if (h==0) { return; }
	document.getElementById("newsBox").style.height=(409+h) + "px";
	document.getElementById("welcomeBox").style.height=(105+h) + "px";
	document.getElementById("videosBox").style.height=(104+h) + "px";
	var he = Math.floor(((103+h) - 40)/3);
	if (he > 33) { he = 33; }
	var w = Math.floor(he*4.45454545);
	document.getElementById("vid1").style.height = he;
	document.getElementById("vid1").style.width = w;
	document.getElementById("vid2").style.height = he;
	document.getElementById("vid2").style.width = w;
	document.getElementById("vid3").style.height = he;
	document.getElementById("vid3").style.width = w;
	// We need to change the height of the welcometext by round down to the nearest
	// ten.
	document.getElementById("welcometext").style.height=(Math.floor((80+h)/16)*16) + "px";
}

function getBrowserHeight(){
    if (window.innerHeight){
        return window.innerHeight;}  
    else if (document.documentElement && document.documentElement.clientHeight != 0){
        return document.documentElement.clientHeight;    }
    else if (document.body){return document.body.clientHeight;}      
        return 0;
}