// JavaScript Document

// The following code creates the Press section drop-down menus.
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

// open hidden layer
function mopen(id) {	
    // cancel close timer
    mcancelclosetime();

    // close old layer
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';

    // get new layer and show it
    ddmenuitem = document.getElementById(id);
    ddmenuitem.style.visibility = 'visible';

}
// close shown layer
function mclose() {
    if (ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// close timer
function mclosetime() {
    closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
    if (closetimer) {
	window.clearTimeout(closetimer);
	closetimer = null;
    }
}

// close layer when click-out
document.onclick = mclose;


// Extract the URL variables
this.getUrlVars = function() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++) {
	hash = hashes[i].split('=');
	vars.push(hash[0]);
	vars[hash[0]] = hash[1];
    }
    return vars;
}
this.getUrlVar = function(name) {
    return getUrlVars()[name];
}

var bHasScrolled = false;
this.scrollToSection = function() {
    if (bHasScrolled) return;
    bHasScrolled = true;

    var param = getUrlVar('section');
    if (param && param != '') {
	var dlay = setInterval(function() {
	    if (jQuery('.sIFR-replaced').length >= sIFR.replacements.length) {
		scrollToNextSection(param, 3000);
		clearInterval(dlay);
	    }
	}, 200);
    }
}

this.selectAboutMenu = function(selectedMenu) {
    $('.menuItem').each(function() {
	if ($(this).attr('id') != selectedMenu)
	    $(this).removeClass('current').addClass('link').html('<a href="javascript: void(0);">\u00a0</a>');
	else
	    $(this).removeClass('link').addClass('current').html('\u00a0');
    });
}
this.scrollToNextSection = function(id, myT) {
    var timer = (myT != undefined && myT != 'undefined' && myT != '') ? myT : 1500;

    selectAboutMenu(id);

    // scroll to top of id element (note: the nav menu plus drop shadow take up 
    // the first 50 pixels, so be sure to remove that from the calculation
    $('html,body').animate({
	scrollTop: $("#" + id + "Section").offset().top - 50
    }, timer, 'easeOutQuad');
}


$(document).ready(function() {

    $('#StoryNewspaperColumns').columnize({columns:2, lastNeverTallest: true, buildOnce: true});
    //$('.staffBioCol').columnize({columns:2, lastNeverTallest: true, buildOnce: true});

   // View All/Hide Some links at bottom of Press section columns
   var bAllPressReleasesDisplayed = false;
    $('#ViewAllPressReleases').click(function() {
	if (bAllPressReleasesDisplayed) {
	    $('#CollapsedPressReleases').slideUp('slow');
	    bAllPressReleasesDisplayed = false;
	    $('#ViewAllPressReleases').html("View All Press Releases for Selected Year");
	} else {
	    $('#CollapsedPressReleases').slideDown('slow');
	    bAllPressReleasesDisplayed = true;
	    $('#ViewAllPressReleases').html("Hide Some Press Releases for Selected Year");
	}
    }); 
    var bAllNewsDisplayed = false;
    $('#ViewAllNewsItems').click(function() {
	if (bAllNewsDisplayed) {
	    $('#CollapsedNewsItems').slideUp('slow');
	    bAllNewsDisplayed = false;
	    $('#ViewAllNewsItems').html("View All News Items for Selected Year");
	} else {
	    $('#CollapsedNewsItems').slideDown('slow');
	    bAllNewsDisplayed = true;
	    $('#ViewAllNewsItems').html("Hide Some News Items for Selected Year");
	}
    });

    // Navigation menus: scroll to next section when clicked
    $('#story').click(function() {
		scrollToNextSection('story');
    });
    $('#capabilities').click(function() {
		scrollToNextSection('capabilities');
    });
    $('#people').click(function() {
		scrollToNextSection('people');
    });
    $('#values').click(function() {
		scrollToNextSection('values');
    });
    $('#contact').click(function() {
		scrollToNextSection('contact');
    });
    $('#press').click(function() {
		scrollToNextSection('press');
    });


    // Contact section - set the local business separators to the same height so the vertical separators
    // are all equal lengths. Add 10px to account for the sIFR image calculations.
    var maxH = 0;
    var h = 0;
    $('.localContactContainer').each(function() {
    	h = $(this).outerHeight(true);
    	maxH = Math.max(h, maxH);
    });
    $('.localContactContainer').height(maxH + 10);

    // Make sure all bio links open in new window
    $('.bioP a').click(function() {
	this.target = "_blank";
    });

    // In Press section, make sure all news items and press releases
    // have the same number of lines in the title
    var maxH = 0;
    $('.prTitle').each(function() {
    	h = $(this).outerHeight(true);
    	maxH = Math.max(h, maxH);
    });
    $('.prTitle').each(function() {
    	h = $(this).outerHeight(true);
    	if (h < maxH) {
			$(this).css('padding-bottom', maxH - h);
		}
    });

    // code for the press section's archive filter drop-downs
    $('#PressReleaseDD').mouseover(function() {
		mopen('PressReleaseMenu');
    });
    $('#PressReleaseDD').mouseout(function() {
		mclosetime('PressReleaseMenu');
    });


    // scroll to the selected section
    var param = getUrlVar('section');
    if (param && param != '') {
		//window.setTimeout(scrollToNextSection, 4000, param, 1500);
		//scrollToNextSection(param, 1500);
    }

    // show/hide styles to the div tags surrounding each year's news items
    $('.newsItemsLink').each(function() {
	var year = $(this).attr('id').replace('NewsItemsFor', '');
	// hide all years' archives; then show the selected year's archives
	$('#NewsItemsFor' + year).click(function() {			
	    $('.newsArchives').removeClass('visiblePressItem').addClass('hiddenPressItem');
			
	    $('#NewsForYear' + year).addClass('visiblePressItem');
	    $('#HiddenNewsForYear' + year).addClass('visiblePressItem');

	    // Hide View All if nItems <= 3. Show if greater than 3.
	    var nItems = $("#NewsForYear" + year + " > div.prItem").size() + $("#HiddenNewsForYear" + year + " > div.prItem").size();
	    $('#ViewAllNewsItems').css('display', (nItems <= 3) ? 'none' : 'block');
			
	    // For current year, be sure to set it to "Current"
	    $('#NewsYearSelected').html((year == (new Date).getFullYear()) ? "Current" : year);
				
	});
    });
	
    $('.currentNewsYear').mouseover(function() {
	mopen('NewsItemsLinks');										 
    });
    $('.currentNewsYear').mouseout(function() {
	mclosetime();										 
    });
    $('#NewsItemsLinks').mouseover(function() {
	mcancelclosetime();										 
    });
    $('#NewsItemsLinks').mouseout(function() {
	mclosetime();										 
    });
	
    // show/hide styles to the div tags surrounding each year's PR items
    $('.prItemsLink').each(function() {
	var year = $(this).attr('id').replace('PRItemsFor', '');
	// hide all years' archives; then show the selected year's archives
	$('#PRItemsFor' + year).click(function() {		
	    $('.prArchives').removeClass('visiblePressItem').addClass('hiddenPressItem');
			
	    $('#PressReleasesForYear' + year).addClass('visiblePressItem');
	    $('#HiddenPressReleasesForYear' + year).addClass('visiblePressItem');

	    // Hide View All if nItems <= 3. Show if greater than 3.
	    var nItems = $("#PressReleasesForYear" + year + " > div.prItem").size() + $("#HiddenPressReleasesForYear" + year + " > div.prItem").size();
	    $('#ViewAllPressReleases').css('display', (nItems <= 3) ? 'none' : 'block');
			
	    // For current year, be sure to set it to "Current"
	    $('#PRYearSelected').html((year == (new Date).getFullYear()) ? "Current" : year);
	});
    });
	
    // Hide View All if nItems <= 3. Show if greater than 3.
    var currYear = (new Date).getFullYear();
    var nItems = $("#PressReleasesForYear" + currYear + " > div.prItem").size() + $("#HiddenPressReleasesForYear" + currYear + " > div.prItem").size();
    $('#ViewAllPressReleases').css('display', (nItems <= 3) ? 'none' : 'block');
    nItems = $("#NewsItemsForYear" + currYear + " > div.prItem").size() + $("#HiddenNewsItemsForYear" + currYear + " > div.prItem").size();
    $('#ViewAllPressReleases').css('display', (nItems <= 3) ? 'none' : 'block');

    $('.currentPRYear').mouseover(function() {
	mopen('PRItemsLinks');										 
    });
    $('.currentPRYear').mouseout(function() {
	mclosetime();										 
    });
    $('#PRItemsLinks').mouseover(function() {
	mcancelclosetime();										 
    });
    $('#PRItemsLinks').mouseout(function() {
	mclosetime();										 
    });

    // Finally, if the sIFR onReplacement call did not work (as in, on Windows FF and IE)
    // be sure to scroll here at the end.
    var pf = WebBrowser.getPlatform();
    if (pf == "win") {
	scrollToSection();
    }
});
