
$(function(){

    var locations = {
        44544: 'Belfast',
        12710: 'Birkenhead',
        582624: 'Caen',
        14876: 'Cairnryan',
        582681: 'Calais',
    585596: 'Cherbourg',
    560472: 'Cork',
    18382: 'Dover',
    560743: 'Dublin',
	560749: 'Dun Laoghaire',
        589284: 'Dunkerque',
    20239: 'Fishguard',
    553314: 'Frederikshavn',
	556646: 'Gedser',
    890869: 'Gothenburg',
    892828: 'Helsingborg (SE)',
    553989: 'Helsingor (DK)',
    23154: 'Heysham',
    23774: 'Holyhead',
	730165: 'Hook of Holland',
    24522: 'Ipswich',
	900028: 'Kapellskar',
    30498: 'Killingholme',
    44941: 'Larne',
    26734: 'Liverpool',
	570131: 'Naantali',
    905000: 'Nynashamn',
	975129: 'Oostende',
    862592: 'Oslo',
    31602: 'Pembroke',
    32185: 'Plymouth',
    32326: 'Poole',
    32452: 'Portsmouth',
	32682: 'Purfleet',
    686290: 'Puttgarden',
    32907: 'Ramsgate',
    556646: 'Rodby',
    620002: 'Roscoff',
    562142: 'Rosslare',
	689776: 'Rostock',
    33585: 'Rosyth',
    733075: 'Rotterdam',
    625394: 'St Malo',
    773964: 'Santander',
	691105: 'Sassnitz',
    36414: 'Stranraer',
	26822951: 'Travemunde',
	908074: 'Trelleborg',
    38255: 'Troon',
	856069: 'Ventspils',
        45226: 'Warrenpoint',
        979128: 'Zeebrugge'
    };

    if ($('#weather').length) {

        var forecast = $.cookie('forecast');
        var woeid = $.cookie('woeid') || 18382; // default to Dover

        if (!forecast) {
            getWeather(woeid, renderWeather);
        }
        else {
            forecast = forecast.split('|');
            forecast.unshift(woeid);
            renderWeather(forecast);
        }        
    }

    function getWeather(woeid, callback) {

        $.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%20%3D%20%22http%3A%2F%2Fweather.yahooapis.com%2Fforecastrss%3Fw%3D{woeid}%26u%3Dc%22&format=xml&diagnostics=false&callback=?'.replace('{woeid}', woeid), function(data){

            if ($.browser.msie) {
                var xml = new ActiveXObject('Microsoft.XMLDOM');
                xml.async = false;
                xml.loadXML(data.results.toString());
            }
            else {
                var xml = data.results.toString();
            }

            var $forecast = $(xml).find('yweather\\:condition');

            var forecast = [
                $forecast.attr('text'),
                $forecast.attr('code'),
                $forecast.attr('temp'),
                $forecast.attr('date').substr(0, 16)
            ];

            // Cache the forecast for the session
            $.cookie('forecast', forecast.join('|'));

            // Remember the location for 7 days
            $.cookie('woeid', woeid, {expires: 7});

            forecast.unshift(woeid);

            callback(forecast);
        });
    }

    function renderWeather(weather) {

        var select = '<select>';

        for (var woeid in locations) {

            if (weather[0] == woeid) {
                select += '<option value="' + woeid + '" selected>' + locations[woeid] + '</option>';
            }
            else {
                select += '<option value="' + woeid + '">' + locations[woeid] + '</option>';
            }
        }

        select += '</select>';

        $('#weather').append('<h2>Weather in</h2>' + select + '<dl><dt>Date</dt><dd>' + weather[4] + '</dd><dt>Temperature</dt><dd id="temp" class="condition-' + weather[2] + '">' + weather[3] + '<span>c</span></dd><dt>Forecast</dt><dd>' + weather[1] + '</dd></dl>').find('select').change(function(){

            var dl = $(this).next();

            dl.css({height: dl.height(), background: 'url(style/images/ajax-loader.gif) 50% 50% no-repeat'}).empty();

            getWeather($(this).val(), function(weather){
                dl.css({height: 'auto', background: 'none'}).append('<dt>Date</dt><dd>' + weather[4] + '</dd><dt>Temperature</dt><dd id="temp" class="condition-' + weather[2] + '">' + weather[3] + '<span>c</span></dd><dt>Forecast</dt><dd>' + weather[1] + '</dd>');
            });
        });
    }
});
