function createMarker(point, options, data) {
	var marker = new GMarker(point,options);
	if (data)
	GEvent.addListener(marker, "click",function() {
		if (data.typ == 40)
			if (data.city || data.street)
				marker.openInfoWindowHtml('<a href="/film/film.aspx?id='+data.id+'">'+data.title+'</a><p>Adres: '+data.city+(data.street ? (', ul. '+data.street+' '+data.houseNr) : '')+'</p>');
			else
				marker.openInfoWindowHtml('<a href="/film/film.aspx?id='+data.id+'">'+data.title+'</a>');
		else if (data.typ == 41)
			if (data.city || data.street)
				marker.openInfoWindowHtml('<a href="/stacjepaliw/default.aspx?tyt='+data.title+ (data.city ? '&miej=' + data.city : '') + '">'+data.title+'</a><p>Adres: '+data.city+(data.street ? (', ul. '+data.street+' '+data.houseNr) : '')+'</p>');
			else
				marker.openInfoWindowHtml('<a href="/stacjepaliw/default.aspx?tyt='+data.title+'">'+data.title+'</a>');
		else if (data.typ == 42)
			if (data.city || data.street)
				marker.openInfoWindowHtml('<a href="/stowarzyszeniaagro/default.aspx?tyt='+data.title+ (data.city ? '&miej=' + data.city : '') + '">'+data.title+'</a><p>Adres: '+data.city+(data.street ? (', ul. '+data.street+' '+data.houseNr) : '')+'</p>');
			else
				marker.openInfoWindowHtml('<a href="/stowarzyszeniaagro/default.aspx?tyt='+data.title+'">'+data.title+'</a>');
		else if (data.typ == 43)
			marker.openInfoWindowHtml('<p>'+data.title+'</p>');
		else	
			if (data.city || data.street)
				marker.openInfoWindowHtml('<a href="/oferta/oferta.aspx?id='+data.id+'">'+data.title+'</a><p>Adres: '+data.city+(data.street ? (', ul. '+data.street+' '+data.houseNr) : '')+'</p>');
			else
				marker.openInfoWindowHtml('<a href="/oferta/oferta.aspx?id='+data.id+'">'+data.title+'</a>');
		
			
		
	});
	return marker;
}


/**
 * @projectDescription Rozszerzenie mapy na rzecz wortalu turystycznego
 * 
 * @copyright 2007 Robert (nospor) Nodzewski
 * @author Robert Nodzewski (nospor at gmail dot com)
 * @version 1.0
 */

/******************************************************************************************************/
/******************************************************************************************************/
/************************************* CONTROLS *******************************************************/
/******************************************************************************************************/
/******************************************************************************************************/


/******************************************************************************************************/
/************************************* MMarkerPositionControl ***************************************************/
/******************************************************************************************************/
function MMarkerPositionControl(params){
	this.params = params;
	this.marker = null;
}

MMarkerPositionControl.prototype = new GOverlay();

/**
 * Inicjalizuje kontrolkę. 
 * 
 * @param {Map} map
 */
MMarkerPositionControl.prototype.init = function(map){
	this.parentInit(map);
}
MMarkerPositionControl.prototype.initialize = function(map) {
	this.map = map;
	var control = this;
	GEvent.addListener(map, 'click', function(overlay,point){
		//if (!MEvent.runEvent(data.control, 'click'))
		//	return true;
		if (point)
		control.set({lat:point.lat(),lng:point.lng()});	
	});
	
	if (typeof this.params.lat != "undefined"){
		this.lat = $((typeof this.params.lat == "string" ? '#'+this.params.lat : this.params.lat));
		if (this.lat.size() == 0)
			alert('Nie znaleziono elementu podane w lat: '+this.params.lat);
	} else
		this.lat = null;	
	if (typeof this.params.lng != "undefined"){
		this.lng = $((typeof this.params.lng == "string" ? '#'+this.params.lng : this.params.lng));
		if (this.lng.size() == 0)
			alert('Nie znaleziono elementu podane w lng: '+this.params.lng);
	} else
		this.lng = null;

}

MMarkerPositionControl.prototype.copy = function() {
  return null;
}


MMarkerPositionControl.prototype.set = function(position){
	if (!this.marker){
		this.marker = createMarker(new GLatLng(position.lat,position.lng),{draggable: true});//new MMarker(position.clone(),{draggable: true});
		this.map.addOverlay(this.marker);
		//MEvent.bind(this.marker,"drag",{control : this}, function(marker, data){data.control.setPosition(marker.position);});
		var marker = this.marker;
		var control = this;
		GEvent.addListener(marker,'drag',function(){
			var latlng = marker.getLatLng();
			control.setPosition({lat:latlng.lat(), lng:latlng.lng()});
		});
	} else {
		//this.marker.position = {lat:position.lat, lng:position.lng};
		this.marker.setLatLng(new GLatLng(position.lat,position.lng));
		this.show();
	}
	this.setPosition(position);

}
MMarkerPositionControl.prototype.setPosition = function(position){
	if (this.lat && this.lng){ //jeśli podany też lng to pozycja w dwóch polach
		this.lat.val(position.lat);
		this.lng.val(position.lng);
	} else if (this.lat)//pozycja w jednym polu po przecinku
		this.lat.val(position.lat + "," + position.lng);
	//MEvent.runEvent(this, "position");	
}

MMarkerPositionControl.prototype.remove = function(){
	this.params = null;
	this.lat = null;
	this.lng = null;
	
}
MMarkerPositionControl.prototype.redraw = function(force) {
	if (!force) return;

	return ;
}

MMarkerPositionControl.prototype.center = function(){
	if (!this.marker || this.marker.isHidden())
		return true;
	this.map.panTo(this.marker.getLatLng());	
}	

MMarkerPositionControl.prototype.hide = function(){
	if (!this.marker)
		return true;
	this.marker.hide();
	this.setPosition({lat : '', lng : ''});
}	

MMarkerPositionControl.prototype.show = function(){
	if (!this.marker)
		return true;
	this.marker.show();	
}	




/******************************************************************************************************/
/************************************* MCheckboxMarkersControl ***************************************************/
/******************************************************************************************************/
/**
 * Kontrolka, która po kliknięciu checkboxa pokazuje przypisane dla niego markery.
 * Z checkboxów pobiera ich value i to ono jest przekazywane postem
 * 
 * @classConstructor
 * @param {Map} params Obiekt parametrów.
 * @optionsFor params
 * @option {String} link (required) link pod jaki wysyłać rządanie.
 * @option {jQuery} assignTo (required) obiekt jQuery, który zawiera checkboxy pod jakie chcemy się podpiąć
 * 
 * @example 
 * var mcmc = new MCheckbocMarkersControl({link: 'http://link.pl', assignTo: jQuery('#iddiv :checkbox')});
 * map.addControl(mcmc);
 * @desc Tworzymy kontrolkę i podczepiamy ją pod checkboxy znajdujące się w div o id iddiv.
 * @html
 * <div id="iddiv"><input type="checkbox" value="1" /><input type="checkbox" value="2" /></div>
 */
function MCheckboxMarkersControl(params){
	function pd(name, def) { if (typeof params[name] == "undefined") { params[name] = def; }; };
	pd("ignoreId", '');

	this.params = params;
	this.markers = {};
}

MCheckboxMarkersControl.prototype = new GOverlay();

MCheckboxMarkersControl.prototype.initialize = function(map) {
	this.map = map;
	this.params.assignTo.bind("click", {control : this}, function(event){
		var control = event.data.control;
		if (this.checked){
			MCheckboxMarkersControl.getMarkers(this, control);
		} else if (typeof control.markers[this.value] != "undefined") {
			var marker;
			for (var _id in control.markers[this.value]){
				marker = control.markers[this.value][_id].hide();
			}
		}
	});
}

MCheckboxMarkersControl.prototype.copy = function() {
  return null;
}

MCheckboxMarkersControl.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
	for (var _i in this.markers){
		for (var _j in this.markers[_i]){
			//this.markers[_i][_j].destroy();
			this.markers[_i][_j] = null;
		}
		this.markers[_i] = null;
	}	
	this.markers = null;
	this.params = null;
}

MCheckboxMarkersControl.prototype.redraw = function(force) {
	if (!force) return;

	return ;
}

MCheckboxMarkersControl.wait = null;
MCheckboxMarkersControl.getMarkers = function(objCheck, control){
	if (!MCheckboxMarkersControl.wait){
		MCheckboxMarkersControl.wait = $('<img src="../images/indicator.gif" />');
		var _map = $('#map');
		var of = _map.offset();
		var l = Math.round(of.left + _map.width()/2);
		var t = Math.round(of.top + _map.height()/2);
		MCheckboxMarkersControl.wait.css({position:'absolute',left:l+'px',top:t+'px'});
		$('body').append(MCheckboxMarkersControl.wait);
	} else
		MCheckboxMarkersControl.wait.show();
	$.ajax({
		type: "POST",
		url: control.params.link,
		dataType: "text",
		data: {oftyp : objCheck.value, ignoreId : control.params.ignoreId, film: control.params.film, stacja: control.params.stacja, agro: control.params.stacja, city: control.params.city},
		success: function(obj) {
			if (!objCheck.checked)
				return true;
			if (typeof control.markers[objCheck.value] == "undefined")
				control.markers[objCheck.value] = {};
			
			
			eval('var mArray = '+obj +';');
			var count = mArray.length;
			var marker,mData; 
			for (var i=0;i<count;i++){
				mData = mArray[i];
				if (typeof control.markers[objCheck.value][mData.id] != 'undefined'){
					control.markers[objCheck.value][mData.id].show();
					continue;
				}	
				marker = null;
				var markerOptions = { icon:MMarker.markers[mData.typ] };
				marker = createMarker(new GLatLng(mData.lat,mData.lng), markerOptions, {id:mData.id,typ: mData.typ,title: mData.tytul,city:mData.miejscowosc,street:mData.ulica,houseNr:mData.nrdomu});
				control.map.addOverlay(marker);
				control.markers[objCheck.value][mData.id] = marker;
			}
			MCheckboxMarkersControl.wait.hide();
		},
		error : function(obj) {alert('Błąd pobierania danych');MCheckboxMarkersControl.wait.hide();}
	});
};
/*function createMarker(point, options, data) {
	var marker = new GMarker(point,options);
	if (data)
	GEvent.addListener(marker, "click",function() {
		marker.openInfoWindowHtml('<a href="/oferta/oferta.aspx?id='+data.id+'">'+data.title+'</a><p>Adres: '+data.city+(data.street ? (', ul. '+data.street+' '+data.houseNr) : '')+'</p>');
	});
	return marker;
}*/

