// JavaScript Document 
var theStation = '';
//if(typeof(defaultLoc) == "undefined"){ 
//	var defaultLoc = 'CAXX0504';
//}
if(typeof(console) == "undefined"){ 
	var console = {};
}
$(function() {
    $('#weatherSelectForm').show();
    $('select.locality').hide();
    $('#select_pro').change(function() {
        $('select.locality').hide();
        $('#select_' + $(this).val()).show();
    });
    $("select.locality").change(function() {
        var theAjaxURL = "/handlers/Weather.ashx?type=fullweather&loc=" + $(this).val();
        var theStation = getStation($(this));
        theAjaxURL += '&station=' + theStation;
        if (!$("#ajaxWeather")[0]) {
            $('#ajaxLoadingCue').height(200);
        } else {
            $('#ajaxLoadingCue').height($("#ajaxWeather").height());
        }
        $('#ajaxLoadingCue').fadeIn('fast', function() {
            $.ajax({
                url: theAjaxURL,
                success: function(data, textStatus, XMLHttpRequest) {
                    $("#ajaxWeatherContainer").html('').append(data);
                    $('#ajaxLoadingCue').fadeOut('slow');
                }
            });
        });
        addToCookie($(this).val());
    });
    $('#selectAir').change(function() {
        if ($(this).val() != "xx") {
            $('#airportFrame').attr('src', $(this).val());
            var theVal = $(this).val();
            $('#selectAir option').each(function() {
                if ($(this).attr('value') == theVal) {
                    $.cookies.set('wAir', $(this).text(), { hoursToLive: 24 * 7 * 2 });
                }
            });
        }
    });
    //	$('#selectRadar').change(function(){
    //		if($(this).val() != "xx"){
    //			$('#radarFrame').attr('src', 'http://esponline.myweather.net/interactiveradar/main.html?host='+$(this).val());
    //			$.cookies.set('wRadar', $(this).val(), {hoursToLive:24*7*2});
    //		}
    //	});
    $('#selectRadar').change(function() {
        if ($(this).val() != "xx") {
            $('#radarFrame').attr('src', '/pages/iframe.aspx?display=radar&locCode=' + $(this).val());
            if ($this.val() != "") {
                $.cookies.set('wRadar', $(this).val(), { hoursToLive: 24 * 7 * 2 });
            }
        }
    });

    $('#selectSkytracker').change(function() {
        if ($(this).val() != "xx" && window.location.href.indexOf('skyLocCode=' + $(this).val()) == -1) {
            var pathname = window.location.pathname + '?skyLocCode=';
            window.location = pathname + this.value ;//+ '&display=skytracker';
            $.cookies.set('wSkytracker', $(this).val(), { hoursToLive: 24 * 7 * 2 });
        }
    });
    loadValues();
});
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')){
		$('#select_pro').val($.cookies.get('wPro')).change();
		$('#select_'+$('#select_pro').val()).val($.cookies.get('wLocCode')).change();
	}
	else{
		$('option[value='+defaultLoc+']').parent('select').val(defaultLoc).change();
		$('select.locality').hide();
		$('#select_pro').val('xx').change();
	}
	if($.cookies.get('wAir') && $('#selectAir option')[0]){
		$('#selectAir option').each(function(){
			if($(this).text() == $.cookies.get('wAir')){
				$('#selectAir').val($(this).attr('value')).change();
			}
		});
	} else {
		$('#selectAir option').each(function(){
			if($(this).attr('value') == $('#airportFrame').attr('src')){
				$('#selectAir').val($(this).attr('value'));
			}
		});
	}
	if($.cookies.get('wRadar') && $('#selectRadar')[0]){
		//alert('test');
		$('#selectRadar').val($.cookies.get('wRadar')).change();
	} else {
		$('#selectRadar option').each(function(){
			if($('#radarFrame').attr('src') == 'http://esponline.myweather.net/interactiveradar/main.html?host='+$(this).attr('value') ){
				$('#selectRadar').val($(this).attr('value'));
			}
		});
	}
	if($.cookies.get('wSkytracker') && $('#selectSkytracker')[0]){
		$('#selectSkytracker').val($.cookies.get('wSkytracker'));
	} 
}
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;
}
