$(document).ready(function(){
//jQuery to load sidebar into the div name sidebarwrapper
$('#sidebarwrapper').load("sidebar.html");
 
 
 //jQuery to load navigation bar  into the div name navwrap
 $('#navwrap').load("nav.html");
 
//jQuery to addClass tab_active to the clicked tab header
//first remove the class tab_active from all tab headers
 $('.tab').click(function(){
     $('.tab').removeClass("tab_active");
     $(this).addClass("tab_active");
 });
 
 //default activated tab is the first one
 $('#first').addClass("tab_active");
 
 //get the gem for today
 // read the config xml file
 $.ajax({
    type: "GET",
    url: "gems.xml",
    dataType: "xml",
    success: parseXml
  });
 
  $.ajax({
    type: "GET",
    url: "daily_devotionals.xml",
    dataType: "xml",
    success: parseXml2
  });
 
});
 
function parseXml(xml)
{
  //variable for closest date
  closestDate="-9999991";
  //variable for the gem of the closest date
  currentGem="";
   //Set the  date
   today=new Date();
   var one_day=1000*60*60*24;
  //check every gem
  $(xml).find("gem").each(function()
  {
    //split up the date in year,month,day
    startdate_array = $(this).find("startdate").text().split("-");
    var gemdate=new Date(startdate_array[0], startdate_array[1]-1,
startdate_array[2]); //Month is 0-11
    //Calculate difference btw the two dates, and convert to days
    diff = Math.ceil((gemdate.getTime()-today.getTime())/(one_day));
    //check if this is the closest date so far
    if (diff < 1 && diff > closestDate){
        //yuhuuu... new closest date
        closestDate = diff;
        //and new current gem
        currentGem = $(this).find("content").text();
    }
  });
  //find the gem section in the sidebar and fill it with the current gem
  $("#gems").html(currentGem);
 
 
   //get the devotional for today
 // read the config xml file
 
}
 
function parseXml2(xml)
{
  //variable for closest date
  closestDate="-9999991";
  //variable for the devotional of the closest date
  currentDevotional="";
   //Set the  date
   today=new Date();
   var one_day=1000*60*60*24;
  //check every gem
  $(xml).find("devotional").each(function()
  {
	//split up the date in year,month,day
    startdate_array = $(this).find("startdate").text().split("-");
    var devotionaldate=new Date(startdate_array[0],
startdate_array[1]-1, startdate_array[2]); //Month is 0-11
    //Calculate difference btw the two dates, and convert to days
    diff = Math.ceil((devotionaldate.getTime()-today.getTime())/(one_day));
    //check if this is the closest date so far
    if (diff < 1 && diff > closestDate){
        //yuhuuu... new closest date
        closestDate = diff;
        //and new current gem
        currentDevotional = $(this).find("content").text();
    }
  });
  //find the devotional section and fill it with the current devotional
  $("#dailydevotional").html(currentDevotional);
  $("#first").find("a").click();
}
