var addthis_pub="kai";

$j(document).ready(function(){
	/*
	$('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'); });
	}
	
	//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');
			}
		});
		*/
		
		$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;
			}
		});
	}
})
