var $J = jQuery.noConflict();


var DonateForm = function() {
	
	this.el = false;
	this.paymentMethodCredit = false;
	this.paymentMethodCheck = false;
	
	
	this.init = function() {
		this.el = $J('#donateForm');
		this.paymentMethodCredit = $J('#paymentMethodCredit');
		this.paymentMethodCheck = $J('#paymentMethodCheck');
		
		
		this.paymentMethodCredit.click(function(){
			BT.donateForm.showCreditFields();
		});
		this.paymentMethodCheck.click(function(){
			BT.donateForm.showCheckFields();
		});
		if($J('#paymentMethodCredit:checked').length > 0) {
			BT.donateForm.showCreditFields();
		}
		if($J('#paymentMethodCheck:checked').length > 0) {
			BT.donateForm.showCheckFields();
		}
		
		$J('#whatsThisLink').click(function(e){
			e.preventDefault();
			$J('#cvv2WhatsThisContent').toggle();
		});
		
		//$J('#creditCardSubmit').click(function(e){
		//	e.preventDefault();
		//	BT.donateForm.validateForm();
		///});
		
		$J("#donateForm").submit(function(){
			BT.donateForm.validateForm();
		});
	
	};
	
	
	this.showCreditFields = function() {
		$J('.fieldset-credit').show();
		$J('.fieldset-check').hide();
	};
	this.showCheckFields = function() {
		$J('.fieldset-check').show();
		$J('.fieldset-credit').hide();
	};
	
	this.validateForm = function(){
		var reqFields = jQuery('.required');
		var failed = false;
		reqFields.each(function(){
			$this = $J(this);
			var errMsg = $J('.err span', $this.parent().parent());
			
			if($this.val() == ''){
				failed = true;
				errMsg.show();
				$this.css({'background-color':'#ffcccc'});
				
			} else {
				errMsg.hide();
				$this.css({'background-color':'#ffffff'});
			}
		});

		if($J('#contribLevelOther:checked').length > 0){
			if(this.assessOpenDonate() == false) {
				failed = true;
			}
		}

		if(failed){
			$J('.form-invalid').show();
			return false;
		} else {
			//$J('#donateForm').submit();
			return true;
		}
		
	};
	
	this.assessOpenDonate = function(){
		var otheramountradio = $J('#contribLevelOther');
		var amountOther = $J('#contribLevelOtherAmount');
		var donationWarn = $J('#donationWarning');
		if(amountOther.val() == '' || isNaN(parseFloat(amountOther.val())) || parseFloat(amountOther.val()) < 10){
			otheramountradio.val(10);
			donationWarn.show();
			amountOther.css({'background-color':'#ffcccc'});
			return false;
		} else {
			donationWarn.hide();
			amountOther.css({'background-color':'#ffffff'});
			otheramountradio.val(amountOther.val());
			return true;
		}
	};
	
	
};

var SubnavModule = function() {
	this.el = false;
	
	
	this.init = function() {
		this.el = $J('.module-subnav');
		
		$J('li a', this.el).click(function(e){
			var rel = $J(this).attr('rel');
			if(rel != '') {
				e.preventDefault();
				BT.subnavModule.linkClick(rel);
			}
		});
		
		$J('.module-subcontent .close-subcontent').click(function(e){
			e.preventDefault();
			BT.subnavModule.closeClick(this);
		});
	};
	
	this.linkClick = function(id) {
		$J('#'+id).toggle();
	};
	
	this.closeClick = function(closeEl) {
		$J(closeEl).parent().hide();
	};
};


var App = function() {
	
	this.donateForm = false;
	this.subnavModule = false;
	
	this.init = function(){
		
		if($J('#donateForm').length > 0) {
			this.donateForm = new DonateForm();
			this.donateForm.init();
		}
		
		if($J('.module-subnav').length > 0) {
			this.subnavModule = new SubnavModule();
			this.subnavModule.init();
		}
		
		// sidebar promos
		$J('.code-link').click(function(e){
			e.preventDefault();
			$J(this).siblings('.promo-teaser').toggle();
			$J(this).siblings('.promo-details').toggle();
			//$J('.promo-details', $J(this).parent().parent()).toggle();
		});
		
	};
	
};



var BT;

$J(document).ready(function(){
	
	BT = new App();
	BT.init();
	
	
});

