if (!wcx) var wcx = {};
wcx.WMFMap = Class.create();
wcx.WMFMap.prototype = {
	
	map: null,
	data: [],
	geocoder: new GClientGeocoder(),
	icon: new GIcon(G_DEFAULT_ICON),
	markerOptions: {},
		
	initialize: function(container, mapCanvas, controlBar, closeButton, innerCurtain) {
		
		this.container = $(container);
		this.mapCanvas = $(mapCanvas);
		this.controlBar = $(controlBar);
		this.closeButton = $(closeButton);
		this.innerCurtain = $(innerCurtain);
		
		this.icon.image = "/_custom/sites/wmf/_com/gfx/pin_weissman.png";
		this.icon.iconSize = new GSize(20,20);
		this.markerOptions = {
			icon: this.icon
		};

        if (this.closeButton) {
            this.closeButton.observe('click', this.close.bind(this));
        }
		//this.loadFilters();
		wcx.WMFMap.instance = this;
	},
	
	loadFilters: function() {
		
		var params = cx.utils.Helpers.createSrvCallParams("WCX_Module_WMF", "getFilterData", [], []);
	    new Ajax.Request('/GTServer.php', {
	        method: 'post',
	        parameters: params,
	        onSuccess: function(res) {
	    		this.filters = res.responseText.evalJSON();
	    		this.setupFilters();
	    		this.loadData();
	    	}.bind(this)
	    });
	},
	
	loadData: function() {

		this.innerCurtain.show();
		var filter = this.getFilterOptions();
		var params = cx.utils.Helpers.createSrvCallParams("WCX_Module_WMF", "getWMFData", [], [filter]);
	    new Ajax.Request('/GTServer.php', {
	        method: 'post',
	        parameters: params,
	        onSuccess: this.onWMFDataLoaded.bind(this)
	    });
	},
	
	onWMFDataLoaded: function(res) {
		
		this.data = res.responseText.evalJSON();
		this.setupMap();
		this.innerCurtain.hide();
	},
	
	open: function() {
		
		if (!this.filters) this.loadFilters();
		//else this.setupMap();
		this.container.appear({
            duration: this.closeButton ? 1 : 0.1,
			afterFinish: function() {
			}.bind(this)
		});
	},
	
	close: function() {
		
		this.container.fade({
			afterFinish: function() {
				//GUnload();
			}
		});
	},
	
	setupFilters: function() {
		
		var br_label = new Element('span').update("Sie wollen sich einen Überblick über bestimmte Branchen verschaffen? Benutzen Sie die Auswahl um Branchen auf der Karte anzuzeigen.<br /><br />").setStyle({
			fontSize: '11px',
			fontWeight: 'bold',
			color: 'black'
		});
		
		this.controlBar.insert(br_label);
		
		var br_label = new Element('span').update("Branchen:").setStyle({
			fontSize: '11px',
			fontWeight: 'bold',
			color: '#9d0a32'
		});
		this.controlBar.insert(br_label);
				
		var cnt = new Element('div');
		var cbx = new Element('input', {
			type: 'checkbox',
			checked: "checked",
			id: 'cbx_master'
		});
		cbx.observe('click', function(evt) {
			this.checkAllBranches(evt.element().checked);
			this.loadData();
		}.bind(this));
		
		var lbl = new Element('label').update("Alle an/abwählen");
		cnt.insert(cbx);
		cnt.insert(lbl);
		this.controlBar.insert(cnt);
		cbx.checked = "checked";
		
		for (var i = 0; i < this.filters.branches.length; i++) {
			
			var cnt = new Element('div');
			var cbx = new Element('input', {
				type: 'checkbox',
				'class': 'wmf_filter',
				checked: "checked",
				name: 'branch',
				value: this.filters.branches[i].name
			});
			//cbx.observe('click', this.loadData.bind(this));
			cbx.observe('click', function(evt) {
				this.onBranchClick(evt);
			}.bind(this));
			
			var lbl = new Element('label').update(this.filters.branches[i].name);
			cnt.insert(cbx);
			cnt.insert(lbl);
			this.controlBar.insert(cnt);
			cbx.checked = "checked";
		}
		
		var af_label = new Element('span').update("Weitere Filter:").setStyle({
			fontSize: '12px',
			fontWeight: 'bold',
			color: '#9d0a32'
		});
		this.controlBar.insert(new Element('br'));
		this.controlBar.insert(af_label);
		
		for (var i = 0; i < this.filters.isFamilyBusiness.length; i++) {
			
			var cnt = new Element('div');
			var cbx = new Element('input', {
				type: 'checkbox',
				'class': 'wmf_filter',
				checked: false,
				name: 'isFamilyBusiness',
				value: 1
			});
			//cbx.observe('click', this.loadData.bind(this));
			cbx.observe('click', function(evt) {
				this.onFamilyBusinessClick(evt);
			}.bind(this));
			
			var lbl = new Element('label').update(this.filters.isFamilyBusiness[i].name);
			cnt.insert(cbx);
			cnt.insert(lbl);
			this.controlBar.insert(cnt);
			//cbx.checked = "checked";
		}
		
		try {
		
			if ($__isSmall) {
				$('map_toolbar').style.height = "594px";
				$('map_canvas').style.height = "612px";
			}
			
			$('map_toolbar').style.overflowY = "scroll";
			$('map_toolbar').style.overflowX = "none";
		}
		catch (e) {}
		
	},
	
	onBranchClick: function(evt) {
		
		var master = $("cbx_master");
		
		if (master.checked) {
			
			master.checked = "";		
			this.checkAllBranches("");			
			evt.element().checked = "checked";			
		}
		
		this.loadData();
	},
	
	onFamilyBusinessClick: function(evt) {
		
		this.loadData();
	},
	
	checkAllBranches: function(checked) {
		
		$$('.wmf_filter').each(function(elem) {
			if (elem.readAttribute('name') == 'branch') {
				elem.checked = checked;
			}
		});
	},
	
	getFilterOptions: function() {
		
		return Form.serializeElements($$('.wmf_filter'), true);
	},
	
	setupMap: function() {
		
		var mapHeight = 698;
		if ($__isSmall) mapHeight = 612;
		
	    if (GBrowserIsCompatible()) {

	        this.map = new GMap2(this.mapCanvas, {
				size: new GSize(650,mapHeight),
			    draggableCursor: "move",
			    draggingCursor: "move"
			});

	        this.map.enableContinuousZoom();
	        this.map.enableScrollWheelZoom();
	        this.mapControl = new GMapTypeControl();
			
	        this.map.addControl(new GLargeMapControl());
	        this.geocoder.getLatLng("Deutschland", this.centerMap.bind(this));
			this.map.removeMapType(G_NORMAL_MAP);
			this.map.addMapType(G_PHYSICAL_MAP);
			this.map.setMapType(G_PHYSICAL_MAP); 
			
	        this.addMarketLeaderLocations();
	    }
	},
	
	centerMap: function(point) {
        
		if (point != null) {
        	this.map.setCenter(point, 6);
        }
		else {
            alert("Unable to get address location!");
        }
    },
	
	addMarketLeaderLocations: function() {
    	
    	for (i = 0; i < this.data.length; i++) {
	        
	    	var d = this.data[i];
	    	var p = new google.maps.LatLng(d.lat, d.lng);
	    	
	    	var marker = new GMarker(p, this.markerOptions);
	    	marker.bindInfoWindow(d.infoText);
	    	this.map.addOverlay(marker);
	    }
	}
};

