// resizes images withing stories depeinding on browser size - used for all stories

function CheckWidthImage(CWphoto,CWminwidth,CWphotowidth){
  var CWwidth = document.body.clientWidth;
  var CWphotofull = CWphotowidth;
  var CWphotohalf = (CWphotowidth/2);
  var CWdivfull = "width:" + CWphotofull + "px;";
  var CWdivhalf = "width:" + CWphotohalf + "px;";
  var CWphotoname = "photo" + CWphoto;
  var CWtablename = "table" + CWphoto;
  var CWdivname = CWphotoname + "div";

  if (CWwidth > CWminwidth) {
    document.getElementById(CWphotoname).setAttribute("width",CWphotofull);
    document.getElementById(CWdivname).setAttribute("style",CWdivfull);
    document.getElementById(CWdivname).style.setAttribute("width",CWphotofull);
    document.getElementById(CWtablename).style.setAttribute("width",CWphotofull);
  } else {
    document.getElementById(CWphotoname).setAttribute("width",CWphotohalf);
    document.getElementById(CWdivname).setAttribute("style",CWdivhalf);
    document.getElementById(CWdivname).style.setAttribute("width",CWphotohalf); 
    document.getElementById(CWtablename).style.setAttribute("width",CWphotohalf); 
  }
}

// randomly returns a value from any number of sent values - used for rotating banners

function RandomScript(){
  x = (Math.round( 0.5 + ( (RandomScript.arguments.length - 0.00001) * Math.random() ) ) - 1);
  return RandomScript.arguments[x];
}

// fixes anchor targets to _blank - used for desktop news

function FixLink(){

  var Anchors=document.getElementsByTagName("a");
  for (var i=0; i < Anchors.length; i++) {
    var Anchor=Anchors[i];
    Anchor.target="_blank";
  } 
}

// allows toggling on/off tables by clicking on a link - used for desktop news

function ToggleView(TableId){

  var TableStatus = document.getElementById(TableId).style.display;

  if (TableStatus == "none"){
    document.getElementById(TableId).style.display = "block";
  } else {
    document.getElementById(TableId).style.display = "none";
  }

  document.getElementById("Explainer").style.display = "none";

}

// allows for a div to scroll its conent vertically

var ScPause = "false";

function Scroll() {

  if (ScPause == "false") {
    document.getElementById("Scroll").scrollTop += 1;
  }
  
  window.setTimeout("Scroll()", 50);

}

// allows for scrolling div to be paused

function ScrollPause(Switch) {

  ScPause = Switch;
  
}

// Podcast code

function LoadMPlayer(url) {  
 url = 'http://robocaster.com/help_files/pe/mplayer.aspx?url=' + url;
 var mplayer;
 mplayer =  window.open(url,'mywin','left=200,top=200,width=300,height=130,toolbar=0,location=0,directories=0,resizable=0,scrollbars=0,menubar=0,status=0');
 mplayer.focus(); 
}

// Podcast code

function checkForiTunes(path)
{
    var conf = confirm('This link will not work unless you have iTunes installed.  Click OK and iTunes will load this Podcast Feed into your saved Podcasts.  If you do not have iTunes installed click cancel to download iTunes.')

    if (conf == true)
    {
        var url = 'itpc://'+path;
        window.location.href = url;
    }
    else
    {
        window.open("http://www.apple.com/itunes/download");
    }
}


function CheckDayTime(DivId,StartDay,StartTime,EndDay,EndTime){

  // Get All Current Time Variables
  var CurDate = new Date();
  var CurDay = CurDate.getDay();
  var CurHours = CurDate.getHours();
  var CurMin = CurDate.getMinutes();
  var OffSet = CurDate.getTimezoneOffset();

  // Add one to Day (to make it easier to program with possible zeros)
  CurDay++;
  
  // Convert StartDay and EndDay to a numerals for easier checking
  var DayArray = new Array("Null","Sun","Mon","Tue","Wed","Thu","Fri","Sat");  
  for (x=0; x<=7; x++) {
    if (StartDay == DayArray[x]) StartDay = x;
	if (EndDay == DayArray[x]) EndDay = x;
  }

  // If the ending day is earlier in the week than the starting, add 7 to make it possible to check for wrapping days
  if (EndDay < StartDay) {
    EndDay = EndDay + 7;
  }
  
  // If current day is earlier than the starting day, add 7 to make it possible to check for wrapping days
  if (CurDay < StartDay) {
    CurDay = CurDay + 7;
  }
  
  
  // Check for Daylight Savings
  var CurrentDate = new Date;
  var StartDaylight = new Date;
  var EndDaylight = new Date;
  
  // Find this years daylight savings start date
  StartDaylight.setMonth(2); // March
  StartDaylight.setDate(1); // 1st of the month
  StartDaylight.setHours(03);  // 3 am for down-to-the-hour accuracy
  StartDaylight.setMinutes(00); // Minutes accuracy
  StartDaylight.setSeconds(00); // Seconds accuracy
  var day = StartDaylight.getDay();// day of week of 1st
  if (day == 0) {
    day = 7; // If the 1st of the month is a Sunday need to correct math
  }
  StartDaylight.setDate(15-day); // Second Sunday
  
  // Find this years daylight savings end date
  EndDaylight.setMonth(10); // November
  EndDaylight.setDate(1); // 1st of the month
  EndDaylight.setHours(02);  // 2 am for down-to-the-hour accuracy
  EndDaylight.setMinutes(00); // Minutes accuracy
  EndDaylight.setSeconds(00); // Seconds accuracy
  day = EndDaylight.getDay();// day of week of 1st
  if (day == 0) {
    day = 7; // If the 1st of the month is a Sunday need to correct math
  }
  EndDaylight.setDate(8-day); // First Sunday
  
  // Check if current time is between start and end daylight
  if (CurrentDate < StartDaylight || CurrentDate >= EndDaylight) {
    Daylight = 0; // Daylight is off
  } else {
    Daylight = 1; // Daylight is on
  }
  
  // Set time zone offsetting
  OffSet = (OffSet / 60) - 8 + Daylight; // PST
  
  // Check for time zone offset
  if (OffSet != 0) {
    CurHours = CurHours - OffSet;
    // Check for previous day due to offset
	if (CurHours < 0) {
	  // Sets hours to valid 24 hour time
	  CurHours = CurHours + 24; 
	  // Sets day back one
	  CurDay = CurDay - 1;
	  // Check to see if day is less than 1 (from Monday to Sunday)
	  if (CurDay < 1) {
	    CurDay = 99;  // Only way this can happen is if the Current Day is before the Start Day
	  }
	// Checks for next day due to offset
	} else if (CurHours > 24) {
	  // Sets hours to valid 24 hour time
	  CurHours = CurHours - 24;
	  // Sets day forward one
	  CurDay = CurDay + 1;
    }
  }  
  
  // Add leading 0 to hours to help with time check
  if (CurHours < 10) {
    CurHours = "0" + CurHours;
  }
  
  // Creating final variables to check if the current time is between start/end
  Current = (CurDay * 10000) + (CurHours * 100) + CurMin;
  Start = StartDay + StartTime;
  End = EndDay + EndTime;

  // Final check and div display
  if (Start <= Current && Current < End) {
    document.getElementById(DivId).style.display = "block";
  }
  
  //document.write("Start: " + Start + "Current: " + Current + "End: " + End);
  
  //Code Usage
  //CheckDayTime('ID','StartDay','StartTime','EndDay','EndTime');
  //ID is the id of the object to be modified from style="display: none;" to style="display: block;"
  //StartDay and EndDay are 3-letter abbreviations
  //StartTime and EndTime are 24-hour format

  //Example of use
  //CheckDayTime('MM_Manual','Thu','1000','Fri','1000');
  
}


