// Navigation Show Hide Animations
function simplePreload() { 
	var args = simplePreload.arguments;
	document.imageArray = new Array(args.length);
	
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}
// Preload Navigation elements
simplePreload('/images/nav/arrow_on.gif', '/images/nav/rm.png', '/images/nav/rma.png', '/images/nav/rma-orange.png');
function showtier1(b) {
	document.getElementById(b).style.background = "url(/images/nav/arrow_on.gif) no-repeat left center #000";
}
function hidetier1(b) {
	document.getElementById(b).style.backgroundImage = "";
}
function show(a,b) {
	document.getElementById(a).style.display = "block";
	document.getElementById(b).style.background = "url(/images/nav/arrow_on.gif) no-repeat left center #000";

}
function hide(a,b) {
	document.getElementById(a).style.display = "none";
	document.getElementById(b).style.backgroundImage = "";
}
//function show_image() {
//	var ranNum= Math.floor(Math.random()*9);
//	var img=new Array(9)
//	img[0]="url(/images/banners/2010/summer/banner-summer10-00.jpg)";
//	img[1]="url(/images/banners/2010/summer/banner-summer10-01.jpg)";
//	img[2]="url(/images/banners/2010/summer/banner-summer10-02.jpg)";
//	img[3]="url(/images/banners/2010/summer/banner-summer10-03.jpg)";
//	img[4]="url(/images/banners/2010/summer/banner-summer10-04.jpg)";
//	img[5]="url(/images/banners/2010/summer/banner-summer10-05.jpg)";
//	img[6]="url(/images/banners/2010/summer/banner-summer10-06.jpg)";
//	img[7]="url(/images/banners/2010/summer/banner-summer10-07.jpg)";
//	img[8]="url(/images/banners/2010/summer/banner-summer10-08.jpg)";
//	return img[ranNum];
//}
function wopen() {
	window.open("videos/inyo-vol-video/inyovideo.html", "Inyo NF Video Highlights PCTA Volunteers", "width=900, height=550");
}
function MM_preloadImages(a, b, c) { // No longer used but left empty to prevent browser errors.
	
}

// add commas to a number
function addCommas(nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
}

function mail_same_change(secondField, firstField) {	
	if($('#mail_same').attr('checked')) {
		$('#'+firstField).val($('#'+secondField).val());
	}
}

// Auto center jquery plugin
jQuery.fn.fixedCenter = function(){
	return this.each(function(){
		var element = jQuery(this);
		centerElement();
		jQuery(window).bind('resize',function(){
			centerElement();
		});
			
		function centerElement(){
			var elementWidth = jQuery(element).outerWidth();
			var elementHeight = jQuery(element).outerHeight();
			var windowWidth = jQuery(window).width();
			var windowHeight = jQuery(window).height();	
			
			var X2 = windowWidth/2 - elementWidth/2;
			var Y2 = windowHeight/2 - elementHeight/2;
	 
			jQuery(element).css({
				'left':X2,
				'top':Y2,
				'position':'fixed'
			});						
		} //end of centerElement function
					
	}); //end of return this.each
}



$(document).ready(function() {
    
	// Home Page Banner Randomization
  //$("#banner").css("background-image", show_image());
  
  	$('#banner').cycle({ 
		fx:      'fade', 
		speed:    1500, 
		timeout:  5000,
		random: 1 
	});
	
	// Year end thermometer
	var start = 145;
	var needed = 200000;
	var current = 67845; // change this value to update thermometer
	var maxheight = 87;
	var percentage = current/needed;
	var boxheight = Math.round(maxheight * percentage);
	var position = Math.round(start - boxheight)+1;
	$('#yearend #fill').css('height', boxheight+'px');
	$('#yearend #fill').css('top', position+'px');
	$('#yearend #togo').html('$'+addCommas(needed-current)+' to go...');

  
  // Completion form change fee if member
  $('#member_0').click(function() {
	  $('#fee').val('0.00');
	  $('#nonmember').css('display', 'none');
	});
	
	// Completion form change fee if non member
	$('#member_1').click(function() {
	  $('#fee').val('5.00');
	  $('#nonmember').css('display', 'table-row');
	});
	
	// Completion form previous page
	$('#prev').click(function() {
		window.location = 'index.php';
	});
	
	// thank you video auto center
	$('#window').fixedCenter(); 
	
	// Show thank you video popup
	$('#vlink').click(function() {
		$('#window').css('display', 'block');
		$('#iframe').css('display', 'block');
		$('#close').css('display', 'block');
	});
	
	// close thank you video popup
	$('#close').click(function() {
		$('#window').css('display', 'none');
		$('#iframe').css('display', 'none');
		$('#close').css('display', 'none');
	});
	
	// Contact us form - ajax
	$('#contact_us').submit(function() {
		note = '';
		if($('#name').val() == "") {
			note = 'Please enter your name.<br />';
		}
		if($('#email').val() == "") {
			note += 'Please enter your email address.<br />';
		}
		emailReg = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
		if(!emailReg.test($('#email').val())) {
			note += 'Please enter a valid email address.<br />';
		}
		if($('#category').val() == '') {
			note += 'Please select a category.<br />';
		}
		if($('#message').val() == '') {
			note += 'Please enter your message.<br />';
		}
		if(note != '') {
			$('#output').show();
			$('#output').html(note);
		} else {
			$('#output').show();
			$('#output').html('Validating Message...');
			formdata = $(this).serialize();
			$.post('validate.php', formdata, function(msg) {
				if(msg != '') {
					$('#output').html(msg);	
				} else {
					$('#output').html('Sending Message...');
					$.post('send.php', formdata, function(response) {
						if(response == 1) {
							$('#output').html('We have received your message and will get back to you soon.  Thank you.');
							$('#contact_us').each(function() {
 								this.reset();
							});
						} else {
							$('#output').html('There was a problem sending your messsage, please try again later.');
						}
					});
				}
			});
		}
	});
	
	// Donation form jquery conversion
	
	
	$('#gift_type_3, #gift_type_4, #gift_type_5').change(function() {
		$('#lamount').html("Enter Amount:");
		$('#memblist').attr('disabled', 'disabled');
		if($("#fund option").size() == 1) {
			$("#fund option[value='Highest Priority Need']").after('<option value="Trail Maintenance">Trail Maintenance</option><option value="Trail Protection">Trail Protection</option><option value="Land Protection Fund">Land Protection Fund</option><option value="Jane and Flicka Endowment">Jane and Flicka Endowment</option>');
		}
	});
	$('#gift_type_6').change(function() {
		$('#lamount').html("Amount Per Month: ($5 Minimum)");
		$('#x_amount').val(5);
		$('#memblist').attr('disabled', 'disabled');
		$("#fund option[value!='Highest Priority Need']").remove();
		$('#fund').val('Highest Priority Need');
	});
	$('#gift_type_0, #gift_type_1, #gift_type_2').change(function() {
		$('#memblist').attr('disabled', '');
		$('#x_amount').val($('#memblist').val());
		$('#lamount').html("Enter Amount:");
		$("#fund option[value!='Highest Priority Need']").remove();
		$('#fund').val('Highest Priority Need');
	});
	$('#memblist').change(function() {
		$('#x_amount').val($(this).val());
	});
	
	$('#x_first_name').change(function() { mail_same_change('x_first_name', 'mail_first_name') });
	$('#x_last_name').change(function() { mail_same_change('x_last_name', 'mail_last_name') });
	$('#x_address').change(function() { mail_same_change('x_address', 'mail_address') });
	$('#x_city').change(function() { mail_same_change('x_city', 'mail_city') });
	$('#x_state').change(function() { mail_same_change('x_state', 'mail_state') });
	$('#x_zip').change(function() { mail_same_change('x_zip', 'mail_zip') });
	$('#x_country').change(function() {
		if($('#mail_same').attr('checked')) {
			$('#mail_country option:selected').attr("disabled", true);
			$('#mail_country').val($('#x_country').val());
			$('#mail_country option:selected').attr("disabled", false);
		}
	});
	
	
	$('#mail_same').change(function() {
		if($('#mail_same').attr('checked')) {
			$('#mail_first_name').val($('#x_first_name').val());
			$('#mail_last_name').val($('#x_last_name').val());
			$('#mail_address').val($('#x_address').val());
			$('#mail_city').val($('#x_city').val());
			$('#mail_state').val($('#x_state').val());
			$('#mail_zip').val($('#x_zip').val());
			$('#mail_country').val($('#x_country').val());
			
			$('#mail_first_name').attr('readonly', true);
			$('#mail_last_name').attr('readonly', true);
			$('#mail_address').attr('readonly', true);
			$('#mail_city').attr('readonly', true);
			$('#mail_state').attr('readonly', true);
			$('#mail_zip').attr('readonly', true);
			$('#mail_country option').not(":selected").attr("disabled", true);
		} else {
			//$('#mail_first_name').val('');	
			//$('#mail_last_name').val('');
			//$('#mail_address').val('');
			//$('#mail_city').val('');
			//$('#mail_state').val('');
			//$('#mail_zip').val('');
			//$('#mail_country').val('');
			
			$('#mail_first_name').attr('readonly', false);
			$('#mail_last_name').attr('readonly', false);
			$('#mail_address').attr('readonly', false);
			$('#mail_city').attr('readonly', false);
			$('#mail_state').attr('readonly', false);
			$('#mail_zip').attr('readonly', false);
			$('#mail_country option').attr("disabled", false);
		}
	});
	// page two
	$('#notify').change(function() {
		if($(this).attr('checked')) {
			$('#tm_first_name, #tm_last_name, #tm_country, #tm_address, #tm_city, #tm_state, #tm_zip, #tm_email, #tm_phone').removeAttr('disabled');
		} else {
			$('#tm_first_name, #tm_last_name, #tm_country, #tm_address, #tm_city, #tm_state, #tm_zip, #tm_email, #tm_phone').attr('disabled', 'disabled');
		}
	});
	
	// Volunteer Resources
	$('img.plus').click(function() {
		var attrib = $(this).attr('src');
		var ident = $(this).attr('id');
		
		if(attrib == '/images/plus.png') {
			$(this).attr('src', '/images/minus.png');
			$('ul#'+ident).show();
			$('ul#'+ident).css('visibility', 'visible');
		} else {
			$(this).attr('src','/images/plus.png');
			$('ul#'+ident).hide()
			$('ul#'+ident).css('visibility', 'hidden');
		}
	});
	
	//$('td.LeftNavLinks[width="92%"]').append('<fb:like href="" send="true" width="350" show_faces="true" font="arial"></fb:like>');

						  
});
// Google Analytics
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5426930-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();


function recordOutboundLink(link, category, action) {
    _gat._getTrackerByName()._trackEvent(category, action);
    setTimeout('document.location = "' + link.href + '"', 100);
}
