/* ------------------------------------------------------
 *     WAVM JavaScript Library
 * ------------------------------------------------------ */



/*
 * This is our global list of sponsors.  Include title and optionally the URL.
 * If you add sponsors, don't forget to add a comma to the previously last 
 * item on the list.  If you need to put apostrophes in the text, you will need
 * to put a BACKSLASH in front of the apostrophe:  Sam\'s Coffee House
 */ 

var SponsorList = new Array(
  'Assabet Valley Tree',
  'Digital Federal Credit Union',
  'Foley & Sons Floor and Tile & Faucetorium',
  'The Hair Connection',
  'Maynard Outdoor Store',
  'Middlesex Savings Bank',
  'Ray and Sons Cyclery & Ski'
  'Maynard Rotary Club',
);


/*
 * This is the 91.7FM Broadcast Schedule
 *
 * It is an array of show objects which consist of the day and hour the show
 * runs (24 hour, eastern time), the name/title of the show, host information, and
 * optionally a link to the show's web page.
 *
 *
 */
var RadioSchedule = new Array(
 {day: "mon", hour: 14, show: "Monday 2PM Show", hosts: "Katie Carlson, Paulette Goddard, and Tony Homan"},
 {day: "mon", hour: 15, show: "Monday 3PM Show", hosts: "Adam Sherman, and Nick Angelini"},
 {day: "mon", hour: 16, show: "Monday 4PM Show", hosts: "Jillian Kelley, Casandra Morrison, and Michaela Boreson"},
 {day: "mon", hour: 17, show: "Monday 5PM Show", hosts: "Alex Martinec, and Luke Dumond"},
 {day: "mon", hour: 18, show: "Monday 6PM Show", hosts: "Mitch Sweeney, and James Lalli"},
 {day: "mon", hour: 19, show: "Monday 7PM Show", hosts: "Paul Harpin, Scott Braman, and Thomas Stevens"},
 {day: "mon", hour: 20, show: "Monday 8PM Show", hosts: "Ashley Stone, Maura Newey, and Patricia Gately"},
 {day: "tue", hour: 14, show: "Tuesday 2PM Show", hosts: "Jon Davis, Cam Faul, and Andrew Sawyer"},
 {day: "tue", hour: 15, show: "Tuesday 3PM Show", hosts: "Jake Sultzer, David Kruszkowski, and Dan McCarthy"},
 {day: "tue", hour: 16, show: "Tuesday 4PM Show", hosts: "Alex LaHair, Tim Wood, and John Panetta"},
 {day: "tue", hour: 17, show: "Tuesday 5PM Show", hosts: "POLKA HOUR"},
 {day: "tue", hour: 18, show: "Tuesday 6PM Show", hosts: "Kendall Baker, Abigail McIver, and Jenny Varnet"},
 {day: "tue", hour: 19, show: "Tuesday 7PM Show", hosts: "Nicole McIver, Karlee McIver, and Amy Baker"},
 {day: "tue", hour: 20, show: "Tuesday 8PM Show", hosts: "Karen MacGillivary and Rachel MacGillivary"},
 {day: "wed", hour: 14, show: "Wednesday 2PM Show", hosts: "Conor McCarthy, Nathan Dee, and Jake Laughlin"},
 {day: "wed", hour: 15, show: "Wednesday 3PM Show", hosts: "Dan Sweeney and Tim Brooks"},
 {day: "wed", hour: 16, show: "Wednesday 4PM Show", hosts: "Danielle Rocheleau, Makayla Meedzan, and Emily Doucette"},
 {day: "wed", hour: 17, show: "Wednesday 5PM Show", hosts: "Brian Fuerst and Kevin Fuerst"},
 {day: "wed", hour: 18, show: "Wednesday 6PM Show", hosts: "Josh Bistany and Darwin Fajardo"},
 {day: "wed", hour: 19, show: "Wednesday 7PM Show", hosts: "Fred Hunter, Tom Hunter, and Greg Porfino"},
 {day: "wed", hour: 20, show: "Wednesday 8PM Show", hosts: "Nick Jacques, Eric Arntzen, and Pat Schomacker"},
 {day: "thu", hour: 14, show: "Thursday 2PM Show", hosts: "Maddie LaCure and Katie Warzewska"},
 {day: "thu", hour: 15, show: "Thursday 3PM Show", hosts: "Ryan Chiasson, Leo Kutlowski, Ryan Fowler, and Brian DeMars"},
 {day: "thu", hour: 16, show: "Thursday 4PM Show", hosts: "Brian Chiasson and Chris Chiasson"},
 {day: "thu", hour: 17, show: "Thursday 5PM Show", hosts: "Francesca Cerqua and Emily Witham"},
 {day: "thu", hour: 18, show: "Thursday 6PM Show", hosts: "Alysha Howes and Lindsay Egan"},
 {day: "thu", hour: 19, show: "Thursday 7PM Show", hosts: "Paul Messier, Ryan Brooks, and Ryan Butterline"},
 {day: "thu", hour: 20, show: "Thursday 8PM Show", hosts: "Tim Egan and Kyle McCarthy"},
 {day: "fri", hour: 14, show: "Friday 2PM Show", hosts: "Rachel Hahn and Anna Marie Kathman"},
 {day: "fri", hour: 15, show: "Friday 3PM Show", hosts: "Matt Cerqua and Michael Cisek"},
 {day: "fri", hour: 16, show: "Friday 4PM Show", hosts: "Matt Hickey and Thomas Manning"},
 {day: "fri", hour: 17, show: "Friday 5PM Show", hosts: "Colton Davila Touchette and Jim Kutlowski"},
 {day: "fri", hour: 18, show: "Friday 6PM Show", hosts: "Michael Newey and Andrew Wilkins"}
);




/*--------------------------------------------------------------------------*
 *
 *   NOTE: Don't modify anything below here unless you REALLY know what 
 *         you are doing.
 *
 *--------------------------------------------------------------------------*/



// Update an img with the id of "webcam" every 30 seconds.

function WAVM_RefreshWebcam() {
    now = new Date(); 
    document.images["webcam"].src = "http://dmg-photography.com/webcam.jpg?" + now.getTime();
    setTimeout("WAVM_RefreshWebcam()", 30000);
}

// Update an img with the id of "webcamThumb" every 30 seconds.

function WAVM_RefreshWebcamThumb() {
    now = new Date(); 
    document.images["webcamThumb"].src = "http://dmg-photography.com/webcam-thumb.jpg?" + now.getTime();
    setTimeout("WAVM_RefreshWebcamThumb()", 30000);
}


/* 
 * Fill a div with N unique but randomly ordered snippets of HTML from an array.
 *
 * Arguments:
 *   snippets - the array containing the HTML snippets
 *   snippetCount - how many unique snippets to select
 *   interSnippet - a bit of HTML to put between them (e.g., '<br>')
 *   divId - id of the div to diddle with.
 *
 * Ideas for future: include ability to have a prefix/suffix text.
 */ 

function WAVM_RandomSnippets(snippets, snippetCount, interSnippet, divId) {
    document.getElementById(divId).innerHTML = '';
    var snipped = new Array();
    var i = 0;
    var snippet, snippetUsed;
    if (snippetCount >= snippets.length) { snippetCount = snippets.length; }
    while (i < snippetCount) {
        snippet = Math.floor(Math.random() * snippets.length);
        snippetUsed = 0;
        for (var s = 0; s < snipped.length; s++) {
            if (snipped[s] == snippet) {
                snippetUsed = 1;
                break;
            }
        }
        if (snippetUsed) continue;
        snipped.push(snippet);
        if (i) {
            document.getElementById(divId).innerHTML += interSnippet;
        }    
        document.getElementById(divId).innerHTML += snippets[snippet];
        i++;
    }
}


/*--------------------------------------------------------------------------*/

//Routines to support getting stuff from the server without reloading the page


// Standard AJAX wrapper...
function WAVM_getXMLObj() {
	var req;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return req;
}


// This routine replaces the content of a div with whatever content is at the URL address
function WAVM_AjaxGet(url, divId) {
	var ajax = WAVM_getXMLObj();
	ajax.open("GET", url, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.onreadystatechange = function() {
		if (ajax.readyState != 4) {
			return;
		}
        if (ajax.status == 200) {
        	document.getElementById(divId).innerHTML = ajax.responseText;
        } else {
        	document.getElementById(divId).innerHTML = ajax.statusText;
        }
	}
	ajax.send(null);
	delete ajax;
}                

/*--------------------------------------------------------------------------*/


/*
 * Updates the specified div with the current radio show.
 *
 * Time is based on the client's clock - so timezones and bad clocks can
 * produce erroneous results.
 *
 * If no show is scheduled, a default message and link is presented.
 *
 * This function reschedules itself to change the div at the top of the
 * hour (or so) with whatever the current show is.
 *
 * Someday: Add a nice animation to this transition.
 *
 * Arguments:
 *    divId - name of the div to put the show information.
 */

function WAVM_RadioOnAir(divId) {
    var show = 'No broadcast scheduled';
    var hosts = '';
    var url = '/radio/schedule.html';
    var now = new Date();
    var days = new Array('sun','mon','tue', 'wed', 'thu', 'fri', 'sat');
    var day = days[now.getDay()];
    var hour = now.getHours();
    var showInfo = '';
    // How many minutes till the next show
    var tillNextShow = (60 - now.getMinutes()) * 60 * 1000;
    // Future: Correct hour/day for timezones... Ugh!

    // Brute force walk through schedule array looking for
    // a candidate show.

    for (var i = 0; i < RadioSchedule.length; i++) {
        if (RadioSchedule[i].day == day && RadioSchedule[i].hour == hour) {
            show = RadioSchedule[i].show;
            hosts = RadioSchedule[i].hosts;
            if (RadioSchedule[i].url) {
                url = RadioSchedule[i].url;
            } else {
                url = '';
            }
            break;
        }
    }

    if (url != '') { showInfo += '<a href="' + url + '">'; }
    showInfo += '<b>' + show + '</b>';
    if (url != '') { showInfo += '</a>'; }
    showInfo += '<br />' + hosts;

    document.getElementById(divId).innerHTML = showInfo;

    // Update the div when the show changes.
    setTimeout("WAVM_RadioOnAir('" + divId + "')", tillNextShow);
}
