//
// Functions written for the use of library.csu.edu
// Copyright (c) 2005,2006 Chicago State University Library.  All rights reserved.
// W. L. Graham, Digital Initiatives
// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//

// Globals
	// Set the locations of the images for <changeVariabeImage>here.  
	// Ideally they should
	// be different.  There should be a large number of them. 
	// There should be at least two and must be at least one.
	// Make certain each file exists and is of the same needed size in pixels.
	// The list may be as long as you reasonably wish.

var nowAt = -1;

function setStyle(title) {
// Chooses the stylesheet based on title; depends upon unique identification of the object
// NB: Only a stylesheet has both a rel= and a title= attribute
   var i, aHeadLink, main;
   for(i=0; (aHeadLink = document.getElementsByTagName("link")[i]); i++) {
     if(aHeadLink.getAttribute("rel").indexOf("style") != -1 && aHeadLink.getAttribute("title")) {
       aHeadLink.disabled = true;
       if(aHeadLink.getAttribute("title") == title) aHeadLink.disabled = false;
     }
   }
}




function delay (gap)
	{ //  gap is in millisecs; 
	  //  NB this is a lousy cpu waster; use for small intervals at infrequent intervals
	var then,now; then=new Date().getTime();
	now=then;
	while((now-then)<gap)
		{
			now=new Date().getTime();
		}
	}
	
if (document.images) {            
// Preload memory with images and create names.  Use these as follows in the body:
// <a HREF="page1.htm" onmouseover="rollimagein('image1')" onmouseout="rollimageout('image1')">
// <img NAME="image1" SRC="http://library.csu.edu/images/button1_off.png" ALT="mouseover button" HEIGHT="30"
// WIDTH="147" VSPACE="0" HSPACE="0" BORDER="0" /></a>
            image1on = new Image();      
            image1on.src = "/images/image1on.png";
			
            image1off = new Image();
			image1off.src = "/images/image1off.png";
			
}



function rollimagein(name,flavor) {
// Put the "on" image
        if (document.images) {
            document[name].src = eval(name + flavor + "on.src");
        }
}



function rollimageout(name) {
// Put the "off" image
        if (document.images) {
            document[name].src = eval(name + "off.src");
        }
}



function show(id) {
// Turn on visibility
	delay (300);
	if (document.layers) {document.layers[id].visibility = 'show';} 
	else if (document.getElementById) {document.getElementById(id).style.visibility = 'visible';}
}



function hide(id) {
// Turn off visibility after a brief delay to lessen flicker

	if (document.layers) {document.layers[id].visibility = 'hide';}
	else if (document.getElementById) {document.getElementById(id).style.visibility = 'hidden';}
}



function leftMenuObject (action,id,menuWidth,menuLeft,menuTop) {
// Show or hide a particular menu (is a flip/flop)
		if (document.layers) { // Netscape 4
			menuObj = eval ("document."+id+".");
		}
		else if (document.all) { // IE 6 and before
			menuObj=eval("document.all."+id+".style")
			if (action == 'show') {
				menuObj.pixelLeft = menuLeft
				show(id)
			}
			else {
				menuObj.pixelLeft = -menuWidth
				hide(id)
			}
			menuObj.pixelTop = menuTop
		}
		else { // assume Netscape 6 (which has no focus on DIV or SPAN elements)
			element = document.getElementById(id);
			if (action == 'show') {
				element.style.left = menuLeft + "px";
				element.style.visibility = 'visible';
			}
			else { // hide it off the screen
				element.style.left = -menuWidth + "px";
				element.style.visibility = 'hidden';
			}
			element.top = menuTop;
		}
}



function hilite(imagename,imagefile,areaid) {
// Set an image and hi-lite an area
        if (document.images) {
            document[imagename].src = imagefile;
			document.getElementById(areaid).style.color='#CC0000';
        }
}
function nolite(imagename,imagefile,areaid) {
// Set an image and dim an area
        if (document.images) {
            document[imagename].src = imagefile;
			document.getElementById(areaid).style.color='#333333';
        }
}
function getMenuObject (element) {
// Obtain a menu object depending upon whether internet explorer
	if (document.all) {
		val=eval("document.all"+element+".style")
	}
	else {
		val=eval("document."+element)
	}
	return val
}



function flipper (value,first,second) {
// If given first it returns second; if given not first it returns first
	if (value == first) {
		val=second
	}
	else {
		val=first
	}
	return val
}



function jumpTo (menuLoc) {
// Go to a value specified in a select-and-go form OPTIONS value
	newPage=menuLoc.options[menuLoc.selectedIndex].value
	if (newPage != "") {
		window.location.href=newPage
	}
}

dayNames = new Array (
		"Sunday",
 		"Monday",
 		"Tuesday",
 		"Wednesday",
 		"Thursday",
 		"Friday",
 		"Saturday");

monthNames = new Array (
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December");



function putNow () {
	now=new Date;
	output = dayNames[now.getDay()] + ", " +
		 monthNames[now.getMonth()] + " " +
		 now.getDate() + " ";
	document.write (output);
	return
}
	         

// Scroller speed here (larger is faster 1-10) 
var scrollerspeed=3;
var pausespeed=scrollerspeed;

function vScrollText () {

// Scroller width and height here 
  var scrollerwidth="450px";
  var scrollerheight="100px"; 

//slows speed down for NS 
  var scrollerspeed=(document.all)? scrollerspeed : Math.max(1, scrollerspeed-1);

// Scrollers content goes here! Keep all of the message on the same line! 
  var scrollercontent='This is vScrollText content' ;

  var pauseit=1;
  var copyspeed=scrollerspeed ;
  var pausespeed=(pauseit==0)? copyspeed: 0 ;

  var iedom = document.all || document.getElementById ;

  var actualheight='';

  var cross_scroller, ns_scroller ;


  function scrollscroller() { 

    if (iedom) { 
		if (parseInt(cross_scroller.style.top)>(actualheight*(-1)+8)) cross_scroller.style.top=parseInt(cross_scroller.style.top)-copyspeed+"px" ;
		else cross_scroller.style.top=parseInt(scrollerheight)+8+"px" ;
	} 
	else if (document.layers) { 
		if (ns_scroller.top>(actualheight*(-1)+8)) ns_scroller.top-=copyspeed ;
    	else ns_scroller.top=parseInt(scrollerheight)+8 ;
	} 
} 

  function populate() { 

    if (iedom) { 
      cross_scroller=document.getElementById? document.getElementById("iescroller") : document.all.iescroller ; 
	  cross_scroller.style.top=parseInt(scrollerheight)+8+"px" ;
      cross_scroller.innerHTML=scrollercontent ;
	  actualheight=cross_scroller.offsetHeight ;
	} 
    else if (document.layers) { 
      ns_scroller=document.ns_scroller.document.ns_scroller2 ;
      ns_scroller.top=parseInt(scrollerheight)+8 ;
	  ns_scroller.document.write(scrollercontent) ;
      ns_scroller.document.close() ;
	  actualheight=ns_scroller.document.height ;
	  } 
	lefttime=setInterval("scrollscroller()",20) ;
  
  } 




//  window.onload=populate;
 



if (iedom || document.layers) { 

	with (document) { 
		if (iedom) { 
			write('<div style="position:relative;width:'+scrollerwidth+';height:'+scrollerheight+';overflow:hidden" onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=scrollerspeed"  onFocus="copyspeed=pausespeed" onBlur="copyspeed=scrollerspeed">') ;
			write('<div id="iescroller" style="position:absolute;left:0px;top:0px;width:100%;">') ;
			write('</div></div>') 
		} 
		else if (document.layers) { 
			write('<ilayer width='+scrollerwidth+' height='+scrollerheight+' name="ns_scroller">') ;
			write('<layer name="ns_scroller2" width='+scrollerwidth+' height='+scrollerheight+'left=0 top=0 onMouseover="copyspeed=pausespeed" onMouseout="copyspeed=scrollerspeed" onFocus="copyspeed=pausespeed" onBlur="copyspeed=scrollerspeed"></layer>') ;
			write('</ilayer>') ; 
		} 
	} 
}

}

function which (rooftop) {

	// Create an equally distributed value of min 0 and max length(rooftop), 
	// useful as an array index.

	// Assumes Math.random produces values between 0 and 1 with equal probability.
	
	var value;
	if (rooftop < 2) {
		value = 0;
		}
	else {
		value = Math.round ( Math.random() * (rooftop - 1) );
	}	
	return value;
}


function showEvents(userDate) {

	// Call the event-display page to list events for all venues (V=*) 
	// for the selected date (form a POSIX date)
	
	document.location="displayevents.php?VENUE=*&DATE="+userDate;
	return False;
	
}


function changeVariableImage (AreaName) {
// Change the image named in AreaName in a page containing a reference to
// this code by selecting a name from a list of image file names.  
// All image files must be sized equally.  
//
// How to: Just put valid file names into the array below; have at least two
//         or the images will not be seen as changing.
//         Array: ImageLocations
//
// This script is provided freely, but there is no expression 
// of warranty or guarantee; use at your own risk.
//
// Trustees of Chicago State University 
// Scripted by W. L. Graham, May 2004	
	Interval = 10;  // Interval in seconds
		
	// In the html file there is an image named "promophoto", and this code 
	// changes the picture via its IMG name by swapping the IMG src
	
	// NB: We don't have to pre-cache (by creating a memory area) the image,
	//     just decide upon a possibly new image to display and display it.
	
	ImageLocations = new Array 
		(
			'/images/homepage/NatlLibraryWeek2008.jpg' ,
			'/images/homepage/ILPPAnnouncement.jpg' 
	);
		
	nowAt = nowAt + 1;		
		
	if ( nowAt >=  ImageLocations.length ) {
		nowAt = 0;
	}
	
	// alert ('nowAt='+nowAt);
	
	document.getElementById(AreaName).src=ImageLocations[nowAt];	

	// And call ourselves to do it again again at intervals if there is > 1 picture.
	
	if (ImageLocations.length > 1) {
		subCall = "changeVariableImage('" + AreaName + "')"
		myTimer = setTimeout ( subCall, Interval * 1000 );
	}
}


