function supportsSvg() {
	if($.browser.msie)
		return false;
	return document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Shape", "1.0") || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1");
}

var flashSwitcher = {
	flash: 0,
	switcher: null,
	on: null,
	off: null,
	iniziato: false,

	init: function (options) {
		flashSwitcher.iniziato = true;

		for(i in options)
			flashSwitcher[i] = options[i];

		flashSwitcher.switcher = $("#bt_switch");
		flashSwitcher.on = $("#bt_switch > #on");
		flashSwitcher.off = $("#bt_switch > #off");

		if(flashSwitcher.flash == 1)
			flashSwitcher.switcher.css("background-image", "url(http://www.centrometeoitaliano.it/cartine/simboli/bottoni/on.png)");
		flashSwitcher.on.click(flashSwitcher.turnOn);
		flashSwitcher.off.click(flashSwitcher.turnOff);
	},
	turnOn: function () {
		if(flashSwitcher.flash == 1) return;
		flashSwitcher.flash = 1;
		flashSwitcher.on.hide();
		flashSwitcher.off.show();
		$.cookie("CMI-flash", "1", { path: '/' });
		var d = parseInt(intMap.day);
		var m = parseInt(intMap.month);
		if(d < 10)		d = '0'+d;
		if(m < 10)		m = '0'+m;

		var swfUrl;
		if(intMap.map == 'Italia' || intMap.map == 'Webcam')
			swfUrl = 'http://www.centrometeoitaliano.it/flash/loader.swf?giorno='+d+'&mese='+m;
		else
			swfUrl = 'http://www.centrometeoitaliano.it/flash/'+intMap.map.toLowerCase()+'.swf?giorno='+d+'&mese='+m;
		flashSwitcher.switcher.css("background-image", "url(http://www.centrometeoitaliano.it/cartine/simboli/bottoni/on.png)");

		$(".bt_blu, .bt_rosso, #bt_legenda, #svgload, #map, #top-container, #img-loader, #bt_flash").fadeOut(500, function () {
			$(".bt_blu, .bt_rosso, #bt_legenda, #svgload, #map, #top-container, #bt_flash").hide();
			$("#flash-container").fadeIn(1000, function () {$("#flash-container").show() });
				$("#flash-container").flash({
						swf: swfUrl,
						width: 642,
						height: 626,
						play: false,
						flashvars: {
							'giorno': d,
							'mese': m
						}
					}
				);

		});
	},

	turnOff: function () {
		if(flashSwitcher.flash == 0) return;
		flashSwitcher.flash = 0;
		flashSwitcher.off.hide();
		flashSwitcher.on.show();
		$.cookie("CMI-flash", "0", { path: '/' });
		flashSwitcher.switcher.css("background-image", "url(http://www.centrometeoitaliano.it/cartine/simboli/bottoni/off.png)");

		$("#flash-container").fadeOut(500, function () {
			$(".bt_blu, .bt_rosso, #bt_legenda, #svgload, #map, #top-container, #img-loader, #bt_flash").fadeIn(1000);
			intMap.loadMap();
		});

	}

};

var intMapSVG = {
	div: null,
	svg: null,
	map: 'Italia',
	day: 1,
	month: 1,
	section: 1,
	temp: 0,
	parent: null,
	init: function(options) {
		var d = new Date();
		this.day = d.getDate();
		this.month = d.getMonth()+1;

		if((d.getHours() >= 0 && d.getHours() < 12))
			this.section = 1;
		else if(d.getHours() >= 12 && d.getHours() < 18)
			this.section = 2;
		else if(d.getHours() >= 18 && d.getHours() < 24)
			this.section = 3;

		this.parent = $('#container');

		for(i in options)
			this[i] = options[i];

		this.div = $('#svgload', this.parent);

		if (this.section == 1)
			$('#bt_mattina', this.parent).addClass("over");
		else if (this.section == 2)
			$('#bt_pomeriggio', this.parent).addClass("over");
		else if (this.section == 3)
			$('#bt_sera', this.parent).addClass("over");
		else if (this.section == 4)
			$('#bt_notte', this.parent).addClass("over");

	 $('#map', this.parent).remove();
		
		this.div.css('background-image', 'url('+this.mapUrl().map+')');
		this.loadSvg();
		if($.cookie("CMI-flash") == 1 && this.temp == 0 && flashSwitcher.iniziato == true) {
			flashSwitcher.turnOn();
		}	else
			this.loadMap();
	},
	
	changeDay: function (d, m) {
		this.day = d;
		this.month = m;
		this.temp = 0;
		this.section = 1;
		this.changeHover($("#bt_mattina", this.parent));
		this.loadMap();
	},
	changeSection: function (obj,sec) {
		this.changeHover(obj);
		this.temp = 0;
		this.section = sec;
		this.loadMap();
	},
	changeMap: function (obj,map) {
		this.changeHover(obj);
		this.map = map;
		this.temp = 0;
		this.loadMap();
	},
	changeHover: function (obj) {
		var cl = $(obj).attr("type");
		$("*[type="+cl+"]", this.parent).removeClass("over");
		$(obj).addClass("over");
	},
	changeTemp: function (obj,temp) {
		$("*[type=map]", this.parent).removeClass("over").hide();
		$("*[type=section]", this.parent).removeClass("over").hide();
		$("#bt_meteo", this.parent).show();
		this.changeHover(obj);
		if(temp == 1)
			this.section = 3;
		if(temp == 2)
			this.section = 1;
		this.temp = temp;
		if(this.map.indexOf('regione') < 0)
			this.map = 'Italia';
		this.loadMap();
	},
	changeWebcam: function (obj) {
		$("*[type=map]", this.parent).removeClass("over").hide();
		$("*[type=section]", this.parent).removeClass("over").hide();
		$("#bt_meteo", this.parent).show();
		this.changeHover(obj);
		this.map = 'Webcam';
		this.loadMap();
	},
	comeMap: function () {
		this.temp = 0;
		this.section = 1;
		this.map = 'Italia';
		$("#bt_mattina, #bt_italia", this.parent).addClass("over");
		$("*[type=map]", this.parent).show();
		$("*[type=section]", this.parent).show();
		$("*[type=temp]", this.parent).removeClass("over");
		this.loadMap();
	},
	loadMap: function () {
		var t = this.div;
		var i = $('#img-loader', this.parent);
		var urls = this.mapUrl();
		var $this = this;
		if(i.attr('src') == urls.map)
			return;
		var $load = $("#loaderbar", this.parent).show();
			i.attr('src', urls.map).show()
				.load(function(){
					$load.hide();
					t.empty().fadeOut(function () {
						t.css('background-image', 'url('+urls.map+')').show();
						i.hide().unbind('load');
						$this.loadSvg();
					});
				});
	},
	loadSvg: function () {
		if(this.map.indexOf('regione') > -1)
			return;
		var $this = this;
    $.ajax({
        url:this.mapUrl().svg,
        dataType:"text",
        success:function(data){
            var nodes = new DOMParser().parseFromString(data,'text/xml').documentElement;
            $this.div.empty().append(document.adoptNode(nodes,true));
						$this.mapState();
        }
    });
	},
	mapState: function() {
		var $this = this;
		$("g", this.parent).each(function(item,value) {
			$(value).attr("style", "cursor: pointer");
			if($this.map == 'Webcam')
				$(value).click(function(){
					location.href = 'http://www.centrometeoitaliano.it/webcam/?chiave_webcam_reg='+$(this).attr('title')+'&idreg='+$(this).attr('id-regione');
				});
			else if($this.temp > 0)
				$(value).click(function(){
					location.href = 'http://www.centrometeoitaliano.it/temperature/?ref='+$(this).attr('id-regione');
				});
			else if($this.map == 'europa')
				$(value).click(function(){
					location.href = 'http://www.centrometeoitaliano.it/previsioni-meteo-mondo/?idmondo='+$(this).attr('id-mondo');
				});
			else
				$(value).click(function(){
					location.href = 'http://www.centrometeoitaliano.it/meteo-regione/?ref='+$(this).attr('id-regione');
				});
			$(value).hover(
				function(){
					$(this).attr('fill-opacity', 0.2);
				},
				function(){
					$(this).attr('fill-opacity', 0);
				}
			);
		});
	},
	mapUrl: function () {
		var urls = {};
		var d;
		urls.svg = 'http://www.centrometeoitaliano.it/cartine/mappe/svg/'+this.map+'.svg';

		if(this.temp == 1)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/minime/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else if(this.temp == 2)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/massime/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else if(this.section == 5)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/media/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else
			urls.map =  'http://www.centrometeoitaliano.it/cartine/meteo-'+this.section+'/'+this.map+'_'+this.day+'-'+this.month+'.png';
		return urls;
	}
}

var intMapIE = {
	img: null,
	area: null,
	map: 'Italia',
	day: 19,
	month: 4,
	section: 1,
	temp: 0,
	parent: null,
	init: function(options) {
		var d = new Date();
		this.day = d.getDate();
		this.month = d.getMonth()+1;

		if((d.getHours() >= 0 && d.getHours() < 12))
			this.section = 1;
		else if(d.getHours() >= 12 && d.getHours() < 18)
			this.section = 2;
		else if(d.getHours() >= 18 && d.getHours() < 24)
			this.section = 3;

		this.parent = $('#container');

		for(i in options)
			this[i] = options[i];

		if (this.section == 1)
			$('#bt_mattina',this.parent).addClass("over");
		else if (this.section == 2)
			$('#bt_pomeriggio',this.parent).addClass("over");
		else if (this.section == 3)
			$('#bt_sera',this.parent).addClass("over");
		else if (this.section == 4)
			$('#bt_notte',this.parent).addClass("over");

		this.img = $('#map',this.parent);
		this.area = $('#mappatura',this.parent);
	 $('#svgload', this.parent).remove();

		if($.cookie("CMI-flash") == 1 && this.temp == 0 && flashSwitcher.iniziato == true)
			flashSwitcher.turnOn();			
		else
			this.loadMap();
	},
	changeDay: function (d, m) {
		this.day = d;
		this.month = m;
		this.temp = 0;
		this.section = 1;
		this.changeHover($("#bt_mattina",this.parent));
		this.loadMap();
	},
	changeSection: function (obj,sec) {
		this.changeHover(obj);
		this.temp = 0;
		this.section = sec;
		this.loadMap();
	},
	changeMap: function (obj,map) {
		this.changeHover(obj);
		this.map = map;
		this.temp = 0;
		this.loadMap();
	},
	changeHover: function (obj) {
		var cl = $(obj).attr("type");
		$("div[type="+cl+"]",this.parent).removeClass("over");
		$(obj).addClass("over");
	},
	changeTemp: function (obj,temp) {
		$("div[type=map]",this.parent).removeClass("over").hide();
		$("div[type=section]",this.parent).removeClass("over").hide();
		$("#bt_meteo",this.parent).show();
		this.changeHover(obj);
		if(temp == 1)
			this.section = 3;
		if(temp == 2)
			this.section = 1;
		this.temp = temp;
		if(this.map.indexOf('regione') < 0)
			this.map = 'Italia';
		this.loadMap();
	},
	changeWebcam: function (obj) {
		$("div[type=map]",this.parent).removeClass("over").hide();
		$("div[type=section]",this.parent).removeClass("over").hide();
		$("#bt_meteo",this.parent).show();
		this.changeHover(obj);
		this.map = 'Webcam';
		this.loadMap();
	},
	comeMap: function () {
		this.temp = 0;
		this.section = 1;
		this.map = 'Italia';
		$("#bt_mattina",this.parent).addClass("over");
		$("div[type=map]",this.parent).show();
		$("div[type=section]",this.parent).show();
		$("div[type=temp]",this.parent).removeClass("over");
		this.loadMap();
	},
	loadMap: function () {
		var t = this.img;
		var i = $('#img-loader',this.parent);
		var urls = this.mapUrl();
		var $this = this;
		if(i.attr('src') == urls.map)
			return;
		var $l = $("#loaderbar").show();
			i.attr('src', urls.map).show()
				.load(function(){
					$l.hide();
					$this.area.empty();
					var $tthis = $this;
					t.fadeOut(function () {
						t.attr('src', urls.map).show();
						i.hide().unbind('load');
						$tthis.loadArea();
					});
				});
	},
	loadArea: function () {
		if(this.map.indexOf('regione') > -1)
			return;
		$('#mappatura', this.parent).load(this.mapUrl().area);

/*,
        dataType:"text",
        success:function(data){
					try {
					$(data).appendTo($this.area);
					console.log($this.area.parent().html());
					} catch(e) { console.log(e.message); }
//            $this.area.append(data);
        }
    });*/
	},
	mapUrl: function () {
		var urls = {};
		var d;
		if(this.temp > 0)
			urls.area = 'http://www.centrometeoitaliano.it/cartine/mappe/area/Temp.php?g='+this.day+'&m='+this.month;
		else
			urls.area = 'http://www.centrometeoitaliano.it/cartine/mappe/area/'+this.map+'.map';
		switch(this.section) {
			case 1:
			case 2:
				d = 'diurno';
				break;
			case 3:
			case 4:
				d = 'notturno';
		}
		if(this.temp == 1)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/minime/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else if(this.temp == 2)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/massime/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else if(this.section == 5)
			urls.map = 'http://www.centrometeoitaliano.it/cartine/media/'+this.map+'_'+this.day+'-'+this.month+'.png';
		else
			urls.map = 'http://www.centrometeoitaliano.it/cartine/meteo-'+this.section+'/'+this.map+'_'+this.day+'-'+this.month+'.png';
		return urls;
	}
}

