/***********************************************
 * Macaulay Library JavaScript 
 ***********************************************/


window.onload = function()
{
    stripe('stripeIt',  '#fff', '#f4f1eb'); 
    stripe('stripeIt2', '#fff', '#f4f1eb'); 
    stripe('stripeIt3', '#fff', '#f4f1eb'); 
    stripe('stripeIt4', '#fff', '#f4f1eb'); 

    var rootOffset;
    var root = document.getElementById("menuList");
    if (root != null)
        rootOffset = root.offsetLeft;

    if (document.getElementById("mainMenu"))
        getMenus(root, root);

    if (document.getElementById("map")) {
        mlGoogleMapLoader();

                             // For the search control in the map
        GSearch.setOnLoadCallback(mlGoogleMapLoader());
    }
}

function feetToMeters(obj)
{
    var tmp = document.getElementById(obj);
    tmp.value = Math.round(tmp.value * 0.3048);
}
function fahrenheitToCelsius(obj)
{
    var tmp = document.getElementById(obj);
    tmp.value = Math.round(100/(212-32) * (tmp.value - 32 ));
}
function celsiusToFahrenheit(obj)
{
    var tmp = document.getElementById(obj);
    tmp.value = Math.round((212-32)/100 * tmp.value + 32);
}


function mlPlayerScrub(seconds)
{
    var player = document.getElementById('flvplayerobj');

    if (player && seconds) {
        player.sendEvent("PLAY","true");  // set playback state to true
        player.sendEvent("PLAY","false"); // pause
        player.sendEvent("SEEK","" + seconds);
    }
}


var map;
var latLonMarkers = [];

function mlGoogleMapPan(i)
{
    i--; // index from 1, since the javascript method call is shown in the browser status window.

    if (map && latLonMarkers.length > i && latLonMarkers[i])
        map.panTo(latLonMarkers[i].getPoint());
}

function mlGoogleMapList(latlons, doSmallControl, doLatLonMarkers, doSearchControl, doCenterMarker)
{
    latlons         = typeof(latlons)         != 'undefined' ? latlons         : [42, -76];
    doSmallControl  = typeof(doSmallControl)  != 'undefined' ? doSmallControl  : 0;
    doLatLonMarkers = typeof(doLatLonMarkers) != 'undefined' ? doLatLonMarkers : 1;
    doSearchControl = typeof(doSearchControl) != 'undefined' ? doSearchControl : 0;
    doCenterMarker  = typeof(doCenterMarker)  != 'undefined' ? doCenterMarker  : 0;

//console.log('mlGoogleMapList:', latlons, doSmallControl, doLatLonMarkers, doSearchControl, doCenterMarker);

    if (GBrowserIsCompatible()) {
        var centerMarker;
        map = new GMap2(document.getElementById("map"));
                             // list is required, but can override lat/lon in Document
        if (map != null && latlons != null && latlons.length > 1) {
            var mylat = latlons[0];
            var mylon = latlons[1];
            if (mylat != null && mylon != null) {

                map.setCenter(new GLatLng(mylat, mylon),   9, G_HYBRID_MAP);
                map.addMapType(G_PHYSICAL_MAP);
                map.addControl(new GMapTypeControl());
                map.addControl(new GScaleControl());
                if (doSmallControl == '1')
                    map.addControl(new GSmallMapControl());
                else
                    map.addControl(new GLargeMapControl());

                if (doSearchControl == '1')
                    map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,20)));


                             // write out the markers in the latLon list
                for (var i = 0; i < latlons.length; i = i+2) {
                    mylat = latlons[i];
                    mylon = latlons[i+1];

                    if (doLatLonMarkers == '1') {
                        var baseIcon = new GIcon(G_DEFAULT_ICON);
                        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
                        baseIcon.iconSize = new GSize(20, 34);
                        baseIcon.shadowSize = new GSize(37, 34);
                        baseIcon.iconAnchor = new GPoint(9, 34);
                        baseIcon.infoWindowAnchor = new GPoint(9, 2);

                             // will be: custom map icon, numbered by location#
                        var number = ((i/2)+1);
                        var letter = String.fromCharCode("A".charCodeAt(0) + number - 1);
                        var birdIcon = new GIcon(baseIcon);
                        birdIcon.image = "/images/map/marker" + letter + ".png";
                        markerOptions = { icon:birdIcon };
                        var tmpMarker = new GMarker(new GLatLng(mylat, mylon), markerOptions);
                        map.addOverlay(tmpMarker);
                        latLonMarkers[(i/2)] = tmpMarker;
                    }
                }

                if (doCenterMarker == '1') {
                    centerMarker = new GMarker(new GLatLng(mylat, mylon));
                    map.addOverlay(centerMarker);

                    GEvent.addListener(map, 'click', function(overlay, point) {
                        if (point) {
                            var mylat = point.lat();
                            var mylon = point.lng();
//console.log('doCenterMarker1:', mylat, mylon);

                            var tmp;
                            tmp = document.getElementById("latitude");
//console.log('doCenterMarker2:', tmp);
                            if (tmp)
                                tmp.value = mylat;

                            tmp = document.getElementById("longitude");
//console.log('doCenterMarker3:', tmp);
                            if (tmp)
                                tmp.value = mylon;

//console.log('doCenterMarker4:', tmp);
                            if (centerMarker)
                                map.removeOverlay(centerMarker);

//console.log('doCenterMarker5:', centerMarker);
                            centerMarker = new GMarker(new GLatLng(mylat, mylon));
//console.log('doCenterMarker6:', centerMarker);
                            map.addOverlay(centerMarker);
//console.log('doCenterMarker7:', centerMarker);
                            map.panTo(centerMarker);
//console.log('doCenterMarker8, panto:', centerMarker);

                        } else if (overlay) {
//console.log('doCenterMarker.overlay:', overlay.getPoint());
                            map.panTo(overlay.getPoint());
                        }
                    });
                }
            }
        }
    }
}

function ravenViewer(format, catalogId, assetId)
{
    mlPlayer('1', format, catalogId, assetId);
}

function flashPlayer(format, catalogId, assetId)
{
    mlPlayer('2', format, catalogId, assetId);
}

function mlPlayer(player, format, catalogId, assetId)
{
    playerUrl   = '';
    playerTitle = '';
    height      = 550;


    if (player == '1')
        height = 550;
    else if (format == '1000')
        height = 230;
    else if (format == '1001')
        height = 550;
    else
        height = 550;

    if (format == '1000')
        playerUrl = '/audio';
    else if (format == '1001')
        playerUrl = '/video';

    if (player == '1')
        playerUrl += '/ravenViewer.do?';
    else if (player == '2')
        playerUrl += '/flashPlayer.do?';

    if (catalogId != '' && catalogId != null) {
        playerUrl += 'id=' + catalogId;
        windowName = 'Catalog' + catalogId;
    } else if (assetId != '' && assetId != null) {
        playerUrl += 'assetId=' + assetId;
        windowName= 'Asset' + assetId;
    }
    //alert("=-=-=-=-=-=-  playerUrl:" + playerUrl + ", playerTitle:" + playerTitle);

    vizWin = window.open(playerUrl, windowName, 'width=775px,height=' + height + 'px,resizable=yes,toolbar=no,menubar=no,scrollbars=no', true);
    vizWin.focus();
}



function toggle(id)
{
    if (document.getElementById(id).style.display == "none") {
        document.getElementById(id).style.display = "block";
    } else {
        document.getElementById(id).style.display = "none";
    }
}

function toggleBit(id)
{
    if (document.getElementById(id).value == "1") {
        document.getElementById(id).value = "0";
    } else {
        document.getElementById(id).value = "1";
    }
}


// Move item from one multiple select box to another
function MoveOption(objSourceElement, objTargetElement)
{
    var aryTempSourceOptions = new Array();
    var x = 0;
    
    //looping through source element to find selected options
    for (var i = 0; i < objSourceElement.length; i++) {
        if (objSourceElement.options[i].selected) {
            //need to move this option to target element
            var intTargetLen = objTargetElement.length++;
            objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
            objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
        }
        else {
            //storing options that stay to recreate select element
            var objTempValues = new Object();
            objTempValues.text = objSourceElement.options[i].text;
            objTempValues.value = objSourceElement.options[i].value;
            aryTempSourceOptions[x] = objTempValues;
            x++;
        }
    }
    
    //resetting length of source
    objSourceElement.length = aryTempSourceOptions.length;
    //looping through temp array to recreate source select element
    for (var i = 0; i < aryTempSourceOptions.length; i++) {
        objSourceElement.options[i].text = aryTempSourceOptions[i].text;
        objSourceElement.options[i].value = aryTempSourceOptions[i].value;
        objSourceElement.options[i].selected = false;
    }
}

// Select all items in a multiple select box, so all values are submitted with form.
function SelectItems(objSourceElement)
{
    //looping through source element and select all items
    for (var i = 0; objSourceElement && i < objSourceElement.length; i++) 
        objSourceElement.options[i].selected = true;
}

function SelectCheckboxes(name)
{
    for (var i = 0; objSourceElement && i < objSourceElement.length; i++) 
        objSourceElement.options[i].selected = true;
}





  
/*
=================================================================================================
>> EASYMENU - XHTML/CSS/DHTML Semantically correct drop down menu 
=================================================================================================
Description:	This script takes a nested set of <ul>s and turns it into a fully functional
		DHTML menu. All that is required is the correct use of class names, and
		the application of some CSS. For the latest version of this script, go to
		http://www.easymenu.co.uk
		
Use:		Use of this code is free for all non-commercial applications. If you would like to 
		use the code on a commercial website, payment is required. 
                You are free to make alterations to the code as required.
		
Copyright:	This script is copyright 2004-2005 easymenu.co.uk. All rights reserved.

Credits: 	Inspiration/Code borrowed from Dave Lindquist (http://www.gazingus.org)
		Menu hide functionality was aided by some code I found on http://www.jessett.com/
		Konqueror functionality was aided by some code Scott Wehrenberg emailed over

REVISION HISTORY
Date		Version		Changes/Bug Fixes/New functionality added
--------------------------------------------------------------------------------------------------
10/01/2005	1.01		Fixed known issue where hovering over a sub-menu, then returning to 
				its parent menu not via the "starter" item on that menu, where the 
				new item moused over is a "starter" caused the two sub-menus 
				to overlay each other until a further movement of the mouse. 
				Fixed known issue where returning to parent menu via a non-"starter"
				menu item on the parent menu would not hide the child menu
08/01/2005	1.00		First full release of easymenu.
				Stylesheets now loaded in through JavaScript to improve accessibility.
01/12/2004	0.9		Base code inherited from client project
				Added suggestion from Scott Wehrenberg enabling Konqueror functionality
--------------------------------------------------------------------------------------------------
*/
	// Load up the stylesheet for the menus. This code is here rather than in the html
	// document to allow for non-javascript browsers to render a seperately styled non-
	// interactive list rather than the DHTML menu
	//document.write("<link rel='stylesheet' href='cssmenu.css' type='text/css' />");

	var currentMenu = null;
	var lastMenuStarter = null;
	var mytimer = null;
	var timerOn = false;
	var opera = window.opera ? true : false;

	if (!document.getElementById)
		document.getElementById = function() { return null; }

	function initialiseDummy(dummy, root) {
		dummy.onmouseover = function() {
			containingMenu = this.parentNode.parentNode;
			for (var x=0;x<containingMenu.childNodes.length;x++) {
				if (containingMenu.childNodes[x].nodeName.toUpperCase()=="LI") {
					if (containingMenu.childNodes[x].getElementsByTagName("ul").length>0) {
						containingMenu.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
					}
				}
			}
		}
		dummy.onfocus = function() {
			dummy.onmouseover();
		}
	}
	
	function initialiseMenu(menu, starter, root) {
		var leftstarter = false;
	
		if (menu == null || starter == null) return;
			currentMenu = menu;
	
		starter.onmouseover = function() {
			if (currentMenu) {
				if (this.parentNode.parentNode!=currentMenu) {
					currentMenu.style.visibility = "hidden";
					hideAllMenus(currentMenu, root);

				}
				if (this.parentNode.parentNode==root) {
					while (currentMenu.parentNode.parentNode!=root) {
						currentMenu.parentNode.parentNode.style.visibility = "hidden";
						currentMenu = currentMenu.parentNode.parentNode;
					}
				}
				currentMenu = null;
				this.showMenu();
	        	}
		}
	
		menu.onmouseover = function() {
			if (currentMenu) {
				currentMenu = null;
				this.showMenu();
	        	}
		}	
	
		starter.showMenu = function() {
			if (!opera) {
				if (this.parentNode.parentNode==root) {
					menu.style.left = this.offsetLeft + "px";
					menu.style.top = this.offsetTop + this.offsetHeight + "px";
					if (menu.offsetWidth < this.offsetWidth) menu.style.width = this.offsetWidth;
				}
				else {
				 	menu.style.left = this.offsetLeft + this.offsetWidth + "px";
				 	menu.style.top = this.offsetTop + "px";
				}
			}
			else {
				var rootOffset = root.offsetLeft;
				if (this.parentNode.parentNode==root) {
					menu.style.left = this.offsetLeft - rootOffset + "px";
					menu.style.width = this.offsetWidth;
					menu.style.top = this.offsetHeight + "px";
				}
				else {
				 	menu.style.left = this.offsetWidth - rootOffset + "px";
				 	menu.style.top = this.offsetTop + "px"; //menu.style.top - menu.style.offsetHeight + "px";
				}

			}
			menu.style.visibility = "visible";
			currentMenu = menu;
		}

		starter.onfocus	 = function() {
			starter.onmouseover();
		}
	
		menu.onfocus	 = function() {
//			currentMenu.style.visibility="hidden";
		}

		menu.showMenu = function() {
			menu.style.visibility = "visible";
			currentMenu = menu;
			stopTime();
		}

		menu.hideMenu = function()  {
			if (!timerOn) {
				mytimer = setInterval("killMenu('" + this.id + "', '" + root.id + "');", 50);
				timerOn = true;
				for (var x=0;x<menu.childNodes.length;x++) {
					if (menu.childNodes[x].nodeName=="LI") {
						if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
							menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
							menuItem.style.visibility = "hidden";
						}
					}
				}
			}
		}

		menu.onmouseout = function(event) {
			this.hideMenu();
		}
		menu.onblur = function() {
			this.hideMenu();
		}
		starter.onmouseout = function() {
			for (var x=0;x<menu.childNodes.length;x++) {
				if (menu.childNodes[x].nodeName=="LI") {
					if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
						menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
						menuItem.style.visibility = "hidden";
					}
				}
			}
			menu.style.visibility = "hidden";
		}
}
	function killMenu(menu, root) {
		var menu = document.getElementById(menu);
		var root = document.getElementById(root);
		menu.style.visibility = "hidden";
		for (var x=0;x<menu.childNodes.length;x++) {
			if (menu.childNodes[x].nodeName=="LI") {
				if (menu.childNodes[x].getElementsByTagName("UL").length>0) {
					menuItem = menu.childNodes[x].getElementsByTagName("UL").item(0);
					menuItem.style.visibility = "hidden";
				}
			}
		}
		while (menu.parentNode.parentNode!=root) {
			menu.parentNode.parentNode.style.visibility = "hidden";
			menu = menu.parentNode.parentNode;
		}
		stopTime();
	}
	function stopTime() {
		if (mytimer) {
		 	 clearInterval(mytimer);
			 mytimer = null;
			 timerOn = false;
		}
	} 

function getMenus(elementItem, root) {
	var selectedItem;
	var menuStarter;
	var menuItem;
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				menuStarter = elementItem.childNodes[x].getElementsByTagName("A").item(0);
				menuItem = elementItem.childNodes[x].getElementsByTagName("UL").item(0);
				getMenus(menuItem, root);
				initialiseMenu(menuItem, menuStarter, root);
			}
			else {
				initialiseDummy(elementItem.childNodes[x].getElementsByTagName("A").item(0), root);
			}
		}
	}
}
function hideAllMenus(elementItem, root) {
	for (var x=0;x<elementItem.childNodes.length;x++) {
		if (elementItem.childNodes[x].nodeName.toUpperCase()=="LI") {
			if (elementItem.childNodes[x].getElementsByTagName("ul").length>0) {
				elementItem.childNodes[x].getElementsByTagName("UL").item(0).style.visibility = 'hidden';
				hideAllMenus(elementItem.childNodes[x].getElementsByTagName("UL").item(0), root);
			}
		}
	}
}
  
//***********************************************	
// ALTERNATING STRIPES
// Script from A List Apart http://www.alistapart.com/articles/zebratables/
//***********************************************

  // this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripe(id) {
    var even = false;
  
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#F8F8EA";
  
    var table = document.getElementById(id);
    if (! table) { return; }
    
    var tbodies = table.getElementsByTagName("tbody");

    for (var h = 0; h < tbodies.length; h++) {
    
      var trs = tbodies[h].getElementsByTagName("tr");
      for (var i = 0; i < trs.length; i++) {
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          var tds = trs[i].getElementsByTagName("td");
        
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        even =  ! even;
      }
    }
  }

