var gMarkerStore = new Class({
	items:[],
	markers:[],
	Implements:[Options,Events],
	options: {
		mode:'local',
		url:null,
		remoteData:{},
		markers:null,
		icons:{}
	},
	initialize:function(opt){
		this.setOptions(opt);
		this.loadStore();
		var items = this.getItems();
		try{
			if($defined(items)){

				//prototype GMarker for storing our elements on a marker...
				GMarker.prototype.addMyElement = function(el){
					this.myElement = el;
				}
				items.each(function(el,index){
					this.addMarkerToSet(el);
				}.bind(this));
			}
		}catch(e){
			this.log('could not find items, or items are not well json formed...');
		}
	},
	loadStore:function(){
		var mode = this.getOptions().mode;
		switch(mode){
			case 'local':
					//var items = JSON.decode(this.getOptions().markers,false);
					var items = this.getOptions().markers;
					this.setItems(items);
				break;
			case 'remote':
				if(this.getOptions().url !== null){
					var loc = window.location;
					var baseUrl = loc.protocol+'//'+ loc.host +'/';
					var request = new Request.JSON({
						async:false,
						url: baseUrl+this.getOptions().url,
						onSuccess:function(result){
							this.setItems(result);
							this.fireEvent('itemsloaded',this);
						}.bind(this)
					}).post(this.getOptions().remoteData);
				}else{
					this.log('remote flag is set but no url given.');
				}
				break;
				default:
					this.log('mode must be remote or local..');
		}
	},
	addMarkerToSet:function(el){
		try{
			var icon = this.getIconForCategory(el.category);
			var point = new GLatLng(el.lat,el.long);
			var marker = new GMarker(point,icon);
			marker.addMyElement(el);
			GEvent.addListener(marker, 'click', function(){
				this.fireEvent('markerclicked',marker);
				if(el.content.search(/^url:/) != -1){
					var content = el.content.substr(4);
					var loc = window.location;
					var baseUrl = loc.protocol+'//'+ loc.host +'/';
					var request = new Request.HTML({
						url: baseUrl+content,
						onSuccess:function(responseTree, responseElements, responseHTML, responseJavaScript){
							marker.openInfoWindowHtml(responseHTML);
						}.bind(marker)
					}).get();
			    	marker.openInfoWindowHtml('wird geladen ...');
				}else{
					var content = el.content;
					if($chk(content)){
						marker.openInfoWindowHtml(content);
					}
				}
	        }.bind(this));
			this.markers.push(marker);
		}catch(e){
			this.log('seems that google API wasnt loaded properly.. could not find GMarker');
		}
	},
	getOptions:function(){
		return this.options;
	},
	setItems:function(items){
		this.items = items;
	},
	getItems:function(){
		return this.items;
	},
	getIconForCategory:function(cat){
		var iconImages = this.getOptions().icons;
		var icon = new GIcon(G_DEFAULT_ICON);
        if(iconImages[cat]){
			icon.image = iconImages[cat];
		}
		icon.imageMap = [7,2,10,3,33,4,37,5,40,6,40,7,40,8,40,9,40,10,40,11,41,12,63,13,68,14,70,15,70,16,71,17,70,18,70,19,69,20,68,21,67,22,66,23,65,24,64,25,63,26,62,27,61,28,60,29,59,30,58,31,57,32,56,33,55,34,54,35,53,36,52,37,51,38,50,39,49,40,15,41,15,42,15,43,15,44,14,45,13,46,11,47,2,47,1,46,0,45,0,44,1,43,1,42,2,41,4,40,4,39,4,38,4,37,4,36,4,35,4,34,4,33,4,32,4,31,4,30,4,29,4,28,4,27,4,26,4,25,4,24,4,23,4,22,4,21,4,20,4,19,4,18,4,17,4,16,4,15,4,14,4,13,4,12,4,11,4,10,4,9,4,8,4,7,4,6,4,5,4,4,4,3,4,2];
        icon.iconAnchor = new GPoint(25, 25);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(77,49);
	return icon;	
	},
	getMarkers:function(){
		return this.markers;
	},
	showMarkers:function(property,value){
		var markers = this.getMarkers();
		markers.each(function(el,index){
			if(el.myElement[property] == value){
				el.show();
			}
		});
	},
	hideMarkers:function(property,value){
		var markers = this.getMarkers();
		markers.each(function(el,index){
			if(el.myElement[property] == value){
				el.hide();
				el.closeInfoWindow();
			}
		});
	},
	getVisibleMarkers:function(){
		var visibleMarkers = [];
		var markers = this.getMarkers();
		markers.each(function(el,index){
			if(el.isHidden()==false){
				visibleMarkers.push(el);
			}	
		});
	return visibleMarkers;
	},
	log:function(string){
		if(window.console){
			console.log(string);
		}
	}
});
