/* 
  for the feng shui homepage tip o day module 
  also used for kaboose partner page /kaboose/feng-shui.php
  
  to use, make sure to have the following elements in yer page:
  image (dispaly none prolly) id: fs_tod_hdr_img
  yesterday | today | tomorrow with these ids: 
    fs_tod_nav_yesterday, fs_tod_nav_today, & fs_tod_nav_tommorrow
  date subheader: fs_tod_date
  tip content id: fengshui_tod
*/
var fs_xhr;

// set up the request object
function createXMLHttpRequest()
{
  var xmlHttp;
  if(window.ActiveXObject)
  {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
  }
  else if(window.XMLHttpRequest)
  {
    xmlHttp = new XMLHttpRequest();
  }
  
  return (xmlHttp);
}


// request the page (called when user clicks link)
// update yesterday | today | tomorrow nav
// passedYTT = yesterday today or tomorrow
function get_fs_tod (passedDate, passedYTT)
{
  fs_xhr = createXMLHttpRequest();
  fs_xhr.onreadystatechange=process_fs_tod;  
  // need to foil caching
  var rand = Math.random() * 100;
  fs_xhr.open("GET", "/fengshui/fengshui_tip_feed.php?rand= " + rand + "&passedDate=" + passedDate);
  // this function updates the nav
  fs_tod_ytt_nav(passedYTT); 
  // this one updates the header image
  fs_tod_update_image (passedYTT);
  fs_xhr.send(null);
} // end of function get_fs_tod 


// deal with the returned data
// from fengshui_tip_feed.php 
function process_fs_tod()
{
   var fs_tip_text;
   var fs_tip_date_hdr = false;
   
  if (fs_xhr.readyState == 4)
  {
    // 4 means we have the data
    if (fs_xhr.status == 200)
    {
      // 200 = successful page request
      var fs_tip_data = fs_xhr.responseText;
      var fs_tip_array = fs_tip_data.split('|||');
      fs_tip_date_hdr = fs_tip_array[0];
      fs_tip_text = fs_tip_array[1];
    } else {
      fs_tip_text = "Sorry, there's been an error.";
    } 
    
  } else
  {
    fs_tip_text = "loading...";
  }
  document.getElementById('fengshui_tod').innerHTML = fs_tip_text;
  if (fs_tip_date_hdr)
  {
    document.getElementById('fs_tod_date').innerHTML = fs_tip_date_hdr;
  }
}



// update the yesterday | today | tomorrow nav
// to deal with whatever day we'er looking at
// vars set in the tpl file by php: fs_tod_nav_yesterday, fs_tod_nav_tomorrow
// they're needed by the php file sending the tip data 
function fs_tod_ytt_nav(passedYTT) 
{
  // defaults
  var yesterday_link = "<a href=\"javascript:get_fs_tod ('" + fs_tod_nav_yesterday + "','yesterday');\">Yesterday</a>"
  var today_link = "<b>Today</b>";
  var tomorrow_link = "<a href=\"javascript:get_fs_tod ('" + fs_tod_nav_tomorrow +"','tomorrow');\">Tomorrow</a>";

  // exceptions for when viewing yesterday's tip
  if (passedYTT == "yesterday")
  {
    yesterday_link = "<b>Yesterday</b>";
    today_link = "<a href=\"javascript:get_fs_tod (0 ,'today');\">Today</a>";
  
  }
  // exceptions for when viewing tomorrow's tip
  else if(passedYTT == "tomorrow")
  {
    tomorrow_link = "<b>Tomorrow</b>";
    today_link = "<a href=\"javascript:get_fs_tod (0 ,'today');\">Today</a>";
  }

  // replace the nav 
  // each is in a span with an id
  document.getElementById('fs_tod_nav_yesterday').innerHTML = yesterday_link; 
  document.getElementById('fs_tod_nav_today').innerHTML = today_link;
  document.getElementById('fs_tod_nav_tommorrow').innerHTML = tomorrow_link;
}


// update the header image
// this uses the javascript date
// everything else uses the php date
// which is kinda living on the edge
function fs_tod_update_image (passedYTT)
{
  // find what day img we want to display
  var date = new Date();
  var dayNum = date.getDay();
  if (passedYTT == "yesterday")
  {
    dayNum --;
  } else if (passedYTT == "tomorrow")
  {
    dayNum++;
  }
  
  // round the corners of the array
  if (dayNum < 0 )
    dayNum = 6;
    
  if (dayNum > 6)
    dayNum = 0;  
  
  // swap out the header image
  var daysAr = new Array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
  var updatedImage = "http://gfx.tarot.com/images/homepage/fengshui/" + daysAr[dayNum] + "s-tip.gif";
  document.getElementById('fs_tod_hdr_img').src = updatedImage;
}
// end of function fs_tod_update_image
