/*
 * TDL Panel Service Scripts
 *
 * Revision history:
 *    2006-12-20 Added methods for Panel persistence (Nikolai Schwertner)
 */

// An object modelling the parameters of a panel
function PANEL (elmnt, imgid, imgreplace, display) {
    this.elmnt = elmnt;
    this.imgid = imgid;
    this.imgreplace = imgreplace;
    this.display = display;
}
    
// Restores a panel to the state stored within the session cookie
PANEL.prototype.restore = function() {
    inspMenu(this.elmnt, this.imgid, this.imgreplace, this.display, getCookieValue ("TDL"+getPage()+this.display));
}

// Restores the panel cookie to the default state
PANEL.prototype.reset = function() {
    deleteCookie ("TDL"+getPage()+this.display);
}

// A set of panels
function PANELS () {
    this.children = new Array;
    var i;
    for (i=0; i<arguments.length; i++)
       this.children[this.children.length]=arguments[i]; 
}
    
PANELS.prototype.restore = function() {
    for (i=0; i<this.children.length; i++)
       this.children[i].restore(); 
}

PANELS.prototype.reset = function() {
    for (i=0; i<this.children.length; i++)
       this.children[i].reset();
}

// Alters the state of a panel to the opposite state (on/off)
function inspMenu(elmnt, imgid, imgreplace, display) {
   inspMenu(elmnt, imgid, imgreplace, display, "undefined");
}

// Returns an identifier for the current TDL JSP page
function getPage () {
   var page = document.getElementById("TDLPage").value;
   if (page == null) page = "default";
   return page;
}

// A method called at the load of every TDL page to set the states of the panels
function setPanels() {

  // Get the location and referrer strings
  var referrer = String(document.referrer);
  var ref_array = referrer.split("?");
  var ref = ref_array[0];
  var location = String(document.location);
  var loc_array = location.split("?");
  var loc = loc_array[0];
  
  // Create the panes arrey
  var p = new PANELS (
                 new PANEL ("basicsearch-toggle","basicsearch-img", "basic_search", "basic-search"),
                 new PANEL ("advsearch-toggle","advsearch-img", "advanced_search", "adv-search"),
                 new PANEL ("collsearch-toggle","collsearch-img", "sidebar_coll_search", "coll-search"),
                 new PANEL ("resourceinsp-toggle","resourceinsp-img", "resource_inspector", "resourceinsp"),
                 new PANEL ("browse-toggle","browse-img", "browse", "browser"),
                 new PANEL ("bookmarks-toggle","bookmarks-img", "bookmark", "bookmarks")
                      );
  
  // Restore the panels to the cookie value, as long as the user stays within the page
  // Otherwise, reset the panel states to the defaults
  if (ref == loc) p.restore();
  else p.reset();
}

//This code defines the functionality of the inspector panel displays
function inspMenu(elmnt, imgid, imgreplace, display, toValue) {
var dropList = document.getElementById(elmnt);
var panel = document.getElementById(display);
if (panel == null) return;

if (toValue == false) return;

//turn a panel display off
if ((panel.style.display == '' || toValue == "off") && toValue != "on") {
	panel.style.display = 'none';
	document.images[imgid].src = 'images/v30/resource_inspector_open.gif';
	document.images[imgid].src = 'images/v30/' +imgreplace+ '_close.gif';
	updatePanelStates(display,"off");

//turn a panel display on
} else if ((panel.style.display == 'none' || toValue == "on") && toValue != "off"){
	panel.style.display = '';
	document.images[imgid].src = 'images/v30/resource_inspector_close.gif';
	document.images[imgid].src = 'images/v30/' +imgreplace+ '_open.gif';
	updatePanelStates(display,"on");

	//make sure the basic search panel display is turned off when any other panel display is turned on
	if (elmnt == 'advsearch-toggle') {
		var basicOff = document.getElementById('basic-search');
		var basicToggle = document.getElementById('basicsearch-toggle');
		basicOff.style.display = 'none';
		document.images['basicsearch-img'].src = 'images/v30/resource_inspector_open.gif';
		document.images['basicsearch-img'].src = 'images/v30/basic_search_close.gif';
		updatePanelStates("basic-search","off");
	}

	//make sure the advanced search panel display is turned off when the basic search panel display is turned on
	if (elmnt == 'basicsearch-toggle') {
		var basicOff = document.getElementById('adv-search');
		var basicToggle = document.getElementById('advsearch-toggle');
		basicOff.style.display = 'none';
		document.images['advsearch-img'].src = 'images/v30/resource_inspector_open.gif';
		document.images['advsearch-img'].src = 'images/v30/advanced_search_close.gif';
		updatePanelStates("adv-search","off");
	}
}
}

// Updates the session cookie for a panel with the provided value
function updatePanelStates (panel, state) {
   if (testSessionCookie()) {
      writeSessionCookie ("TDL"+getPage()+panel,state);
      //alert ("TDL"+getPage()+panel + ":" + state);
   }
}

//This code is for exandable dropdown menus; currently, only the tables of contents
function dropMenu(elmnt,img_name)      {
var dropList = document.getElementById(elmnt)
                if (dropList.style.display == '')       {
                        dropList.style.display = 'none'
                        document.images[img_name].src = 'images/sidebar_arrow_blue_closed.gif'
                } else if (dropList.style.display == 'none')    {
                        dropList.style.display = ''
                        document.images[img_name].src = 'images/sidebar_arrow_brown_open.gif'
                }
}


//This code is for the thumbnail viewer in texts with embedded images
function swapImage(direction,image_swap,href_swap,fedora_host,fedora_port,app_host,app_port,app_dir)	{
	//Uncomment to debug element calls
	//alert ("the first element of the array is :" + images[0]);

	load_image += direction;

	//if the user is at the beginning of the array and askes for the previous image, wrap to the end of the array
	//if the user is at the end of the array and askes for the next image, wrap to the beginning of the array
	if (load_image < 0) {
		load_image = number_of_images -1;
	} else if (load_image == number_of_images) {
		load_image = 0;
	}

	// swap the image
	if (document.images) {
		document.images[image_swap].src= "http://"+fedora_host+":"+fedora_port+"/fedora/get/"+pids[load_image]+"/bdef:TuftsImage/getThumbnail";
		document.getElementById(href_swap).href="http://"+app_host+":"+app_port+"/"+app_dir+"/view_image.jsp?urn="+urns[load_image];
    }
}



//This code provides a stadards compliant way of opening new browser windows from XHTML links 
//All extrernal links need to have the following attribute value: rel="external"
function externalLinks() {

 //test to see if the browser is DOM 1.0 compliant; if so, proceed, if not bail
 if (!document.getElementsByTagName) return;

 //index <a> tags
 var anchors = document.getElementsByTagName("a");

 //loop through <a href> tags, find the "rel=external" attribute, set a virtual "target" attribute 
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

//Fire up the externalLInks function on load
window.onload = externalLinks;


//Complete document information toggle
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = ""; }
else { document.getElementById(d).style.display = "none"; }
}

