$(document).ready(function(){

	var loopimgs;

	if($('#worldpopulation').length>0)
	{

		time	= new Date();
		clockStart	= time.getTime();

		if($.cookie('starttime'))
		{
			clockStart	= $.cookie('starttime');
		} else {
			$.cookie('starttime', clockStart, { expires: 1 });
		}

		window.setTimeout("getSecs()", 1);

		if($('#picslist').length>0)
		{
			window.setTimeout("loophomeimgs()", 4000);
		}

		$('.toplogo').click(function(){
			window.open('/','_self');
		});

	}

	if($('#emailsignup').length>0)
	{
	    eminit();
	}

});

function eminit()
{
    $('#emailsignup').submit(function(event){
		event.preventDefault();
	    });
	    $('#emailsignupgo').click(function(){
		var email = $('#emailsignup').val();
		$.ajax({
		    type: 'POST',
		    url: '/members/signup/',
		    data: "email=" + $('#emailaddress').val() + "&key=44",
		    async: false,
		    success: function(response){
			$('#rightrailsignup').html(response);
			if(response.indexOf("form")!=-1) // if response does not contain FORM
			{
			    $('#emailsignup').html(response);
			    eminit();
			} else {
			    $('#emailsignup').html(response);
			    eminit();
			}
		    }
		});
	    });
}

if($('#worldpopulation').length>0)
{

	startday = new Date();

	function initStopwatch() {
		var myTime = new Date();
		var timeNow = myTime.getTime();
		var timeDiff = timeNow - clockStart;
		this.diffSecs = timeDiff/1000;
		return(this.diffSecs);
	}

	function getSecs() {
		var mySecs = initStopwatch();
		var mySecs = "" + mySecs;
		var worldStart	= parseInt($('#worldpopulation').html().replace(/,/g,''));
		var worldCount = "" + parseInt(worldStart + 2.37);
		$('#worldpopulation').html(worldCount);
		mySecs = parseInt(mySecs.substring(0,mySecs.indexOf(".")) * 2.37);
		$('#netgrowth').html(mySecs);
		$('#netgrowth').format({format:"#,###,###,###", locale:"us"});
		$('#worldpopulation').format({format:"#,###,###,###", locale:"us"});
		window.setTimeout('getSecs()',1000);
	}

}

if($('#picslist').length>0)
{
	var counter = 0;

	var list	= $('#picslist');
	var labels	= $('#hometitles');

	var alabels = $('#hometitles').children();

	alabels.each(function(e){
		$(this).click(function(){
			alloff();
			clearTimeout(loopimgs);
			$('li:visible', list).hide();
			$('li:eq(' + e + ')', list).fadeIn();
			$(this).attr('class','on');

		});
	});

	function alloff()
	{
		alabels.each(function(e){
			$(this).attr('class','off');
		});
	}

	function loophomeimgs()
	{
		$('li:visible', list).hide().next('li').show();
		$('li:eq(' + counter + ')',labels).attr('class','off');
		counter++;

		if($('li:visible',list).length<1)
		{
			$('li:first',list).show();
			counter = 0;
		}

		$('li:eq(' + counter + ')',labels).attr('class','on');

		loopimgs = window.setTimeout('loophomeimgs()',4000);
	}

}