function IE6(){return (jQuery.browser.msie  && (Math.floor(jQuery.browser.version) == 6))}

function getInnerHeight(el)
{
	return el.height() + (parseInt(el.css('paddingTop'))||0) + (parseInt(el.css('paddingBottom'))||0);
}

function replaceSubmitButton(wrapper, cssClass)
{
	var submitBtn = wrapper.find('input:submit');
	var submitLink = jQuery('<a href="#" class="' + cssClass + '"><span>' + submitBtn.val() + '</span></a>');
	submitBtn.replaceWith(submitLink);
	submitLink
		.click(function()
				{
					var form = jQuery(this).parents('form');
					form.submit();
					return false;
				})
}

function getPledgeForm()
{
	var formWrapper = jQuery('#PledgeFormWrapper');
	formWrapper.append('<div class="loading">loading...</div>');
	jQuery.ajax({
		    	url: 'pledge.html',
			type: 'GET',
			cache: false,
			success: function(htmlResponse)
					{
						formWrapper.html(htmlResponse);
						setPledgeFormEvents(formWrapper);
						initJSTracker(formWrapper);
					},
			error: function()
				{
					alert('An unknown error has occured. Please refresh the page.');
				}
		    })
}

function setPledgeFormEvents(formWrapper)
{
	if(!formWrapper)
		var formWrapper = jQuery('#PledgeFormWrapper');
	
	formWrapper.find('form')
		.submit(function()
			{
				var frm = jQuery(this);
				formWrapper.empty().append('<div class="loading">loading...</div>');
				jQuery.ajax({
						url: 'pledge.html',
						type: 'POST',
						data: frm.serialize(),
						cache: false,
						success: function(htmlResponse)
								{
									formWrapper.html(htmlResponse);
									setPledgeFormEvents(formWrapper);
									initJSTracker(formWrapper);
								},
						error: function(err)
							{
								alert('An unknown error has occured. Please refresh the page.');
							}
					    });
				return false;
			})
		
	
	formWrapper.find('input:text')
		.each(function()
			{
				var i = jQuery(this);
				if (jQuery.trim(i.val()) == '')
					i.addClass('bg' + i.attr('name'));
			})
		.click(function()
			{
				var i = jQuery(this);
				if (jQuery.trim(i.val()) == '')
					i.removeClass('bg' + i.attr('name'));
			})
		.focus(function()
			{
				var i = jQuery(this);
				if (jQuery.trim(i.val()) == '')
					i.removeClass('bg' + i.attr('name'));
			})
		.blur(function()
			{
				var i = jQuery(this);
				if (jQuery.trim(i.val()) == '')
					i.addClass('bg' + i.attr('name'));
			});
	replaceSubmitButton(jQuery('form[name=MakeAPledge]'), 'btnMakeAPledge');
}



function resizeMainCols()
{
	var c1 = jQuery('#MainContent');
	var c2 = jQuery('#SideContent');
	
	if(c1.size() + c2.size() == 2)
	{		
		var c1h = getInnerHeight(c1);
		var c2h = getInnerHeight(c2);
		
		var maxHeight = Math.max(c1h, c2h);
		var minHeightProperty = IE6()?'height':'minHeight';
		
		if(c1h < maxHeight)
		{
			var heightDiff = maxHeight - c1h;
			
			var innerWrapper = c1;
			innerWrapper.css(minHeightProperty, innerWrapper.height() - parseInt(innerWrapper.css('paddingTop')) - parseInt(innerWrapper.css('paddingBottom')) + heightDiff + 'px');
		}
		
		if(c2h < maxHeight)
		{
			var heightDiff = maxHeight - c2h;
			var innerWrapper = c2;
			innerWrapper.css(minHeightProperty, innerWrapper.height() - parseInt(innerWrapper.css('paddingTop')) - parseInt(innerWrapper.css('paddingBottom')) + heightDiff + 'px');
		}
	}
}

function initJSTracker()
{
	jQuery('ul.snLinks a')
		.click(function()
			{
				var trackLink = jQuery(this);
				trackThis(trackLink.text());
			})
}

jQuery(function(){
	initJSTracker();
	getPledgeForm(); // replace with setPledgeFormEvents() in case the form is rirectly loaded into the HTML
})

jQuery(window).load(function(){
	resizeMainCols();
})