/**
 * @author krm
 */
var addthis_config = {
	username: "ccci",
	services_compact: 'email, facebook, twitter, digg, more'
}

$j(document).ready(function(){
	var optionLength = $j('form select option').length;
	var firstLinkText = $j('form select option:first').text();
	var firstLinkVal = $j('form select option:first').attr('rel');
	var insertLinkCode = '';
	for (i=0; i<optionLength; i++) {
		var linkText = $j('form select option:eq(' + i + ')').text();
		var linkValue = $j('form select option:eq(' + i + ')').attr('rel');
		insertLinkCode += '<a href="#" rel="' + linkValue + '">' + linkText + '</a>';	
	}
	$j('input#dropdown-selected').val(firstLinkText);
	$j('input#dropdown-value').val(firstLinkVal);
	$j('div#give-form select').hide();
	$j('div#dropdown-other').hide();
	$j('input#dropdown-selected').show();
	$j('div#dropdown-button').show();
	$j('input#button-hit-area').show();
	
	insertLinkCode += '<a href="#" rel="other-amt">Another amount</a>';
	$j('div#dropdown-links').html(insertLinkCode);
	
	$j('input#dropdown-selected').click(function () {
		if ($j('div#dropdown').is(':hidden')) {
			$j('div#dropdown')
				.slideDown("fast")
				.css({ display: "block" });
			$j('div#dropdown-other').css({
				display: "none"
			});
			$j('a[rel=other-amt]').click(function(e){
				$j('div#dropdown').css({ display: "none" });
				$j('div#dropdown-other').css({ display: "block" });
				$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-text').val(dropName);
				$j('input#dropdown-value').val(dropVal);
				$j('div#dropdown').css({ display: "none" });
				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;
			});
		} else {
			$j('div#dropdown').css( { display: "none" } );
		}
	});
	$j('div#dropdown-button').click(function (e) {
		$j('input#dropdown-selected').trigger('click');			
	});
	
	$j('input#button-hit-area').click(function() {
		$j('form#give').trigger('submit');
	});
	
	$j('form#give').submit(function() {
		var submitIt = true;
		if ($j('input#dropdown-selected').val() == 'Another amount') {
			if ($j('div#dropdown-other input').val() == '' || !parseInt($j('div#dropdown-other input').val())) { 
				alert('Please enter a valid dollar amount in the amount field.');
				submitIt = false;
			} 
		} else {
			if ($j('dropdown-selected').val() == 'Another amount') {
				if (!parseInt($j('input#dropdown-value').val())) {
					submitIt = false;
				}
				else {
					submitIt = true;
				}
			}
		}
		if (!submitIt) {
			return false;
		} else {
			return true;
		}
	});
	
	
	
});

