//alert("hi"); //trying to see if I can get it to cache this js file 

//need to set both subwindows to closed under two conditions
//first: user is opening the main window for the first time
//second: user reloads the main page and it loses the reference (happens when reload reloads the frame rather than 
//just the current document (Safari seems to do this, IE mac reloads just the document). anyway, without the reference
//you don't want javascript to try to set the focus to the window even if it is open because without the reference, it can't.
//on the other hand, the articles window doesn't use the set focus, it uses targeting (since a new file is being loaded)
//so if the user reloads the main window (or closes it and then reopens it) you can still have that window as "open" because
//the targeting doesn't need the window reference, presumably the name is still known by the browser. (it's too bad there's no
//way to get a reference to a window using its name). I don't want to solve this by making the articles window dependent 
//on the main window since I want to make the articles window pretty fancy eventually and therefore self-sufficient.
//If I want to keep track of whether the articles window is closed I will have to make it framed just like the main window
//then I can use the onunload handler in the frame. Otherwise, if the user closes the articles window the homewindow will have
//no way of knowing it's closed and will just use target. Then targetting will be used all the time which would be okay except
//then I can't control the size of the window or where it appears. So I need to frame it. All of this assume that you don't
//know if it is closed by checking for the existence of the reference or that you choke when you check a reference to a closed
//window. If this really is the case then you need framing. If not, you can just check the existence of the reference and still
//use the full focus-target-open function. 

//reading this over again, i don't know why i don't just do a window.open with a name every time like in aristotleisdead
//i don't see why i need to know if the window is open unless iewin opens multiple window even if you give it the name
//otherwise, this is overly complicated. actually I think iewin does to multiple windows, I'll check at library
//what's happening is when you use target you aren't opening a window you are targeting a window whose opener was closed
//so the window has no opener and so it puts in the controls. probably should have the checking not based on an opener but
//based on a flag that gets set when the link is clicked in main.html

//Right now both the articles window and the questions window have the problem that if the user reloads the main page they
//are set to closed and then when they are opened again by the main page, IE windows opens another window and you have five windows
//open. (this doesn't happen if you reopen the main page from the questions window because I'm using the window.opener property
//and the questions window cookie gets set to open in the code below when the questions window reopens the main page).
//So this is the reload--->multiple windows problem. It can be solved for the articles window by only setting it to closed if the
//cookie is empty (the handlecookies file has to load before this file). 
//However, this won't work for the questions window because it uses the reference. 
//Making them dependent won't work because I don't want the questions window to close when you close the main window. 
//I guess I could try to figure out how to have the questions window close on a reload but not when it is reloaded from
//the questions window. . . Hmmmm. . . I don't think there's a way to do that because you wouldn't have the reference.
//maybe you can just have it use target if the window is open but you've lost the reference. this whole thing actually
//may not be a problem because IE windows may not reload the frame on command-R. But I'd still like to fix it because
//I don't want to have to depend on browser behavior.

//if the window is open but you've lost the reference you can use target but you have to make sure that
//when you reload losing the reference, you don't reset the cookie. that's easy enough but then it has to 
//know when the window is open but you've lost reference.

//these references will be reset if user hits refresh and reference to those windows will be lost. 
//and even if these lines don't run, a refresh will wipe all variables from this page. 
//frames prevent putting new documents in this window from erasing references but a 
//reload or refresh is just a hard wipeout. this is okay though. however if IE user
//refreshes and then clicks for the questions, he'll get two windows. I guess I could 
//try having the questions window close if the user refreshes the main window. but I woundn't
//want the questions window to close if the user closed the main window. 

//can't do window.opener to handle closing main window and reopening from questionswindow 
//because someone might open main window from another window and then it would go back to that one
//actually I can because I can check the window name!!

//first check to see if there is a reference to an opener window
//then if the opener was the questions window, get the reference
//to that window so that openorfocus_questionswindow() will just switch the focus

//I want it very clear that if there is no opener or if there is an opener but it isn't the questions window
//then the questions window will be set to closed. the else statements go outside the if {} so you can tell
//which if the else belongs to. can't do window.opener.name right off because it might choke. 

//don't need to set the questions window to open becaue it already is if it opened the main window.

//safari can't make a window reference into a string

//I've got the windows sending their reference to whoever opens them.

//apparently I did this because if you load the file into the window using target
//after a refresh then you want the homewindow to get the reference so that next time
//you can just reset the focus.
//I'm wondering if this makes the cookies unnecessary. maybe when you refresh you don't
//lose the window.opener property. if that's true then you'll never lose a reference by
//refreshing because it will always be reset when the file loads and it finds the opener.
//it will find the opener but if you opened a window and then you refresh, you'll lose
//the reference to the window you opened.

//I'm also wondering what happens to window.opener when the opener has been closed
//I assume it returns null so if (window.opener) doesn't execute

//the questionswindow gets the homewindow ref by using window.opener and vice versa if the questions window opens the home window

//baseref will get always made from http://www.crushthetest.com/main.html because that's the only file that loads this .js file
//and baseref gets made when the .js file is loaded, not by calling a function
//of course it will work in file:// mode also apparently only needed when the file is in a different folder and also only online???

var questionswindowreference = null;
var articleswindowreference = null;
var soundwindowreference = null;

var articlesnoneorblock = "none";
var articlesplusorminus = "+";


var redisplus = true;
var greenisplus = true;
var blueisplus = true;
var red = 100;
var green = 0;
var blue = 50; 
var colorchangeon=true;

var globalarticlefileurl = null;

var baseref = location.href.substring(0,location.href.lastIndexOf("/") + 1); 
//can't use relative urls if this function is called by a file not in the main directory

var roaredonce=false;

window.name='crushthetesthome'; //name main window so questions window and articles window can check openers name

//alert('about to check for openers');

if (window.opener)
{ 
   //alert('there is an opener');
   if (window.opener.parent.name == 'crushthetestquestions')
   {
      questionswindowreference = window.opener.parent;
      questionswindowreference.homewindowreference = self;
   }
   if (window.opener.parent.name == 'crushthetestarticles')
   {
      //alert('resetting articleswindowreference');
      articleswindowreference = window.opener.parent;
      articleswindowreference.homewindowreference = self;
   }
}

//need to find out if window.opener is the frame or is automatically the top window. actually new windows are opened by top window I think

//       articleswindowreference.articlesframe.location.href = globalwhicharticle; //safari gets confused on "here" link IE punts completely

//IE and safari disagree about how to use a relative reference IE thinks we are in the top document, safari thinks we are in the 
//document that called the function. however, safari switches its ideas when loadarticleinwindow is called. or maybe this isn't true
//i don't know anymore unless the problem only appears when online 

/*
Here's the deal. When you try to load an article file by clicking a link in a file in mainpagelinks, it can't get the articles frameset
because it tries to find it in mainpagelinks even though the function (this function) is owned by the parent which is in the main
directory. this is a very good reason to use baseref all the time. In particular if you try to load scoring.html from howtobreak700.html
which in in mainpagelinks, it won't work with relative urls.
*/

function setroaredonce()
{
   roaredonce=true;
}

function loadarticleinwindow()
{     
      //alert('in loadarticleinwindow');
      //this function is called when a new articles window is opened
      //this function is called by the articles window frameset
      //this function knows the globalarticlefileurl so it can load the proper file
      //I'm not sure if you could put this function in the articles window frameset
      //because how would you pass the globalarticlefileurl
      //it's actually called by the blankarticle.html file not by the frameset itself

      if (!articleswindowreference) 
      { 
         //alert('no reference yet');
         setTimeout("loadarticleinwindow();",200);
      }
      else
      { 
         //alert('found reference');
         articleswindowreference.articlesframe.location.href = globalarticlefileurl; 
      }
}

function openortarget_articleswindow(articlefilename)
{  

   //alert('inopenortarget');
   globalarticlefileurl = baseref + "articles/" + articlefilename; //if function called by file in mainpagelinks, relative urls won't work

   //alert(location.href); //this gives the index.html location even though the file that called it is main.html and that's good
                          //because this function belongs to index.html 

   if (articleswindowreference) 
                                 // quirksmode says use !articleswindowreference.closed
                                 //quirksmode idea caused it to choke, jumped out of function
                                //will be set to null when window closes by onunload event handler in the frameset so don't have to 
                                //worry about choking which I think happens. if it doesn't you don't need frames
   {
      //alert('changing focus');
      articleswindowreference.focus();
      articleswindowreference.articlesframe.location.href = globalarticlefileurl; //put article in tab but doesn't do focus
      return false;
   }
   else
   {
      if (getcookiedata("articleswindow") == "open")
      {
        // alert('using target');
         return true;
      }
      else
      { 
         //alert('opening new window');
         var windowsettings = "height=500,width=500,top=100,left=450,resizable=yes,status=yes,scrollbars=yes"; //need status for safari or messed up
         var windowname = "crushthetestarticles";
         var articlesframefileurl = baseref + "ctt_articles_fset.html"; //if this function is called by a file in mainpagelinks, it won't find this fset w/o a baseref
      
         window.open(articlesframefileurl,windowname,windowsettings);  //I don't think the blank article thing is necessary, could just load the article
         //maybe the idea was to have a frame for the articles so loading new ones wouldn't kill variables. 
      
         return false;
      }
   }
}

function openorfocus_questionswindow()
{

//IE loses references when you close a window so e.g. alert(reference) will cause a choke. instead of checking for open window using
//the reference I'm using cookies. when the home window loads, the cookies for the subwindows are set to closed. when a subwindow
//is opened, the corresponding cookie is set to open. if the user closes the subwindow, the cookie is set back to closed using
//the onunload event handler. the getcookiedata function belongs to the top frame via the handlecookes.js file. this file is 
//also loaded by the top frame so the top.getcookiedata() could also just be getcookiedata().

//you have to remember to make the input to the getcookiedata function a string.

//if closed then open a new one, if open but no reference, return true and use target, if open with reference set focus.
//problem IE has with references to closed windows will be irrelevant because if the window is closed the cookie will know
//and direct the opening of a new window. if the window is open but the reference is lost, it will be set to null by the top
//of this file. this assumes cookie is either open or closed but it will do the else even if cookie is blank. this should never
//happen because whenever window opens it sets the cookie to open and the top of this file sets the cookie to closed when it loads
//unless this file was loaded by the questions window in which case it has already been set to open. 

//actually the questionswindowreference is set to null when the questions window is closed. this prevents any possible choke.

//quirksmode says IE5 for mac needs the !questionswindowreference.closed. apparently if window has been closed
//IE 5 mac will choke on questionswindowreference.location or maybe it will return some location as if the window
//is still open so first make sure it hasn't been opened then closed then if that's not the case see if it is open
//I used to have just if (questionswindowreference) but IE7 with tabs seems to be having trouble with this so I'll
//try the quirksmode solution where you look at a characteristic of questionswindowreference rather than the reference
//itself. quirksmode doesn't work
   
   if (questionswindowreference)
   {
      //alert('setting focus to questions window');
      questionswindowreference.focus(); 
      return false;
   }
   else
   {
      if (getcookiedata("questionswindow") == "open") //if the window is open but reference is lost, could happen with refresh
      {
         //alert('ref lost but cookie says window open so return true and use target');
         self.blur(); //this is the only thing that works
         return true;
      }
      else //window is not open
      {
        //alert("trying to open new window");
        var windowsettings='location=no,toolbar=no,status=yes,menubar=no,resizable=yes,scrollbars=yes,left=10,top=10,height=600,width=900';
        var windowname='crushthetestquestions';
        var filetoload= baseref + 'ctt_questions_fset.html'; //ctt_questions_fset.html since this function is called from main.html which is in the same directory, relative urls would work, but I'll stay consistent
        //alert('filetoload is ' + filetoload); 
        questionswindowreference = window.open(filetoload,windowname,windowsettings); //(filetoload,windowname,windowsettings) prob redundant to get the ref here since it is sent by questionswindowcontrol.js when window opens.
        //alert('opened new window');
        return false;
      }          
   }
}

//function setfocustoquestionswindow()
//{
//   alert ('in setfocustoquestionswindow')
//   alert('questionswindowreference = ' + questionswindowreference);
//   questionswindowreference.focus();
//}
