// JavaScript Document
var theStation = '';
$(function() {
    $('select.locality').hide().addClass('noLeftMargin');
    $('select.province').addClass('bottomMargin');
    $('#forecastWrap').show();
    $('#select_pro').change(function() {
        $('select.locality').hide();
        $('#select_' + $(this).val()).show();
    });
    $("select.locality").change(function() {
        var theAjaxURL = "/handlers/Weather.ashx?type=threeday&loc=" + $(this).val();

        var theStation = getStation($(this));
        if ($("#ajaxLoadingCue").length == 0) {
            $('#forecastWrap').before('<div style="position: relative; " class="layout con_04"><div class="layout col_04" id="ajaxLoadingCue" style="height: 200px; display: none; left:0;">&nbsp;</div></div>');
        }
        $('#ajaxLoadingCue').height($("#forecastWrap").height());
        $('#ajaxLoadingCue').fadeIn('fast', function() {
            $.ajax({
                url: theAjaxURL,
                dataType: 'html',
                success: function(data, textStatus, XMLHttpRequest) {
                    $("#forecastWrap").html('').append(data);
                    $('#ajaxLoadingCue').fadeOut('slow');
                    $('#ww_forecast_title').append(' - <a class="rrWeatherChange" href="#">(change)</a>');
                }
            });
        });
        addToCookie();
        $('#weatherSelectForm').hide().removeClass('con_08').addClass('leftMargin');
    });
    loadValues();
    $('a.rrWeatherChange').live('click', function() {
        $('#weatherSelectForm').show();
        return false;
    });
});
function addToCookie()
{
	var hoursToLive = 24*7*2;
	var selectPro = $('#select_pro');
	var selectLoc = $('#select_' + selectPro.val());
	$.cookies.set('wPro', selectPro.val(), {hoursToLive:hoursToLive});
	$.cookies.set('wLocCode', selectLoc.val(), {hoursToLive:hoursToLive});
	$.cookies.set('wStation', getStation(selectLoc), {hoursToLive:hoursToLive});
}
function loadValues()
{
	if($.cookies.get('wLocCode') && $.cookies.get('wPro')){
		//alert('wLocCode): '+$.cookies.get('wLocCode')+'; wPro: '+$.cookies.get('wPro'));
		$('#select_pro').val($.cookies.get('wPro')).change();
		$('#select_'+$('#select_pro').val()).val($.cookies.get('wLocCode')).change();
	}
	else{
		$('option[value='+defaultLoc+']').parent().val(defaultLoc).change();
		//alert($('option[value='+defaultLoc+']').parent().val());
		$('select.locality').hide();
		$('#select_pro').val('xx').change();
	}
}
function getStation(locSelect)
{
	var theStation = '';
	var theOption = $("option[value="+locSelect.val()+"]");
	if(theOption.hasClass('gBC')){
		theStation='gBC';
	} else if(theOption.hasClass('gOK')){
		theStation='gOK';
	} else if(theOption.hasClass('gLB')){
		theStation='gLB';
	} else if(theOption.hasClass('gCAL')){
		theStation='gCAL';
	} else if(theOption.hasClass('gED')){
		theStation='gED';
	} else if(theOption.hasClass('gRE')){
		theStation='gRE';
	} else if(theOption.hasClass('gSA')){
		theStation='gSA';
	} else if(theOption.hasClass('gTO')){
		theStation='gTO';
	} else if(theOption.hasClass('gWI')){
		theStation='gWI';
	} else if(theOption.hasClass('gMA')){
		theStation='gMA';
	} else if(theOption.hasClass('gMA')){
		theStation='gMA';
	} else if(theOption.hasClass('gMA')){
		theStation='gMA';
	} else if(theOption.hasClass('gMA')){
		theStation='gMA';
	} else if(theOption.hasClass('gMO')){
		theStation='gMO';
	} else {
		theStation='none';
	}
	return theStation;
}
