function formHide(f) {
	var fw = $(f).innerWidth();
	var fh = $(f).innerHeight();
	var fid = $(f).attr('id');
	$(f).parent('.fwrap').remove();
	$(f).wrap(function() {
		return '<div class="fwrap '+ fid +'" style="width:'+ fw +'px;height:'+ fh +'px" />';
	});
	$(f).fadeOut(400,function() {
		if ( ! $('div.fwrap.finished').length) $('div.fwrap.'+ fid).css('background','transparent url(/cr/images/spinner.gif) no-repeat center center');
	});
}
function formShow(f,m,r,h) {
	$(f).parent('.fwrap').prepend(m).addClass('finished').css('background','none');
	r = typeof(r) != 'undefined' ? r : false;
	if (r) $(f).show();
	h = typeof(h) != 'undefined' ? h : false;
	if (h && !r) $(f).parent('.fwrap').animate({height:h+'px'},200);
}



$.expr[':'].external = function(o) {
	return !o.href.match(/^mailto\:/) && (o.hostname !== location.hostname);
};



$(function() {
	
	
	
	if ($('#home-alert').length)
	{
		var aShows = getCookie('homeAlert04272010');
		if (aShows===null || aShows < 1)
		{
			aCount = (aShows===null)? 1: Number(aShows) + 1;
			$('a#home-alert-trigger').fancybox({
				padding:0,
				hideOnContentClick:false,
				overlayOpacity:0.7,
				onClosed:function() {
					var today = new Date();
					var expire = new Date();
					expire.setTime(today.getTime() + 360000*24*30);
					document.cookie = "homeAlert04272010="+aCount
									+ ";expires="+expire.toGMTString();
					}
			}).click();
		}
	}
	
	// External Links in New Windows
	$('a:external').attr('target','_blank').addClass('external');
	
	
	
	if ($('a.yt-auto').length){
		$('a.yt-auto').each(function(){
			if ($(this).is('.tv')) {
				var vW = 279;
				var vH = 157;
			} else {
				var vW = 440;
				var vH = 330;
			}
			var vID = $(this).attr('href').split('=');
			vID = vID[1].split('&').shift();
			$(this).after('<object width="'+vW+'" height="'+vH+'"><param name="movie" value="https://www.youtube.com/v/'+vID+'&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="https://www.youtube.com/v/'+vID+'&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+vW+'" height="'+vH+'"></embed></object>');
			$(this).hide();
		});
	}
	
	
	
	if ($('#cm').length)
	{
		$('a#cm-trigger').fancybox({
			padding:20,
			overlayOpacity:0.7,
			autoDimensions:false,
			width:400,
			height:500
		}).click();
		$('#cm-pop h1').css({
			fontSize:'2em',
			fontWeight:'bold',
			paddingBottom:'18px'
		});
		$('#cm-pop p').css({
			fontSize:'0.75em',
			lineHeight:'1.5em',
			paddingBottom:'18px'
		});
	}
	
	
	
	// Primary Nav
	if ($('ul.sf-menu').length) {
		if ($('ul.hidden-news-cats li:has(a[href=/news/archive/])').length) {
			$('ul.hidden-news-cats').clone().removeClass('sub-nav').appendTo('ul.sf-menu li:has(a[href=/news/])');
		}
		if ($.browser.msie && $.browser.version < 7) $('ul.sf-menu > li').reverseOrder();
		$('ul.sf-menu').supersubs({
				minWidth: 17,
				maxWidth: 32,
				extraWidth: 1
			}).superfish({
				autoArrows:false,
				dropShadows:false,
				delay:1000,
				animation:{opacity:'show',height:'show'},
				speed:300
				});
	}
	
	
	
	// MP3 Handling
	if ($('a.mp3').length) {
		$('a.mp3').media({mp3Player:'/cr/swf/player.swf'});
	}
	
	
	
	// Feature Banner
	if ($('ul.feature').length && $('ul.feature li').length > 1) {
		$('ul.feature').cycle({
			fx:'fade',
			speed:750,
			timeout:3500,
			random:1,
			easing:'easeOutBack',
			after: function(cs,ns) {
				$('span',cs).hide();
				$('span.line-1',ns).animate({width:'show'},250,'easeInCubic',function() {
					if ($('span.line-2',ns).length) {
						$('span.line-2',ns).animate({width:'show'},250,'easeInCubic',function() {
							if (!$('span a.fixed',ns).length) {
								var l1w = $('span.line-1 a',ns).width();
								var l2w = $('span.line-2 a',ns).width();
								var nw = (l1w > l2w)? l1w: l2w;
								nw = nw + 4;
								$('span.line-1 a,span.line-2 a',ns).addClass('fixed').css('width',nw+'px');
							}
						});
					}
					});
			}
			});
	} else if ($('ul.feature').length) {
		/*
		console.log('test');
		$('ul.feature li').css('display','block').load(function(){
			$('span.line-1',this).animate({width:'show'},250,'easeInCubic',function() {
				if ($('span.line-2',this).length) {
					$('span.line-2',this).animate({width:'show'},250,'easeInCubic',function() {
						if (!$('span a.fixed',this).length) {
							var l1w = $('span.line-1 a',this).width();
							var l2w = $('span.line-2 a',this).width();
							var nw = (l1w > l2w)? l1w: l2w;
							nw = nw + 4;
							$('span.line-1 a,span.line-2 a',this).addClass('fixed').css('width',nw+'px');
						}
					});
				}
				});
			
		});*/
	}
	
	
	
	/* /// FORM SUBMISSIONS /// */
	
	// Track Form Loads on GA
	if ($('form').length) {
		$('form').each(function() {
			var id = $(this).attr('id');
			if (id !== '') {
				id = (id == 'petition')? id + ' - ' + $('#petition-id').val(): id;
				_gaq.push(['_trackEvent','Forms','Form Loaded',id]);
			}
		});
	}
	
	
	
	// Form :: Email Subscription
	if ($('form.em-sub').length) {
		$('form.em-sub input#em-sub-sms').click(function() {
			if ($('form.em-sub input#em-sub-sms:checked').length) {
				$('form.em-sub ul li.sms').slideDown(200,function() {
					$('form.em-sub ul li.sms input').focus();
				});
			} else {
				$('form.em-sub ul li.sms').slideUp(200,function() {
					$('form.em-sub ul li.sms input').val();
				});
			}
		});
		$('form.em-sub input').focus(function() {
			$(this).parents('li').addClass('active');
		}).blur(function() {
			$(this).parents('li').removeClass('active');
		});
		
		var emSubValidate = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				zip:'required',
				cphone: {
					required: {
						depends: function(e) {
							return $('form.em-sub input#em-sub-sms:checked').length;
						}
					}
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:'/cr/processor/em-sub/',
					dataType:'json',
					data:{
						action:'em-sub',
						'em-sub':'1',
						'sms-sub':$('input#em-sub-sms:checked',f).length,
						jssub:'1',
						fname:$('input#em-sub-fname',f).val(),
						lname:$('input#em-sub-lname',f).val(),
						email:$('input#em-sub-email',f).val(),
						zip:$('input#em-sub-zip',f).val(),
						cphone:$('input#em-sub-cphone',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','em-sub']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','em-sub, json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						_gaq.push(['_trackEvent','Forms','Form Error','em-sub']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form.em-sub').validate(emSubValidate);
	}



	// Form :: Static / Petition
	if ($('form#petition').length) {
		$('form#petition input#static-sms').click(function() {
			if ($('form#petition input#static-sms:checked').length) {
				$('form#petition ul li.sms').slideDown(200,function() {
					$('form#petition ul li.sms input').focus();
				});
			} else {
				$('form#petition ul li.sms').slideUp(200,function() {
					$('form#petition ul li.sms input').val();
				});
			}
		});
		$('form#petition input').focus(function() {
			$(this).parents('li').addClass('active');
		}).blur(function() {
			$(this).parents('li').removeClass('active');
		});

		var petValidate = {
			debug:false,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				zip:'required',
				mobile: {
					required: {
						depends: function(e) {
							return $('form#petition input#static-sms:checked').length;
						}
					}
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:'/cr/processor/petition/',
					dataType:'json',
					data:{
						action:'petition',
						jssub:'1',
						petition:$('input#petition-c',f).val(),
						ptrack:$('input#petition-id',f).val(),
						fname:$('input#petition-fname',f).val(),
						lname:$('input#petition-lname',f).val(),
						email:$('input#petition-email',f).val(),
						zip:$('input#petition-zip',f).val(),
						mobile:$('input#petition-mobile',f).val(),
						'em-sub':$('input#petition-em-sub:checked').length,
						'sms-sub':$('input#petition-sms-sub:checked').length
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','petition - ' + $('input#petition-id',f).val()]);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','petition - ' + $('input#petition-id',f).val() + ', json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						var m = '<p>There was a problem signing the petition. Please try again later.</p>';
						formShow(f,m,true);
						_gaq.push(['_trackEvent','Forms','Form Error','petition - ' + $('input#petition-id',f).val()]);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#petition').validate(petValidate);
	}



	// Form :: Static / Petition
	if ($('form#simple-form').length) {
		$('form#simple-form input').focus(function() {
			$(this).parents('li').addClass('active');
		}).blur(function() {
			$(this).parents('li').removeClass('active');
		});
		var sfValidate = {
			debug:false,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				zip:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				
				// Package Form Data
				var fData = $('#simple-form').serialize();
				fData = 'action=simple-form&jssub=1&' + fData;
				
				$.ajax({
					type:'POST',
					url:'/cr/processor/simple-form/',
					dataType:'json',
					data: fData,
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','simple-form - ' + $('input#simple-form-id',f).val()]);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','simple-form - ' + $('input#simple-form-id',f).val() + ', json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						var m = '<p>There was a problem submitting the form. Please try again later.</p>';
						formShow(f,m,true);
						_gaq.push(['_trackEvent','Forms','Form Error','simple-form - ' + $('input#simple-form-id',f).val()]);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide <strong>'+fn+'</strong>.</span>');
				}
			}
		};
		$('form#simple-form').validate(sfValidate);
	}
	
	
	
	// Form :: Contact
	if ($('form.contact').length) {
		var contactVal = {
			debug:false,
			rules: {
				fname:'required',
				lname:'required',
				zip:'required',
				email: {
					required:true,
					email:true
				},
				cphone: {
					required: {
						depends: function(e) {
							return $('form.contact input#contact-sms-sub:checked').length;
						}
					}
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				$.ajax({
					type:'POST',
					url:'/cr/processor/contact/',
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						lname:$('input#contact-lname',f).val(),
						addr:$('input#contact-addr',f).val(),
						addr2:$('input#contact-addr2',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						email:$('input#contact-email',f).val(),
						'em-sub':$('input#contact-em-sub:checked',f).length,
						cphone:$('input#contact-cphone',f).val(),
						'sms-sub':$('input#contact-sms-sub:checked',f).length,
						message:$('textarea#contact-message',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','contact']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','contact, json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						_gaq.push(['_trackEvent','Forms','Form Error','contact']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		}
		$('form.contact').validate(contactVal);
	}
	
	
	
	// Form :: Volunteer
	if ($('form.volunteer').length) {
		var volunteerVal = {
			debug:false,
			rules: {
				fname:'required',
				lname:'required',
				zip:'required',
				email: {
					required:true,
					email:true
				},
				cphone: {
					required: {
						depends: function(e) {
							return $('form.volunteer input#volunteer-sms-sub:checked').length;
						}
					}
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formHide(f);
				var tasks = [];
				$('input[name=voltask]:checked').each(function(){tasks.push($(this).val());});
				$.ajax({
					type:'POST',
					url:'/cr/processor/volunteer/',
					dataType:'json',
					data:{
						action:'volunteer',
						varietal:$('input#volunteer-varietal',f).val(),
						source:$('input#volunteer-source',f).val(),
						jssub:'1',
						fname:$('input#volunteer-fname',f).val(),
						lname:$('input#volunteer-lname',f).val(),
						addr:$('input#volunteer-addr',f).val(),
						addr2:$('input#volunteer-addr2',f).val(),
						city:$('input#volunteer-city',f).val(),
						state:$('select#volunteer-state',f).val(),
						zip:$('input#volunteer-zip',f).val(),
						email:$('input#volunteer-email',f).val(),
						'em-sub':$('input#volunteer-em-sub:checked',f).length,
						cphone:$('input#volunteer-cphone',f).val(),
						'sms-sub':$('input#volunteer-sms-sub:checked',f).length,
						voltask:tasks
						//volPhone:$('input#volunteer-task-phone:checked',f).length,
						//volLetters:$('input#volunteer-task-letters:checked',f).length,
						//volSigns:$('input#volunteer-task-signs:checked',f).length,
						//volOnline:$('input#volunteer-task-online:checked',f).length,
						//volSticker:$('input#volunteer-task-sticker:checked',f).length,
						//volOffice:$('input#volunteer-task-office:checked',f).length,
						//volSign:$('input#volunteer-task-sign:checked',f).length
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Submitted','volunteer']);
						} else {
							m = '<p>'+json.message+'</p>';
							_gaq.push(['_trackEvent','Forms','Form Error','volunteer, json error']);
						}
						formShow(f,m,false,100);
					},
					error:function(x,t,e) {
						formShow(f,'<p>Your message could not be sent. Please try again.</p>');
						_gaq.push(['_trackEvent','Forms','Form Error','volunteer']);
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		}
		$('form.volunteer').validate(volunteerVal);
	}
	
	
	
	
	
	
	// To Make Buttons Submit Forms (IE <= 7 Only)
	if ($.browser.msie && $.browser.version <= 7) {
		$('form button').click(function() {
			$(this).parents('form').submit();
		});
	}
	
	
	
	if ($.browser.msie && $('a > button').length) {
		$('a > button').click(function() {
			var l = $(this).parents('a').attr('href');
			window.location = l;
		});
	}
	
	
	
	// Asst'd Functions
	
	
	
	// Pause
	$.fn.pause = function(d,cb) {
		$(this).animate({dummy:1},d,cb);
		return this;
	}
});



//get cookie
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

