/** RSH must be initialized after the
    page is finished loading. */

dojo.require("dojo.undo.browser");

function removeHash(hashValue) {
      if (hashValue == null || hashValue == undefined)
         return null;
      else if (hashValue == "")
         return "";
      else if (hashValue.length == 1 && hashValue.charAt(0) == "#")
         return "";
      else if (hashValue.length > 1 && hashValue.charAt(0) == "#")
         return hashValue.substring(1);
      else
         return hashValue;
}
dojo.addOnLoad(function(){
	var initialLocation = removeHash(location.hash);
	if(initialLocation) {
		locArr = initialLocation.split(":");
		updateSubsite(1,locArr[1],1);
		updateSubsite(2,locArr[2],1);
		updateSubsite(3,locArr[3],1);
		updateSubsite(4,locArr[4],1);
		updateSubsite(locArr[0],locArr[locArr[0]],1);
		theDiv = locArr[0];
	} else {
		var loc = new Array();
       _doAjaxUpdate(1,activePageInCol[1],1);
       updateSubsite(2,activePageInCol[2],1);
       _doAjaxUpdate(3,activePageInCol[3],1);
       _doAjaxUpdate(4,activePageInCol[4],1);
	   theDiv = 2;
	}
	var appState = new ApplicationState("",theDiv,activePageInCol);
	//appState.showStateData()
	dojo.undo.browser.setInitialState(appState);


});

//window.onload = initialize;
var isReady= 1;
function initialize() {

  // initialize RSH

  dhtmlHistory.initialize();
  // add ourselves as a listener for history
  // change events
  dhtmlHistory.addListener(handleHistoryChange);



  // determine our current location so we can
  // initialize ourselves at startup
  var initialLocation = dhtmlHistory.getCurrentLocation();

  // if no location specified, use the default


   if (initialLocation == null || initialLocation=="") {
//    initialLocation = "location1";
   //if(dhtmlHistory.isFirstLoad()) {
      //alert("Initializing MOC Systems site since no initialLocation is given...");
      _doAjaxUpdate(1,activePageInCol[1],1);
      updateSubsite(2,activePageInCol[2],1);
      _doAjaxUpdate(3,activePageInCol[3],1);
      _doAjaxUpdate(4,activePageInCol[4],1);
   } else {
    //alert("Initial location: "+initialLocation);
      locArr = initialLocation.split(":");
      updateSubsite(1,locArr[1],1);
      updateSubsite(2,locArr[2],1);
      updateSubsite(3,locArr[3],1);
      updateSubsite(4,locArr[4],1);
      updateSubsite(locArr[0],locArr[locArr[0]],1);
   }
   isReady = 1;

  // now initialize our starting UI
  //updateUI(initialLocation, null);
}

/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation,
                             historyData) {
  // use the history data to update our UI
  //alert('Test: '+newLocation+"::"+historyData);
  //alert("Last changed col was: "+historyData);
  //updateUI(newLocation, historyData);
  if(historyData == null) {
    if(newLocation != null && newLocation != "") {
        //alert("Need to  update all columns with location: "+newLocation);
    }
  } else {
    locArr = newLocation.split(':');
    //alert("Update col "+historyData+" with content uid="+locArr[historyData]);
    updateSubsite(historyData,locArr[historyData],1);
  }
}


var activePage = 0;
var activePageInCol = new Array(0,0,0,0,0);
function menuClick(imgObj) {
    imgObj.src = eval(name+"_a.src");
}
/*
  Updates a single subsite with so a given page is active.

*/
var activeCol = null;
var activeColHeader = null;
function updateSubsite(theDiv,uid,dontUpdateHistory){
     target = 'col'+theDiv+'_content';
     col = document.getElementById(target);
     col.number = theDiv;
     if(uid==0) {
        uid=activePageInCol[theDiv];
     }
     if(activeCol != col) {
         //Need to loop over all menu images in the new col and reset state.
         if(activePage != 0) {
            /*var old_imgname = "mocmenu_image_"+activePage;
            document[old_imgname].src =eval("mocmenu_ACTOTHERCOL_image_"+activePage+".src");
            */
            setMenuState(activePage,theDiv,'ACTOTHERCOL');
         }
     } else {
         if(activePage != 0) {
            /*
            var old_imgname = "mocmenu_image_"+activePage;
            document[old_imgname].src =eval(old_imgname+".src");
            */
             setMenuState(activePage,theDiv,'NO');
         }
     }
     if(activePageInCol[theDiv]!=0) {
       /*old_act = "mocmenu_image_"+activePageInCol[theDiv];
       document[old_act].src=eval("mocmenu_image_"+activePageInCol[theDiv]+".src");;
       */
       setMenuState(activePageInCol[theDiv],theDiv,'NO');
     }

     if(activeCol) {
        activeCol.style.backgroundColor = "#d4dadf";
        setTopMenuState(activeCol.number,'NO');
     }
     col.style.backgroundColor = "#ffffff";
     setTopMenuState(theDiv,'ACT');

     colHeader = document.getElementById('col'+theDiv+'_header');
     if(activeColHeader) {
        activeColHeader.style.backgroundColor = "#d4dadf";
     }
     colHeader.style.backgroundColor = "#ffffff";
     activeColHeader = colHeader;
     activeCol = col;
     activeCol.number = theDiv;
     activePageInCol[theDiv]=uid;
     //alert("Updating frame (div) " + target + " with uid="+uid);


     setMenuState(uid,theDiv,'ACT');

     activePage = uid;
     //var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
     _doAjaxUpdate(theDiv,uid,dontUpdateHistory);

}
/**
*
*/
function moc_menu_click(theDiv,uid,dontUpdateHistory){
    if(isReady) {
        updateSubsite(theDiv,uid,dontUpdateHistory);
    }
}
/**
*
*/
function topMenuClick(col,uid) {
    if(isReady) {
        updateSubsite(col,uid);

    }
}
/**
*
*/
function setMenuState(uid,col,state) {
     var imgname = "mocmenu_image_"+uid;
     if(state == "NO") {
        var newname = "mocmenu_image_"+uid;
     } else {
        var newname = "mocmenu_"+state+"_image_"+uid;
     }
     if(document[imgname]) {
        document[imgname].src =eval(newname+".src");
     }
}
/**
*
*/
function setTopMenuState(col,state) {
     var imgname = "moctopmenu_image_"+col;
     var newname = "moctopmenu_"+state+"_image_"+col;
     if(document[imgname]) {
        document[imgname].src =eval(newname+".src");
     }
}
/**
*
*/
function _doAjaxUpdate(theDiv,uid,dontUpdateHistory) {
     var url = 'index.php';
     var pars = 'id='+uid+'&type=59';
     dojo.io.bind({
        url: url+'?'+pars,
        load: function(type, data, evt) {

          document.getElementById('col'+theDiv+'_content').innerHTML = data;
          if(dontUpdateHistory == null) {
/*
             hash = theDiv+':'+activePageInCol[1]+':'+activePageInCol[2]+':'+activePageInCol[3]+':'+activePageInCol[4];
             dhtmlHistory.add(hash,theDiv);
             /**/
             var state = new ApplicationState(data,theDiv,activePageInCol);
			 dojo.undo.browser.addToHistory(state);
          }
        },
        mimetype: "text/plain",
        encoding: "utf-8",
        transport: "XMLHTTPTransport"
     });
}

ApplicationState = function(stateData, activeBox, boxArr){
	this.activeBox = activeBox;
	this.boxArr = boxArr;
	this.stateData = stateData;
	//this.changeUrl = bookmarkValue;
	//             hash = theDiv+':'+activePageInCol[1]+':'+activePageInCol[2]+':'+activePageInCol[3]+':'+activePageInCol[4];

	this.changeUrl = activeBox+':'+boxArr[1]+':'+boxArr[2]+':'+boxArr[3]+':'+boxArr[4];
}

ApplicationState.prototype.back = function(){
//  alert("Going back with box "+this.activeBox+"::"+this.boxArr[this.activeBox]);
  //alert("Data: "+this.stateData);
  updateSubsite(this.activeBox,this.boxArr[this.activeBox],1);
}

ApplicationState.prototype.forward = function(){
  //alert("Going back with box "+this.activeBox+"::"+this.boxArr[this.activeBox]);
  updateSubsite(this.activeBox,this.boxArr[this.activeBox],1);
}

