
/* 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 currentWeatherButtonId = '#buttonJohannesburg';

var DEFAULT_INET = "ZRUSM,GBPZAR,ERZRM,ZRJYM,GOLDM,PLATM,BRSPOT,J203";
var JSE_TODAY = "J200/J201/J202/J203/J210/J211/J212/J232/J150";

//publicationURL = testPublicationURL; //use the test server URL


function getPublicationURL()
{
    var hostName = window.location.host;
    var localServerURL = "http://ece-dev2.avusa.co.za:8080/sowetanlive"
    var testServerURL = "http://test.escenic.avusa.co.za:8088/sowetanlive";
    var productionServerURL = "http://www.sowetanlive.co.za";

    if(hostName.indexOf("ece-dev")!= -1)
        return localServerURL;

    if(hostName.indexOf("test.escenic")!= -1)
        return testServerURL;

    if(hostName.indexOf("www.sowetanlive.co.za")!= -1)
        return productionServerURL;

    return localServerURL;
}

/*END:UTILITY FUNCTIONS*/

function loadMarketsBody()
{
    $.get( getPublicationURL() + '/fetch?inet=' + DEFAULT_INET + '&section=body&format=xml',
        null,function(data){
            $('#markets').html(data);
        },'text');

}
 


/*
 * Convenience method for retrieving the synop number for a given city.
 * Defaults to Johannesburg if the city cannot be found in the
 * global citySynopNoMap Associative Array.
 * */
function getSynopNumber(userCity)
{
    var synopNumber = citySynopNoMap[userCity];

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

/**
 *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( getPublicationURL() + '/fetch?synopNumber='+getSynopNumber(cityName)+'&section=forecast&format=xml',null,function(data){
        $('#forecast').html(data);
    },'TEXT');
    if($(whichButtonId))
    {
        $(currentWeatherButtonId).removeClass("selected");
        $(whichButtonId).addClass("selected");
        currentWeatherButtonId = whichButtonId;
    }
}

/* Right bar Change City <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();

    //Default to Johannesburg if "Change city" is selected
    if(synopNumber == 'option1'){
        synopNumber = DEFAULT_SYNOP_NUMBER;
    }
    
    $.get( getPublicationURL() + '/fetch?synopNumber='+synopNumber+'&section=forecast&format=xml',null,function(data){
        $('#forecast').html(data);
    },'TEXT');

    return false;
}


/* Add City */
function addCity()
{
    // The synop number that was selected from the <select/> tag.
    var synopNumber = $('#synopNumberList1').val();

    //Default to Johannesburg if "Change city" is selected
    if(synopNumber == 'option1'){
        synopNumber = DEFAULT_SYNOP_NUMBER;
    }

     $.get( getPublicationURL() + '/fetch?synopNumber=' + synopNumber + '&section=today-el&format=xml',
        null,function(data){
            $('#newCity').html(data);
        },'text');
    return false;
}
 
/*START:FINANCIAL DATA FUNCTIONS*/

/* Gets a quote for a given symbol. */
function getQuickQuote(inputElementId,trElementId)
{
    var symbol = $(inputElementId).val();

    if(symbol)
    {
        $.ajax({
            type: "GET",
            url: getPublicationURL() + '/fetch',
            data: { inet:symbol, section:'quick', format:'xml' },
            contentType: "application/text; charset=utf-8",
            dataType: "text",
            error: function(xhr, desc, exceptionobj)
                   {
                    $(trElementId).html("<td colspan='4'>Connection error.</td>");
                   },
            success: function(inData)
                     {
                        if(inData.length>0)
                            $(trElementId).html(inData);
                        else
                            $(trElementId).html("<td colspan='4'>Share not found.</td>");
                     }

        });
        $(trElementId).css("visibility","visible");
    }
    else
    {
        $(trElementId).html("<td colspan='4'>Enter share symbol below.</td>");
        $(trElementId).css("visibility","visible");
    }
}

/*END:FINANCIAL DATA FUNCTIONS*/


