
/* Associative Array for mapping Cities and their synop number.*/
var citySynopNoMap = {
    "Alexander Bay":68403,
    "Aliwal North":68546,
    "Alldays":68171,
    "Badplaas":99991,
    "Beaufort West":68727,
    "Bela Bela":68268,
    "Bethlehem":68461,
    "Bhisho":68752,
    "Bloemfontein":68443,
    "Calvinia":68618,
    "Cape Agulhas":68920,
    "Cape Point":68916,
    "Cape St. Francis":68939,
    "Cape Town":68819,
    "Clan William":68711,
    "Cradock":68744,
    "De Aar":68538,
    "Durban":68588,
    "East London":68858,
    "Ermelo":68267,
    "Excelsior":68719,
    "Fauresmith":68541,
    "Ficksburg":68449,
    "Fort Beaufort":68747,
    "Geelbek":68811,
    "George":68828,
    "Giants Castle":68589,
    "Graaff-Reinet":68737,
    "Grabouw":68925,
    "Graskop":68287,
    "Groblersdal":99992,
    "Hermanus":68918,
    "Ixopo":68575,
    "Johannesburg":68361,
    "Joubertina":68833,
    "Kimberley":68438,
    "Klerksdorp":68347,
    "Kokstad":68577,
    "Komatidraai":68297,
    "Kroonstad":68355,
    "Kuruman":68333,
    "Ladysmith":68479,
    "Lephalale":68155,
    "Lydenburg":68185,
    "Mafikeng":68242,
    "Malmesbury":68715,
    "Margate":68591,
    "Molteno":688191,
    "Mooriver":68485,
    "Mossel Bay":68928,
    "Mthatha":68668,
    "Musina":68180,
    "Nelspruit":68289,
    "Newcastle":68377,
    "Ngqura (Coega)":68837,
    "Oudtshoorn":68827,
    "Paarl":68713,
    "Phalaborwa":68191,
    "Piet Retief":68385,
    "Pietermaritzburg":68581,
    "Plettenberg Bay":68931,
    "Polokwane":68174,
    "Port Alfred":68843,
    "Port Elizabeth":68842,
    "Port St. Johns":68674,
    "Postmasburg":68429,
    "Potchefstroom":68350,
    "Pretoria":68262,
    "Queenstown":68647,
    "Redelinghuys":68710,
    "Richards Bay":68495,
    "Riversdale":68926,
    "Robertson":68718,
    "Rustenburg":68255,
    "Skukuza":68296,
    "Slangkop":68912,
    "Springbok":68512,
    "St. Lucia Estuary":68492,
    "Standerton":68372,
    "Stilbaai":68927,
    "Strand":68911,
    "Sutherland":68722,
    "Taung":68335,
    "Thabazimbi":68253,
    "Thohoyandou":68183,
    "Tzaneen":68188,
    "Uitenhage":68839,
    "Ulundi":68493,
    "Underberg":68572,
    "Upington":68424,
    "Van Reenen":68471,
    "Vereeniging":68353,
    "Vredendal":68614,
    "Vryburg":68338,
    "Vryheid":68387,
    "Welkom":68345,
    "Witbank":68273,
    "Worcester":68821,
    "NO_NAME":00000
};
var DEFAULT_SYNOP_NUMBER = 68361; // Johannesburg;
var hostName = window.location.host;

var currentWeatherButtonId = '#buttonJohannesburg';

//-----------------------------weather page methods starts here------------------------------------
/* Weather page <select/> function. Reloads the displayed 5 day forecast depending on the selected value. Return
 * false to prevent the form containing the <select/> tag submitting. */
function changeCity()
{
    // The synop number that was selected from the <select/> tag.
    var synopNumber = $('#synopNumberList').val();

    // The selected city name
    var whichCity = $('#synopNumberList option:selected').text();

    // id for which button was displayed last
    var whichButtonId = "#button"+$.trim(whichCity.replace(/ /g,''));

    // HTML to chunk to display busy/loading animation.
//    var busyHTML = "<div style='height:260px;'><div style='position:relative;top:130px;left:400px;'><img src='/timeslive/template/html_v1-0/img/icons/busy/remembermilk_orange.gif' align='left'>&nbsp;Loading 5 day forecast...</div></div></div>";

    $(currentWeatherButtonId).removeClass("selected");

    if($(whichButtonId))
    {
        $(whichButtonId).addClass("selected");
        currentWeatherButtonId = whichButtonId;
    }

//    $('#weatherContent').html(busyHTML);
    $.get( publicationURL + '/weatherAjax?cityCode='+synopNumber+'&body=body',null,function(data){$('#weatherContent').html(data);},'TEXT');

    return false;
}
/**
 *AJAX method to reload the 5 day weather from the <select/> element on the page. Also sets the buttons
 *above the 5 day display to match the select city.
 */
function viewCity(cityName)
{
    var whichButtonId = "#button"+$.trim(cityName.replace(/ /g,''));
    $.get( publicationURL + '/weatherAjax?cityCode='+getSynopNumber(cityName)+'&body=body',null,function(data){$('#weatherContent').html(data);},'TEXT');
    if($(whichButtonId))
    {
        $(currentWeatherButtonId).removeClass("selected");
        $(whichButtonId).addClass("selected");
        currentWeatherButtonId = whichButtonId;
    }
}
//----------------------------------------end of weather page method-------------------------------
function loadSingleShareCode(sharecode)
{
    $.get( publicationURL + '/fetch?inet=true&section=singleshare&sharecode='+sharecode+'&format=xml',
        null,function(data){
            $('#div' + sharecode).html(data);
        },'text');
}

function loadJSEToday()
{
    $.get( publicationURL + '/fetch?inet=true&section=jsetoday&sharecode=false&format=xml',
        null,function(data){
            $('#mi_jsetoday').html(data);
        },'text');
}

function loadCommodities()
{
    $.get( publicationURL + '/fetch?inet=true&section=commodities&sharecode=false&format=xml',
        null,function(data){
            $('#mi_commodities').html(data);
        },'text');
}

function loadCurrencies()
{   
    $.get( publicationURL + '/fetch?inet=true&section=currencies&sharecode=false&format=xml',
        null,function(data){
            $('#mi_currencies').html(data);
        },'text');
}

function loadSportsTracker()
{
    $.get( publicationURL + '/fetch?sportstracker=true&format=xml',
        null,function(data){
            $('#sportstracker').html(data);
        },'text');
}

function loadTimesliveTopstories()
{
        $.get( publicationURL + '/fetch?timeslivefeed=true&format=xml',
        null,function(data){
            $('#timeslive_content').html(data);
        },'text');
}

function getSynopNumber(userCity)
{
    var synopNumber = citySynopNoMap[userCity];

    if(synopNumber)
        return synopNumber;
    else
        return DEFAULT_SYNOP_NUMBER; // Default to Johannesburg
}

function viewWeatherCity(cityName)
{
    $.get(publicationURL + '/fetch?synopNumber='+getSynopNumber(cityName)+'&section=header&format=xml',
        null,
        function(data){
            $('#weather').html(data);
        },'text');
}

function getMapIndicators()
{
    $.get( publicationURL + '/fetch?type=market',
        null,function(data){
            $('#div_indicators').html(data);
        },'text');
}
function getMapCurrencies(){
    $.get( publicationURL + '/fetch?type=currencies',
        null,function(data){
            $('#div_currencies').html(data);
        },'text');
}
