// -------------------------------------------
// Functions called by the calendar and search pages
// -------------------------------------------

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM.. note: this won't find nested layers
		return document.layers[objectId];
    } else {
		return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.visibility = newVisibility;
		return true;
    } else {
		// we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectVisibility

function changeObjectDisplay(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.display = newVisibility;
		return true;
    } else {
		// we couldn't find the object, so we can't change its visibility
		return false;
    }
} // changeObjectDisplay

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		if (document.layers) {
			styleObject.left = newXCoordinate;
			styleObject.top = newYCoordinate;
		} else if (document.getElementById || document.all) {
			styleObject.left = newXCoordinate + "px";
			styleObject.top = newYCoordinate + "px";
		}

	return true;
    } else {
		// we couldn't find the object, so we can't very well move it
		return false;
    }
} // moveObject

function showPopupCalY (targetObjectId, imgName, yoffset) {
//alert(targetObjectId)
	// displays popup relative to an image
	hideCurrentPopup();

	// Now get the image coordinates
	var newXCoordinate = 0
	var newYCoordinate = 0
	if(document.getElementById) {
		var img = document.getElementById(imgName)
		if (!document.all) {
			newXCoordinate = img.offsetLeft
			newYCoordinate = img.offsetTop
		} else {
			i = topPos(img)
			nexYCoordinate = topPos(img)
			newXCoordinate = leftPos(img)
			newYCoordinate = i
		}
	} else if (document.layers) {
		newXCoordinate = document.images[imgName].x
		newYCoordinate = document.images[imgName].y
	} else if (document.all) {
		// IE 4
		var img = document.all(imgName)
		newXCoordinate = leftPos(img)
		newYCoordinate = topPos(img)
	}

	moveObject(targetObjectId, newXCoordinate, newYCoordinate+yoffset);
	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = targetObjectId;
	    return true;
	} else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	}

} // showPopupImage

function hideCurrentPopup() {
//alert("hiddding....")
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup

function topPos(el) {
	return doPosLoop(el, "Top");
}

function leftPos(el) {
	return doPosLoop(el, "Left");
}

function doPosLoop(el, val) {
	var temp = el;
	var x = eval("temp.offset" + val);
	while ((temp.tagName!="BODY") && (temp.offsetParent.style.position != "absolute")) {
		temp = temp.offsetParent;
		x += eval("temp.offset" + val);
	} return x;
}

function togglepanel(elm,ontext,offtext) {
	var obj = document.getElementById(elm);
	var objctl = document.getElementById(elm+"ctl");
	if (!obj) return;
	if (obj.style.display=="none") {
		obj.style.display = "";
		if (objctl) objctl.innerText = offtext;
	} else {
		obj.style.display = "none";
		if (objctl) objctl.innerText = ontext;		
	}
	return false;
}


// -------------------------------------------
// Functions called by the menu system at load time
// -------------------------------------------

// Declarations
var galleryinfo = new Array();
var galleryimage = null;
var gallerylink = null;

// Subroutines
$(document).ready(function(){
	gallery__generate();  // Set up image gallery if it exists
	fix_ie_objects(); // Handle issue with IE and need to activate flash
});

function gallery__generate() {
	var nav = document.getElementById('gallerynav'); // Get reference to div
	galleryimage = document.getElementById('galleryimage'); // Get reference to div
	if (!nav || !galleryimage) return;
	var lnks = nav.getElementsByTagName('A'); // Get reference to links in div 
	for (var i=0;i<lnks.length;i++)
      galleryinfo[galleryinfo.length] = new gallery__setup(i,lnks[i]);
}
function gallery__setup(i,lnk) {
	// Get image in link
	var imgs=lnk.getElementsByTagName('IMG');
	if (imgs.length!=1) return;
	this.img=imgs[0];
	// Check to see if this is the fullsize
	if (this.img==galleryimage) gallerylink=lnk
	else {
		// Save link
		this.href = lnk.href;
		// Prepare image swap
		this.tmpimg = new Image();	
		this.offsrc = this.img.src;
		this.onsrc = this.offsrc.replace(/\_th\.jpg/,'.jpg');		
		this.tmpimg.src = this.onsrc;
		// Set up state changes
		lnk.onmouseover = new Function("gallery__show("+galleryinfo.length+")");		
		return this;
	}
}
function gallery__show(i) {
	// Activate item
	if (galleryinfo[i]) {
		galleryimage.src = galleryinfo[i].onsrc;
		if (gallerylink) gallerylink.href = galleryinfo[i].href;
	}
}
function fix_ie_objects() {
	var objects=document.getElementsByTagName("object");
	for (var i=0;i<objects.length;i++) objects[i].outerHTML = objects[i].outerHTML;
}

var popupwin=null;
function popup(href,options) {
	if (!options) options="width=500,height=500,resizable=no,scrollbars=no,status=no,toolbar=no";
	var popupwin=window.open(href,"popupwin",options);
	popupwin.focus();
	return false;
}	

	


