﻿function getSenderValue(e){
    var sender;
    if (!e){
        var e = window.event;
    }
    if (e.target){
        sender = e.target;
    }
    else if (e.srcElement){
        sender = e.srcElement;
    }
    // defeat Safari bug
    if (sender.nodeType == 3){
	    sender = targ.parentNode;
	}
	return sender.value;
}

function checkEnter(e, buttonId, noEmpty){
    var cCode;
    if ( e && e.which ) {
        cCode = e.which;
    }
    else {
        cCode = e.keyCode;
    }
    if ( cCode == 13 ) {
        if (!noEmpty || (noEmpty && getSenderValue(e) != '')){
            var button = document.getElementById(buttonId);
            if (button.nodeName.toLowerCase() == 'a'){
                eval(button.href.replace('javascript:', ''));
            }
            else{
                button.click();
            }
            return false;
        }
    }
    else {
        return true;
    }
   }


function clearField(field, defaultValue){
	if (field.value == defaultValue){
		field.value = '';
	}
}

function defaultField(field, defaultValue){
	if (field.value == ''){
		field.value = defaultValue;
	}
}

function expandListing(sender, id){
    if (sender.className.replace(/^\s+|\s+$/g, '') == 'heading'){
		sender.className = 'heading open';
	}
	else {
		sender.className = 'heading';
	}
    showHide(id);
    
}

function showHide(id){
	var e = document.getElementById(id);
	if (e.className == 'over'){
		e.className = '';
	}
	else {
		e.className = 'over';
	}
}
// extWindows() makes links open in a different window. use rel="external" instead of target="_blank" and rel="popup" or rel="popup|[width]|[height]" for popups
function extWindows(){
   	$("a[rel=external]").attr("target","_blank");
	$("a[rel^=popup]").click(function(){
		theHref = $(this).attr("href");
//		$(this).attr("href","javascript:void(0)");
		var popupWidth, popupHeight
		var relSplit = $(this).attr("rel").split("|");
		if(relSplit[1]){
			popupWidth = relSplit[1];
			popupHeight = relSplit[2];
		} else {
			popupWidth = 435;
			popupHeight = 350;
		}
		popMeUp(theHref,popupWidth,popupHeight)
		return false;
	});
	
}
function popMeUp(strURL, strWidth, strHeight) {
    theWin = window.open(strURL, "popupWin", "scrollbars,resizable,height=" + strHeight + ",width=" + strWidth);
    theWin.focus();
}

function clickLinkWithArrow() {
	var newArrow = "/UI/Images/article-listing-arrow2.gif";
	var oldArrow = "/UI/Images/article-listing-arrow.gif";
	
	$(".linkArrow, span.heading").parent("a").mouseover(function() {
	$(".linkArrow", this).attr("src", oldArrow);
		$(".linkArrow", this).attr("src", newArrow);
	});
	$(".linkArrow, span.heading").parent("a").mouseout(function() {
		$(".linkArrow", this).attr("src", oldArrow);
	});
}
//start Overlay
function buildOverlay() {
	$("body").prepend("<div id='pPopOlay'></div>");
	$("#pPopOlay").css("height", $(document).height());
	$("#pPopOlay").css("opacity", 0).fadeTo("medium", 0.6);
}

function setCenterPos() {
	var winTop = $(window).scrollTop();
	var setLeft = ($(window).width() / 2) - ($("#pPop").width() / 2);
	var setTop = ($("#pPop").height() > ($(window).height() - 20)) ? (10 + winTop) + "px" : (($(window).height() / 2) - ($("#pPop").height() / 2) + winTop) + "px";


	$("#pPop").css({ top: setTop, left: setLeft });
}

function openOverlay() {
	//$(".calendarInfo a").click(function(e) {
	
	
	
	$(".lightbox").click(function(e) {
	    
		e.preventDefault();
		
		if(!document.getElementById("pPopPlaceHolder")) {
		    $("body").prepend("<div id='pPopPlaceHolder'></div>");

		    buildOverlay();
			
		    var id=$(this).attr("id");
		    $("#pPopPlaceHolder").load("/UI/Pages/Dialogs/LightBox.aspx?pageID=" + id, function(e){

				setCenterPos();

			    $("#pPopClose, #pPopOlay").click(function() {
				    $("#pPopPlaceHolder, #pPopOlay").remove();
			    });
			
		    });
		 }
	});
}	
//end Overlay

function overflowHidden() {
	// hide the last news item if it overflows the tab
	var newsDivHeight = $("#newstab").height();
	var newsLinkHeight = 0;
    
	$("#newstab a").each(function() {
		var marginBottom = $(this).css("margin-bottom").replace("px", "");
		newsLinkHeight = newsLinkHeight + $(this).height() + parseInt(marginBottom, 10);

		if ((newsLinkHeight - marginBottom) > newsDivHeight) {
			$(this).css({"display":"none"});
		}
	});

	// hide the last calendar item if it overflows the tab
	var calendarDivHeight = $("#calendartab").height();
	var calendarLinkHeight = 0;

	$("#calendartab a").each(function() {
		var marginBottom = $(this).css("margin-bottom").replace("px", "");
		calendarLinkHeight = calendarLinkHeight + $(this).height() + parseInt(marginBottom, 10);

		if ((calendarLinkHeight - marginBottom) > calendarDivHeight) {
			$(this).css({ "display": "none" });
		}
	});
    
}

$(document).ready(function() {
	clickLinkWithArrow();
	extWindows();
	openOverlay();
	overflowHidden();
	
	$(".sitemap").children("ul").each(function() {
		var currentTallest = 0;
		$(this).children().each(function(i) {
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({ 'height': currentTallest + "px" }); }
		$(this).children().css({ 'min-height': currentTallest + "px" });
	});
	return this;

});

