var $ = jQuery;
$.fn.setAllToMaxHeight = function(){
	return this.height( Math.max.apply(this, $.map( this , function(e){ return $(e).height() }) ) );
};

$(window).load(function(){
	
	//$('.top-box .body').setAllToMaxHeight();
	$('.home-news .news').setAllToMaxHeight();
	if ($('#content .sidebar').length > 0){
		$('body').not('.subpage-1').find('#content .col').setAllToMaxHeight();
	}
});

// add first/last child and other needed CSS classes
$(document).ready(function(){
	$(".top-box .body ul li:last-child").addClass('last');
	$(".home-news .news:last-child").addClass('last');
	$(".top-box-sub li:last-child").addClass('last');
	$("#content .search-results tr:last-child").addClass('last');
	$("ul.testimonials li:nth-child(even)").addClass('odd');
	$('table tr:nth-child(2n+1)').addClass('odd');
});

$(document).ready(function(){
	$('.help-offered strong').addClass('hover');
	$('.help-offered strong').removeClass('hover');
	$('.help-offered strong').css('opacity','0.4');
	$('.help-offered a, .help-offered strong').each(function() {
		$('<em></em>').appendTo(this);
		$(this).find('span, em').css('opacity','0');
		var pos = $(this).position().top;
		$(this).find('em').css('top',pos+66);
		$(this).find('span').css('top',pos+71);

		$(this).hover(function () {
				$(this).addClass('hover');
				var width = $(this).find('span').outerWidth();
				$(this).find('span').css('margin-left',-(width/2));
				$(this).find('em').animate({
					opacity: 1,
					top: pos+46
					}, 300, function() {
					
				});
				$(this).find('span').animate({
					opacity: 1,
					top: pos+51
					}, 200, function() {
					
				});
			}, 
			function () {
				$(this).removeClass('hover');
				$(this).find('span, em').css('opacity','0');
				$(this).find('em').css('top',pos+66);
				$(this).find('span').css('top',pos+71);
			}
		);
		$(this).click(function () {
			return false;
		});
	});
});


$(document).ready(function(){
	$('#content .box .form  fieldset .clr').each(function() {
		var width = $(this).find('.wrap').width();
		$(this).find('.wrap').next('div.error').css("margin-right",width);
	});
});
/* sub nav functions */
$(document).ready(function(){
	$('#nav-subpage ul.tabs li a').hover(function(){
		$("#nav-subpage ul.tabs li a").removeClass("hover");
		$(this).addClass("hover");
		var thisClass = $(this).parent().attr('class').split(" ")[0];
		var id = '#'+thisClass;
		if( $('#nav-subpage').find(id).is(':visible') ) {
			return false;
		}
		else {
			$(".top-box-sub").hide();
			$('#nav-subpage').find(id).fadeIn();
		}
	});
});
$(function(){
	var path = location.pathname.substring(1);
		if ( path )
			$('#nav-subpage a[href$="' + path + '"]').attr('class', 'hover');
});
 $(function(){
	if(window.location.href.indexOf('volunteer') != -1) {
		$('#nav-subpage .tabs .volunteers a').attr('class', 'hover');
		//$('#nav-subpage #help').css('display', 'none');
	}
	else if(window.location.href.indexOf('people') != -1) {
		$('#nav-subpage .tabs .help a').attr('class', 'hover');
		//$('#nav-subpage #volunteers').css('display', 'none');
	}
	if(window.location.href.indexOf('faqv')!= -1){
		$('#nav-subpage').addClass('nav-1');
	}
	else if(window.location.href.indexOf('signup')!= -1) {
		$('#nav-subpage').addClass('nav-2');
	}
	else if( (window.location.href.indexOf('getpass')!= -1) || (window.location.href.indexOf('profile')!= -1) || (window.location.href.indexOf('search')!= -1) ) {
		$('#nav-subpage').addClass('nav-3');
	}
	else if(window.location.href.indexOf('testimonialsv')!= -1) {
		$('#nav-subpage').addClass('nav-4');
	}
});
$(function(){
	if($('ul.sales-points').size() > 0) {
		$('ul.sales-points').cycle({
			fx:'scrollRight',
			sync:1,
			speedIn:700,
			speedOut:50,
			timeout:3000
		});
	}
});

// Adding sounds to icon rollovers
$(function(){
	if ($('.help-offered').length) {
		$('body').append('<div id="jplayer"></div>')
		var jp = ($('#jplayer'));
		jp.jPlayer({
			swfPath: '/js',
			supplied: 'mp3',
			wmode: 'window'
		});
		$('.help-offered li a').click(function(){
			jp.jPlayer("stop");
			jp.jPlayer("setMedia", {mp3: '/sounds/' + $(this).closest('li').get(0).className.toString() + '.mp3'});
			jp.jPlayer("play");
		});
	}
});
