// -------------------------------------------
// 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)
		i = topPos(img)
		nexYCoordinate = topPos(img)
		newXCoordinate = leftPos(img)
		newYCoordinate = i
	}

	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
jQuery(function(){
	gallery__generate();  // Set up image gallery if it exists
	fix_ie_objects(); // Handle issue with IE and need to activate flash

    // Apply 2 col templates
    $("meta[content='2col']").parents("html").find(".maintable").attr("class","maintable2column");

    // Swap images
    $('.swap').each(function(){
      var lnk=$(this);
      var img=(lnk.is("input"))?lnk:$("img",lnk);
      var src=img.attr("src");
      lnk.mouseout(function(){img.attr('src',src);}).
        mouseover(function(){img.attr('src',src.replace(/\.gif/,'_on.gif'));}).
        mousedown(function(){img.attr('src',src.replace(/\.gif/,'_click.gif'));});
    });

    // New gallery
    $('.ad-gallery').adGallery({
      effect: 'fade',
      slideshow: { enable: 1, autostart: 1, speed: 5000 }
    });

    // Lightbox
    var fbrotate=null;
    $('.lightbox').fancybox({
      titlePosition: 'inside',
      cyclic: true,
      speedIn: 500,
      speedOut: 500,
      onComplete: function(){ fbrotate=setInterval(function(){$.fancybox.next();},5000); },
      onCleanup: function(){ clearInterval(fbrotate); }
    });
    $('.lightbox650').fancybox({
      titlePosition: 'inside',
      autoScale: false,
      autoDimensions: false,
      cyclic: true,
      width: 650,
      height: 550,
      speedIn: 500,
      speedOut: 500,
      onComplete: function(){ fbrotate=setInterval(function(){$.fancybox.next();},5000); },
      onCleanup: function(){ clearInterval(fbrotate); }
    });

});

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;
}	

	


