//==========================================================
// This script contains the logic for the drop down menu
//==========================================================

var inmenu=false; //variable to show if the mouse pointer is in the menu
var lastmenu="list0"; //the last menu that was opened.
//----------------------------------------------------------
// Position 0 shows 1 subitems. Position 1 has three subitems, position 2 has 3
// subitems and so on. There are six main menu items and the string is six
// characters long.

var menulength=[1,3,4,1,5,4]; //change this variable if number of subitems changes.

//-----------------------------------------------------------
// This function attaches the mouse events to the appropriate function.
//-----------------------------------------------------------
function setup(){
	// preload rollovers if we do them
  //(new Image()).src = "gifs/hhome.gif";
  //(new Image()).src = "gifs/hmyblog.gif";
  
	for (i=0; i<menulength.length; i++){
		document.getElementById('menuheader'+i).style.visibility="visible";
		document.getElementById('menuheader'+i).onmouseover=openmenuprep;
		document.getElementById('list'+i).onmouseout=closemenuprep;
		for (j=0; j<menulength[i];j++){
			document.getElementById(('item'+i)+j).onmouseover=submenu;
		}
		var pageno = document.getElementById('page').getAttribute('name');
		document.getElementById('item'+pageno).onclick=block;
	}
}

//------------------------------------------------
// This function prevents reloading the current page when its menu item is
// clicked.
//------------------------------------------------
function block(){
	return false;
}

//------------------------------------------------
//This function sets the timer to open the menu that the mouse is hovering over.
//------------------------------------------------
function openmenuprep(){
	var menuid = this.id;
	var menunum = parseInt(menuid.slice(menuid.length-1));
	inmenu=true;
	if (lastmenu != menuid) {
		closemenu(lastmenu);
		lastmenu = menuid;
	}
	window.setTimeout("openmenu("+menunum+", '"+menuid+"');", 400);
}

//------------------------------------------------
//This function actually opens the menu. It checks to see if the pointer is
//still over the same menu.
//------------------------------------------------
function openmenu(menunum, someid) {
	if (inmenu && (lastmenu==someid)) {
		for (i=0; i<menulength[menunum];i++){
			document.getElementById(('item'+menunum)+i).style.visibility="visible";
		}
	}
}

//------------------------------------------------
// This function prepares to close the menu by setting a timer.
//------------------------------------------------
function closemenuprep(){
	inmenu = false;
	window.setTimeout("closemenu('"+lastmenu+"');", 200);
}

//------------------------------------------------
// This function actually closes the menu if the mouse pointer is not still
// over the menu.
//-------------------------------------------------
function closemenu(someid){
	if (inmenu && lastmenu==someid){
		return;
	}
	menunum = parseInt(someid.slice(someid.length-1));
	for (i=0; i<menulength[menunum];i++){
		document.getElementById(('item'+menunum)+i).style.visibility="hidden";
	}
}

//-------------------------------------------------
// This function makes sure that the 'inmenu' value stays true when the mouse
// is over a subitem of an open menu.
//-------------------------------------------------
function submenu() {
	inmenu = true;
}

//-------------------------------------------------
// This function cleans up as the page closes.
//-------------------------------------------------
function closeout(){
	inmenu=false;
	closemenu(lastmenu);
}

/**
 * Drag.js: drag absolutely positioned HTML elements:
 * Arguments:
 *
 *   elementToDrag: the element that received the mousedown event or
 *     some containing element. It must be absolutely positioned. Its
 *     style.left and style.top values will be changed based on the user's
 *     drag.
 *
 *  event: the Event object for the mousedown event.
 **/
 function drag(elementToDrag, event){
	 // mouse position in window coordinates
	 var startX = event.clientX;
	 var startY = event.clientY;
	 // The original position in document coordinates of the element
	 var origX = elementToDrag.offsetLeft;
	 var origY = elementToDrag.offsetTop;
	 // we can compute the distance between these two point because
	 // the scrollbar position does not change during the drag.
	 var deltaX = startX - origX;
	 var deltaY = startY - origY;
	 // register handlers for the mousemove and mouseup events that
	 // follow this mousedown event.
	 if (document.addEventListener) {
		 document.addEventListener("mousemove",moveHandler, true);
		 document.addEventListener("mouseup", upHandler, true);
	 }
	 else if (document.attachEvent) {
		 elementToDrag.setCapture();
		 elementToDrag.attachEvent("onmousemove", moveHandler);
		 elementToDrag.attachEvent("onmouseup", upHandler);
		 // treat loss of capture as mouseup event
		 elementToDrag.attachEvent("onlosecapture", upHandler);
	 }
	 else {
		 var oldmovehandler = document.onmousemove; //used by upHandler()
		 var olduphandler = document.onmouseup;
		 document.onmousemove = moveHandler;
		 document.onmouseup = upHandler;
	 }
	 // We've handled the event so don't allow it to bubble up
	 if(event.stopPropagation) event.stopPropagation();
	 else event.cancelBubble = true;
	 // prevent default action
	 if (event.preventDefault) event.preventDefault();
	 else event.returnValue = false;
	 
	 /**
	 * This is the handler that captures the mousemove events and
	 * moves the element
	 **/
	 function moveHandler(e) {
		 if (!e) e = window.event;
		 // move the element to the current mouse location adjusted
		 // by the offset of the initial mouseclick
		 elementToDrag.style.left = (e.clientX - deltaX) + "px";
		 elementToDrag.style.top = (e.clientY - deltaY) + "px";
		 // don't bubble
		 if(e.stopPropagation) e.stopPropagation();
		 else e.cancelBubble = true;
	 }
	 
	 /**
	 * this is the handler for the mouse up at the end of the drag
	 **/
	 function upHandler(e){
		 if (!e) e = window.event;
		 // unregister the capturing event handlers
		 if (document.removeEventListener) {
			 document.removeEventListener("mouseup", upHandler, true);
			 document.removeEventListener("mousemove",moveHandler, true);
		 }
		 else if (document.detachEvent) {
			 elementToDrag.detachEvent("onlosecapture", upHandler);
			 elementToDrag.detachEvent("onmouseup", upHandler);
			 elementToDrag.detachEvent("onmousemove", moveHandler);
			 elementToDrag.releaseCapture();
		 }
		 else {
			 document.onmouseup = olduphandler;
			 document.onmousemove = oldmovehandler;
		 }
		 // stop propagation
		 if(e.stopPropagation) e.stopPropagation();
		 else e.cancelBubble = true;
	 }
 }
 
 function openwindow(picnum, e){
	 var thescroll = 0;
	 if(window.InnerWidth){
		 thescroll=window.pageYOffset;
	 }
	 else if (document.documentElement && document.documentElement.clientWidth){
		 thescroll=document.documentElement.scrollTop;
	 }
	 else if (document.body.clientWidth){
		 thescroll=document.body.scrollTop;
	 }
	 var myframe=document.getElementById("bigframe");
	 var picheight = picnum.charAt(picnum.length - 1)=='v'?1097:630;
	 var startletter=picnum.charAt(0)=='v'?'v':'u';
	 var mypic=document.getElementById("bpicture");
	 myframe.style.height=picheight+"px";
	 myframe.style.top=(50+thescroll)+"px";
	 mypic.style.height=(picheight-30)+"px";
	 mypic.style.background="url(pictures/"+startletter+"09/"+picnum+".jpg)";
	 myframe.style.visibility="visible";
 }
 
 function closepicture(){
	 document.getElementById("bigframe").style.visibility="hidden";
 }
 
 // Global variables slideshow
var numslides=0;
var currentslide=0,oldslide=4;
var x=0;
var slides = new Array();
var allLoaded = 0;
var numberSlides = 8;

function CheckImages() {
  allLoaded++;
  if (allLoaded==numberSlides) MakeSlideShow();
}

function MakeSlideShow() {
  // find the headings with the slide instructions
  headings=document.getElementsByTagName("h4");
  for (i=0; i<headings.length; i++) {
    if (headings[i].className == "loading") 
	    headings[i].style.visibility="hidden";
    if (headings[i].className == "loaded") 
	    headings[i].style.visibility="visible";
  }
  // find all the images with the class 'slide'
  imgs=document.getElementsByTagName("img");
  for (i=0; i<imgs.length; i++) {
    // hide animated gif
    if (imgs[i].className == "animation") imgs[i].style.visibility="hidden";
    if (imgs[i].className != "slide") continue;
    slides[numslides]=imgs[i];
    // stack images with first slide on top
    if (numslides==0) {
      imgs[i].style.zIndex=10;
      imgs[i].style.visibility="visible";
    } else {
      imgs[i].style.zIndex=0;
      imgs[i].style.visibility="visible";
    }
    imgs[i].onclick=NextSlide;
    numslides++;
  } // end for loop
} // end MakeSlideShow()

function NextSlide() {
  // Set current slide to be under the new top slide
  slides[currentslide].style.zIndex=9;
  // Move older slide to bottom of the stack
  slides[oldslide].style.zIndex=0;
  oldslide=currentslide;
  currentslide++;
  if (currentslide >= numslides) currentslide = 0;
  // Start at the right edge
  slides[currentslide].style.left=600;
  x=600
  // Move the new slide to the top
  slides[currentslide].style.zIndex=10;
  AnimateSlide();
}

function AnimateSlide() {
  // Lower moves slower; higher moves faster
  x = x-25;
  slides[currentslide].style.left=x;
  // previous image moves off the left edge
  // (comment out the next line for a different effect)
  slides[oldslide].style.left= x-600;
  // repeat until slide is at zero position
  if (x >0) window.setTimeout("AnimateSlide();",10);
}

 
window.onload=setup;
window.onunload=closeout;

