
var yt_ident = "<img id=\"edpic\" src=\"images/Y.jpg\" alt=\"Yorkshire Times\" />";
var Towns = ["Barnsley", "Bedale", "Beverley", "Bingley", "Bradford", "Bridlington", "Brighouse", "Castleford", "Catterick Garrison",
                 "Cleckheaton", "Cottingham", "Darlington", "Dewsbury", "Doncaster", "Driffield", "Elland", "Filey",
                 "Goole", "Guisborough", "Halifax", "Harrogate", "Hawes", "Hebden Bridge", "Heckmondwike", "Hessle", "Holmfirth", "Huddersfield",
                 "Hull", "Ilkley", "Keighley", "Knaresborough", "Knottingley", "Leeds", "Leyburn", "Liversedge", "Malton", "Mexborough", "Middlesbrough",
                 "Mirfield", "Morley", "Northallerton", "Normanton", "Osset", "Otley", "Pickering", "Pontefract", "Pudsey", "Redcar", "Richmond", "Ripon",
                 "Rotherham", "Saltburn-by-the-Sea", "Scarborough", "Selby", "Settle", "Sheffield", "Shipley", "Skipton", "Sowerby Bridge", "Stockton-on-Tees",
                 "Tadcaster", "Thirsk", "Todmorden", "Wakefield", "Wetherby", "Whitby", "Yarm", "York"
                ];

// collect and show the weather
function getWeather() {
    // hide the old weather
    $('#todays_weather').fadeOut(500, function () {
        // show the weather
        $.simpleWeather({
            location: currentLocation + ', united kingdom',
            unit: 'c',
            success: function (weather) {
                show_weather(weather);
            },
            error: function (error) {
                //alert(error);
            }
        });
    });
}

// populate locations widget and add event code for saving chosen location, then refresh weather
function populateLocsWidget() {
    var t = "";
    for (var i = 0; i < Towns.length; i++) {
        t += "<div id=\"town" + i + "\">" + Towns[i] + "</div>";
    }
    $('#locns').html(t);
    $('#locns div').click(function () {
        currentLocation = Towns[$(this)[0].id.replace("town", "")];
        createCookie("yt_location", currentLocation, 90);
        $('#lName').html(currentLocation);
        $('#locns').toggle(false);
        getWeather();
    });
}

function radio() {
    var rheight = 160;
    var rwidth = 330;
    var bottom_space = 100;
    var ht = screen.height - rheight - bottom_space;
    window.open("radio.php", "myWindow",
            "height=" + rheight + ",width=" + rwidth + ",left=0,top=" + ht + ",menubar=no,location=no,titlebar=no,status=no")
}

// show the loading graphic
function show_wait() {
    $('#wait').show();
}
function hide_wait() {
    $('#wait').hide();
}

// goto a page off the root
function goto(url) {
    document.location.href = "../" + url;
}

// show the weather elements
function show_weather(weather) {

    // masthead display of current weather
    var html = '<img src="' + weather.image + '" alt="Current Weather"><div id="todays_temp">' + weather.temp + '&deg;' + weather.units.temp + '</div>';
    $('#todays_weather').html(html);
    $('#todays_weather').fadeIn(1000, function () {
        $('#todays_weather').css('zIndex', '1');
        $('.menu').css('zIndex', '10');
    });

    // show tomorrow's weather
    html = '<div id="wTitle">TOMORROW IN ' + currentLocation + '</div>';
    html += '<img src="' + weather.tomorrow.image + '">';
    html += '<div id="wContent">';
    html += '<strong>High</strong>: ' + weather.tomorrow.high + '&deg;C<br /><strong>Low:</strong> ' + weather.tomorrow.low + '&deg;C';
    html += '<br /><a target="_blank" href="' + weather.link + '">more</a>';
    html += '</div>';
    html += '<div id="wContent1">';
    html += '<strong>Forecast</strong>: ' + weather.tomorrow.forecast;
    html += '</div>';
    // add to tomorrow's weather widget
    if ($("#weather").length > 0) $("#weather").html(html);
}

// format and display a masthead time for yorkshire times 
function ytTime() {
    var ytTime = new Date();
    var h = ytTime.getHours();
    var am_pm = "AM";
    if (h > 12) {
        am_pm = "PM";
        h -= 12;
    }
    var m = ytTime.getMinutes();
    if (m < 10) m = "0" + m;
    var t = h + ":" + m + am_pm;

    $('#tim').html(t);
    window.setTimeout("ytTime()", 1000);
}

// format a masthead date for yorkshire times
function ytDate() {
    var dt = new Date();
    var days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
    var months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

    var d = dt.getDate();                     // number of day of month
    var strDay = days[dt.getDay()];           // name of day
    var strMonth = months[dt.getMonth()];     // name of month
    var yr = dt.getFullYear();                // 4 digit year

    var ext = "th";
    if (d == 1 || d == 21 || d == 31) ext = "st";
    else if (d == 2 || d == 22) ext = "nd";
    else if (d == 3 || d == 23) ext = "rd";

    return strDay + " " + d + ext + " " + strMonth + " " + yr;
}



// COOKIE FUNCTIONS
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

// END OF COOKIE FUNCTIONS


function worldPopulation() {
    // currently linear increase at 2.5 per second from 6,980,500 on August 1st 2011
    // forecast by UN population dept. to reach 7Bn by October 31st 2011
    var d0 = new Date(2011, 6, 29, 0, 0, 0, 0);
    var d1 = new Date();
    var diffSecs = Math.abs((d1.getTime() - d0.getTime()) / 1000);
    
    return numberFormat(parseInt(6980340000 + diffSecs * 2.5));
    
}
// This function formats numbers by adding commas
function numberFormat(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1))
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    return x1 + x2;
}


function getPopulation() {
    $('#wpop_small').html("World Population: " + worldPopulation());
    window.setTimeout("getPopulation()", 1000);
}

// replace missing favicons with default image
function defIcon(me) {
    me.src = "images/defaultIcon.jpg";
}


