﻿var tipLocation = document.getElementById('AoA_Health_Tip').firstChild;
var previousLocation = document.getElementById('AoA_Previous_Tip').firstChild;
var now = new Date();
var currentYear = now.getFullYear();
var previousYear = currentYear - 1;
var previousID = 0;


var rootPath = "http://www.aoa.gov/AoAroot/Press_Room/News/";

var baseURL = rootPath + currentYear + "/Healthy_Tip/"; // change URL here

var showTip=0;

// Replace the tip title as needed
// JSONFile set up as Array of 0-52
// Set corresponding Title to array starting at 1 NOT on zero
var JSONFile = {"someVar": [{"tip": "Week 0 does not exit, next tip start week 1"},
                {"tip": "Happy New Year! Make a resolution to live a healthier life as you age"},
                {"tip": "Are you using dietary supplements safely?"},
                {"tip": "Keep track of the medications you take."},
                {"tip": "Getting a good night’s sleep is key to good health."},
                {"tip": "Help yourself or a woman in your life reduce the risk of heart disease. "},
                {"tip": "Prevent falls by improving your balance."},
                {"tip": "Can you recognize the signs of a stroke and prevent them from happening?"},
                {"tip": "Game on! Join in fun activities for older persons."},
                {"tip": "Are you eating well? Nutrition Facts may have the answer."},
                {"tip": "Build your brain power!"},
                {"tip": "Emergencies happen. Are you prepared?"},
                {"tip": "When was your last colon cancer screening?"},
                {"tip": "Are you getting your beauty sleep?"},
                {"tip": "Do you know the simple steps to preventing falls?"},
                {"tip": "Have you safeguarded your personal health information?"},
                {"tip": "Are you being screened to reduce your cancer risk?"},
                {"tip": "Are you lightening your load to prevent travel injuries?"},
                {"tip": "Eat well to Live Long!"},
                {"tip": "Promote bone health. Prevent osteoporosis."},
                {"tip": "Exercise to Age Strong!"},
                {"tip": "Depression and aging: Treatment works."},
                {"tip": "Get set for a healthy summer"},
                {"tip": "Have you planned for your future?"},
                {"tip": "Don’t Let Heart Disease Sneak Up On You"},
                {"tip": "Are you still a safe driver?"},
                {"tip": "Reacting to cataracts: steps you can take to minimize the impact of cataracts on your life"},
                {"tip": "Stop, drop, and roll. Fire safety in your home."},
                {"tip": "Keep a smile on your face, look after your dentures."},
                {"tip": "It’s more than a bite. Protect yourself with mosquito control."},
                {"tip": "Defend against diabetes."},
                {"tip": "Prevent and control hypertension following the DASH eating plan."},
                {"tip": "Feeling the effects of osteoarthritis? Fight back."},
                {"tip": "Be smart about over-the-counter medicine. Find out what’s right for your body."},
                {"tip": "Control your blood pressure. Your kidneys may depend on it."},
                {"tip": "Protect yourself from financial abuse."},
                {"tip": "Eat healthy on a budget: Some tips for making high-fiber, low-cost choices."},
                {"tip": "Choose fats carefully as a part of a healthy diet."},
                {"tip": "Both good and bad cholesterol count, and your good health is in the balance."},
                {"tip": "Cognitive decline and memory loss: Is it Alzheimer’s or normal aging?"},
                {"tip": "40 tip to be announced"},
                {"tip": "41 tip to be announced"},
                {"tip": "42 tip to be announced"},
                {"tip": "43 tip to be announced"},
                {"tip": "44 tip to be announced"},
                {"tip": "45 tip to be announced"},
                {"tip": "46 tip to be announced"},
                {"tip": "47 tip to be announced"},
                {"tip": "48 tip to be announced"},
                {"tip": "49 tip to be announced"},
                {"tip": "Stay safe this winter by preparing in advance. "},
                {"tip": "Volunteering may be good for your health. Find a volunteer activity in your community."},
                {"tip": "How can you beat the holiday blues? "},
              ]};
                
eval(JSONFile);


// this function grabs the current week number of the year
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function getWeek(year,month,day) {
    var when = new Date(year,month,day);
    var newYear = new Date(year,0,1);
    var offset = 7 + 1 - newYear.getDay();
    if (offset == 8) offset = 1;
    var daynum = ((Date.UTC(y2k(year),when.getMonth(),when.getDate(),0,0,0) - Date.UTC(y2k(year),0,1,0,0,0)) /1000/60/60/24) + 1;
    var weeknum = Math.floor((daynum-offset+7)/7);
    if (weeknum == 0) {
        year--;
        var prevNewYear = new Date(year,0,1);
        var prevOffset = 7 + 1 - prevNewYear.getDay();
        if (prevOffset == 2 || prevOffset == 8) weeknum = 53; else weeknum = 52;
    }
    return weeknum;
}


// displays current week link text and hyperlink
function displayTip ()
{
   //check for last week of the year
    if (showTip == 52)
    {
     //baseURL = rootPath + previousYear + "/Healthy_Tip/"; // navigate to last year's folder
       baseURL = rootPath + currentYear + "/Healthy_Tip/"; //Change Made by Malik
    }
    
    tipLocation.innerHTML = JSONFile.someVar[showTip].tip;// write out title of tip 
    tipLocation.href = baseURL + showTip + ".aspx"; // update hyperlink to full health tip page
    previousLocation.href = "#";  //required otherwise onClick will not work in IE
    previousLocation.onClick = "backOne();return false"; //otherwise will refresh and jump to top of page
}


// update link text and hyperlink to previous week's tip
function backOne ()
{
    // check for first week of the year 
    if (showTip == 1)
    {
       showTip = 52; //assign to last week of previous year
       
       baseURL = rootPath + previousYear + "/Healthy_Tip/"; // ...navigate to last year's folder
        
    }
    else
    {
        showTip = showTip -1;
        
    }
    tipLocation.href = baseURL + showTip + ".aspx";
    tipLocation.innerHTML = JSONFile.someVar[showTip].tip
    
}

// this function is never called on
function checkWeek ()
{
    if (showTip < 19){
    //remove Previous link at 18th week;
    document.getElementById("AoA_Previous_Tip").className="AoAHideText";
    }

}


showTip = getWeek(y2k(now.getYear()),now.getMonth(),now.getDate());

displayTip();

