﻿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": "14 tip to be announced"},
                {"tip": "15 tip to be announced"},
                {"tip": "16 tip to be announced"},
                {"tip": "17 tip to be announced"},
                {"tip": "Celebrate Older Americans Month by taking steps to prevent chronic disease"},
                {"tip": "Women should schedule regular checkups."},
                {"tip": "Celebrate Physical Fitness Month by adding physical activity to your daily routine."},
                {"tip": "Learn to recognize the signs of depression."},
                {"tip": "During Home Safety Month, make sure your home is safe for every family member. "},
                {"tip": "Focus on Men’s Health "},
                {"tip": "Learn to spot the warning signs of elder abuse."},
                {"tip": "Planning ahead for long-term care means more control over your future."},
                {"tip": "Did you know that volunteering is good for your health?"},
                {"tip": "What can you do to stay cool this summer?"},
                {"tip": "Learn how to prevent or slow down osteoporosis"},
                {"tip": "Your healthy eating plan should include smart calorie targets"},
                {"tip": "Learn to prevent or lower high blood pressure"},
                {"tip": "Small changes in muscle strength can make a big difference in your lifestyle"},
                {"tip": "Learn how to reduce your risk of food-borne illnesses"},
                {"tip": "Regular eye exams can help save your sight"},
                {"tip": "Learn which online health information sources are current and reliable"},
                {"tip": "Create an emergency kit and emergency plan for your family"},
                {"tip": "Raise your awareness about suicide and suicidal behaviors"},
                {"tip": "Incorporate fruits and vegetables into your diet"},
                {"tip": "Fit cardio exercise into your weekly routine"},
                {"tip": "If you don’t understand something your doctor is telling you, ask for clarification. "},
                {"tip": "Do you have a fire safety plan in place for your home? "},
                {"tip": "Choose carbohydrate foods wisely as part of a healthy diet."},
                {"tip": "Are you taking your prescription medications correctly? "},
                {"tip": "Prevent scams by taking an active role in protecting your health care benefits."},
                {"tip": "Can you spot the warning signs of Alzheimer’s disease? "},
                {"tip": "If you’re caring for a family member, don’t forget to take care of yourself. "},
                {"tip": "Medicare Part D Open Enrollment begins this week. "},
                {"tip": "One in five U.S. adults are at risk for type 2 diabetes. Are you? "},
                {"tip": "Increase your awareness of AIDS prevention and testing"},
                {"tip": "Have you had your flu shot? "},
                {"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();

