﻿$(document).ready(function()
{
    //See if the hide cookie exists
    if ($.cookie('SeniorSOS-Hide-Heart'))
    {
        $('#RandomHeartsOuterContainer').hide();        
    }

    //Get the first heart then start the interval
    GetRandomHeart();
    HeartInt = setInterval('GetRandomHeart();', 5000);
    
    //If someone clicks hide the heart then hide it and create a cookie
    $('.btnHideHearts').click(function()
    {
        HeartInt = null;
        $('#RandomHeartsOuterContainer').hide();
        $.cookie('SeniorSOS-Hide-Heart', 'Hide', { expires: 30 });
        return false;
    });
});

function GetRandomHeart()
{
    //Daisy chain the fade with all of the callback functionality
    $('.divMainContain').fadeOut('500', function()
    {
        var strRootPath = $('.RootPath').val();
        var objHttpSearchRequest = null;
        
        // abort any previous AJAX requests
	    if (objHttpSearchRequest)
	    {
		    objHttpSearchRequest.abort();
 	    }

        objHttpSearchRequest = $.getJSON(strRootPath + 'ajax/modules-seniorsos-heartsrandom.ashx', function(objResults)
        {
            //clear out any previous results
            $('.HeartText').html(objResults.strScreenName + ' shows a heart for ' + objResults.strFor + '.');
            $('.HeartImage').attr('src','https://www.seniorsos.com/files/hearts-' + objResults.intCause + '.jpg');
        });
    }).fadeIn('500');
}