$(document).ready(function(){
	$('input[alt]:text').click(function(){
		if ($(this).val() == $(this).attr('alt')) {
			$(this).val('');
		}
	});
	$('input[alt]:text').blur(function(){
		if ($(this).val() == "") {
			$(this).val($(this).attr('alt'));
		}
	});
	$('input[alt]:text').each(function(){
		$(this).val($(this).attr('alt'));
	});
	
	// The signup needs to be intercepted to set a cookie.
	if (document.cookie.indexOf('newsletter=1') == -1) {
		$('.signup form').submit(function(){
			// Stop the submit and handle via ajax instead.
			var thisForm = this;
			$.post(
				$(this).attr('action'), 
				{ "email" : $('input.email', this).val(),
				  "rType" : "json"
				},
				function(data) {
					if ( (data.success*1) != 1) {
						alert(data.message);
						$('input.email', thisForm).focus();
					} else {
							// success
						document.cookie = "newsletter=1;domain=" + document.location.host + ";path=/";
						$('.signup form').remove();
						$('.signup p').text("You have successfully subscribed to the newsletter.");
					}
				}, "json"
			);
			return false;
		});
	} else {
		$('.signup').hide();
		$('.testimonial').css('margin-top', '0px');
	}
	
	/*if (document.cookie.indexOf('newsletter=1') == -1) {
		$('.signup form').submit(function(){
			// Stop the submit and handle via ajax instead.
			$.post(
				$(this).attr('action'), 
				{ "cm-dhirky-dhirky" : $('input.email', this).val() },
				function(html) {
					if (html.indexOf("success") == -1) {
						$('.signup p').text("There is a problem with your email address.");
					} else {
						document.cookie = "newsletter=1;domain=" + document.location.host + ";path=/";
						$('.signup form').remove();
						$('.signup p').text("You have successfully subscribed to the newsletter.");
					}
				}, "html"
			);
			return false;
		});
	} else {
		$('.signup').hide();
		$('.testimonial').css('margin-top', '0px');
	}*/
	
	$('.spread-the-word form').submit(function(){
		addthis_sendto('email');
		spreadTheWord();
		return false;
	});
	
	// Enable Flash or apply non-Flash header
	if ((typeof(noflash) == 'undefined' || noflash !== true) && swfobject.hasFlashPlayerVersion("9.0.0")) {
		var flashvars = {
			donationGoal : "1200000000", /* write without comas, just a straight number */
			totalDonated : "0", /* write without comas, just a straight number */
			donationGoalWrite: "$1.2 billion" /* formate as you like, it will show instead of the long number */
		};
		if (typeof(donationStatus) == 'object') {
			flashvars = $.extend(flashvars, donationStatus);
		}
		var params = {
			base: "/_ui/campaignne/swf",
			allowfullscreen: "true",
			wmode : "transparent"
		};
		var url = "";
		if ($('.main').is('.home-page')) {
			url = "main_loader.swf";
		} else {
			url = "main_loader_interior.swf";
		}
		swfobject.embedSWF(params.base + "/" + url, 'header', '100%', '100%', '9.0.0', '' /* express install */, flashvars, params);
	} else {
		$('.main').addClass('flash-off');
		$('#header').css('background', 'url(/_ui/campaignne/images/bg-inner-header.jpg) no-repeat');
	}
	
	/* Adjust markup to allow for more complex styles. */
	$('.testimonial:not(:has(img)) cite').prepend('<span class="open"></span>');
	$('#content ul').not('.tabs').not('.areas-list').not('.button-list').addClass('listing');
//	$('#content .content img').not('.image img').not('.right-image img').not('a img').wrap('<div class="image"></div>');
	$('#content .content img, #content .article img, .box img').each(function(){
		if ($(this).parents('p').length > 0) {
			$(this).parents('p').before(this);
		}
		if ($(this).parents('.teaser').length > 0) {
			if ($(this).prevAll().length ==0) {
				$(this).css('margin-top', $.browser.msie ? '23px' : '3px').removeAttr('hspace').removeAttr('vspace');
			}
			return;
		}
		if ($(this).parents('.photo').length > 0) {
			return;
		}
		if ($(this).parents('a').length > 0) {
			$(this).parent().filter('.image-caption-table a').wrap('<div class="image"></div>');
			return;
		}
		if ($(this).is('.floatRight')) {
			if ($(this).is('.floatRight') && $(this).prevAll().length == 0 && $(this).parent().is('.body')) {
				$(this).wrap('<div class="right-photo"></div>').wrap('<div></div>').removeAttr('align');
				$(this).parent().parent().css('margin-top', $.browser.msie ? '23px' : '3px');
			} else {
				$(this).wrap('<div class="right-photo"></div>').wrap('<div></div>').removeAttr('align');
			}
			$(this).removeAttr('vspace').removeAttr('hspace');
			return;
		}
		if ($(this).is('.floatLeft')) {
			if ($(this).is('.floatLeft') && $(this).prevAll().length == 0 && $(this).parent().is('.body')) {
				$(this).wrap('<div class="left-photo"></div>').wrap('<div></div>').removeAttr('align');
				$(this).parent().parent().css('margin-top', $.browser.msie ? '23px' : '3px');
			} else {
				$(this).wrap('<div class="left-photo"></div>').wrap('<div></div>').removeAttr('align');
			}
			$(this).removeAttr('vspace').removeAttr('hspace');
			return;
		}
		if ($(this).parents('.right-photo').length > 0) {
			$(this).wrap('<div></div>');
			return;
		}
		$(this).wrap('<div class="image"></div>');
	});
	$('p.bodyCallout').removeClass('bodyCallout').wrap('<div class="text-box">');
	$('.borderedBodyCallout').removeClass('borderedBodyCallout').wrap('<div class="info-box" style="clear:both">').wrap('<div>');
	$('#content .inner-content .article').not(':has(.block.articleWrapper)').prepend('<div style="position: absolute; top: 15px; left: 39px; height: 4px; width: 81px; background-color: #000; border: 0; font-size:1px"></div>');
	$('#content .inner-content .article .block.articleWrapper').prepend('<div style="height: 4px; width: 81px; background-color: #e51836; border: 0; margin: -15px 0 5px 0; font-size:1px" class="article-bar"></div>');
	$('#content #internalContentAON .article div.clear:last-child').css('margin', '0');
	$('#content #internalContentAON h2:not(:has(a))').hide();
	$('.li-more:only-child').each(function(){ var p = $(this).parent(); p.prev().append('&nbsp;').append($(this)); p.remove(); });
	$('.image-caption-table').css('margin', '20px 0 0 15px');
	$('.image img').removeAttr('vspace');
	addthis_firstload(2000);
});

function addthis_firstload(t){
	if ($('body > div.main').prevAll('div').hide().length > 0) {
		return;
	}
	if (t < 0) {
		return;
	}
	setTimeout(function() { addthis_firstload(t - 250); }, 250);
}

function spreadTheWord() {
	if ($('#at_to').length == 0) {
		setTimeout("spreadTheWord()", 200);
	}
	$('#at_to').val($('.spread-the-word input[name=to]').val());
	$('#at_from').val($('.spread-the-word input[name=from]').val());
}

var addthis_config = {
	"username" : "ccooper155",
	"ui_click" : true
};
