// WeatherWidget.js

var keyTimer;
var mouseTimer;
var varPath = '/ca/';
var language = 'fr';

function LoadWeather(cityInfo, lang) {
    
    language = lang == 'FRC' ? 'fr' : 'en';
    sendAjaxRequest('weather-widget', varPath+'php/weather-widget.php', 'lang='+language + '&' + (null == cityInfo ? '' : 'cityinfo='+cityInfo), LoadWeather_Complete);
}
function LoadWeather_Complete(response) {
    
    $('weather-widget').innerHTML = response;
    $('weather-city-search').removeEvents();
    $('weather-city-search').addEvent('mouseup', function() {
        if ('Enter Location' == $('weather-city-search').value || "Entrer l'emplacement" == $('weather-city-search').value) {
            $('weather-city-search').value = '';
            $('weather-city-search').focus = true;
        }
    });
    $('weather-city-search').addEvent('keyup', function() {
        if (null != keyTimer) { clearTimeout(keyTimer); }
        keyTimer = setTimeout('showCitySearch("'+language+'")', 500);
    });
    $('weather-city-list').removeEvents();
    $('weather-city-list').addEvent('mouseout', function() {
        mouseTimer = setTimeout('hideCitySearch()', 1000);
    });
    $('weather-city-list').addEvent('mouseover', function() {
        clearTimeout(mouseTimer); 
    });
    
    if (navigator.userAgent.match('MSIE 6')) {
        promoImageUrl = $('weather-promo-image').src;
        $('weather-promo-image').src = varPath+'images/weather/blank.gif';
        $('weather-promo-image').style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + promoImageUrl + '")';
    }
}
function showCitySearch(lang) {
    $('weather-city-search-btn').innerHTML = '<img src="'+varPath+'images/weather/weather-search-close.gif" alt="x" />';
    $('weather-city-search-btn').onclick = function(){ return hideCitySearch(); }
    
    var city = $('weather-city-search').value.clean().replace(/[,"\?&].*$/g, '');
    city = encodeURI(city).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\'/g, '%27');
    sendAjaxRequest('weather-city-list', varPath+'php/weather-cities.php', 'lang='+lang + '&city=' + city, showCitySearch_Complete);
    
    return false;
}
function showCitySearch_Complete(response) {
    
    $('weather-city-list').innerHTML = response;
    $('weather-city-list').getElements('a').addEvent('mouseover', function() {
        clearTimeout(mouseTimer); 
    });
}
function hideCitySearch() {
    
    $('weather-city-search-btn').innerHTML = '<img src="'+varPath+'images/weather/weather-search-go.gif" alt="&raquo;" />';
    $('weather-city-search-btn').onclick = function(){ return showCitySearch(language); }
    $('weather-city-list').style.display = 'none';
    
    return false;
}
function showCityDetails(cityCode, isZipCode) {
    divId = 'cityDetails' + cityCode;
    sendAjaxRequest(divId, varPath+'php/weather-stations.php', 'citycode='+cityCode + '&iszipcode='+isZipCode, function(response) { $(divId).innerHTML = response; });
}
function sendAjaxRequest(divId, url, query, onComplete) {
    var loaderName = 'weather-widget' == divId ? 'weather' : 'ajax';
    $(divId).innerHTML = '<img src="'+varPath+'images/weather/'+loaderName+'-loader.gif" alt="loading" />';
    $(divId).style.display = 'block';
    
    var request = new Request({
        method: 'get',
        url: url,
        data: query + '&' + Math.random(),
        noCache: true,
        onComplete: onComplete
    });
    
    request.send();
}
