// SMU COX Flash navigation helper script

// 6/2007 - Joe Hanish - jhanish@cox.smu.edu
// *****************************************
//
// Everything in this script starts with showMenu() which is called from a page that needs nav.
// This assembles all the required info and calls the flash movie.
// It also pulls the size that it THINKS the menu is from the DB.
// When the dynamic nav is finished building itself, it compares the size that it REALLY is,
// with the size that the database thinks it is.  If they are unequal, this script writes the
// new value to the DB and then reloads the nav, with it's correct sizing in the HTML.
//
// The reason for all this rigamarole is plenty...
// You can't hide a movie in IE.  If it's hidden, it's not loading, and it has no way 
// to figure out how big it is, nor can it recursively call itself.  BOO!
// The movie would do a nasty double blink when it reloaded itself every single time,
// which is why I started storing the height in the DB.  Now, when the script starts,
// it throws a quick call to the server to get the height, but it loads perfect now.
// Only the first person to see the menu gets to see the rotten double blink.
// Yay for dynamic nav!!!!!


var TESTPREFIX_URL = "http://www.cox.smu.edu";
var MENU_SERVER_ADDRESS = "nav.cox.smu.edu";
//var MENU_SERVER_ADDRESS = "129.119.81.239:3000";
var DESTINATION_DIV_TAG = "coxnav";

var coxnav_height = 0;
var coxnav_url = "";
var coxnav_testpre = false;

function randomXDigitNumber(size) {
  return Math.floor(Math.random() * (Math.pow(10,size)));
}


function whack(str_to_whack, num_chars_to_whack) {
  if (str_to_whack.length <= num_chars_to_whack) {
    return "";
  }
  return str_to_whack.substr(0, str_to_whack.length - num_chars_to_whack);
}


function whack_trailing_slash(str_to_whack) {  
  if (str_to_whack.charAt(str_to_whack.length - 1) == "/") {
    return whack(str_to_whack, 1);
  }
  else {
	return str_to_whack;
  }
}


function loadContent(file){
  var head = document.getElementsByTagName('head').item(0)
  var scriptTag = document.getElementById('loadScript');
  if(scriptTag) head.removeChild(scriptTag);
  script = document.createElement('script');
  script.src = file;
  script.type = 'text/javascript';
  script.id = 'loadScript';
  head.appendChild(script)
}


function loadContentWithoutResponse(file){
  var head = document.getElementsByTagName('head').item(0)
  var scriptTag2 = document.getElementById('loadScript2');
  if(scriptTag2) head.removeChild(scriptTag2);
  script = document.createElement('script');
  script.src = file;
  script.type = 'text/javascript';
  script.id = 'loadScript2';
  head.appendChild(script)
}

function getIntFromRails(url) {
  var s = document.createElement("script");
  s.src = url;
  s.type = 'text/javascript';
  alert(s);
  //s.defer = true;
  
  var head = document.getElementsByTagName('head').item(0);
  head.appendChild(s);
  
  return s;
}

function sizer_error(err) {
	alert(err);
}

function showMenu(height, menuurl, testprefix) {
    
  // Fake default args.
  var height = (height == null) ? 0 : height;
  var menuurl = (menuurl == null) ? "" : menuurl;
  var testprefix = (testprefix == null) ? false : true;	

  // If it wasn't specifically designated in the function call,
  // figure out what menu we're going to load from the rails xml server
  // by grabbing the path from the browser.	
  if (menuurl == "") {
    menuurl = "http://" + MENU_SERVER_ADDRESS + "/menu" + location.pathname;
  }

  // Whack the trailing "/" if there is one...
  menuurl = whack_trailing_slash(menuurl);
  
  // Put this in our global so the callback has access.
  coxnav_url = menuurl;
  
  // Get the url together to request the height of this movie from the server.
  sizepath = "http://" + MENU_SERVER_ADDRESS + "/size" + location.pathname;
  
  // Load up the dynamic js to get the menu height, and then continue on.
  // (what the server returns has a callback to continueNavDisplay() )
  loadContent(sizepath);
  
}


function continueNavDisplay() {
  
  random = randomXDigitNumber(6);

  var movie_url = "http://" + window.location.host + "/cms/nav/coxnav.swf?r=" + random + "&menu_url=" + coxnav_url + "&navheight=" + coxnav_height;
  
  if (coxnav_testpre != false) {
	  movie_url += "&testprefix=" + TESTPREFIX_URL;
  }

  var so = new SWFObject(movie_url, "flashmovie", "150", coxnav_height, "6", "#0F1D4E");
  so.write(DESTINATION_DIV_TAG);   
}


function storeMenuHeight(newheight) {
	// Gather up the url for the call to the server.
	var ajax_url = "http://" + MENU_SERVER_ADDRESS + "/size/repair/" + newheight + location.pathname;
	ajax_url = whack_trailing_slash(ajax_url);
	
	// Push the new height into the database
	loadContentWithoutResponse(ajax_url);
	
	// and then show the menu...
	showMenu(newheight);			
}



