var addthis_pub="kai";

$j(document).ready(function(){
	$j('body').addClass('hasJS');
	/*
	$('ul#nav').superfish({
		delay:50,
		animation : { opacity:"show",height:"show",width:"show"},
		speed : 500
	});
	*/
	if ($j.browser.msie && $j.browser.version < 7) { //li:has(ul)
		$j('li#nav-kidsaliveis,li#nav-rescuingkids')
			.mouseover(function(){ 
				$j(this).addClass('sfHover'); 
			})
			.mouseout(function(){ 
				$j(this).removeClass('sfHover'); 
			});
		$j('li#nav-aroundtheworld,li#nav-involvement,li#nav-donations')
			.mouseover(function(){ 
				$j(this).addClass('sfHover');
				$j('#sponsor-search-form select').css({ visibility:'hidden' });
			})
			.mouseout(function(){ 
				$j(this).removeClass('sfHover');
				$j('#sponsor-search-form select').css({ visibility:'visible' }); 
			});
	}
	//FANCYBOX SETUP
	$j('a.fancy').fancybox({
		overlayOpacity: 0.8,
		frameWidth: 550,
		frameHeight: 350,
		zoomOpacity: true,
		zoomSpeedIn: 300,
		zoomSpeedOut: 300,
		hideOnContentClick:false
	});
	// MAILING LIST SIGNUP
	$j('input[name=emma_member_name_first]').attr('id', 'mailinglist-name');
	$j('input[name=emma_member_email]').attr('id', 'mailinglist-email');
	$j('#mailinglist-name').data('init','Enter your name.');
	$j('#mailinglist-email').data('init','Enter your email address.');
	$j('#mailinglist-name, #mailinglist-email').each(function(){
		if ($j(this).val() == '') { $j(this).val($j(this).data('init')); }
		$j(this)
			.focus(function(){ if ($j(this).val() == $j(this).data('init')) { $j(this).val(''); } })
			.blur(function(){ if ($j(this).val() == '') { $j(this).val($j(this).data('init')); } });
	});
	// SPONSORSHIP INFO TABS
	$j('ul#sponsor-info').css({ paddingTop:'0px' });
	$j('ul#sponsor-info li[rel]').css({ display:'none' }).eq(0).css({ display:'block' });
	$j('ul#sponsor-info li.tab').css({ display:'inline' })
		.children('a').click(function(){
			var thisLI = $j(this).attr('href').split('#')[1];
			$j('ul#sponsor-info li.tab a').removeClass('on');
			$j('ul#sponsor-info li[rel]').css({ display:'none' });
			$j('li[rel="'+thisLI+'"]').fadeIn(250);
			$j(this).addClass('on');
			return false;
		}).eq(0).addClass('on');
	
	if ($j('form.appeal-donation')) {
		$j('form.appeal-donation input#custom-value').focus(function(){ $j('form.appeal-donation input#suggested-other').attr('checked','checked'); });
	}
	//Removing links from region navigation
	$j('#nav-aroundtheworld li a.level-2').click(function(){return false;});
	
	//Making Dynamic FAQs work
	$j('ul.dynamic-faq > li > a').click(function(){
		$j(this).next('ul').toggle();
		return false;
	});
	//$j('ul.dynamic-faq li li a').unbind('click');
	
	
	//Radio Station Pages
	if($j('body.radio form#give-form')) {
		var optionLength = $j('form select option').length;
		var firstLinkText = $j('form select option:first').html();
		var firstLinkVal = $j('form select option:first').attr('value');
		var insertLinkCode = '';
		
		// Create dropdown links
		for (i=0; i<optionLength; i++) {
			var linkText = $j('form select option:eq(' + i + ')').html();
			var linkValue = $j('form select option:eq(' + i + ')').attr('value');
			insertLinkCode += '<a href="#" rel="' + linkValue + '">' + linkText + '</a>';	
		}
		insertLinkCode += '<a href="#" rel="donor-entered" value="donor-entered">Another amount</a>';
		$j('div#dropdown').prepend(insertLinkCode);
		
		// Remove old select, substitute hidden input with same name
		$j('div#give-bg select').remove();
		$j('form#give-form').append('<input id="dropdown-value" type="hidden" name="quantity" value="" />');
		
		// Add the .withScript class for value entry fields
		$j('div#give-bg')
			.addClass('withScript')
			.append('<input id="dropdown-selected" type="text" name="select-text" value="" readonly="readonly" /><a href="#" id="button-hit-area">&nbsp;</a>')
		$j('div#dropdown-other')
			.addClass('withScript')
			.hide();
		// Load init values and show dropdown field
		$j('input#dropdown-selected').val(firstLinkText);
		$j('input#dropdown-value').val(firstLinkVal);
		$j('input#dropdown-selected').show();
		
		//Set click actions for dropdown
		$j('input#dropdown-selected').click(function () {
			if ($j('div#dropdown').is(':hidden')) {
				$j('div#dropdown')
					.slideDown("fast")
					.css({ display: "block" });
				$j('div#dropdown-other').hide();
				$j('a[rel=donor-entered]').click(function(e){
					$j('div#dropdown').css({ display: "none" });
					$j('div#dropdown-other').show();
					$j('div#dropdown-other input').focus();
					e.preventDefault;
				});
				$j('div#dropdown a').click(function(e){
					var dropName = $j(this).text();
					var dropVal = $j(this).attr('rel');
					$j('input#dropdown-selected').val(dropName);
					$j('input#dropdown-value').val(dropVal);
					$j('div#dropdown').css({ display: "none" });
					//For testing
					//var alertText = $j('input#dropdown-value').val();
					//$j('div#test').html(alertText);
					if ($j(this).is(":not('div#dropdown a:last')")) {
						$j('div#dropdown-other input').val('');
					}
					e.preventDefault;
					return false;
				});
			} else {
				$j('div#dropdown').css( { display: "none" } );
			}
		});	
		$j('a#button-hit-area').click(function (e) {
			$j('input#dropdown-selected').triggerHandler('click');
			e.preventDefault;
			return false;			
		});
		
		// FORM VALIDATION AND SUBMISSION
		
		//uncomment below if the optional amount field should say "enter amount"
		/*
		$j('input#custom-amount').focus(function() {
			if($j(this).val() == 'Enter amount') {
				$j(this).val('');
			}
		});
		$j('input#custom-amount').blur(function() {
			if($j(this).val() == '') {
				$j(this).val('Enter amount');
			}
		});
		*/
		
		//One Time Donation Monthly Checks
		if($j('form.appeal-donation').get(0)) {
			$j('.monthly-check, .monthly-disclaimer').hide();
			var giftAmount = 1;
			var dynamicAmount = 1;
			
			$j('input[name="quantity"]').click(function() {
				$j('p.monthly-check').fadeIn(300, function() {
					$j('p.monthly-check').addClass('monthly-check-on');
				});
				giftAmount = $j(this).val();
				if ($j('#suggested-other').is(':checked')) { 
					if ($j('#custom-value').val() == '') { 
						dynamicAmount[0] = 'a gift';
						$j('.amount-statement:first').html(dynamicAmount[0]);
						$j('.amount-statement:eq(1)').html('');
					} else { 
						$j('.amount-statement').html('$' + $j('custom-value').val());
					}
				} else { 
					dynamicAmount = giftAmount.split('suggested-');
					dynamicAmount[0] = '$' + dynamicAmount[0];
					$j('.amount-statement:first').html('<b style="font-size:14px;">' + dynamicAmount[0] + '</b>');
					$j('.amount-statement:eq(1)').html(dynamicAmount[0]);
					$j('#custom-value').val('');
				}	
			});
			$j('p.monthly-check input[type="checkbox"]').click(function() {
				$j('.monthly-disclaimer').toggle();
				$j(this).parents('p.monthly-check').toggleClass('monthly-check-active');
			});
			
			$j('input#custom-value').keyup(function() {
				calcAmt($j(this).val());
				if ($j(this).val() == '') { 
					$j('.amount-statement:first').html('a gift');
					$j('.amount-statement:eq(1)').html('');
				} else {
					calcAmt($j('input#custom-value').val());
				}
			});
			
			function calcAmt(val) {
				if (parseInt(val)) {
					$j('.amount-statement:first').html('<b style="font-size:14px;">$' + addCommas(val) + '</b>');
					$j('.amount-statement:eq(1)').html('of $' + addCommas(val));
					$j('input#custom-value').css('color', 'black');
				} else {
					$j('input#custom-value').css('color', 'red');
				}
				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;
				}
			}
			
		}
		
		//Pop-up Monthly
		if($j('form.inline-form').get(0)) {
			$j('.monthly-disclaimer').hide();
			$j('p.monthly-check input[type="checkbox"]').live('click',function() {
				console.log('here');
				$j('.monthly-disclaimer').toggle();
				$j(this).parents('p.monthly-check').toggleClass('monthly-check-active');
			});
			
		}	
		//Sponsorship Schedule change
		
		if($j('div.sponsor-moreinfo').get(0)) {
			var thisMonth = new Date();
			var nextYear = new Date();
			nextYear = parseInt(nextYear.getFullYear());
			
			var months = new Array();
			months[0] = 'January';
			months[1] = 'February';
			months[2] = 'March';
			months[3] = 'April';
			months[4] = 'May';
			months[5] = 'June';
			months[6] = 'July';
			months[7] = 'August';
			months[8] = 'September';
			months[9] = 'October';
			months[10] = 'November';
			months[11] = 'December';
			
			var q1Index = thisMonth.getMonth()+3;
			var q2Index = thisMonth.getMonth()+6;
			var q3Index = thisMonth.getMonth()+9;
		
			
			if (q1Index > 11) {
				q1Index = q1Index - 12;
			}
			if (q2Index > 11) {
				q2Index = q2Index - 12;
			}
			if (q3Index > 11) {
				q3Index = q3Index - 12;
			}	
			
			var monthly = 'on or around the first business day of each <b>month</b> until canceled.';
			var quarterly = 'in the first week of <b>' + months[q1Index] + '</b>, <b>' + months[q2Index] + '</b>, <b>' + months[q3Index] + '</b> and <b>' + months[thisMonth.getMonth()] + '</b> until canceled.';
			var semiannually = 'in the first week of <b>' + months[q2Index] + '</b> and <b>' + months[thisMonth.getMonth()] + '</b> until canceled.';
			var annually = 'in the first week of <b>' + months[thisMonth.getMonth()] + ' (starting in ' + (nextYear+1) + ')</b> until canceled.';
			
			
			$j('#schedule-dropdown').hide().before('<a href="#schedule-dropdown" id="schedule-change">Choose another payment schedule</a>');
			$j('#schedule-change').click(function() {
				$j('#schedule-dropdown').fadeIn(300, function() {
					$j('#schedule-change').slideUp(300);
				});
				return false;
			});
			var frequency = $j('#schedule-dropdown select').val().split('-');			
			$j('#schedule-dropdown select').change(function() {
				frequency = $j(this).val().split('-');
				if (frequency[0] == 'monthly') { frequency[2] = monthly; quantity = 1;}
				if (frequency[0] == 'quarterly') { frequency[2] = quarterly; quantity = 3;}
				if (frequency[0] == 'semiannually') { frequency[0] = 'semi-annually'; frequency[2] = semiannually; quantity = 6;}
				if (frequency[0] == 'annually') { frequency[2] = annually; quantity = 12;}
				
				$j('#schedule-dropdown p .freq').html(frequency[0]);
				$j('#schedule-dropdown p .amt').html(frequency[1]);
				$j('#schedule-dropdown p .payment-specifics').html(frequency[2]);
				
				$j('a.sponsor-now').each(function(){
					$j(this).attr('href', $j(this).attr('href').replace(/CustomField2=[\w-]+/,'CustomField2='+frequency[0]));	
					$j(this).attr('href', $j(this).attr('href').replace(/quantity=\d+/,'quantity='+quantity));	
				});
				
			});
			
			$j('a.sponsor-now').click(function(){$j('#schedule-dropdown select').trigger('change')});
			
		}
		
		
		$j('form#give-form').submit(function(){
			if ($j('input#dropdown-selected').val() == 'Another amount' && !parseInt($j('input#custom-amount').val())) {
				alert('Please enter a valid dollar amount in the amount field.');
				return false;
			}
		});
	}
})

