﻿
// Add a class name to the body as a global flag that the user has javascript.
document.observe('dom:loaded', function(){
	var body = $(document.body);
	body.addClassName('js-enabled');
});
// end

var panelHoverShow =
Class.create({
	initialize: function(eWatchHover, eRelDiv) {
		this.hoverItem = eWatchHover;                       // item we want to watch hover on
		this.relDiv = eRelDiv;                              // item to hide / show based on hover
        
        if (!this.hoverItem || !this.relDiv) { return; }    // if these id's don't exist on page, return
		
		this.hideRelDivs();                                 // hide all related divs onload
			
		// detect mouseovers	
		Event.observe(this.hoverItem, "mouseover", this.__MouseOver.bindAsEventListener(this));	     // watch mouseover on hover item
		Event.observe(this.relDiv, "mouseover", this.__MouseOver.bindAsEventListener(this));	    // also watch mouseover on related item (as we only want to force this div to stay on until mouseout)	
		
	},
	
    /**
	 * Hide the Related Divs
	 */
	hideRelDivs: function() {
	    var relItems = $$('div#corresponding_home_panels div');                     // all related divs
	    for(var i=0; i < relItems.length; i++) {
		    relItems[i].removeClassName("position_div").addClassName("hide");	    // remove any potential positioning and hide
		}
        if(!relItems) {
            return;
        }
	},
	

	/**
	 * Deal with a mouseover
	 */
	__MouseOver: function(e) {
		var e = e || window.event;

		this.hideRelDivs();	  // hide all related divs
		this.relDiv.removeClassName("hide").addClassName("position_div");                   // remove hide class from homePanelHide, add trigger class which positions div
		
		Event.observe(this.relDiv, "mouseout", this.__MouseOut.bindAsEventListener(this));  // observe mouseout
		     
	},
	
	/**
	 * Deal with a mouseout
	 */
	__MouseOut: function(e) {
	    var e = e || window.event;
	     
        this.hideRelDivs();     // hide all related divs
	},
	
	
	hoverOffOrig: function() {
	    if (!this.hoverItem) {
		    return;
		}
	    // By default mouseout is based on the related div, here we are also watching mouseout on original hover div as well
		Event.observe(this.hoverItem, "mouseout", this.__MouseOut.bindAsEventListener(this)); 		

	}


});


var TabSwitcher = Class.create({
			initialize: function(aTabs, aContents) {
				this.tabs = aTabs;
				this.contents = aContents;
				// Loop through each and attach a click handler
				for(var i=0; i<this.tabs.length; i++) {
					Event.observe(this.tabs[i], 'click', this.__Click.bindAsEventListener(this));
				}
				// Show the first content item
				this.show(0);
			},
			/**
			 * Show a specific content item
			 */
			show: function(iWhich) {
				for (var i=0; i<this.contents.length; i++) {
					if (i == iWhich) {					    
						this.contents[i].style.display = 'block';
						$(this.tabs[0].parentNode).addClassName("on"); // turn on first li by default when page loads
						if (this.elementLi) {                          // if an li has been clicked
						    this.hideClassName();                     // turn of all other li's first
						    this.elementLi.addClassName("on");	      // then turn on specific li					   
						}
					} else {
						this.contents[i].style.display = 'none';
					}
				}
			},
			
			hideClassName: function() {
			    var allLi = $$('.round_right ul li');
			    for (var i=0; i<allLi.length; i++) {
			        allLi[i].removeClassName("on");
			    }
			},
			
			/**
			 * Deal with a clicked link
			 */
			__Click: function(e) {
				var e = e || window.event;
				Event.stop(e);
				var element = Event.element(e);
				this.elementLi = Event.findElement(e, "LI");    // storing li relative to event
				for (var i=0; i<this.tabs.length; i++) {
					if (this.tabs[i] == element) {
						this.show(i);											
						break;
					}
				}
			}
		});

		
		
		// this function adds an extra css class in the calendar, for this week
		function calendarWeek() 
		{
			// a performance tweak is to only search the dom for an object once
			
			var objects = $$('div.date span.NextWeek');
			
	        if (objects.length == 0) {
				return;
	        }
		    var weekParent = $(objects[0].parentNode.parentNode).addClassName("this_week");
		}
		
		
		
		function hideEmptyUl() {
		    if ($$('ul.subsubnav2')[0]) {
		        var allSubNav = $$('ul.subsubnav2');
		        for (var i=0; i< allSubNav.length; i++) {
		            if (allSubNav[i].empty()) {
		                allSubNav[i].up('li').hide();
		                }	            
		            }
		            
		        }
		    }

/// Attach common load handlers only
///		, as specific page handlers are attached on each page in the headContentPlaceHolder.

Event.observe(window, 'load', function() 
{
	// tab switch could be used on any page, so it's run on every page
	
	tabswitchy = new TabSwitcher($$('.round_right ul li a'), $$('#tab_content > div'));
	hideEmptyUl();
	
});



// ****** start: misc ******

function printPage()
{
    if (window.print){window.print();}
}

function hide(element)
{
	document.getElementById(element).style.display = 'none';
	return;
}
function show(element)
{
	document.getElementById(element).style.display = 'block';
	return;		
}

// ****** end: misc ******


// *** start: popup links ***

function doPopups()
{

	if (document.getElementById('my_popup_link')) //example
	{
		var my_popup_link = document.getElementById('my_popup_link');
		var my_popup_link_Href = my_popup_link.getAttribute('href');
		my_popup_link.onclick = function()
		{
			return(popuplink(my_popup_link_Href, this, 600, 400, false));
		}
	}

}

// start: popup window
// usage: popuplink(['js-only url',] this[, w[, h[, scroll[, extras]]]])
// basic usage: <a href="popup.html" target="_blank" onclick="return(popuplink(this));">new pop</a>
// advanced usage: <a href="popup_nojs.html" target="_blank" onclick="return(popuplink('popup_yesjs.html', this, 200, 100, false));">new pop</a>
// site-wide defaults:
popup_w = 400;
popup_h = 300;
popup_scroll = true;
popup_extras = 'location=0,statusbar=0,menubar=0';
function popuplink() {
	var undef, i=0, args=popuplink.arguments;
	var url = (typeof(args[i])=='string') ? args[i++] : args[i].getAttribute('href');
	var target = args[i++].getAttribute('target') || '_blank';
	var w = args[i++];
	var h = args[i++];
	var s = (args[i]===undef) ? popup_scroll : args[i++];
	var features = 'width=' + (w || popup_w)
				 + ',height=' + (h || popup_h)
				 + ',scrollbars=' + (s ? 'yes,' : 'no,')
				 + (args[i] || popup_extras);
	var win = window.open(url, target, features);
	win.focus();
	return false;
}

//Event.observe(window, "load", doPopups);

// *** end: popup links ***

