/* core.js */
/*
	A great deal of this code depends on the jQuery library by John Resig.
	More information on jQuery is available from http://jquery.com/
*/
/**
 * @fileoverview JavaScript library for RedCross.org
 * @projectDescription JavaScript library for RedCross.org
 * @author Navigation Arts (http://navarts.com/)
 * @version 2006-08-13
 */
// variables and configuration items
	/** speed that animation effects should take place at, options are: normal, fast, slow, x-slow, x-fast */
	var animspeed = 'normal';
	/** API key for yahoo maps API */
	var mapapikey = 'redcross_demo';
	var map_marker = new CustomImageMarker('./_res/swf/mappin.swf'); // this works best with a root relative path
	/** default text strings for self-clearing form fields */
	var searchfield_txt = 'Search';
	var zipfield_txt = 'Enter your zip code';
	/** state managing variables for homepage subsection menu */
	var nav_main = false;
	var nav_sub = false;
	var nav_visible = false;
	var nav_open = false;
	var nav_close = false;
	/** */
	var useflash = false;
// reusable functions
	/**
	 * This function clips excess whitespace characters from a String.
	 * @param {String} str The text string you want to trim excess whitespace from.
	 * @return {String} The trimmed string.
	 */
	function trim(str) {
		str = this != window? this : str;
		return str.replace(/^\s+/g,'').replace(/\s+$/g,'');
	}
	/**
	 * Checks if the homepage expanding menu should be open or closed and adjusts it appropriately.
	 */
	function checkmenu(){
		if((nav_main || nav_sub) && !nav_visible){ 
			if(!nav_open && !nav_close){
				nav_open = true;
				$('div#subsections').slideDown(
					animspeed,
					function(){
						$('#flash-anim *').hide();
						nav_open = false;
						nav_visible = true;
					}
				);
			}
		}else if(!nav_main && !nav_sub && nav_visible){
			if(!nav_close && !nav_open){
				nav_close = true;
				$('#flash-anim *').show();
				$('div#subsections').slideUp(
					animspeed,
					function(){
						nav_close = false;
						nav_visible = false;
					}
				);
			}
		}
	}
// execute these actions when the DOM is ready to be manipulated
	$(document).ready(function(){
		/**
		 * This code manages the self-clearing and restoring "site search" form field.
		 */
			$('#sitesearch-q').val(searchfield_txt);
			$('#sitesearch-q').focus(function(){
				if($(this).val()==searchfield_txt){
					$(this).val('');
				}
			});
			$('#sitesearch-q').blur(function(){
				if(trim($(this).val())==''){
					$(this).val(searchfield_txt);
				}
			});
		/**
		 * This code manages the self-clearing and restoring Zip code form field for local content.
		 */
			$('input#localzip').val(zipfield_txt);
			$('input#localzip').focus(function(){
				if($(this).val()==zipfield_txt){
					$(this).val('');
				}			
			});
			$('input#localzip').blur(function(){
				if(trim($(this).val())==''){
					$(this).val(zipfield_txt);
				}			
			});
		/**
		 * This code looks for an hCard entry for local chapters and if found adds a link to display a Yahoo! Maps API map of that chapter's location.
		 */
			if($('div#rightrail #local-your .vcard').get(0)){
				$('div#rightrail .vcard .street-address').append(' <a href="#" class="maplink"><img src="../../public_html/_res/js/./_res/img/icon-map.gif" alt="show/hide map" /><\/a>');
				$('div#rightrail .vcard').append('<div id="vcardmap"><\/div>');
				var centermap = $('div#rightrail .vcard .street-address').text() +', '+ $('div#rightrail .vcard .postal-code').text();
				var map = new Map('vcardmap',mapapikey,centermap,3);
				map.addMarkerByAddress(map_marker,centermap);
				$('div#local-your a.maplink').toggle(
					function(){
						$(this).parents('.vcard').find('#vcardmap').slideDown(animspeed);
						$(this).get(0).blur();
						return false;
					},
					function(){
						$(this).parents('.vcard').find('#vcardmap').slideUp(animspeed);
						$(this).get(0).blur();
						return false;
					}
				);
			}
		/**
		 * This code adds a map link to hCalendar items for local content.
		 */
			if($('div#rightrail div#local-events li.vcard div.street-address').get(0)){
				var eventmaps = 0;
				var arr_maps = new Array();
				// for each item with a street address...
				$('div#rightrail div#local-events li.vcard div.street-address').each(function(){
					// add the show/hide icon
					$(this).parents('li.vcard').find('div.location').append(' <a href="#" class="maplink"><img src="../../public_html/_res/js/./_res/img/icon-map.gif" alt="show/hide map" /><\/a>');
					// add a div to hold the map
					$(this).parents('li.vcard').append('<div class="eventmap" id="eventmap'+eventmaps+'"><\/div>');
					arr_maps.push(new Map('eventmap'+eventmaps,mapapikey,$(this).parents('li.vcard').find('.street-address').text()+', '+$(this).parents('li.vcard').find('.postal-code').text(),2));
					arr_maps[eventmaps].addMarkerByAddress(map_marker,$(this).parents('li.vcard').find('.street-address').text()+', '+$(this).parents('li.vcard').find('.postal-code').text());
					// toggle display link
					$(this).parents('li.vcard').find('a.maplink').toggle(
						function(){
							$(this).parents('li.vcard').find('div.eventmap').slideDown(animspeed);
							$(this).get(0).blur();
							return false;
						},
						function(){
							$(this).parents('li.vcard').find('div.eventmap').slideUp(animspeed);
							$(this).get(0).blur();
							return false;
						}
					);
					eventmaps++;
				});
			}
		/**
		 * This code manages the second tier navigation on the homepage
		 */
		if($('body#home').get(0)){
			timeit = setInterval('checkmenu()',100);
			$('ul#mainnav').mouseover(function(){
				nav_main = true;
			});
			$('ul#mainnav').mouseout(function(){
				nav_main = false;
			});
			$('div#subsections').mouseover(function(){
				nav_sub = true;
			});
			$('div#subsections').mouseout(function(){
				nav_sub = false;
				$('a.highlightsub').removeClass('highlightsub');
			});
			/**
			 * highlight top level homepage sections based on second-level hovers.
			 */
			$('#prep').mouseover(function(){
				$('#nav-prepare a').addClass('highlightsub');
				$('#nav-assist a').removeClass('highlightsub');
				$('#nav-involve a').removeClass('highlightsub');
				$('#nav-workwith a').removeClass('highlightsub');
			});
			$('#asst').mouseover(function(){
				$('#nav-prepare a').removeClass('highlightsub');
				$('#nav-assist a').addClass('highlightsub');
				$('#nav-involve a').removeClass('highlightsub');
				$('#nav-workwith a').removeClass('highlightsub');
			});
			$('#invo').mouseover(function(){
				$('#nav-prepare a').removeClass('highlightsub');
				$('#nav-assist a').removeClass('highlightsub');
				$('#nav-involve a').addClass('highlightsub');
				$('#nav-workwith a').removeClass('highlightsub');
			});
			$('#work').mouseover(function(){
				$('#nav-prepare a').removeClass('highlightsub');
				$('#nav-assist a').removeClass('highlightsub');
				$('#nav-involve a').removeClass('highlightsub');
				$('#nav-workwith a').addClass('highlightsub');
			});
		}
		/**
		 * highlight rows in response steps sections
		 */
			$('body#home div.response-steps ul li').mouseover(function(){
				$(this).addClass('over');
			});
			$('body#home div.response-steps ul li').mouseout(function(){
				$(this).removeClass('over');
			});
		/**
		 * flash replacement
		 */
			if($('body#home').get(0)){
				if(usemovie){
					swfobj = new SWFObject(usemovie,'myswf','776','172','7','#ffffff');
					swfobj.addParam('quality','high');
					swfobj.addParam('menu','false');
					swfobj.addParam('wmode','transparent');
					swfobj.write('flash-anim');
				}
			}
	});