//by Adrian Carstea
var mapping = Class.create({

		initialize: function(params){
			this.params = params || {};
			this.current_lat_long = { latitudine:"0", longitudine:"0" };
			this.onmap_symbols = { ewindow:{},marker:{} };
			this.hide_symbols = true;
			
			if(this.params.is_world){
				Event.observe(window,"load",this.google_map_world.bindAsEventListener(this));
			}
			else{
				Event.observe(window,"load",this.google_map.bindAsEventListener(this));
			}
			Event.observe(window,"unload",GUnload.bindAsEventListener(this));
		},

		google_map: function() {
			  if (GBrowserIsCompatible()) {
				this.map = new GMap2($(this.params.mapID));

				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)); 


				this.map.setMapType(G_PHYSICAL_MAP);
				this.map.addControl(new GSmallZoomControl());

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

		google_map_world: function() {
			  if (GBrowserIsCompatible()) {
				this.map = new GMap2($(this.params.mapID));

				this.map.setCenter(new GLatLng(0,0), 6);
				this.map.setMapType(G_PHYSICAL_MAP);

				this.addAddressToMap(this.params.position.latitudine,this.params.position.longitudine,this.params.id_oras);
				
				this.map.addControl(new GSmallZoomControl());
				this.map.addControl(new GOverviewMapControl(new GSize(100, 100)));
			}
		},

		search_adress: 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=new Element("div",{"id":"icon_"+icon});
			var parentIcon=new Element("div",{"id":"parent_icon_"+icon});
			
			parentIcon.appendChild(flashIcon);
			$("flash_obj").appendChild(parentIcon);

			swfobject.embedSWF("http://statik.kappa.ro/modules/vremea/images/png/"+icon+".swf", "icon_"+icon, "45", "45", "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='"+this.params.classIconitze+"'>"+$("parent_icon_"+icon).innerHTML+"</div><div 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+"\">"+nume+"</a></div>", new GPoint(-27,20));

			/*var baseIcon = new GIcon();
				baseIcon.iconSize = new GSize(18, 19);
				baseIcon.iconAnchor = new GPoint(9, 19);
				baseIcon.infoWindowAnchor = new GPoint(9, 19);

			var letteredIcon = new GIcon(baseIcon);

			if( latitudine == this.current_lat_long.latitudine && longitudine == this.current_lat_long.longitudine){
				letteredIcon.image = "http://statik.kappa.ro/modules/vremea/images/pin_vreme_crt.png";
			}
			else{
				letteredIcon.image = "http://statik.kappa.ro/modules/vremea/images/pin_vreme.png";
			}*/

			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 = "http://statik.kappa.ro/modules/vremea/images/pin_vreme_crt_new.png";
			}
			else{
				//letteredIcon.image = (document.all?"/images/pin_vreme_new.gif":"/images/pin_vreme_new.png");
				letteredIcon.image = "http://statik.kappa.ro/modules/vremea/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.onmap_symbols.ewindow["icon_"+id] = ewindow;
			this.onmap_symbols.marker["icon_"+id] = marker;
			return false;
		},

		
		addAddressToMap: function(latitudine,longitudine,id) {
			var latlng = new GLatLng(latitudine,longitudine);
			this.map.setCenter(latlng, 6);

			var ewindow = new EWindow(this.map, E_STYLE_1); 
			this.map.addOverlay(ewindow);

			var flashIcon = new Element("div",{"id":"icon_"+this.params.currently_icon});
			var parentIcon = new Element("div",{"id":"parent_icon_"+this.params.currently_icon});
				parentIcon.appendChild(flashIcon);
				$("flash_obj").appendChild(parentIcon);


			swfobject.embedSWF("http://statik.kappa.ro/modules/vremea/images/png/"+this.params.currently_icon+".swf", "icon_"+this.params.currently_icon, "60", "60", "9.0.0","",{},{wmode:"transparent"});

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

			var back = "#"+redTmp+"00"+blueTmp;

			ewindow.openOnMap(latlng, "<div style='padding-left:43px;' class='vreme_iconitze'>"+$("parent_icon_"+this.params.currently_icon).innerHTML+"</div><div class=\"temp_on_map_world\">Temp: <b  style='color:"+back+";text-shadow:1px 1px 1px #484848;'>"+this.params.currently_temperatura+"&deg; C</b><br />Realitate: <b style='color:"+back+";text-shadow:1px 1px 1px #484848;'>"+this.params.currently_realfeel+"&deg; C</b></div>", new GPoint(-50,0))

			/*var baseIcon = new GIcon();
				baseIcon.iconSize = new GSize(18, 19);
				baseIcon.iconAnchor = new GPoint(9, 19);

			var letteredIcon = new GIcon(baseIcon);
			letteredIcon.image = "http://statik.kappa.ro/modules/vremea/images/pin_vreme.png";*/
			
			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);
			letteredIcon.image = "http://statik.kappa.ro/modules/vremea/images/pin_vreme_crt_new.png";

			var markerOptions = { icon:letteredIcon, clickable:false };
			var marker = new GMarker(latlng,markerOptions);
			this.map.addOverlay(marker);
			$('comenzi').style.visibility="visible";
		},

		hide_other_cities: function(id,obj){
			try{
				var all_cities = $$("."+this.params.classIconitze);
				for(var i=0;i<all_cities.length;i++){
					var iconID = $(all_cities[i]).getAttribute("id");
					if(iconID !== "icon_"+id){
						if(this.hide_symbols){
							this.onmap_symbols.ewindow[iconID].hide();
							this.onmap_symbols.marker[iconID].hide();
						}
						else{
							this.onmap_symbols.ewindow[iconID].show();
							this.onmap_symbols.marker[iconID].show();
						}
					}
				}
				
				obj.update(this.hide_symbols ? "arata toate locatiile" : "arata doar locatia curenta" );
				this.hide_symbols = this.hide_symbols ? false : true;
			}catch(err){}
			return false;
		},

		items_on_map: function(clasa,obj){
			var items = $$("."+this.params.classIconitze);
			for(var i=0;i<items.length;i++){
				if(items[i].style.display=='none'){
					items[i].style.display="";
					$(obj).update("ascunde iconuri");
				}
				else{
					items[i].style.display="none";
					$(obj).update("arata iconuri");
				}
			}
		},

		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);
		}

});