
WM.donate = function () {};

WM.donate.addStyle = function () {
	var liHeight = 0;
	var tempHeight = 0;
	//stripe the panels
	$("#donatemenu p:last").addClass("last");
	$("#donatemenu li:even").addClass("even");
	$("#donatemenu li:odd").addClass("odd");
	//loop throught the LI elements and find the largest panel, and save it's height
	$("#donatemenu li h4").each( function () {
		tempHeight = $(this).height();
		if (tempHeight >= liHeight) {
			liHeight = tempHeight;
		}
	});
	liHeight = liHeight+'px'
	$("#donatemenu li h4").css( { 'height': liHeight } );
	liHeight = 0;
	tempHeight = 0;
	$("#donatemenu li").each( function () {
		//$(this).find('div.buttons a').after('<br/><span class="moretoggle"><a href="#">Read more</a></span>');
		tempHeight = $(this).height();
		if (tempHeight >= liHeight) {
			liHeight = tempHeight;
		}
	});
	liHeight = liHeight+'px'
	$("#donatemenu li").css( { 'height': liHeight } );
	WM.donate.addAmounts();
};
WM.donate.addAmounts = function () {
	$("#donatemenu li").each( function () {
		var listItem = $(this);
		$(this).find('.moretoggle').find('a').click( function () {
			showText = $(this).closest('li').find('div.extra').html();
			$('<div></div>').html(showText).dialog({
			  title: 'More information'
			});
			return false;
		});
		var donlink = $(this).find('h4 a').attr("href") + '&direct=Hld4h0kJ';
		$(this).find('p').css('cursor','pointer').click(function () {
		  window.location = donlink;
			return false;
		});
		var imgP = $(this).children('p.image')[0];
		var amount = $(this).find('strong').text();
		$(imgP).css( { 'position': 'relative' });
		$(imgP).children("img").css('display','block').after("<span class='amount'>"+amount+"</span>");
		$("#donatemenu .amount").hover( function () { $(this).css('color', '#ffff00') }, function () { $(this).css('color','#fff') } );
	});
};





WM.donate.t = 0;

WM.donate.taxInit = function () {
		var formCode = '<form style="margin: 0; padding: 0" action="donation.asp"> \
        <table width="360" style="width: 360px; margin: 0;"> \
        	<tr> \
        		<td width="33%"><label for="tDon">Intended donation</label><input style="width:110px" type="text" id="tDon" name="Donation" /></td> \
        		<td width="33%"><label for="tDon">Your income</label> \
        		<select id="tInc"  style="width:110px"  name="Inc"> \
        		<option disabled="disabled" selected="selected" value="0">Please select</option> \
        		<option value="0">$0-$6,000</option> \
        		<option value="15">$6,000-$37,000</option> \
        		<option value="30">$37,000-$80,000</option> \
        		<option value="37">$80,000-$180,000</option> \
        		<option value="45">over $180,000</option> \
        		</select></td> \
        		<td width="33%"><label for="tBen">Possible tax benefit*</label><input disabled="disabled" style="width:110px" type="text" id="tBen" name="Ben" /></td> \
        	</tr> \
        </table> \
        <p><input type="submit" class="form-button" id="taxSub" value="Donate this amount" disabled="disabled" />\
				<input type="hidden" name="cause" value="Tax time donation" /> \
				<input type="hidden" name="Campaign_Code" value="XXXXXXXXX" /></p>\
				</form>';
				
			$('#tax').html(formCode).css({'height': 'auto'});
			$('#tax input, #tax select').keypress(function () {
			  		WM.donate.startTime();
			}).change( function () {
			  WM.donate.doTaxCalc();
			});
};

WM.donate.startTime = function () {
		clearTimeout(WM.donate.t);
		WM.donate.t = setTimeout(WM.donate.doTaxCalc, 250);
};

WM.donate.updateButton = function (calc, don) {
		if ((calc > 0 && don > 0)) {
			 $('#taxSub').val('Donate $'+don).attr('disabled', false);
		} else {
			 $('#taxSub').val('Donate this amount').attr('disabled', 'disabled');		
		}
};

WM.donate.doTaxCalc = function () {
	var rate = parseInt($('#tInc').val(), 10);
  var don = parseInt($('#tDon').val().replace(/[\$\,]*/g,''), 10);
	var calc = (rate > 0 && don > 0) ? Math.round(don * (rate/100)) : 0;
  $('#tBen').val('$' + calc);	
	WM.donate.updateButton(calc, don);
};


WM.donate.init = function () {
		WM.donate.addStyle();
		WM.donate.taxInit();
};

$(document).ready(function () {
  WM.donate.init();
});

