$(document).ready(function () {
    ajax_request(locations.Ireland.RepublicofIreland.Dublin);
    $('#Dublin').bind('click', function () {
        ajax_request(locations.Ireland.RepublicofIreland.Dublin);
        return false;
    });
    $('#Sydney').bind('click', function () {
        ajax_request(locations.Australia.NSW.Sydney);
        return false;
    });
});

function ajax_request($location) {
    $('#label').css('display', 'none');
    $('#spinner').css('display', 'inline');
    $.ajax({
        type: 'GET',
        url: '/app/scripts/getxml.php',
        data: 'url=http://weather.yahooapis.com/forecastrss&p=' + $location + '&u=c',
        dataType: 'xml',
        cache: false,
        complete: function (data) {
            var json = $.xmlToJSON(data.responseXML);
            $('#spinner').css('display', 'none');
            $('#label').css('display', 'inline');
            $('#debug').html(json.channel[0].location[0].city);
            if (json.channel[0].location[0].city == 'Dublin') {
                $('#Sydney').css('text-decoration', '');
                $('#Dublin').css('text-decoration', 'underline');
            }
            else if (json.channel[0].location[0].city == 'Sydney')
            {
                $('#Dublin').css('text-decoration', '');
                $('#Sydney').css('text-decoration', 'underline');
            }
            $('#conditions').html('<b>Current conditions:</b><br /><img src="http://l.yimg.com/us.yimg.com/i/us/nws/weather/gr/' + json.channel[0].item[0].condition[0].code + 's.png" width="61" height="34" title="' + json.channel[0].item[0].condition[0].text.toLowerCase() + '" alt="' + json.channel[0].item[0].condition[0].text.toLowerCase() + '" /> ' + json.channel[0].item[0].condition[0].text.toLowerCase() + ', ' + json.channel[0].item[0].condition[0].temp.toLowerCase() + '°' + json.channel[0].units[0].temperature);
            $(function(){$('div#conditions').pngFix();});
            var forecast = '<b>Forecast:</b><br />';
            for (var i = 0; i < json.channel[0].item[0].forecast.length; i++)
            {
                forecast += json.channel[0].item[0].forecast[i].day;
                forecast += ' ' + json.channel[0].item[0].forecast[i].date + ':';
                forecast += ' ' + json.channel[0].item[0].forecast[i].low + '/' + json.channel[0].item[0].forecast[i].high + '°' + json.channel[0].units[0].temperature;
                forecast += ' ' + json.channel[0].item[0].forecast[i].text;
                forecast += '<br />';
            }
            $('#forecast').html(forecast);
        }
    });
}

var locations = {
    'Australia': {
        'ACT': {
            'Canberra': 'ASXX0023'
        },
        'NSW': {
            'Sydney': 'ASXX0112',
            'Albury': 'ASXX0364',
            'Armidale': 'ASXX0003',
            'Bathurst': 'ASXX0385',
            'Broken Hill': 'ASXX0255',
            'Campbelltown': 'ASXX0022',
            'Dubbo': 'ASXX0265',
            'Goulburn': 'ASXX0051',
            'Newcastle': 'ASXX0083',
            'Parramatta': 'ASXX0087',
            'Penrith': 'ASXX0088',
            'Queanbeyan': 'ASXX0095',
            'Tamworth': 'ASXX0113',
            'Wollongong': 'ASXX0124'
        },
        'NT': {
            'Darwin': 'ASXX0032'
        },
        'QLD': {
            'Brisbane': 'ASXX0016',
            'Bundaberg': 'ASXX0194',
            'Cairns': 'ASXX0020',
            'Caloundra': 'ASXX0021',
            'Gladstone': 'ASXX0046',
            'Gold Coast': 'ASXX0047',
            'Ipswich': 'ASXX0061',
            'Mackay': 'ASXX0070',
            'Maryborough': 'ASXX0217',
            'Mount Isa': 'ASXX0079',
            'Redcliffe': 'ASXX0098',
            'Rockhampton': 'ASXX0100',
            'Townsville': 'ASXX0117'
        },
        'SA': {
            'Adelaide': 'ASXX0001',
            'Mount Gambier': 'ASXX0078',
            'Port Lincoln': 'ASXX0244',
            'Victor Harbor': 'ASXX0118',
            'Whyalla': 'ASXX0245'
        },
        'TAS': {
            'Hobart': 'ASXX0057',
            'Devonport': 'ASXX0370',
            'Launceston': 'ASXX0331'
        },
        'VIC': {
            'Melbourne': 'ASXX0075',
            'Ararat': 'ASXX0288',
            'Benalla': 'ASXX0307',
            'Ballarat': 'ASXX0293',
            'Bendigo': 'ASXX0295',
            'Geelong': 'ASXX0043',
            'Hamilton': 'ASXX0286',
            'Mildura': 'ASXX0077',
            'Swan Hill': 'ASXX0291',
            'Wangaratta': 'ASXX0308'
        },
        'WA': {
            'Perth': 'ASXX0089',
            'Bunbury': 'ASXX0228',
            'Geraldton': 'ASXX0044',
            'Fremantle': 'ASXX0041',
            'Kalgoorlie': 'ASXX0063',
            'Mandurah': 'ASXX0071'
        }
    },
    'Ireland': {
        'RepublicofIreland': {
            'Dublin': 'EIXX0014',
            'Cork': 'EIXX0011',
            'Galway': 'EIXX0017',
            'Limerick': 'EIXX0026',
            'Waterford': 'EIXX0047',
            'Kilkenny': 'EIXX0021'
        },
        'NorthernIreland': {
            'Armagh': 'UKXX0545',
            'Belfast': 'UKXX0015',
            'Lisburn': 'UKXX0082',
            'Newry': 'UKXX0830'
        }
    }
};

