function urlencode (str) {
    // URL-encodes string  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/urlencode    // +   original by: Philip Peterson
    str = (str + '').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
    replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

$(function(){
	// Search box functionality
	$('#searchInput').focus(function(){
		if($('#searchInput').val() == 'Search')
		{
			$('#searchInput').val('');
		}
	}).keypress(function(event) {
		if (event.which == '13')
		{		
			event.preventDefault();
			
			var urlPrefix = '';
			if($('meta[name=overridesearchurl]').length > 0)
				urlPrefix = $('meta[name=overridesearchurl]').attr('content');
			window.location = urlPrefix + '/Search.aspx?searchmode=anyword&searchtext=' + urlencode($('#searchInput').val());
		}
	});
	
	// Header dropdown
	$('#headerDropdown')
		.change(function(){
			if($('#headerDropdown').val() != '' && $('#headerDropdown').val() != null)
			{
				window.open($('#headerDropdown').val());
			}
		});

	// Homepage rollovers
	$('#homeBusinessesContainer > a.hover')
	    .each(function(){
	            $(this)
	                .hover(function(){
	                        $(this).find('img:first')
	                            .animate({opacity: 1}, 'fast');
	                    },
	                    function(){
	                        $(this).find('img:first')
	                            .animate({opacity: 0}, 'fast');                        
	                    });
	        });
	
	// Top menu highlight
	var patt =/\/([A-z0-9\-]+)/g;
	var match = patt.exec(window.location.pathname);
	if(match != null && match.length > 1)
	{
	    switch(match[1].toString().toLowerCase())
	    {
	        case 'about-us':
	            $('#mainMenu > ul > li > a:eq(0)')
	                .css('color', '#000');
	        break;
	        
	        case 'businesses':
		    case 'forest-resources':
	            $('#mainMenu > ul > li > a:eq(1)')
	                .css('color', '#000');
	        break;
	    
	        case 'commitment':
	            $('#mainMenu > ul > li > a:eq(2)')
	                .css('color', '#000');
	        break;
	    
	        case 'careers':
	            $('#mainMenu > ul > li > a:eq(3)')
	                .css('color', '#000');
	        break;
	    
	        case 'news':
	            $('#mainMenu > ul > li > a:eq(4)')
	                .css('color', '#000');
	        break;
	    
	        case 'community':
	            $('#mainMenu > ul > li > a:eq(6)')
	                .css('color', '#000');
	        break;
	    }
	}

	// Conservation galleries
	if($('div#thumbs').length > 0)
	{

		$('div.navigation').css({'width' : '380px', 'float' : 'left'});
		$('div.content').css('display', 'block');
	
		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});
		
		// Initialize Advanced Galleriffic Gallery
		var gallery = $('#thumbs').galleriffic({
			delay:                     2500,
			numThumbs:                 50,
			preloadAhead:              10,
			enableTopPager:            false,
			enableBottomPager:         false,
			maxPagesToShow:            7,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          false,
			renderNavControls:         false,
			playLinkText:              'Play Slideshow',
			pauseLinkText:             'Pause Slideshow',
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false,
			autoStart:                 false,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
		});

	}


	// Fade for the Conservation gallery
	$('#galleryThumb > img, .green350BG a img')
		.css('background-color', 'white')
		.hover(function(){ 
				$(this).fadeTo('fast', 0.6); 
			}, 
			function(){ 
				$(this).fadeTo('fast', 1); 
			});

});


function gallerySwap(thumbObj){
	//This function takes the name of a thumbnail, makes the SRC the Src of the larger image, and then takes the ALT of the thumb and moves it into the caption area.
	$("#galleryWindow > img").attr('src', $(thumbObj).attr('src')); 
	$("#galleryCaption").html($(thumbObj).attr('alt')); 
}

function insertFlash(container, swfPath, swfWidth, swfHeight, imagePath, imageAlt, isVideo, videoPath, playerID, autostart)
{
	if(imageAlt == null)
		imageAlt = '';
	if(isVideo == null)
		isVideo = false;
	if(videoPath == null)
		videoPath = '';
	if(playerID == null)
		playerID = 'playerID';
	if(autostart == null)
		autostart = false;


	$('#' + container)
		.empty()
		.append($('<img />')
					.attr({src: imagePath, width: swfWidth, height: swfHeight, alt: imageAlt})
				);

	var flashvars = {};
	if(isVideo)
	{
		flashvars = {
			'file':   videoPath, 
			'autostart': autostart
		};
	}
	
   var params = {
	  'allowfullscreen':        'true',
	  'allowscriptaccess':      'always'
   };
 
   var attributes = {
	  'id':                     playerID,
	  'name':                   playerID
   };

   swfobject.embedSWF(swfPath, container, swfWidth, swfHeight, '9', '/swf/expressInstall.swf', 
	   flashvars, params, attributes);

}
