	
	$(function() {
		
		/*--------------------------------------------------------*/
		/* MODULES
		/*--------------------------------------------------------*/
		
		// REAL PEOPLE, REAL STORIES TOUT
		$('div.column div.rprs_tout').each(function() {
			var li=$(this).find('li');
			for(i=0;i<li.length;i++) {
				$(li[i]).attr('id','rprs_tout_'+i);
				if(i!=0) $(li[i]).css('display','none');
			}
		});
		$('div.column div.rprs_tout a.previous').bind('click',previous_rprs_tout);
		$('div.column div.rprs_tout a.next').bind('click',next_rprs_tout);
		
		// NEWS EVENTS SLIDER
		$('div.news_events_slider ul').each(function() { $(this).find('li:last').css('border','none'); });
		$('div.news_events_slider a.option').click(function() {
			$('div.news_events_slider a.active').removeClass('active');
			$('div.news_events_slider ul:visible').css('display','none');
			$('div.news_events_slider ul.'+$(this).text().toLowerCase()).css('display','block');
			$(this).addClass('active');
			return false;
		});
		
		// DIVIDED LISTS
		$('ul.divided').each(function() { $(this).find('li:last').css({borderBottom:'none',paddingBottom:'0'}); });
		$('div.people div.column').each(function() { $(this).find('ul:last').css({borderBottom:'none'}); });
		$('div.videos:last').each(function() { $(this).css({borderBottom:'none'}); });
		
		// BOX GRIDS
		$('div.grid').each(function() {
			if($(this).hasClass('left')) {
				var left_height=$(this).height();
				var right_height=$(this).next('div.grid').height();
				if(left_height>right_height) {
					$(this).next('div.grid').css('height',left_height+'px');
				} else {
					$(this).css('height',right_height+'px');
				}
			}
		});
		
		// PHYSICIAN FINDER
		$('div.search_results a.show_physician').click(function() {
			open_popup('div#physician_finder');
			$('div#physician_finder div.profile').html('').removeClass('loading').addClass('loading');
			$.ajax({
				type: "POST",
				url: "/cma/physician-finder/detail",
				data: "id="+get_id($(this).attr('id')),
				success: function(profile){
					$('div#physician_finder div.profile').html(profile).removeClass('loading');
				}
			});
			return false;														  
		});
		$('div#physician_finder a.close').click(function() {
			close_popup('div#physician_finder');
			return false;												 
		});
		
		// DONATION FORM
		$('div.donate_form div.funds div.choose select').change(function() {
			var description=$(this).find("option:selected").attr("title");
			if(description.length>0) {
				$(this).parent().parent().find('div.amount input').removeAttr('disabled');
				$(this).parent().find("div.help span.text").text(description);
			} else {
				$(this).parent().parent().find('div.amount input').val('$0.00').attr('disabled','disabled');
				$(this).parent().find("div.help span.text").text('');
			}
			update_donation_total();
		});
		$('div.donate_form div.funds div.choose select').change();
		$('div.donate_form div.funds div.choose a.help').click(function() { return false; });
		$('div.donate_form div.funds div.choose a.help').hover(function() {
			if($(this).parent().find('div.help span.text').text()!='') {
				$(this).parent().find("div.help").css("display","block");
			}
		},function() {
			$(this).parent().find("div.help").css("display","none");
		});
		$('div.donate_form div.funds div.amount input').focus(function() {
			if($(this).val()=='$0.00') $(this).val('');
		});
		$('div.donate_form div.funds div.amount input').blur(function() { update_donation_total(); });
		$('div.donate_form div.funds div.controls a.remove').click(function() {
			var total_items=$('div.funds li.fund').length;
			if(total_items==1) {
				var li=$(this).parent().parent();
				$(li).find('div.choose select').attr('id','fund_1');
				$(li).find('div.choose select option:selected').removeAttr('selected');
				$(li).find('div.choose select option:first').attr('selected','selected');
				$(li).find('div.choose div.help span').text('');
				$(li).find('div.amount input').attr('id','amount_1').val('$0.00');
				$(li).find('div.choose h5').text('Fund #1');
			} else {
				$(this).parent().parent().remove();
				var items=$('div.funds li.fund');
				for(var i=0;i<items.length;i++) {
					$(items[i]).find('div.choose select').attr('id','fund_id_'+(i+1));
					$(items[i]).find('div.choose h5').text('Fund #'+(i+1));
					$(items[i]).find('div.amount input').attr('id','fund_amount_'+(i+1));
				}
			}
			update_donation_total();
		});
		$('div.donate_form div.funds a.add_fund').click(function() {
			var fund_number = $("div.funds ul li.fund").length + 1;
			var fund = $("div.funds ul li.fund:first").clone(true);
			$(fund).find("div.choose h5").text("Fund #"+fund_number);
			$(fund).find("div.choose select").attr("id","fund_id_"+fund_number).find("option:selected").removeAttr("selected");
			$(fund).find("div.choose select option:first").attr("selected","selected");
			$(fund).find("div.amount input").attr("id","fund_amount_"+fund_number).val("$0.00");
			$("div.funds ul li.fund:last").after(fund);
			return false;
		});
		
		// CLASS REGISTRATION
		$('select#payment_type').change(function() { if($(this).val()=='credit_card') $('div.credit_card').css('display','block'); else $('div.credit_card').css('display','none'); });
		$('select#payment_type').each(function() { $(this).change(); });
		$('div.registration_review div.tout').each(function() { $(this).css('height',$(this).height()+'px'); });
		
		// IMAGE SLIDER
		$('div.image_slider a.previous').bind('click',previous_image_slider);
		$('div.image_slider a.next').bind('click',next_image_slider);
		$('div.image_slider ul.thumbnails a').click(function() {
			$('div.image_slider div.image img').attr('src',$(this).attr('title'));
			$('div.image_slider div.image span').text($(this).parent().find('img').attr('title'));
		});
		
		// ADS
		$('li.ad').each(function() { ad_start(); });
		$('li.ad').hover(function() {
			clearInterval(ad_timer);
			ad_timer=null;
		},function() {
		  	ad_start();
		});
		
		// PAGE HEIGHT
		$('div#main').each(function() {
			var main_height=$(this).height();
			var sidebar_height=$('div#sidebar').height();
			if(main_height>sidebar_height) {
				var tout_height=$('div#sidebar ul.touts').height()+(main_height-sidebar_height);
				$('div#sidebar ul.touts').css('height',tout_height+'px');	
			} else if(sidebar_height>main_height) {
				var right_height=$('div.right_height').height()+(sidebar_height-main_height);
				$('div.right_height').css('height',right_height+'px');
			}
		});

	});
		
		/*--------------------------------------------------------*/
		/* HELPERS
		/*--------------------------------------------------------*/
		
		var ad_timer=null,ad_position=0;
		function ad_rotate() {
			ad_position++;
			var ads=$('li.ad a');
			if(ad_position>=ads.length) ad_position=0;
			$('li.ad a:visible').fadeOut(500,function() { $(this).css({opacity:1}); });
			$(ads[ad_position]).fadeIn(500,function() { $(this).css({opacity:1}); });
		}
		function ad_start() {
			if($('li.ad a').length>1) {
				ad_timer=setInterval(ad_rotate,5000);
				$('li.ad a').css({display:'none'});
				$('li.ad a:first').css({display:'block'});
			}
		}
		
		// REAL PEOPLE, REAL STORIES TOUT
		function previous_rprs_tout() {
			disable_rprs_tout();
			var li=$('div.column div.rprs_tout ul li');
			var total=li.length;
			var current=parseInt(get_id($('div.column div.rprs_tout ul li:visible').attr('id')));
			var previous=current-1;
			if(previous<0) previous=total-1;
			$(li[previous]).css({left:'-265px',display:'block'}).animate({left:0},300,function() { enable_rprs_tout(); });
			$(li[current]).animate({opacity:0},200,function() { $(this).css({display:'none','opacity':1}); });
			return false;
		}
		function next_rprs_tout() {
			disable_rprs_tout();
			var li=$('div.column div.rprs_tout ul li');
			var total=li.length;
			var current=parseInt(get_id($('div.column div.rprs_tout ul li:visible').attr('id')));
			var next=current+1;
			if(next>=total) next=0;
			$(li[next]).css({left:'265px',display:'block'}).animate({left:0},300,function() { enable_rprs_tout(); });
			$(li[current]).animate({opacity:0},200,function() { $(this).css({display:'none','opacity':1}); });
			return false;
		}
		function disable_rprs_tout() {
			$('div.column div.rprs_tout a.previous').unbind('click',previous_rprs_tout).bind('click',empty);
			$('div.column div.rprs_tout a.next').unbind('click',next_rprs_tout).bind('click',empty);
		}
		function enable_rprs_tout() {
			$('div.column div.rprs_tout a.previous').unbind('click',empty).bind('click',previous_rprs_tout);
			$('div.column div.rprs_tout a.next').unbind('click',empty).bind('click',next_rprs_tout);
		}
		
		// DONATION FORM
		function update_donation_total() {
			var total=0;
			$("li.fund div.amount input").each(function() { 
				var amount=convert_to_number($(this).val());
				$(this).val('$'+number_format(amount,2,".",","));
				total+=amount;
			});
			$("div.funds div.total h6").text("$"+number_format(total,2,".",","));
		}
		
		// IMAGE SLIDER
		function previous_image_slider() {
			disable_image_slider();
			var top=$('div.image_slider ul.thumbnails').css('top');
			top=parseInt(top);
			if(top<=-82) {
				$('div.image_slider ul.thumbnails').animate({top:'+=82px'},200,function() { enable_image_slider(); });
			} else {
				enable_image_slider();	
			}
			return false;
		}
		function next_image_slider() {
			disable_image_slider();
			var top=$('div.image_slider ul.thumbnails').css('top');
			top=parseInt(top);
			var height=$('div.image_slider ul.thumbnails').height();
			if(top>=((-1)*(height-$('div.image_slider div.frame').height()))) {
				$('div.image_slider ul.thumbnails').animate({top:'-=82px'},200,function() { enable_image_slider(); });
			} else {
				enable_image_slider();	
			}
			return false;
		}
		function disable_image_slider() {
			$('div.image_slider a.previous').unbind('click',previous_image_slider).bind('click',empty);
			$('div.image_slider a.next').unbind('click',next_image_slider).bind('click',empty);
		}
		function enable_image_slider() {
			$('div.image_slider a.previous').unbind('click',empty).bind('click',previous_image_slider);
			$('div.image_slider a.next').unbind('click',empty).bind('click',next_image_slider);
		}
		