// JavaScript Document


$(document).ready(function() {
						   $('.sub-pop-content').hide();
						   $('.subMenuSection').hide();
						   
						   //  Swap body content on accordion section click
						   //
						   $('.menu-button a').click(function() {
															  
										var iframe = $(this).attr('rel');
										var frameSource = $('#content-right');
										
										if (frameSource != iframe) {
													function frameSwapper() {
														$('#content-right').attr('src', iframe);
													}
													frameSwapper();
										}
							});
						
						   //  Swap body content on footer section click
						   //
						   $('.item a').click(function() {
															  
										var iframe = $(this).attr('rel');
										var frameSource = $('#content-right');
										
										if (frameSource != iframe) {
													function frameSwapper() {
														$('#content-right').attr('src', iframe);
													}
													frameSwapper();
										}
							});

						   
						   //  Top and Bottom Navigation functionality
						   //
						   $('.center-div div').click(function() {
															   
										var navLocation = $(this).parent().parent().attr('id');
										
										
										//  Defines if click is in the top or bottom menu
										//  then sets the target div to open and subsequent opposite target div to close
										//
										if (navLocation == 'nav-top'|| navLocation == 'nav-footer') {
											target = $('#nav-top-drop-content');
											oppositeTarget = $('#nav-bottom-drop-content');
										}
										else if (navLocation == 'nav-bottom') {
											target = $('#nav-bottom-drop-content');
											oppositeTarget = $('#nav-top-drop-content');
										}
										
										var linkTitle = $(this).attr('title');
										var targetSubSection = $('.subMenuSection[rel=' + linkTitle + ']');
										var oldSubSection = $('.subMenuSection:visible');
										var oldSubSectionRel = $('.subMenuSection:visible').attr('rel');
										
										//  Opens and closes the target divs as needed
										//
										function contentChange_checker() {
											
											if ((navLocation == 'nav-footer') && (linkTitle == 'About' || linkTitle == 'Privacy' || linkTitle == 'Switch and Data Home')) {
												if (linkTitle == 'About' || linkTitle == 'Privacy') {
//													alert('Open ' + linkTitle + ' in one of those trendy lightboxes!');
//
												}
												else {
//													alert('Open ' + linkTitle + ' in _blank');
//													return false;
												}
											}
											else if (linkTitle == oldSubSectionRel) {
												// clicked same link that is already open... closes submenu
												$(target).slideUp();
												$(oldSubSection).hide();
											}
											else if (oldSubSection != linkTitle) {
												
												if ($(target).is(':visible') && $(oppositeTarget).is(':hidden')) {
													// new content in the active submenu leaving submenu open
													$(oldSubSection).hide();
													$(targetSubSection).show();
												}
												else if ($(target).is(':hidden') && $(oppositeTarget).is(':visible')) {
													// new content in inactive submenu closing the previously active submenu
													$(oppositeTarget).slideUp();
													$(oldSubSection).hide();
													$(targetSubSection).show();
													$(target).slideDown();
												}
												else {
													// new content in inactive submenu where nothing was previously active
													$(oldSubSection).hide();
													$(targetSubSection).show();
													$(target).slideDown();
												}
											}
										}
										
										// Original actions without subsection swapping
										//
										/*if ($(target).is(':visible') && $(oppositeTarget).is(':hidden')) {
											$(target).slideUp();
											$('.subMenuSection').hide();
										}
										else if ($(target).is(':hidden') && $(oppositeTarget).is(':visible')) {
											$(oppositeTarget).slideUp();
											$('.subMenuSection').hide();
											$(targetSubSection).show();
											$(target).slideDown();
										}
										else {
											$('.subMenuSection').hide();
											$(targetSubSection).show();
											$(target).slideDown();
										}
										*/
										contentChange_checker();
										$.idle(function() { $(target).slideUp(); $(targetSubSection).hide(); }, 5000);
						   });
						   
});


/////////// Cookie functions /////////////////////////////////
function read_cookie (key, skips)
{
        // Set skips to 0 if parameter was omitted:
        if (skips == null)
                skips = 0;

        // Get cookie string and separate into individual cookie phrases:
        var cookie_string = "" + document.cookie;
        var cookie_array = cookie_string.split ("; ");

        // Scan for desired cookie:
        for (var i = 0; i < cookie_array.length; ++ i)
        {
                var single_cookie = cookie_array [i].split ("=");
                if (single_cookie.length != 2)
                        continue;
                var name  = unescape (single_cookie [0]);
                var value = unescape (single_cookie [1]);

                // Return cookie if found:
                if (key == name && skips -- == 0)
                        return value;
        }

		// Cookie was not found:
        return null;
}

function delete_cookie (name, path)
{
        // Build expiration date string:
        var expiration_date = new Date ();
        expiration_date.setYear (expiration_date.getYear () - 1);
        expiration_date = expiration_date.toGMTString ();

        // Build set-cookie string:
        var cookie_string = escape (name) + "=; expires=" + expiration_date;
        if (path != null)
                cookie_string += "; path=" + path;

        // Delete the cookie:
        document.cookie = cookie_string;
}

function delete_all_cookies (path)
{
        // Get cookie string and separate into individual cookie phrases:
        var cookie_string = "" + document.cookie;
        var cookie_array = cookie_string.split ("; ");

        // Try to delete each cookie:
        for (var i = 0; i < cookie_array.length; ++ i)
        {
                var single_cookie = cookie_array [i].split ("=");
                if (single_cookie.length != 2)
                        continue;
                var name = unescape (single_cookie [0]);
                delete_cookie (name, path);
        }
}


function writePersistentCookie (CookieName, CookieValue, periodType, offset) {

  var expireDate = new Date ();
  offset = offset / 1;
  
  var myPeriodType = periodType;
  switch (myPeriodType.toLowerCase()) {
    case "years": 
     var year = expireDate.getYear();     
     // Note some browsers give only the years since 1900, and some since 0.
     if (year < 1000) year = year + 1900;     
     expireDate.setYear(year + offset);
     break;
    case "months":
      expireDate.setMonth(expireDate.getMonth() + offset);
      break;
    case "days":
      expireDate.setDate(expireDate.getDate() + offset);
      break;
    case "hours":
      expireDate.setHours(expireDate.getHours() + offset);
      break;
    case "minutes":
      expireDate.setMinutes(expireDate.getMinutes() + offset);
      break;
    default:
      alert ("Invalid periodType parameter for writePersistentCookie()");
      break;
  } 
  
  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}  

