// bottom.js: accessed by every page to chance color of tab, even home.html.
// bottom needs a few global vars such as ForumLink...(due to parallel loading, var set by home.html may not be seen...
// vars stored in index.html are accessed.
// 
// 10-10-11: paypal problem remove frame for myAccount
// v 1.13 bottom hover background?
// v 1.12: problem with ass.array 'map' could not be solved in canada.
// v 1.11 mouse over and out color scheme changed, we can get rid of aiFlag..
// v 1.10 introduce Chrome with IE for special handling with the blue line over task bar.
// v 1.9 forward Problems
// v 1.8 to myaccount simplified 1.7 do not need videoclip here any more..
// v 1.6 goHome
// v 1.5 import from TEST mini with local oTop..
// v 1.4 forum redirect..
// v 1.3: move logout?
// working as of 5-31-10
var oTop = oGetTESTTopLocal(); //type Window, top page no matter who calls this
var iDEBUG = oTop.iDEBUG;
var bWEB = oTop.bWEB;
var  FORUMLINK = oTop.FORUMLINK; // does not work, picks up 'xxx' as oTop may not have been defined?
var  VIDEOLINK = oTop.VIDEOLINK;



var asTip = null;
asTip = [];
asCurrentTip = [];
   // =[new Array()]];   //Associative Array
asTip["home"]= "<nobr>thermofluids.net or myTEST in local drive</nobr>";
asTip["daemons"]="<nobr>Systematic navigation to thermodynamic calculators</nobr>";
asTip["rias"]="<nobr>Simulation of thermal systems by rich internet apps (RIAs)</nobr>";
asTip["tables"]="<nobr>Traditional tables and charts in SI and English units</nobr>";
asTip["map"]="<nobr>Jump to a daemon page using this navigation map</nobr>";
asTip["tutorial"]="<nobr>How to use daemons and other resources</nobr>";
asTip["examples"]="<nobr>Manual and TEST solutions of selected problems</nobr>";
asTip["problems"]="<nobr>Chapter-end problems</nobr>";
asTip["animations"]="<nobr>Illustrations of systems and fundamental concepts</nobr>";
asTip["myAccount"]="<nobr>Change settings, subscribe, download license file</nobr>";
asTip["blog"]="<nobr>Post a comment or question</nobr> ";
asTip["logout"]="<nobr>Remember to log out";
asTip["release"]="<nobr>F.A.Q.";

asCurrentTip["home"]= "<br/>the home page of TEST";
asCurrentTip["daemons"]="<br/>the Daemons module";
asCurrentTip["rias"]="<br/>the RIAs module";
asCurrentTip["tables"]="<br/>the Tables module";
asCurrentTip["map"]="<br/>the navigation map";
asCurrentTip["tutorial"]="<br/>the Tutorial module";
asCurrentTip["examples"]="<br/>the Examples module";
asCurrentTip["problems"]="<br/>the Problems module";
asCurrentTip["animations"]="<br/>the Animations module";
asCurrentTip["myAccount"]="<br/>the MyAccount module";
asCurrentTip["blog"]="<br/>the Forum";
asCurrentTip["logout"]="<nobr>Remember to log out";
asCurrentTip["release"]="<nobr>F.A.Q.";


//asTip["release"]="<nobr>F.A.Q.";


//set the global vars during render
function setGlobals(){
    oTop = oGetTESTTopLocal(); //type Window, top page no matter who calls this
    iDEBUG = oTop.iDEBUG;
    bWEB = oTop.bWEB;
    VIDEOLINK = oTop.VIDEOLINK;
}
//get top window (thermofluids. local, vs test..),also in reg.js
function oGetTESTTopLocal(){
    // the following logic moves up but never goes to thermofluids...
    var oTop = parent;
    while (oTop.frames[0].name !='main') oTop = oTop.parent;
    return oTop;
}

//forward to my account
function  handleToMyAccount(){

    
    // 0. test if local
    if(!oTop.bWEB){
        var oPopup = new PopupT('floatWindow');
        oPopup.alertFloat('You have to be online to log on to the myAccount page. Please visit www.thermofluids.net (or a suitable mirror site) and try again. ');
        return;
    }
//    confirm('The MyAccount page will open in a new window/tab. For account security, you have to log in (with the same id and password) even if you are currently logged into TEST. Proceed?');
//
//   // 5. old way (use of myaccount.php) vs. new way local ajaxcodes/license/authenticateMyAccount.php are controlled in Global.js with iPUBLISH
//
//    //changeClassName("myAccount");
//    //oTop.main.location = oTop.MYACCOUNTURL;
//    //window.top.location = oTop.MYACCOUNTURL;
//    window.open(oTop.MYACCOUNTURL, 'licenseWindow');
//    // 15. change color
//    //changeClassName("myAccount");

       if( confirm('The MyAccount page will open in a new window/tab. For account security, you have to log in (with the same id and password) even if you are currently logged into TEST. Proceed?')) window.open(oTop.MYACCOUNTURL, 'licenseWindow');
       else return;
}


//forward to problems
function  handleToProblems(){
    // 0. test if local
    changeClassName("problems");
    // 5. forward
    if(!oTop.bWEB){
        var s = 'For the latest release of the Problems module and a wider selection of problems, please log on to the Web edition of TEST at www.thermofluids.net. \n\nThe local page that follows carries only a subset of the online Problems module. ';
        //alertAppletBottom(s);
        alert(s);
    }

    oTop.main.location = "Test/problems/problemsIndex.html";
}

//forward to forum
function  handleToForum(){
    //alert('forum' +oTop.bWEB);
    // 0. test if local
    if(!oTop.bWEB){
        var oPopup = new PopupT('floatWindow');
        oPopup.alertFloat('The forum is accessible only online, and not from local installations. Please log on to the Web edition of TEST at www.thermofluids.net and try again.');
        return;
    }
    // 5. test if not logged in
    if(!oTop.bLOGIN){
        var oPopup = new PopupT('floatWindow');
        oPopup.alertFloat('You must login before you can access the forum. ');
        return;
    }

    // 10. allow every host to link to FORUMLINK set in Global.js
    oTop.main.location = oTop.FORUMLINK;
    changeClassName("blog");
    return;
}

// draw the bottom taskbar
function renderTaskBar(){
    setGlobals();
   //while on web, oTop does not get updated info from home.html as they load parallel
    //alert('in bottom.js renderTaskBar'+     FORUMLINK+" "+oTop.FORUMLINK + " ");
    document.write(getTableHTML());
    //alert('in render bot: '+oTop.bLOGIN);
    //if not logged in or local, do not show logout.
    //alert(oTop.bWEB);
    if(!oTop.bLOGIN && oTop.bWEB){
        //xRemoveClass($('logout'),'notSelected');
        //xRemoveClass($('logout'),'logoutStyle');
        xAddClass($('logout'),'hide');
    }
    if(!oTop.bWEB){
        //alert('remove logout');
        xRemoveClass($('logout'),'notSelected');
        xRemoveClass($('logout'),'logoutStyle');
        xAddClass($('logout'),'hide');
    }
}

    // draw the lgout button, called from Home.js
    function showLogout(){
        if($('logout')){
            $('logout').style.display = 'inline';
            xRemoveClass($('logout'),'hide');
            //alert('set classes');
        }
    }

    //logout accessible from all pages..even called from Home.js
    function handleLogout(){

        //construct inJson
        var sID = oTop.asCOOKIEITEM[6];//hardcode needed as reg.js is unknown at this point.
        var sCEXP = oTop.sSERVEREXP; //mysql time stamp..in server time
        var sInJSON = sInJSON = '{' + '"sID":"' + sID + '"' + ', "sCEXP":"' + sCEXP + '"' + '}';
        var oBlogSynch = new SynchronizerPHP("ajaxcodes/phpcurl/", oTop.PHPSERVLETPATH);
        oBlogSynch.standardUpdate(sInJSON, 'logout.php', this.logOutCallback, this);


        //removing (resetting) cookie, must be done at teh very end after logout call to remove server cookie.

        var oNow = new Date();
        var sCookieName = 'ThermoCookie';
        document.cookie = sCookieName+"="+oTop.GLOBALCOOKIEWDEFAULT + "; expires="+oNow.toGMTString()+"; path=/";
        //var sRead = document.cookie;
       // alert('cookie reset: '+sRead);

        oTop.tEXPIRATION = 0;
        oTop.bLOGIN = false;
        oTop.setGlobalVars(); // reset ram cookie to Mr. Guest

    }


    // callback after all the cookie arrive
    function  goHome() {
        //alert('in bottom');
        oTop.location = parent.location; //"home.html";
    }
    // callback after all the cookie arrive
    function  logOutCallback() {
        var args = $A(arguments);
        var oSynchronizer = args.shift(); // to nullify
        var sJSON = args.shift();  // all info comes back in it
        var oThis = args.shift();   // want this

        try{

            $('logout').style.display = 'none';
            var oRpatt = /\{.*\}/; //extract json from verbose return string
            var asJson = sJSON.match(oRpatt);
            if( asJson!=null && asJson.length == 1) sJSON = asJson[0];
            var oInData = eval("(" + sJSON + ")"); // object containing form data

            //alert('call back after ajax logout in bot'+sJSON+' '+oInData.iError);
            oTop.bLOGIN = false;
            // need to reset cookie...
            oTop.main.location = "home.html";
            }
            catch(err){
                oSynchronizer = null;
                alert("The operation could not be completed; please check your network connection and try again.");
                return;
            }
            oSynchronizer = null;
        return;
    }




//instead of using this function use changeClassName directly.
// this is called by most pages to change the hightlight of the selected item on task bar.
function highlight(){
    if(!parent.main) return;
    var ref = parent.main.location.href;
    //alert('in  hightlishg refrence'+ref);
    //alert(document.getElementsByTagName(moduleNames[1]).style);
    if ( ref.indexOf("/myaccount.html")!= -1){
        changeClassName("myAccount");
        return ;
    }
    if ( ref.indexOf("/solve/")!= -1){
        changeClassName("daemons");
        return ;
    }
    if ( ref.indexOf("/rias/")!= -1){
        changeClassName("rias");
        return ;
    }
    if ( ref.indexOf("/intro/")!= -1){
        changeClassName("tutorial");
        return ;
    }

    if ( ref.indexOf("/problems/")!= -1&& ref.indexOf("Ex.html")!= -1){
        changeClassName("examples");
        return ;
    }
    if ( ref.indexOf("/problems/")!= -1){
        changeClassName("problems");
        return ;
    }
    if ( ref.indexOf("/vtAnimations/")!= -1){
        changeClassName("animations");
        return ;
    }
//    if ( ref.indexOf("/visualtour/")!= -1){
//        changeClassName("slides");
//        return ;
//    }
    if ( (ref.indexOf("help.html")!= -1)
        || (ref.indexOf("faq")!= -1)){
        //top.bottomPanel.changeIt(9);
        changeClassName("release");
        return ;
    }
    if ( ref.indexOf("navigation")!= -1){
        //top.bottomPanel.changeIt(2);
        //alert('nav caught');
        changeClassName("map");
        //alert('here');
        return ;
    }
    if ( ref.indexOf("/testhome/")!= -1){
        changeClassName("home");
        return ;
    }
}

//static way to write stuff in a header...bottom.html calls it from the head... a dynamic way
function loadCss(){
    document.write('<link href="ajaxcodes/login/global.css" rel="stylesheet" type="text/css">');

    document.write('<link rel="stylesheet" href="jscripts/styleSheetBottomM.css" type="text/css">');
}

// the dynamic way also require an url, not already loaded codes.
function dhtmlLoadScript(url){
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}


// this alert can be called from any page, mostly by applets through pageMaker.
function alertAppletBottom(s){
    //alert('trying to do alert applet');
    var oPopup = new PopupT('floatWindow');
    oPopup.alertApplet(s);
}



// draw the bottom taskbar
function getTableHTML(){
    var s = '<div class="borders">';
    s += '<ol  id="toc" class="taskbar">';
    s += '<li id="home" class="normal" onclick= \'goHref("home", "home.html")\' onmouseover=mouseOver("home"); onmouseout=mouseout("home");>Home</li>';
    s += '<li id="map"  class="normal"  onclick= \'goHref("map", "Test/help/navigation.html")\' onmouseover=mouseOver("map"); onmouseout=mouseout("map"); >Map</li>';
    s += '<li id="daemons"  class="normal" onclick= \'goHref("daemons", "Test/solve/solve.html")\' onmouseover=mouseOver("daemons"); onmouseout=mouseout("daemons");>Daemons</li>';
    s += '<li id="rias"  class="normal" onclick= \'goHref("rias", "Test/rias/rias.html")\'  onmouseover=mouseOver("rias"); onmouseout=mouseout("rias");  >RIAs</li>';
    s += '<li id="tables"  class="normal" onclick= \'goHref("tables", "Test/solve/basics/tables/tables.html")\'onmouseover=mouseOver("tables") onmouseout=mouseout("tables");>Tables</li>';
    s += '<li id="animations"  class="normal" onclick= \'goHref("animations", "vtAnimations/index.html")\'  onmouseover=mouseOver("animations") onmouseout=mouseout("animations");>Animations</li>';
    s += '<li id="problems"  class="normal" class="default" onclick=handleToProblems() onmouseover=mouseOver("problems") onmouseout=mouseout("problems") >Problems</li>';
    s += '<li id="examples"  class="normal" onclick= \'goHref("examples", "Test/problems/problemsIndexEx.html")\' onmouseover=mouseOver("examples") onmouseout=mouseout("examples");>Examples</li>';
    s += '<li id="tutorial"  class="normal" onclick= \'goHref("tutorial", "Test/intro/introcontrols/introIndex.html")\'  onmouseover=mouseOver("tutorial") onmouseout=mouseout("tutorial");>Tutorial</li>';
    s += '<li id="blog"  class="normal" onclick=handleToForum() onmouseover=mouseOver("blog") onmouseout=mouseout("blog")>Forum</li>';
    s += '<li id="myAccount"  class="normal" class="default" onclick=handleToMyAccount() onmouseover=mouseOver("myAccount") onmouseout=mouseout("myAccount")>MyAccount</li>';
    s += '<li id="logout"  class="normal" onclick=handleLogout() onmouseover=mouseOver("logout") onmouseout=mouseout("logout") >Logout</li>';
    s += '<li id="release"   class="normal" onclick= \'goHref("release", "help.html")\' onmouseover=mouseOver("release") onmouseout=mouseout("release")> Version:' + parent.sVERSION + '; Copyright: Prof. Subrata (Sooby) Bhattacharjee&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>';
//alert(parent.sVERSION);
    s += '</ol>';
    s += '</div>';
    return s;
}

//change background color and tooltip
function mouseOver(sModuleName){

    var sTip = this.asTip[sModuleName];
    var oModule = document.getElementById(sModuleName);
    //alert(oModule.className);
    if(oModule.className.indexOf('occupied') == -1){
        oModule.className = 'normal hover';
    }
    else{ //selected module
        sTip = 'You are at '+this.asCurrentTip[sModuleName];
    }
    //var sTip = this.asTip[sModuleName];
    Tip(sTip, OFFSETY, -4, OFFSETX, 20, DELAY, 2000);
}

//mouse has moved out of a bottom block
function mouseout(sModuleName){
    UnTip('');
    var oModule = document.getElementById(sModuleName);
    if(oModule.className.indexOf('occupied') == -1){
        oModule.className = 'normal';
    }

    if(oModule.className == 'hover') oModule.className="notSelected";
}
//forward to my account, also duplicated in registration.js
function  goHref(sID, sPageURL){
    // 0. set relative path
    oTop.main.location = sPageURL;
    // 15. change color
    changeClassName(sID);
}
//change background color of a module on task bar to green, module nams are keys at tht top, not what is in the test task bar.
function  changeClassName(sModuleName, sLevel){
  //if(sModuleName == 'problems') alert('map caught');
    if(!sLevel) sLevel = 1;
    var oDoc, oModule;
    if (sLevel==1 && parent.bottomPanel)oDoc = parent.bottomPanel.document;
    else if (sLevel==2 && parent.parent.bottomPanel) oDoc = parent.parent.bottomPanel.document;
    else if (sLevel==0)oDoc = document;

     for (var sName2 in this.asTip){

     //if(sModuleName == 'daemons') alert('myAccount caught:' + sName2);
        if(oDoc){
          oModule = oDoc.getElementById(sName2);
          if(oModule){
            if(sName2 == 'logout') break;
            if(sName2 == sModuleName) {
              //alert('match found for' + sModuleName +' ' + sName2);

               oModule.className="normal occupied"; // allows cascading now
            }
            else oModule.className="normal"; //used just to remove current
          }
        }
    }
}
