// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Description : This library should contain ROOMS~specific functions.
// This file is NOT Velocitized
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Supervisor: Dave Dean
// Programmer: Jeremy Ford
// Date: *before* December 5, 2003
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



   function isDebug()
   {
        return false;
   }



function doSearch(theForm) {
//   var searchType = theForm.search_type.options[theForm.search_type.selectedIndex].value
   var searchType = theForm.search_type.value;
   
   if(searchType == "SingleSearch") {
     var f = document.SingleSearchForm;
     f.queryStatement.value = theForm.query.value;
     beforeSubmit(f);
     f.submit();
   } else if (searchType == "Lucene") {
     var f = document.RoomsSearchForm;
     f.query.value = theForm.query.value;

     f.submit();
   }
}

function framedRedirect(url,openInPopup) {
    var f = document.FramedRedirectForm;
    
    if (f.open_in_popup == null) {
        //consolidated view
        if (openInPopup == "false") {
            window.open(url,"MainRoomsWindow");
        }
        else {
            window.open(url,"_blank");
        }
    }

    else {
    
        f.target="";
        f.open_in_popup.value = "no";
        f.url.value = url;
    
        if((openInPopup == "yes") || (openInPopup == "true")) {
            f.target="_blank";
            f.open_in_popup.value = "yes";
        }
    
        f.submit();
    }
}

function framedRedirectRateIt(url, openInPopup, id, score, votes) {
    var f = document.FramedRedirectForm;

    f.target="";
    f.open_in_popup.value = "no";
    f.url.value = url;
    f.id.value = id;
    f.score.value = score;
    f.votes.value = votes;

    if((openInPopup == "yes") || (openInPopup == "true")) {
        f.target="_blank";
        f.open_in_popup.value = "yes";
    }
    
	f.submit();
}

var myWindow;
function openChild(myURL, myName, myParams) { 
    if(myWindow && !myWindow.closed) { 
        myWindow.location = myURL; 
        setWindowFocus(myWindow);
    } else { 
        myWindow = window.open(myURL, myName, myParams);
    }
}

function setStatus(msg) {
    window.status=msg;
}

function setHash(hash) {
  document.location.hash= hash;
}


function addEngine(basePath, plugin)
{
  if ((typeof window.sidebar == "object") &&
      (typeof window.sidebar.addSearchEngine == "function")) {
      window.sidebar.addSearchEngine(basePath + 'plugins/' + plugin + '.src',
                                     basePath + 'pugins/' + plugin + '.jpg',
                                     'Academic',
                                     'Academic');
      
  } else {
      alert("Cannot add Search Engine");
  }
}

// functions called from default.vm

function setContainerWidth() {
		//getPreferred();
		if (window.name.substr(0,13) == "BUILDER_POPUP" || window.name == "HISTORY_POPUP")
		{
			//alert('greater than 1024 or equal: '+screen.width);
			document.getElementById('containerDiv').style.width = '100%';
		}
		else
		{
			//alert('less than 1024: '+screen.width);
			document.getElementById('containerDiv').style.width = '980px';
		}  
}

function chooseStyle (newstyle){
	var expdate = new Date();
  	expdate.setTime(expdate.getTime() + (1000*3600*24*365));
  	document.cookie = newstyle + '; expires=' + expdate.toGMTString() + '; path=/';
      setActiveStyleSheet(newstyle);
}

function setActiveStyleSheet(newstyle) {
//alert(newstyle);
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == newstyle) a.disabled = false;
    }
  }
}

function getPreferred() {
	if(document.cookie.indexOf('small')>=0) {
		setActiveStyleSheet('small');
	}
	else if(document.cookie.indexOf('medium')>=0) {
		setActiveStyleSheet('medium');
	}
	else if(document.cookie.indexOf('large')>=0) {
		setActiveStyleSheet('large');
	}
	else
	{
		setActiveStyleSheet('medium');
	}
}

function setFontSize() {
  document.getElementById('containerDiv').style.width = '100%';
  if (screen.width == 800) {
    chooseStyle('small');
  }
  else if (screen.width == 1024) {
    chooseStyle('medium');
  }
  else if (screen.width > 1024) {
    chooseStyle('large');
  }
}

// fix png support for ie 6

function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

// builder bar
function builderBar() {
  if (window.name.substr(0,13) == "BUILDER_POPUP" && document.getElementById("builder_bar_context")) {document.getElementById("builder_bar_context").style.display = "none";}
    if (window.name != "MainRoomsWindow" && document.getElementById("builder_bar_main")) {
        document.getElementById("builder_bar_main").style.display = "none";
        if (document.getElementById("builder_bar_context")){
            document.getElementById("builder_bar_context").style.display = "none";
        }
    }
}

// user is not anon

function IdleReset() {
  var thisRoomName;
  try{
    thisRoomName = window.opener.name;
  }catch(err){}
  
  if(window.opener && thisRoomName == mainRoomsName){
      window.opener.seconds_idle = 0;
      window.opener.poppedUp = false;
  }
  if(window.opener && thisRoomName == "BUILDER_POPUP"){
      try{ 
          window.opener.opener.seconds_idle = 0;
          window.opener.opener.poppedUp = false; 
      }catch(err){}
  }
  seconds_idle = 0;
  poppedUp = false;
}

function popupOpen(){
  var openerWindowName;
  try{
      openerWindowName = window.opener.name;
  }catch(err){}
      if(window.opener && openerWindowName == mainRoomsName)
         return window.opener.poppedUp;
  
      return poppedUp;
}

function logout(){
  if(window.name == mainRoomsName){
      window.location.href=logoutAction;
      window.document.loggedout=true;
  }
  else{
      if(window.opener && window.opener.name == mainRoomsName){
       window.opener.location.href=logoutAction;
       window.opener.loggedout=true;
      }
      document.close();
      document.open();
      document.writeln("<HTML><HEAD><TITLE>Logout Window<\/TITLE><\/HEAD><body><BR><CENTER<B>You are now logged out<\/B><\/CENTER><\/body><\/HTML>");
      document.close();
  }
}

function isloggedout(){
    return loggedout;
}
          
function DetectIdle(){
  seconds_idle++;
  var timeout = -1;
  try{
      timeout = window.opener.seconds_idle
  }catch(err){}
  

  if (((window.name==mainRoomsName && seconds_idle > alert_interval+5)||(window.name!=mainRoomsName && timeout > alert_interval)) && !isloggedout()) {
      var openerWindowName;
      try {
          openerWindowName = window.opener.name;
      }catch(err){}
  
      if(!popupOpen()){
        if(window.opener && openerWindowName == mainRoomsName){
            window.opener.poppedUp = true;
        }
        else{
            poppedUp = true;
        }
        newwindow = window.open ("","mywindow1","status=1,width=350,height=150");
        newwindow.document.writeln("<HTML><HEAD>")
        newwindow.document.writeln('<TITLE>Logout Window<\/TITLE><\/HEAD><body><script>')
        newwindow.document.writeln('setTimeout("window.opener.logout(); window.close(); ",30000);')
        newwindow.document.writeln('<\/script><BR><CENTER>You will be logged out in 30 seconds. <BR>Do you want to continue?<form><BR>')
        newwindow.document.writeln('<input type=button value="Yes, Continue"  onclick="window.opener.IdleReset(); window.close(); " />')
        newwindow.document.writeln('<input type=button value="No, log me out" onclick="window.opener.logout(); window.close(); " />')
        newwindow.document.writeln('<\/CENTER><\/form><\/body><\/HTML>')
        newwindow.document.close();
        if (newwindow) {
            newwindow.focus()
        }
     }
  }
}


