function get_nav_language() {
    var nav_lng;
    if(navigator.userlanguage) nav_lng = navigator.userlanguage;
    if(navigator.browserLanguage) nav_lng = navigator.browserLanguage;
    if(navigator.systemLanguage) nav_lng = navigator.systemLanguage;
    if(navigator.language) nav_lng = navigator.language;
    return new String(nav_lng).toLowerCase();
}

var coolweather_nav_lng = get_nav_language();
var coolweather_nav_language = "en, ro";
coolweather_nav_lng = 'en';

var oras = get_cookie('coolweather_locid');
if (oras == null) {
//set_cookie('coolweather_locid', 'EUR|RO|RO010|BRASOV|');
set_cookie('coolweather_locid', 'EUR|RO|RO009|BRASOV|');
}
var coolweather_lang = [];

coolweather_lang['en'] = [];
coolweather_lang['en']['currentconditions'] = "";
coolweather_lang['en']['changecity'] = "Alege alt oras";
coolweather_lang['en']['temperature'] = "Temperatura: ";
coolweather_lang['en']['hightemperature'] = "Maxima: ";
coolweather_lang['en']['lowtemperature'] = "Minima: ";
coolweather_lang['en']['realfeel'] = "Real<sup>&reg;</sup>: ";
coolweather_lang['en']['realfeelhigh'] = "Real<sup>&reg;</sup> Maxim: ";
coolweather_lang['en']['realfeellow'] = "Real<sup>&reg;</sup> Min: ";
coolweather_lang['en']['windspeed'] = "Viteza vantului: ";
coolweather_lang['en']['windforce'] = "Intensitate vant: ";
coolweather_lang['en']['scale'] = "scara";
coolweather_lang['en']['winddirection'] = "Directe vant: ";
coolweather_lang['en']['today'] = "Azi: ";
coolweather_lang['en']['tomorrow'] = "Maine: ";
coolweather_lang['en']['daytime'] = "ziua";
coolweather_lang['en']['nighttime'] = "noaptea";
coolweather_lang['en']['loading'] = "Se incarca...";
coolweather_lang['en']['city'] = "Oras: (Exemplu: Bucuresti)";
coolweather_lang['en']['firstseehint'] = "Vrei sa aflii vremea ?";
coolweather_lang['en']['location'] = "Locatie: ";
coolweather_lang['en']['nocitymatched'] = "Nu am gasit acest oras !";
coolweather_lang['en']['morecitymatched'] = "Rezultate multiple, alege un oras.";
coolweather_lang['en']['back'] = "inapoi";
coolweather_lang['en']['choosecity'] = 'alege un oras din lista';
coolweather_lang['en']['unit'] = 'Unitati de masura: ';
coolweather_lang['en']['unita'] = 'American';
coolweather_lang['en']['unitm'] = 'Metric';
coolweather_lang['en']['N'] = 'N';
coolweather_lang['en']['S'] = 'S';
coolweather_lang['en']['W'] = 'V';
coolweather_lang['en']['E'] = 'E';
coolweather_lang['en']['NE'] = 'NE';
coolweather_lang['en']['SE'] = 'SE';
coolweather_lang['en']['NW'] = 'NV';
coolweather_lang['en']['SW'] = 'SV';
coolweather_lang['en']['ENE'] = 'ENE';
coolweather_lang['en']['ESE'] = 'ESE';
coolweather_lang['en']['WNW'] = 'VNV';
coolweather_lang['en']['WSW'] = 'VSV';
coolweather_lang['en']['NNE'] = 'NNE';
coolweather_lang['en']['NNW'] = 'NNV';
coolweather_lang['en']['SSE'] = 'SSE';
coolweather_lang['en']['SSW'] = 'SSV';

// Romanian translate by ovidiu
coolweather_lang['ro'] = [];
coolweather_lang['ro']['currentconditions'] = "Vremea acum";
coolweather_lang['ro']['changecity'] = "Alege orasul";
coolweather_lang['ro']['temperature'] = "Temperatura: ";
coolweather_lang['ro']['hightemperature'] = "Temperatura maxima: ";
coolweather_lang['ro']['lowtemperature'] = "Temperatura minima: ";
coolweather_lang['ro']['realfeel'] = "Temperatura simtita&reg;: ";
coolweather_lang['ro']['realfeelhigh'] = "Temperatura simtita&reg; Maxima: ";
coolweather_lang['ro']['realfeellow'] = "Temperatura simtita&reg; Minima: ";
coolweather_lang['ro']['windspeed'] = "Viteza vantului: ";
coolweather_lang['ro']['windforce'] = "Intensitatea vantului: ";
coolweather_lang['ro']['scale'] = "scara";
coolweather_lang['ro']['winddirection'] = "Directia vantului: ";
coolweather_lang['ro']['today'] = "Azi: ";
coolweather_lang['ro']['tomorrow'] = "Maine: ";
coolweather_lang['ro']['daytime'] = "Pe timp de zi";
coolweather_lang['ro']['nighttime'] = "Pe timp de noapte";
coolweather_lang['ro']['loading'] = "Incarcare...";
coolweather_lang['ro']['city'] = "Localitate: (Exemplu: Sibiu)";
coolweather_lang['ro']['firstseehint'] = "Poate folosesti pentru prima oara acest browser, asa ca alege localitatea pentru care vrei sa vezi starea vremii.";
coolweather_lang['ro']['location'] = "Localitate: ";
coolweather_lang['ro']['nocitymatched'] = "Nu exista nici o localitate care sa corespunda datelor introduse!";
coolweather_lang['ro']['morecitymatched'] = "Mai multe localitati se potrivesc datelor introduse, alege pe cea dorita.";
coolweather_lang['ro']['back'] = "Inapoi";
coolweather_lang['ro']['choosecity'] = 'Alege localitatea';
coolweather_lang['ro']['unit'] = 'Unitate de masura: ';
coolweather_lang['ro']['unita'] = 'Americana';
coolweather_lang['ro']['unitm'] = 'Metrica';
coolweather_lang['ro']['N'] = 'N';
coolweather_lang['ro']['S'] = 'S';
coolweather_lang['ro']['W'] = 'V';
coolweather_lang['ro']['E'] = 'E';
coolweather_lang['ro']['NE'] = 'NE';
coolweather_lang['ro']['SE'] = 'SE';
coolweather_lang['ro']['NW'] = 'NV';
coolweather_lang['ro']['SW'] = 'SV';
coolweather_lang['ro']['ENE'] = 'ENE';
coolweather_lang['ro']['ESE'] = 'ESE';
coolweather_lang['ro']['WNW'] = 'VNV';
coolweather_lang['ro']['WSW'] = 'VSV';
coolweather_lang['ro']['NNE'] = 'NNE';
coolweather_lang['ro']['NNW'] = 'NNV';
coolweather_lang['ro']['SSE'] = 'SSE';
coolweather_lang['ro']['SSW'] = 'SSV';

coolweather_unit = [];
coolweather_unit[0] = [];
coolweather_unit[0]['temp'] = '&deg;F';
coolweather_unit[0]['speed'] = ' mph';
coolweather_unit[1] = [];
coolweather_unit[1]['temp'] = '&deg;C';
coolweather_unit[1]['speed'] = ' m/s';

function get_cookie(name) {
    function get_cookie_val(offset) {
        var endstr = document.cookie.indexOf(";", offset);
        if (endstr == -1) endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    }
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) return get_cookie_val(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function set_cookie(name, value, expires) {
    var exp = new Date();
    if (expires) {
        exp.setTime(exp.getTime() + expires);
    }
    else {
        exp.setTime(exp.getTime() + 315360000000);
    }
    document.cookie = [name, "=", escape(value), "; expires=", exp.toGMTString(), ";"].join('');
}

function delete_cookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 60000);
    document.cookie = [name, "=; expires=", exp.toGMTString(), ";"].join('');
}

function coolweather_get_windforce(windspeed) {
    var scales = [0.2, 1.5, 3.3, 5.4, 7.9, 10.7, 13.8, 17.1, 20.7, 24.4, 28.4, 32.6];
    for (var i = 0; i < scales.length; i++) {
        if (windspeed <= scales[i]) {
            return i;
        }
    }
    return 12;
}

var coolweather_rpc = new PHPRPC_Client(get_coolweather_rpc(), ['get_location', 'get_weather']);

var coolweather_report = null;
function get_coolweather_rpc()
{
	var coolweather_rpc_list=[
	'http://phprpc-weather.appspot.com/',
	'http://phprpc-weather1.appspot.com/',
	'http://phprpc-weather2.appspot.com/',
	'http://phprpc-weather3.appspot.com/',
	'http://phprpc-weather4.appspot.com/',
	'http://phprpc-weather5.appspot.com/',
	'http://phprpc-weather6.appspot.com/',
	'http://phprpc-weather7.appspot.com/',
	'http://phprpc-weather8.appspot.com/',
	'http://phprpc-weather9.appspot.com/',
	];
	var _count=coolweather_rpc_list.length;
	return coolweather_rpc_list[Math.floor(Math.random()*_count)];
}
function coolweather_set_weather(number, day_or_night) {
    var weather = PHPSerializer.unserialize(coolweather_report);
    var locid = weather['locid'];
    var metric = weather['metric'];
    var coolweather_hint = document.getElementById('coolweather_hint');
    var unit = [coolweather_lang[coolweather_nav_lng]['unit'],
        '<a href="javascript:void(0)" onclick="coolweather_get_weather(',
        "'", locid, "', ", '0);" style="display: inline; padding: 0; margin: 0">',
        coolweather_lang[coolweather_nav_lng]['unita'], '</a> ',
        '<a href="javascript:void(0)" onclick="coolweather_get_weather(',
        "'", locid, "', ", '1);" style="display: inline; padding: 0; margin: 0">',
        coolweather_lang[coolweather_nav_lng]['unitm'], '</a>'].join('');
    if (number == 0) {
        if (metric == 1) {
            var windforce = [coolweather_lang[coolweather_nav_lng]['windforce'],
                coolweather_get_windforce(weather['windspeed']), ' ',
                coolweather_lang[coolweather_nav_lng]['scale'], '<br />'].join('');
        }
        else {
            var windforce = '';
        }
		
		if (weather['weathertext'] == "Partly Sunny") {weather['weathertext'] = "<b>Insorit partial</b>";};
		if (weather['weathertext'] == "Mostly Sunny") {weather['weathertext'] = "<b>Preponderent soare</b>";};
		if (weather['weathertext'] == "Sunny") {weather['weathertext'] = "<b>Insorit</b>";};
		if (weather['weathertext'] == "Clear") {weather['weathertext'] = "<b>Senin</b>";};
		if (weather['weathertext'] == "Mostly Cloudy") {weather['weathertext'] = "<b>Preponderent innorat</b>";};
		if (weather['weathertext'] == "Light Fog") {weather['weathertext'] = "<b>Putina Ceata</b>";};
		if (weather['weathertext'] == "Foggy") {weather['weathertext'] = "<b>Ceata</b>";};
		if (weather['weathertext'] == "Cloudy") {weather['weathertext'] = "<b>Innorat</b>";};
		if (weather['weathertext'] == "Ground Fog") {weather['weathertext'] = "<b>Ceata joasa</b>";};
		if (weather['weathertext'] == "Partly Cloudy") {weather['weathertext'] = "<b>Partial innorat</b>";};
		
		if (weather['weathertext'] == "Overcast") {weather['weathertext'] = "<b>Cer acoperit de nori</b>";};
		if (weather['weathertext'] == "Mostly cloudy and breezy with a little rain") {weather['weathertext'] = "<b>Partial innorat, racoros si ploaie usoara</b>";};
		if (weather['weathertext'] == "A thick cloud cover with a brief shower") {weather['weathertext'] = "<b>Innorat si ploua</b>";};
		if (weather['weathertext'] == "Clouds breaking") {weather['weathertext'] = "<b>Nori rari</b>";};
		if (weather['weathertext'] == "Rain and drizzle") {weather['weathertext'] = "<b>Ploaie si burnita</b>";};
		if (weather['weathertext'] == "Partly sunny") {weather['weathertext'] = "<b>Partial insorit</b>";};

        coolweather_hint.innerHTML = ['<div style="margin-bottom: 0.5em; display: block;"><strong>',
            coolweather_lang[coolweather_nav_lng]['currentconditions'],
            '</strong><img src="', coolweather_iconspath,
            weather['weathericon'], '.gif" alt="', weather['weathertext'],
            '" align="left" valign="absmiddle" width="64" height="40" style="margin-right: 0.5em" />',
            '<a href="javascript:void(0)" onclick="coolweather_input_city();" title="',
            coolweather_lang[coolweather_nav_lng]['changecity'],
            '" style="display: inline; padding: 0; margin: 0">', weather['city'].substr(0,20), '<br><font style="font-size: 10px;">(alege alt oras)</font></a><br />',
            weather['state'], '</div><br />', weather['weathertext'], '<br />',
            coolweather_lang[coolweather_nav_lng]['temperature'],
            weather['temperature'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['realfeel'],
            weather['realfeel'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['windspeed'],
            weather['windspeed'], coolweather_unit[metric]['speed'], '<br />',
            windforce,
            coolweather_lang[coolweather_nav_lng]['winddirection'],
            coolweather_lang[coolweather_nav_lng][weather['winddirection']], '<br />',
            coolweather_lang[coolweather_nav_lng]['today'],
            '<a href="javascript:void(0)" onclick="coolweather_set_weather(1, 0);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['daytime'],
            '</a> / <a href="javascript:void(0)" onclick="coolweather_set_weather(1, 1);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['nighttime'], '</a><br />',
            coolweather_lang[coolweather_nav_lng]['tomorrow'],
            '<a href="javascript:void(0)" onclick="coolweather_set_weather(2, 0);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['daytime'],
            '</a> / <a href="javascript:void(0)" onclick="coolweather_set_weather(2, 1);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['nighttime'], '</a><br />', ,
            ''].join('');
    }
    else {
        if (metric == 1) {
            var windforce = [coolweather_lang[coolweather_nav_lng]['windforce'],
            coolweather_get_windforce(weather[number][day_or_night]['windspeed']), ' ',
            coolweather_lang[coolweather_nav_lng]['scale'], '<br />'].join('');
        }
        else {
            var windforce = '';
        }
        coolweather_hint.innerHTML = ['<div style="margin-bottom: 0.5em; display: block;"><strong>',
            ((number == 1) ? coolweather_lang[coolweather_nav_lng]['today'] : coolweather_lang[coolweather_nav_lng]['tomorrow']),
            ((day_or_night == 0) ? coolweather_lang[coolweather_nav_lng]['daytime'] : coolweather_lang[coolweather_nav_lng]['nighttime']),
            '</strong><br /><br />',
            '<img src="', coolweather_iconspath,
            weather[number][day_or_night]['weathericon'], '.gif" alt="',
            weather[number][day_or_night]['weathertext'],
            '" align="left" valign="absmiddle" width="64" height="40" style="margin-right: 0.5em" />',
            '<a href="javascript:void(0)" onclick="coolweather_input_city();" title="',
            coolweather_lang[coolweather_nav_lng]['changecity'],
            '" style="display: inline; padding: 0; margin: 0">',
            weather['city'], '</a><br />', weather['state'],
            '</div><br />', weather[number][day_or_night]['weathertext'], '<br />',
            coolweather_lang[coolweather_nav_lng]['hightemperature'],
            weather[number][day_or_night]['hightemperature'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['lowtemperature'],
            weather[number][day_or_night]['lowtemperature'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['realfeelhigh'],
            weather[number][day_or_night]['realfeelhigh'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['realfeellow'],
            weather[number][day_or_night]['realfeellow'], coolweather_unit[metric]['temp'], '<br />',
            coolweather_lang[coolweather_nav_lng]['windspeed'],
            weather[number][day_or_night]['windspeed'], coolweather_unit[metric]['speed'], '<br />',
            windforce,
            coolweather_lang[coolweather_nav_lng]['winddirection'],
            coolweather_lang[coolweather_nav_lng][weather[number][day_or_night]['winddirection']], '<br />',
            '<a href="javascript:void(0)" onclick="coolweather_set_weather(0);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['currentconditions'], '</a><br />',
            coolweather_lang[coolweather_nav_lng]['today'],
            '<a href="javascript:void(0)" onclick="coolweather_set_weather(1, 0);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['daytime'],
            '</a> / <a href="javascript:void(0)" onclick="coolweather_set_weather(1, 1);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['nighttime'], '</a><br />',
            coolweather_lang[coolweather_nav_lng]['tomorrow'],
            '<a href="javascript:void(0)" onclick="coolweather_set_weather(2, 0);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['daytime'],
            '</a> / <a href="javascript:void(0)" onclick="coolweather_set_weather(2, 1);" ',
            'style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['nighttime'], '</a><br />'].join('');
    }
}

coolweather_rpc.get_weather_callback = function (result, args) {
    if (result instanceof PHPRPC_Error) {
        var coolweather_hint = document.getElementById('coolweather_hint');
        coolweather_hint.innerHTML = ['<img src="', coolweather_iconspath, 'swa.gif" alt="error" align="left" width="64" height="40" /><span style="color: red">', result.toString(), '</span>'].join('');
    }
    else {
        if (result == null) {
            result = [];
        }
        result['locid'] = args[0];
        result['metric'] = args[1];
        var weather = PHPSerializer.serialize(result);
        set_cookie('coolweather_metric', args[1]);
        set_cookie('coolweather_weather', weather, 900000);
        coolweather_report = weather;
        coolweather_set_weather(0);
    }
}

coolweather_rpc.get_location_callback = function (result) {
//alert(result[0]['location']);
    var metric = get_cookie('coolweather_metric');
    if (metric == null) metric = 1;
    var coolweather_hint = document.getElementById('coolweather_hint');
    var coolweather_input = document.getElementById('coolweather_input');
    if (result instanceof PHPRPC_Error) {
        coolweather_hint.innerHTML = ['<span style="color: red">', result.toString(), '</span>'].join('');
        coolweather_input.disabled = false;
    }
    else if (result.length == 0) {
        coolweather_hint.innerHTML = coolweather_lang[coolweather_nav_lng]['nocitymatched'];
        coolweather_input.disabled = false;
        coolweather_input.focus();
    }
    else if (result.length == 1) {
        set_cookie('coolweather_locid', result[0]['location']);
        coolweather_get_weather(result[0]['location'], metric);
    }
    else {
        coolweather_hint.innerHTML = [coolweather_lang[coolweather_nav_lng]['morecitymatched'],
            '<br /><div align="right"><a href="javascript:void(0)" onclick="coolweather_input_city()" style="display: inline; padding: 0; margin: 0">',
            coolweather_lang[coolweather_nav_lng]['back'], '</a></div>'].join('');
        var coolweather_input_container = document.getElementById('coolweather_input_container');
        coolweather_input_container.innerHTML = ['<label for="coolweather_input">', coolweather_lang[coolweather_nav_lng]['location'], '</label><br />'].join('');
        coolweather_input = document.createElement('select');
        var opt = document.createElement('option');
        opt.text = coolweather_lang[coolweather_nav_lng]['choosecity'];
        opt.value = '';
        opt.defaultSelected = true;        if (coolweather_input.options.add) {
            coolweather_input.options.add(opt);
        }
        else {
            coolweather_input.appendChild(opt);
        }
        for (var i = 0; i < result.length; i++) {
            var opt = document.createElement('option');
            opt.text = [result[i]['city'], result[i]['state']].join(', ');
            opt.value = result[i]['location'];
            if (coolweather_input.options.add) {
                coolweather_input.options.add(opt);
            }
            else {
                coolweather_input.appendChild(opt);
            }
        }
        coolweather_input.id = "coolweather_input";
        coolweather_input.onchange = function () {
            var locid = this.value;
            set_cookie('coolweather_locid', locid);
            coolweather_get_weather(locid, metric);
        }
        coolweather_input_container.appendChild(coolweather_input);
        coolweather_input.focus();
    }
}

function on_coolweather_city_input(event) {
    if (window.event) event = window.event;
    if (event.keyCode == 13) {
        var city = this.value;
        var coolweather_hint = document.getElementById('coolweather_hint');
        coolweather_hint.innerHTML = coolweather_lang[coolweather_nav_lng]['loading'];
        var coolweather_input = document.getElementById('coolweather_input');
        coolweather_input.disabled = true;
        coolweather_get_location(city);
    }
}

function coolweather_get_location(city) {
    coolweather_rpc.get_location(city);
}

function coolweather_get_weather(locid, metric) {
    var coolweather_container = document.getElementById('coolweather_container');
    coolweather_container.innerHTML = ['<div id="coolweather_hint">', coolweather_lang[coolweather_nav_lng]['loading'], '</div>'].join('');
    coolweather_rpc.get_weather(locid, metric);
}

function coolweather_input_city() {
    delete_cookie('coolweather_locid');
    coolweather_init();
}

function coolweather_init() {
    var coolweather_container = document.getElementById('coolweather_container');
    var locid = get_cookie('coolweather_locid');
    if (locid == null) {
        coolweather_container.innerHTML = ['<div id="coolweather_input_container">',
            '<label for="coolweather_input">',
            coolweather_lang[coolweather_nav_lng]['city'],
            '</label><br /><input type="text" id="coolweather_input" /></div><div id="coolweather_hint">',
            coolweather_lang[coolweather_nav_lng]['firstseehint'], '</div>'].join('');
        var coolweather_input = document.getElementById('coolweather_input');
        coolweather_input.onkeypress = on_coolweather_city_input;
        coolweather_input.focus();
    }
    else {
        coolweather_container.innerHTML = ['<div id="coolweather_hint">', coolweather_lang[coolweather_nav_lng]['loading'], '</div>'].join('');
        var metric = get_cookie('coolweather_metric');
        if (metric == null) metric = 1;
        if (get_cookie('coolweather_weather') == null) {
            coolweather_get_weather(locid, metric);
        }
        else {
            coolweather_report = get_cookie('coolweather_weather');
            coolweather_set_weather(0);
        }
    }
}
