	 
var GoodLy = {}

GoodLy.InitializeTheUniverse  = function(){
	
	//@TODO Apply the time stylings
	
	GoodLy.TimedBackgroundsHappyFunTime();

	//@TODO Attach select events to charities
	$('.charitytile').click(GoodLy.ClickedAPicture);
	$('#charitytile_soon').unbind("click");
	
	
	$(".yourlink input").click(function(){ $(".yourlink input").select() });
	
	$(".yourlink input").select()
	
	$("#checkAction").click(GoodLy.CheckMerchantability);
}

GoodLy.TimedBackgroundsHappyFunTime = function(){
	var currentDate = new Date()

	hour = currentDate.getHours();

	display = "night";

	moon = "moon_"+GoodLy.MoonPhase(currentDate.getYear()+1900, currentDate.getMonth(), currentDate.getDay());

	if (hour > 5 && hour < 8){
		display = "dawn";
	} else if (hour > 5 && hour < 18){
		display = "daytime";
		moon = "";
	} else if (hour > 17 && hour < 20){
		//#display = "dusk";
		display = "daytime";
	}


	document.getElementsByTagName('body')[0].className= display+" "+moon;
}

GoodLy.ClickedAPicture = function(obj){
	charity = this.id.split('_')[1];

	$('.charitytile').removeClass('charitySelected');
	$(this).addClass('charitySelected');

	$('#charity_'+charity).attr("checked","checked");

	return false;
}

GoodLy.MoonPhase = function (y, m, d)
{
    /*
      calculates the moon phase (0-7), accurate to 1 segment.
      0 = > new moon.
      4 => full moon.
      */


    if (m < 3) {
        y--;
        m += 12;
    }
    ++m;
    c = 365.25*y;
    e = 30.6*m;
    jd = c+e+d-694039.09;  /* jd is total days elapsed */
    jd /= 29.53;           /* divide by the moon cycle (29.53 days) */
    b = Math.round(jd);		   /* int(jd) -> b, take integer part of jd */
    jd -= b;		   /* subtract integer part to leave fractional part of original jd */
    b = jd*8 + 0.5;	   /* scale fraction from 0-8 and round by adding 0.5 */
    b = b & 7;		   /* 0 and 8 are the same so turn 8 into 0 */
    return b;
};

GoodLy.MerchantabilityResult = function(result){
	

	$('#checkSpinner').fadeOut("slow", function(){
		$('#checkAction').fadeIn('slow');
		
	});
	
	if (result['status'] != 0){
			
		// Error
		$('#checkResult').removeClass('good');
		$('#checkResult').addClass('bad');
		$('#checkResult').html('Unfortunatly we were unable to check that URL.');
		
	} else {
		
		if (result['result'] == 1 ){

			$('#checkResult').html('Yes! That URL is something that the charity will earn a commision on, if the user buys from it.');

			$('#checkResult').removeClass('bad');
			$('#checkResult').addClass('good');
		} else {
			
			$('#checkResult').html('No :( That site doesn\'t generate commisions from sales. Try another site with the same product. Soon we\'ll let you search our database of which sites make money for charities');

			$('#checkResult').removeClass('good');
			$('#checkResult').addClass('bad');
			
		}
		
	}
	$('#checkResult').show('slow');
	
};

GoodLy.CheckMerchantability = function (){
	
	url = $('#url').attr('value');
		

	$('#checkResult').hide('slow');
	$('#checkAction').fadeOut("slow", function(){
		$('#checkSpinner').fadeIn('slow',
			function(){
			
				bodyContent = $.ajax({
				      url: "/-checkmerchant",
				      global: false,
				      type: "POST",
				      data: ({url : url}),
				      dataType: "json",
				      success: function(msg){
				         GoodLy.MerchantabilityResult(msg);
				      }
				   }
				).responseText;
			
			});
	});


};



$(document).ready(function(){
	GoodLy.InitializeTheUniverse();
});

