$(document).ready(function(){
	$('.msgok').each(coolReveal);
	$('.msgerror').each(coolReveal);

	$('#jsddm > li').bind('mouseover', jsddm_open);
	$('#jsddm > li').bind('mouseout',  jsddm_timer);
			
	$('A[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
});

function coolReveal() {
	$(this).hide();
	$(this).slideDown('slow');
}






var oTestimonials = undefined;
var iTestimonialCount = 0;
var iCurrentTestimonialIndex = -1;
var iTestimonialTransSpeed = 800;
var iTestimonialLingerTime = 6000;

function showNextTestimonial() {
	// hide old one
	if (iCurrentTestimonialIndex >= 0) {
		var oOldTestimonial = oTestimonials.eq(iCurrentTestimonialIndex);
		if (oOldTestimonial.length > 0) {
			oOldTestimonial.fadeOut(iTestimonialTransSpeed);
		}
	}
	
	// figure out index for new one
	iCurrentTestimonialIndex = iCurrentTestimonialIndex + 1;
	if (iCurrentTestimonialIndex < 0) {
		iCurrentTestimonialIndex = (iTestimonialCount - 1);
	} else if (iCurrentTestimonialIndex >= iTestimonialCount) {
		iCurrentTestimonialIndex = 0;
	}
	
	// show new one
	var oNewTestimonial = oTestimonials.eq(iCurrentTestimonialIndex);
	if (oNewTestimonial.length > 0) {
		oNewTestimonial.delay(iTestimonialTransSpeed).fadeIn(iTestimonialTransSpeed, function() {
			// queue next one
			setTimeout('showNextTestimonial()', iTestimonialLingerTime);
		});
	}	
}

function initializeTestimonialRotator() {
	oTestimonials = $('.testimonialitem');
	iTestimonialCount = oTestimonials.length;
	if (iTestimonialCount > 0) {
		showNextTestimonial();
	}
}







var oSavedAmountDollars = undefined;
var fSavedAmount = 1779300.64;
var iNumSecsBeforeEachUpdate = 30;
var iNumOf30SecIntervalsIn1Year = 365 * 24 * 60 * (60/iNumSecsBeforeEachUpdate);
var iNumMilliSecsBeforeEachUpdate = iNumSecsBeforeEachUpdate * 1000;

function calculateInitialSavedAmount() {
	fSavedAmount = 1779300.64;
	updateSavedDisplay();
}

function updateSavedAmount() {
	var fAmountPerYear = 684346.40;
	fSavedAmount = fSavedAmount + (fAmountPerYear/iNumOf30SecIntervalsIn1Year);
	updateSavedDisplay();
	setTimeout('updateSavedAmount()', iNumMilliSecsBeforeEachUpdate);
}

function updateSavedDisplay() {
	oSavedAmountDollars.text(formatCurrency(fSavedAmount));
	oSavedAmountDollars.css({ color : '#96dc2d' });
	oSavedAmountDollars.animate({ color : '#f1ba09' }, 1500);
}

function formatCurrency(num) {
	num = num.toString();
	if (isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10) cents = '0' + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
	return ('$' + num + '.' + cents);
}

function initializeSaved() {
	oSavedAmountDollars = $('#savedamountdollars');
	if (oSavedAmountDollars.length > 0) {
		calculateInitialSavedAmount();
		setTimeout('updateSavedAmount()', iNumMilliSecsBeforeEachUpdate);
	}
}









var timeout = 300;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close()
{
	if (ddmenuitem)
	{
		ddmenuitem.css('visibility', 'hidden');
	}	
}

function jsddm_timer()
{
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = jsddm_close;



