// SFEI_ALLPAGES.JS
// JOHN J ORAM
// 7.1.09
//
// COLLECTION OF JAVASCRIPT/JQUERY FUNCTIONS TO ADD TO ALL PAGES OF THE DRUPAL CMS.
//
// RELATED FILES: 
// 		/SITES/ALL/THEMES/SFEI/PAGES.TPL.PHP
//			THE PAGE TEMPLATE FILE THAT DEFINES THE OVERALL LAYOUT OF THE SITE
//			AND ADDS ELEMENTS USED IN THE FUNCTIONS CONTAINED HERE
			
			
function initTopMap() {
	topmap = new GMap2( $("#mapContainer").get(0) );
	topmap.setCenter(new GLatLng(37.785, -122.2), 6); // need this ...
	topmap.addMapType(G_PHYSICAL_MAP);
	topmap.setMapType(G_PHYSICAL_MAP);
	topmap.addControl(new GSmallMapControl());
	topmap.addControl(new GMenuMapTypeControl());
	//topmap.addControl(new GScaleControl());
	//topmap.addControl(new GOverviewMapControl());
	topmap.enableContinuousZoom();
	topmap.enableDoubleClickZoom();
	$.cookie('mapState','initialized',{expires:7});
}

function showBannerImage() {
	// this function populates the banner imgContainer with a randomly selected image
	var number_of_banners = 6;	// set this to the number of banners in the SFEI theme/images folder
	var imgname = '/sites/all/themes/SFEI/images/banner-' + $.randomBetween(1,number_of_banners) + '.jpg';
	imgname = "<img src='" + imgname + "' alt='' />";
	$('#imgContainer').html(imgname).show();
	$('#mapContainer').hide();
	$.cookie('bannerState','image',{expires:7});
}

function hideBannerImage() {
	
}

function showBannerMap() {
	$('#mapContainer').slideDown(function(){ 
		if ( $.cookie('mapState') == null) {
			// not yet initialized, do it now
//			initTopMap();  // commented out for now -- will reactivate when map functionality is implemented
		}
		$('#mapalert').show();
	});
	if ($('#imgContainer').is(':visible')){ $('#imgContainer').slideUp(); }
	$('#mapToggle').attr('src','/sites/all/themes/SFEI/images/mapTabOpen.gif');
	$('#allToggle').attr('src','/sites/all/themes/SFEI/images/icon_close.gif');	
	$.cookie('bannerState','map',{expires:7});
}

function hideBannerMap() {
	$('#mapContainer').slideUp();
	$('#mapalert').hide();
	//$('#imgContainer').slideDown();
	$('#mapToggle').attr('src','/sites/all/themes/SFEI/images/mapTabClosed.gif');
}

function toggleBannerAll() {
		if ( ($.cookie('bannerState')=='image') | ($.cookie('bannerState')=='map') ) {
			$('#mapContainer').slideUp();
			$('#mapalert').hide();
			$('#imgContainer').slideUp();
			$('#mapToggle').attr('src','/sites/all/themes/SFEI/images/mapTabClosed.gif');
			$('#allToggle').attr('src','/sites/all/themes/SFEI/images/icon_plus.gif');
			$.cookie('bannerState','none',{expires:7});
		} else {
			$('#imgContainer').slideDown();
			showBannerImage();
			$('#allToggle').attr('src','/sites/all/themes/SFEI/images/icon_close.gif');
			$.cookie('bannerState','image',{expires:7});
		}
}


function filterProjects(filter) {
	// Function to filter the Project page by Program
	$('#filter-projects-by-program').append('&nbsp;<img src="/sites/all/themes/SFEI/images/ajax-loader.gif" alt="" />');
   document.location.href = filter;
}

function initTopAlert(html) {
	// Function to initialize the top-alert panel.

	// Use a cookie to determine whether or not to show the alert
	if ($.cookie('topAlert') == null) {
		// cookie has not been set, activate the alert
		$('#top-alert-content').html(html);
		$('#top-alert').slideDown();
		$.cookie('topAlert','displayed'); // set the cookie to 'displayed' to indicate that the alert has already
													  // been shown to the user. do not show again for 7 days
	}
	
	// make the alert hideable
	$('#top-alert-close').click(function(){ 
		$(this).parent().slideUp(); 
		$.cookie('topAlert','displayed',{expires:7}); // remember the cookie for 7 days
	});	
	$('#top-alert-close').attr('title','Close');
}

function initExpandableMenus(){
	// Function to make primary menus (across top of page) expandable
	//
	// make the Programs menu hoverable
	var html = "<ul>" +
					"<li><a href='/bi'>Biological Invasions</a></li>" +
					"<li><a href='/cb'>Conservation Biology</a></li>" +
					"<li><a href='/cmr'>Contaminant Monitoring & Research</a></li>" +
					"<li><a href='/informatics'>Environmental Informatics</a></li>" +
					"<li><a href='/he'>Historical Ecology</a></li>" +
					"<li><a href='/it'>Information Technology</a></li>" +
					"<li><a href='/rmp'>Regional Monitoring Program</a></li>" +
					"<li><a href='/ws'>Watershed Science</a></li>" +
					"<li><a href='/wl'>Wetland Science</a></li>" +
				"</ul>";
	$('#main').append('<div id="program-hover" class="menu-hover"></div><div id="aboutus-hover" class="menu-hover"></div>');
	$('#program-hover').html(html);
	$('#aboutus-hover').html( $('#aboutus-hover-tmp').html() ); // the contents for this hover div are placed in an invisible div
																// by a custom block (Block Name = About Us Navigation - Invisible) 
	$('.menu-hover').hide().hover(
		function(){},
		function(){
			$(this).hide();
			$('li.menu-538').removeClass('expanded').addClass('collapsed');
			$('li.menu-535').removeClass('expanded').addClass('collapsed');
		}
	); 
	$('li.menu-535, li.menu-538').addClass('collapsed');
	$('ul.links > li').hover(
		function(){
			// define the hover function
			if ( $(this).hasClass('menu-535') ) {
				var thispos = $(this).offset();
				var x = thispos.left;
				var y = thispos.top + $(this).css('height');
				$('#program-hover').css('left',x + 'px').css('top',y + 'px');
				$(this).removeClass('collapsed').addClass('expanded');
				$('#program-hover').show();
				$('.menu-hover:not("#program-hover")').hide();
			} else if ( $(this).hasClass('menu-538') ) {
				var thispos = $(this).offset();
				var x = thispos.left;
				var y = thispos.top + $(this).css('height');
				$('#aboutus-hover').css('left',x + 'px').css('top',y + 'px');
				$(this).removeClass('collapsed').addClass('expanded');
				$('#aboutus-hover').show();
				$('.menu-hover:not("#aboutus-hover")').hide();
			} else {
				$('li.menu-535').removeClass('expanded').addClass('collapsed');
				$('li.menu-538').removeClass('expanded').addClass('collapsed');
				$('.menu-hover').hide();
			}
		},
		function(){}
	);
	// use the logo-title div to close the menu-hover div.  This prevents stranded open menu-hover divs
	$('#logo-title').hover(
		function(){ 
			$('li.menu-538').removeClass('expanded').addClass('collapsed');
			$('li.menu-535').removeClass('expanded').addClass('collapsed');
			$('.menu-hover').hide(); 
		},
		function(){}
	);	
}

$(document).ready(function(){

    // ---- Menu trail ----
	// Add and active-trail class so that CSS styles are propoagated.
	// Extends the Active-Trail Drupal module.
      $('li.active-trail').children().addClass('active-trail');
	// ---- End menu trail ----

	// ---- Custom page banner ----
	// Create the custom banner on all pages
	// add a temporary alert to the map container ...
	var html = '<div id="mapalert" class="ui-state-error"><h3>Coming Soon!</h3><p>We are developing a new interface to allow you to find the content you are looking for through this map interface.</p></div>';
	$('body').append(html);
	$('#mapalert').hide();
	$('#mapContainer').hide();
	$('#imgContainer').hide();
	// Set/get a cookie to remember the state of the top banner ----
	$.cookie('mapState',null,{expires:7}); // on every page must refresh/reset the map state
	if ( $.cookie('bannerState') == null ) {
		$.cookie('bannerState','none',{expires:7});	// default to none (ie., no banner)
	}
	if ( $.cookie('bannerState') == 'image' ) {
		showBannerImage();	// add a random image to the banner
	} else	if ( $.cookie('bannerState') == 'map' ) {
		showBannerMap();
	} else if ( $.cookie('bannerState') == 'none' ) {
		$('#mapContainer').hide();
		$('#mapalert').hide();
		$('#imgContainer').hide();
		$('#mapToggle').attr('src','/sites/all/themes/SFEI/images/mapTabClosed.gif');
		$('#allToggle').attr('src','/sites/all/themes/SFEI/images/icon_plus.gif');
	}

	// Make the banner toggle clickable
	$('#mapToggle').click(function(){
		if ( $('#mapContainer').is(':visible') ) {
			hideBannerMap();
			//showBannerImage();
		} else {
			showBannerMap();
		}
	});

	// Make a toggle to hide all banner content
	$('#allToggle').click(function(){
		toggleBannerAll();
	});
	// ---- End custom page banner ----
	
	// ---- Custom selectors ----
	// activate selHeads (custom accordion)
	$('.selContent').hide(); // just in case
	$('.selHead').click(function(){
		if ( $(this).next().is(':hidden') ) {
			$('.selContent').hide(); 						// hide any if visible to avoid overlap
			$('.selHead').removeClass('selHead-active'); 	// same as above, just fix classes
			$(this).addClass('selHead-active');
			$(this).next().show();
		} else {
			$(this).removeClass('selHead-active');
			$(this).next().hide();
		}
		return false;
	});
	// ---- End custom selectors ----
	
	// ---- Collapsable headers (h2) ---- 
	$('.h2toggle').click( function(){ 
		if ( $(this).next().is(':visible') ) {
			$(this).next().slideUp();
			$(this).addClass('h2toggle-collapsed');
			$('#eventsContent_toggle').show();
		} else {
			$(this).next().slideDown();
			$(this).removeClass('h2toggle-collapsed');
			$('#eventsContent_toggle').hide();
		}
	});
	$('#eventsContent_toggle').click( function(){ $('.h3toggle').trigger('click'); });
	
	
	// ---- Expandable lists in right sidebar ----
	// hide by default
	$('#expandable-list .content').hide();
	// ---- End expandable lists ----
	
	// ---- Custom programs menu ----
//	initExpandableMenus();
	// ---- End custom progams menu ----

	// Filter Projects by Program -- if a filter is in place add it to the selector
    var p = location.pathname;
	p = p.replace(/%20/g,' '); // need to account for spaces in the URL
    $("#project-filter option[value=" + p + "]").attr('selected','selected');

	// Top Alert Panel
	// Edit the html variable to create a customized alert banner at the the top of the page.
	// Comment out the "initTopAlert(htmL);" line to disable the alert
//	var html = '<p>Welcome to our new website. We have made attempts to ensure that information from our old site is represented within the structure of this new site. Still, we recognize that we may have missed some information.  To ensure that critical information is not lost, we have created a <a href="http://legacy.sfei.org" target="_blank">legacy site</a> to preserve our old site in perpetuity.  That site can be accessed at any time by clicking the link in the upper left-hand corner of your browser.  Please provide feedback to <a href="mailto:webfeedback@sfei.org">webfeedback@sfei.org</a> and we will move the missing information into this new site.</p>';
//	initTopAlert(html);
	
	// User Login Block
	$('#navigation-top-toggle').click( function(){ 
		if ($('#navigation-top-content').is(':visible')) {
			$('#navigation-top-content').slideUp();
			$('#navigation-top-toggle').addClass('closed').removeClass('open');
		} else {
			$('#navigation-top-content').slideDown();
			$('#navigation-top-toggle').addClass('open').removeClass('closed');
		}
	});
		
	
	// PopularProjects (ie, Featured Projects) Table on Home Page
	$('.SFEI-PopularProjects-icon').hover( 
		function(){
			// Mouse In
			// populate the html for the display div and show it
			$(this).find('img').css('border','solid 2px #f60');
			var html = $(this).next().html();
			$('#popularProjectsDisplay').html(html).slideDown();
		},
		function(){
			// Mouse Out
			$(this).find('img').css('border','solid 2px transparent');
		}
	);

	// Lightbox - prettyPhoto --> any link (a) with a rel='prettyPhoto' tag will get a lightbox treatment
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	// Debugging : This is just a check to make sure all the javascript code was executed successfully.
	 //alert('JavaScipt Success');
});



 

