var Map = {

    profile: null,
    time: null,
    today: null,
    place: null,
    first: null,
    theday: null,
    showPoints: null,
    
    init: function(time){
		Map.setTime(time);
		Map.showPoints = true;
    },
    
    show: function(){
		$('#map_image').attr('src', 'http://www.swz.at/maps/' + this.place + '/' + this.profile + '-' + this.time + '.png');    
		setTimeout("Map.showMappoints()",1000);
	},
	
	showMappoints: function() {
		if(Map.showPoints && $('#map_image').attr('src') != 'http://www.swz.at/assets/images/mapNotFound.png' && $('#map_image').attr('src') != 'http://www.swz.at/assets/images/loadingMaps.png' &&
			Map.showPoints && $('#map_image').attr('src') != '/assets/images/mapNotFound.png' && $('#map_image').attr('src') != '/assets/images/loadingMaps.png') {
			$('#mappoints').show();
		}
	},
    
    preloadImages: function(time){
		
		$('#map_image').attr('src', 'http://www.swz.at/assets/images/loadingMaps.png');
		
		$('#mappoints').hide();
        time = parseInt(time, 10);
        
        this.first = true;
        var images = new Array()
        
        for (var i = 0; i < 8; i++) {
            tmptime = (i * 3600 * 3) + time;
            //for (var j = 1; j <= 1; j++) {
                images.push(this.profile + '-' + tmptime);
           
			//}
        }
        
        
        
        $.preload(images, {
	    base: '/maps/'+this.place+'/',
            ext: '.png',
            onFinish: Map.finish,
            onComplete: Map.loaded
        });
        
        $('#time_' + this.time).attr('class', 'time active');
        
    },
    
    loaded: function(e){
        if (!e.found && this.first) {
            this.first = null;
            Map.error();
        }
    },
    
    finish: function(e){
		Map.show();
    },
    
    error: function(e){
		Map.showPoints = false;
        $('#map_image').attr('src', 'http://www.swz.at/assets/images/mapNotFound.png');
        $('#mappoints').hide();
    },
	
	showThumbs: function()  {
		if (this.place) {
			$('#thumb_d0').attr('src', 'http://www.swz.at/maps/' + this.place + '/thumb-' + this.profile + '-' + (this.today + 3600 * 12) + '.png');
			$('#thumb_d1').attr('src', 'http://www.swz.at/maps/' + this.place + '/thumb-' + this.profile + '-' + (this.today + 3600 * 12 * 3) + '.png');
			$('#thumb_d2').attr('src', 'http://www.swz.at/maps/' + this.place + '/thumb-' + this.profile + '-' + (this.today + 3600 * 12 * 5) + '.png');
		}
	},
    
    setPlace: function(p){
        this.place = p;
		this.showThumbs();
		this.preloadImages(this.theday);
    },
    
    setProfile: function(p){
        this.profile = p;
		this.showThumbs();
		if (this.place) {
			this.preloadImages(this.theday);
		}
		
		var link;
		$('.map_url').each(function() {
			if (this.href.indexOf('?p=') <= 0) {
				if (p != 2) {
					link = this.href + '?p=' + p;
				}
			}
			else {
				if (p != 2) {
					link = this.href.replace('?p=1', '?p=' + p);
					link = link.replace('?p=2', '?p=' + p);
					link = link.replace('?p=3', '?p=' + p);
					link = link.replace('?p=4', '?p=' + p);
				} else {
					link = this.href.substr(0,this.href.indexOf('?p='));
				}
			}
			$(this).attr('href', link);
		});
    },
    
    setTime: function(t){
        this.time = t;
    },
	
	setToday: function(t){
        this.today = t;
    },

	setTheDay: function(t){
		this.theday = t;
	}
}


$(document).ready(function(){
	
	if(document.getElementById('map_image') != null) {
		document.getElementById('map_image').onerror=function (evt) { Map.error(); }
	}

    $('.change_profile').mouseover(function(){
        $('#profile_title').html($(this).children().attr('alt'));
    });
    $('.change_profile').mouseout(function(){
        $('#profile_title').html('&nbsp;');
    });
    $('.change_profile').click(function(){
        $('#profile').html($(this).children().attr('alt'));
        Map.setProfile($(this).attr('id').replace('profile_',''));
    });
    $('.time').mouseover(function(){
        Map.setTime($(this).attr('id').replace('time_',''));
        $('.time').attr('class', 'time');
        $(this).attr('class', 'time active');
        Map.show();
    });
    $('.tab li').click(function(){
        $('.tab li').removeAttr('class');
        $(this).attr('class', 'active');
        var id = $(this).attr('id').replace('tab', 'details');
        $('.details').hide();
        $('#' + id).show();
    });
    
    $('.windtype td').click(function(){
        $('.windtype td').removeAttr('class');
        $(this).attr('class', 'active');
  		var id = $(this).attr('id').replace('windtype', '');
		$('.stations').hide();
		$('#type_'+id).show();    
    });
	$('.change_type').change(function() {
		
		var index = $(this).get(0).selectedIndex;
		$('.change_type').each(function() {
			this.selectedIndex = index;
		});
		
		$('.kn').hide();
		$('.bf').hide();
		$('.kmh').hide();
		$('.'+$(this).val()).show();
	});
    
    
    $('#show_windalert').click(function(){
        if ($(this).attr('href') != '') {
            return true;
        }
        else {
            $('.partial').hide();
            $('#windalert').show();
            return false;
        }
    });
    $('#show_uwz').click(function(){
        if ($(this).attr('href') != '') {
            return true;
        }
        else {
            $('.partial').hide();
            $('#uwz').show();
            return false;
        }
    });
    $('#show_meteo').click(function(){
        if ($(this).attr('href') != '') {
            return true;
        }
        else {
            $('.partial').hide();
            $('#meteo').show();
            return false;
        }
    });
    $('.show_details').click(function(){
        $('.partial').hide();
        $('#details').show();
    });
    
    $('.country').change(function(){
        $('.waters').hide();
        $('#nowaters').hide();
        if ($('#waters' + ($(this).val())).attr('id') != undefined) {
            $('#waters' + ($(this).val())).show();
        }
        else {
            $('#nowaters').show();
        }
    });

	$('.widget').change(function() {
		if($(this).val() != 0 && $('#terms:checked').val() == 1) {		
			showWidget($(this).val());
		}
	});

	function showWidget(value) {
		var data = value.split("/");
		

		$('#widget_code img').attr('src','/widget/'+data[1]);
		$('#widget_code textarea').val('<a href="http://www.swz.at/'+data[0]+'/'+data[1]+'/" title="Segelwetter: '+data[2]+'"><img border="0" src="http://www.swz.at/widget/'+data[1]+'" alt="Segelwetter: '+data[2]+'" /></a>');
		$('#widget_code').show();

	}
	
	$('#terms').change(function() {
		if($('#terms:checked').val() != 1) {
			$('#widget_code').hide();
		}		

		if($('.widget:visible').val() != undefined && $('.widget:visible').val() != 0 && $('#terms:checked').val() == 1) {
			showWidget($('.widget:visible').val());
		}
	});

	

    $('.ucountry').change(function(){
        $('.waters').hide();
        $('#unowaters').hide();
        if ($('#uwaters' + ($(this).val())).attr('id') != undefined) {
            $('#uwaters' + ($(this).val())).show();
        }
        else {
            $('#unowaters').show();
        }
    });
    
    $('#toogle_points').click(function(){
        if ($('#mappoints').css('display') == 'none') {
            $('#mappoints').show();
			Map.showPoints = true;
        }
        else {
            $('#mappoints').hide();
        	Map.showPoints = false;
		}
    });
    
    $('#uwz_start').change(function(){
        $('.ends').attr('disabled', 'disabled');
        var id = $(this).get(0).selectedIndex;
        $('.ends').attr('selected', 'false');
        $('#end' + id).removeAttr('disabled');
        $('#end' + (id + 1)).removeAttr('disabled');
        $('#end' + (id + 2)).removeAttr('disabled');
        $('#end' + (id + 3)).removeAttr('disabled');
        $('#end' + id).attr('selected', 'selected');
    });
    
    $('#sm_windalert').click(function(){
        var country = parseInt($('#country').val(), 10);
        if (country == 0) {
            var water = 0;
        }
        else {
            var water = parseInt($('#waters' + country).val(), 10);
        }
        var email = $('#email').val();
        if ($('#agb:checked').val() != 1) {
            var agb = 0;
        }
        else {
            var agb = 1;
        }

		var from = $('#windalert_from').val();
		var to = $('#windalert_to').val();
        
        if (country == 0 || water == 0 || agb == 0 || email.length < 5) {
            alert('Formular unvollständig');
            return false;
        }
        else {
            return true;
        }
        
    });
	
	$('#sm_uwz').click(function(){
        var country = parseInt($('#ucountry').val(), 10);
        if (country == 0) {
            var water = 0;
        }
        else {
            var water = parseInt($('#uwaters' + country).val(), 10);
        }

        var email = $('#uemail').val();
		var phone = $('#uphone').val();
        if ($('#uagb:checked').val() != 1) {
            var agb = 0;
        }
        else {
            var agb = 1;
        }
        
        if (country == 0 || water == 0 || agb == 0 || phone.length < 5) {
            alert('Formular unvollständig');
            return false;
        } else if(/^\+\d+$/.test(phone) == false) {
			alert('Ungültiges Format der Telefonummer');
            return false;
		}
        else {
			return true;
        }
        
    });
    
});

