var harta = Class.create();

harta.prototype = {
	initialize: function(params){

		this.params=params;
		Event.observe(
								window,
								'load',
								this.google_map.bindAsEventListener(this)
							);
		Event.observe(
								window,
								'unload',
								GUnload.bindAsEventListener(this)
							);
		this.current_lat_long={
								latitudine:"0",
								longitudine:"0"
							};

		this.overlay_markers=[];
		this.date_orase=[];
		this.first_hour=0;
		this.next_time=0;
		this.stop=false;
		this.is_last=true;
		this.command="play";
		this.timeout=0;
		this.only_crt_location=false;
		this.hours_scala=[];
		this.div_hour_on_map="";

		this.luna_scurt=[
							"Ian",
							"Feb",
							"Mar",
							"Apr",
							"Mai",
							"Iun",
							"Iul",
							"Aug",
							"Sep",
							"Oct",
							"Noi",
							"Dec"
						];
						
		this.zi_scurt=[
							"Dum",
							"Lun",
							"Mar",
							"Mie",
							"Joi",
							"Vin",
							"Sam"
						];
	},

	google_map: function() {
	  if (GBrowserIsCompatible()) {
			this.map = new GMap2($('map'));
			//var latlng = new GLatLng(45.943161,24.96676);
			
			if(this.params.type=='romania'){
				var sw = new GLatLng(43.6272964477539, 20.2699699401855);  
				var ne = new GLatLng(48.2669525146484, 29.6910572052002);  
				 
				var bounds = new GLatLngBounds(sw, ne);  
				this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds)); 
			}
			if(this.params.type=='lume'){
				var latlng = new GLatLng(this.params.lat,this.params.long);
				this.map.setCenter(latlng, 6);
			}
			if(this.params.type=='tara'){
				var sw = new GLatLng(this.params.bbs, this.params.bbw);  
				var ne = new GLatLng(this.params.bbn, this.params.bbe);  
				 
				var bounds = new GLatLngBounds(sw, ne);  
				this.map.setCenter(bounds.getCenter(), this.map.getBoundsZoomLevel(bounds)); 
			}

			this.map.setMapType(G_PHYSICAL_MAP);
			this.map.addControl(new GSmallZoomControl());
			var params_loading={
					"top":"140px",
					"left":"210px",
					"width":"32px",
					"height":"32px",
					"src":"/images/ajax-loader.gif",
					"div_id":"map",
					"id":"loading_harta"
			};

			loading(params_loading);

			this.div_hour_on_map=document.createElement("div");
				$(this.div_hour_on_map).setStyle({
												position:"absolute",
												top:"3px",
												left:"370px",
												fontWeight:"bold",
												color:"red",
												textShadow:"1px 1px 0 #C0C0C0",
												background:"white",
												padding:"0px 5px 0px 5px"
											});
			$("map").appendChild(this.div_hour_on_map);

			$('process_harta').setAttribute("src","/include/process_harta.php?id_oras="+this.params.geonameid+"&type="+this.params.type+(this.params.istoric=='off'?"&istoric=off":""),0);
		}
		
	},

	put_hour_on_map: function(txt){
		
	},

	put_oras: function(id,nume,icon,temp,url,latitudine,longitudine){
		var latlng = new GLatLng(latitudine,longitudine);
		
		var ewindow = new EWindow(this.map, E_STYLE_1); 
		this.map.addOverlay(ewindow);

		var flashIcon=document.createElement("div");
		flashIcon.setAttribute("id","icon_"+icon,0);
		var parentIcon=document.createElement("div");
		parentIcon.setAttribute("id","parent_icon_"+icon,0);
		parentIcon.appendChild(flashIcon);
		$("flash_obj").appendChild(parentIcon);

		if(this.params.type=='romania'){
			swfobject.embedSWF("/images/vremea_40x40/"+icon+".swf", "icon_"+icon, "45", "45", "9.0.0","",{},{wmode:"transparent"});
		}
		else{
			swfobject.embedSWF("/images/vremea_40x40/"+icon+".swf", "icon_"+icon, "60", "60", "9.0.0","",{},{wmode:"transparent"});

		}
		if(temp >= 0){
			var redTmp = temp>=25?"FF":this.dec2hex(temp * 4.26 + 128);
			var blueTmp = temp>=25?"00":this.dec2hex(128 - temp * 4.26);
		}
		else{
			var redTmp = temp<=25?"00":this.dec2hex(temp * 4.26 + 128);
			var blueTmp = temp<=25?"FF":this.dec2hex(128 + temp * 4.26);
		}

		var back = "#"+redTmp+"00"+blueTmp;
		
		ewindow.openOnMap(latlng, "<div id='icon_"+id+"' style='padding-left:25px;' class='vreme_iconitze'>"+$("parent_icon_"+icon).innerHTML+"</div><div "+(document.all?"class='temp_on_map_IE'":"class='temp_on_map'")+"><span class='temp_text'  style='color:"+back+";'>"+temp+"&deg;</span>, <a href=\""+url+"\" alt=\""+temp+"&deg;, "+nume+"\" title=\""+temp+"&deg;, "+nume+"\" style='text-decoration:none;'>"+nume+"</a></div>", new GPoint(-32,22));
		
		var baseIcon = new GIcon();
		baseIcon.iconSize = new GSize(16, 25);
		baseIcon.iconAnchor = new GPoint(14, 25);
		baseIcon.infoWindowAnchor = new GPoint(14, 25);
		var letteredIcon = new GIcon(baseIcon);

		if(latitudine==this.current_lat_long.latitudine && longitudine==this.current_lat_long.longitudine){
			//letteredIcon.image = (document.all?"/images/pin_vreme_crt_new.gif":"/images/pin_vreme_crt_new.png");
			letteredIcon.image = "/images/pin_vreme_crt_new.png";
		}
		else{
			//letteredIcon.image = (document.all?"/images/pin_vreme_new.gif":"/images/pin_vreme_new.png");
			letteredIcon.image = "/images/pin_vreme_new.png";
		}

		// Set up our GMarkerOptions object
		var markerOptions = { icon:letteredIcon,clickable:false };
		var marker = new GMarker(latlng,markerOptions);
		this.map.addOverlay(marker);

		this.overlay_markers[this.overlay_markers.length]={
															"ewindow":ewindow,
															"marker":marker,
															"lat":latitudine,
															"long":longitudine
															};
		
		this.show_hide_locations({
								"ewindow":ewindow,
								"marker":marker,
								"lat":latitudine,
								"long":longitudine
							});
		return false;
	},
	
	show_hide_locations:function(params){
		if(params.lat!=this.current_lat_long.latitudine && params.long!=this.current_lat_long.latitudine){
			if(this.only_crt_location){
				params.ewindow.hide();
				params.marker.hide();
			}
			else{
				params.ewindow.show();
				params.marker.show();
			}
		}
	},

	cycle_hours: function(next_time){
	
		if (!this.stop){
			var didi=0;
			this.is_last=true;
			for (var i in this.date_orase) {
				if(i==next_time){
					didi=1;
					continue;
				}
				if(didi){
					this.show_icons_at_hour(i);
					this.next_time=i;
					
					this.is_last=false;
					//$('debug').innerHTML=this.next_time;
					this.timeout=setTimeout(function(){go_harta.cycle_hours(go_harta.next_time);},2000);
					break;
				}
			}
			if(this.is_last){
			
				clearTimeout(this.timeout);
				this.show_icons_at_hour(this.first_hour);
				this.command="play";
				$('play').setAttribute("src","/images/play.gif",0);
			}
		}
	},

	stop_cycle: function(){
		this.stop=true;
	},

	show_icons_at_hour: function(timestamp){

		for(var k=0;k<this.overlay_markers.length;k++){
			this.map.removeOverlay(this.overlay_markers[k].ewindow);
			this.map.removeOverlay(this.overlay_markers[k].marker);
		}
		this.overlay_markers=[];
		
		for(var i=0;i<this.hours_scala.length;i++){
			//$('scala_'+this.hours_scala[i].tmst).style.background="black";
			//$('scala_'+this.hours_scala[i].tmst).style.color="black";
			$('scala_'+this.hours_scala[i].tmst).style.fontWeight="normal";
		}
		var tmst_crt=timestamp.replace(/^time_/,'');
		//$('scala_'+tmst_crt).style.background="red";
		//$('scala_'+tmst_crt).style.color="red";
		$('scala_'+tmst_crt).style.fontWeight="bold";
		this.grid_scala.style.left=parseInt($('scala_'+tmst_crt).style.left)+4+"px";
		
		var data_crt = new Date( tmst_crt * 1000);
		
		this.div_hour_on_map.innerHTML=this.zi_scurt[data_crt.getDay()]+", ora "+(data_crt.getHours()<10?"0"+data_crt.getHours():data_crt.getHours());

		
		for (var j in eval('this.date_orase.'+timestamp)) {	
			this.put_oras(
						j,
						eval('this.date_orase.'+timestamp+'.'+j+'.name'),
						eval('this.date_orase.'+timestamp+'.'+j+'.symbol'),
						eval('this.date_orase.'+timestamp+'.'+j+'.temp'),
						eval('this.date_orase.'+timestamp+'.'+j+'.url'),
						eval('this.date_orase.'+timestamp+'.'+j+'.lat'),
						eval('this.date_orase.'+timestamp+'.'+j+'.long')
					);			
		}
	},

	show_commands: function(){
		Event.observe(
								$('play'),
								'click',
								this.play_stop.bindAsEventListener(this)
							);
		
		Event.observe(
								$('show_only_crt_location'),
								'click',
								this.show_only_location.bindAsEventListener(this)
							);

	},

	play_stop: function(e){
		var el=e.nodeType==1?e:Event.element(e);
		if(this.command=="play"){
			this.command="pause";
			el.setAttribute("src","/images/pause.gif",0);
			this.stop=false;
			if(this.next_time==0 || this.is_last){
				this.next_time=this.first_hour;
			}
			clearTimeout(this.timeout);
			this.cycle_hours(this.next_time);
		}
		else{
			this.command="play";
			el.setAttribute("src","/images/play.gif",0);
			this.stop_cycle();
		}
	},

	go_to_hours: function(e){
		var el=e.nodeType==1?e:Event.element(e);

		if(el.nodeName!='DIV'){
			el=el.up('div');
		}
		this.command="play";
		$('play').setAttribute("src","/images/play.gif",0);
		var go_to_tmst=el.getAttribute("id").replace(/^scala_/,"time_");
		this.next_time=go_to_tmst;
		this.is_last=false;
		this.stop_cycle();
		
		this.show_icons_at_hour(go_to_tmst);
	},

	show_only_location: function(e){
		var el=e.nodeType==1?e:Event.element(e);
		if(!this.only_crt_location){
			this.only_crt_location=true;
			el.innerHTML="arata toate locatiile&nbsp;&nbsp;&nbsp;&nbsp;+";
		}
		else{
			this.only_crt_location=false;
			el.innerHTML="arata doar locatia curenta&nbsp;&nbsp;&nbsp;&nbsp;+";
		}

		
		for(var i=0;i<this.overlay_markers.length;i++){
			if(this.overlay_markers[i].lat!=this.current_lat_long.latitudine && this.overlay_markers[i].long!=this.current_lat_long.latitudine){
				if(this.only_crt_location){
					this.overlay_markers[i].ewindow.hide();
					this.overlay_markers[i].marker.hide();
				}
				else{
					this.overlay_markers[i].ewindow.show();
					this.overlay_markers[i].marker.show();
				}
			}
		}
	},

	make_scala: function(){
		var scala_harta=$('scala_harta');
		var tmst=[];
		for (var j in eval('this.date_orase')) {
			tmst[tmst.length]=j.replace(/^time_/,'');
		}

		this.hours_scala=[];
		for(var i=0;i<tmst.length;i++){
			var ore = new Date( tmst[i] * 1000);
			this.hours_scala[this.hours_scala.length]={
														"ora":ore.getHours()<10?"0"+ore.getHours():ore.getHours(),
														"ziua":this.zi_scurt[ore.getDay()],
														"tmst":tmst[i]
													}
		}

		var metric=(parseInt(scala_harta.style.width)/this.hours_scala.length);
		for(var i=0;i<this.hours_scala.length;i++){
			if(i==0){
				this.div_hour_on_map.innerHTML=this.hours_scala[i].ziua+", ora "+this.hours_scala[i].ora;
			}
			
			var div_grid=document.createElement("div");
				$(div_grid).setStyle({
										position:"absolute",
										cursor:"pointer",
										width:"6px",
										height:"8px",
										top:"0px",
										left:(metric*i)+(metric/2)+"px",
										fontWeight:(i==0?"bold":"normal")
								});
				div_grid.setAttribute("id","scala_"+this.hours_scala[i].tmst,0);

			var grid=document.createElement("span");
				$(grid).setStyle({
										position:"absolute",
										background:"black",
										color:"black",
										width:"1px",
										height:"8px",
										top:"0px",
										left:"3px"
								});

			var text_liniuta=document.createElement("span");
				$(text_liniuta).setStyle({
										position:"absolute",
										width:"10px",
										top:"13px",
										fontSize:"11px",
										left:"-3px"
								});

				text_liniuta.innerHTML=this.hours_scala[i].ora;
				grid.appendChild(text_liniuta);
				div_grid.appendChild(grid);
				scala_harta.appendChild(div_grid);
			
			Event.observe(
								$(div_grid),
								'click',
								this.go_to_hours.bindAsEventListener(this)
							);
		}
		/*
		this.grid_scala=new Element("img",{src:"/images/grid_scale.gif",border:"0"}).setStyle({
								position:"absolute",
								width:"7px",
								height:"17px",
								top:"-4px",
								left:(metric/2)+"px",
								zIndex:"2"
							});*/
		
		this.grid_scala=new Element("div").setStyle({
								position:"absolute",
								width:(metric-1)+"px",
								height:"5px",
								fontSize:"0px",
								top:"1px",
								left:parseInt($('scala_'+this.hours_scala[0].tmst).style.left)+4+"px",
								zIndex:"2",
								background:"#dae252"
							});
		
		scala_harta.appendChild(this.grid_scala);
	},

	dec2hex: function(n){
		n = parseInt(n); var c = 'ABCDEF';
		var b = n / 16; var r = n % 16; b = b-(r/16); 
		b = ((b>=0) && (b<=9)) ? b : c.charAt(b-10);    
		return ((r>=0) && (r<=9)) ? b+''+r : b+''+c.charAt(r-10);
	}

};