/*************************************************************************************************/
/* La Lupa website javascript functions                                                          */
/* John James, 2009                                                                              */
/*************************************************************************************************/


/*************************************************************************************************/
/* jquery function to get a random 'fact' from the 'factoid' file and load it into the index     */
/* 'Did You know' element                                                                        */
/*************************************************************************************************/
$(document).ready(function() {	

	$.ajax({
		type: "GET",
		url: "xmlfiles/factoids.xml",
		dataType: "xml",
		success: function(xml) {
			var count = $(xml).find('fact').length;
			var i = Math.floor(Math.random()* (count));
			document.getElementById("factoid").innerHTML = replaceFormatting($(xml).find('fact').eq(i).text());
		}
	});

	$.ajax({
		type: "GET",
		url: "xmlfiles/news.xml",
		dataType: "xml",
		success: function(xml) {
			$(xml).find('item').each(function(){
				var title = replaceFormatting($(this).find('title').text());
				var subtitle = replaceFormatting($(this).find('subtitle').text());
				var text = replaceFormatting($(this).find('text').text());
				var markup =	'<div class="box">' +
								'   <div class="box-title">' + title + '</div>' +
								'   <div class="box-subtitle">' + subtitle + '</div>' +
								'   <div class="box-content">' + text + '</div>' +
								'</div>';
				$(markup).appendTo('#newsbox');
				});
		}
	});
});

/****************************************************************************************************/
/* Function replaces the src tag within the 'mainframe' iframe, thus changing the webpage displayed */
/****************************************************************************************************/

function changeObjectSrc(obj, url){
        alert("changeObjectSrc called with " + obj + ", " + url);
        document.getElementById(obj).src = url;
}

/*************************************************************************************************/
/* Functions to format the current date as Dayname, Day Month Year for display on index page     */
/* Updated June 4th 2009 by JWJ to display Ordinal Day number (1st, 2nd, 3rd, etc)               */
/*************************************************************************************************/

function displayDate(dateVal) {
    var DaystoAdd=dateVal;
    var TodaysDate = new Date();
    var TodaysDay = ['Sunday', 'Monday', 'Tuesday','Wednesday', 'Thursday', 'Friday', 'Saturday'];
    var TodaysMonth = ['January', 'February', 'March','April', 'May','June', 'July', 'August', 'September','October', 'November', 'December'];
    var DaysinMonth = ['31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31'];
	var DayString = ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th',
	                 '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st'];

function LeapYearTest (Year) {
    if (((Year % 400)===0) || (((Year % 100)!==0) && (Year % 4)===0)) {
        return true;
        }
    else {
        return false;
        }
    }
    var CurrentYear = TodaysDate.getYear();
    if (CurrentYear < 2000) {
        CurrentYear = CurrentYear + 1900; } 
    var currentMonth = TodaysDate.getMonth();
    var DayOffset = TodaysDate.getDay();
    var currentDay = TodaysDate.getDate();
    var month = TodaysMonth[currentMonth];
    if (month == 'February') {
        if (((CurrentYear % 4)===0) && ((CurrentYear % 100)!==0) || ((CurrentYear % 400)===0)) {
            DaysinMonth[1] = 29;
        }
        else {
            DaysinMonth[1] = 28;
            }
    }
    var days = DaysinMonth[currentMonth];
    currentDay += DaystoAdd;
    if (currentDay > days) {
        if (currentMonth == 11) {
            currentMonth = 0;
            month = TodaysMonth[currentMonth];
            CurrentYear = CurrentYear + 1;
        }
        else {
            month = TodaysMonth[currentMonth+1];
        }
    currentDay = currentDay - days;
    }
    DayOffset += DaystoAdd;

function offsettheDate (offsetCurrentDay) {
    if (offsetCurrentDay > 6) {
        offsetCurrentDay -= 6;
        DayOffset = TodaysDay[offsetCurrentDay-1];
        offsettheDate(offsetCurrentDay-1);
    }
    else {
        DayOffset = TodaysDay[offsetCurrentDay];
        return true;
        }
    }
    offsettheDate(DayOffset);
    var TheDate  = DayOffset + ', ';
    TheDate += DayString[currentDay - 1] + ' '; 
    TheDate += month + ', ';
    if (CurrentYear<100) { CurrentYear="19" + CurrentYear; }
        TheDate += CurrentYear;
        document.write(' '+TheDate);
    }
