
function Proto_HaritaStroer()
{
	
	this.mode = false;
	
	this.area = false;
	
	this.distance = false;
	
	this.region = false;
	
	this.areaDistance = false;
	
	this.route = false;
	
	this.sortOrder = false;
	
	
	this.containerModes = false;
	this.containerModesName = false;
	
	this.containerCategories = false;
	this.containerCategoriesName = false;
	
	this.containerAdspaces = false;
	this.containerAdspacesName = false;
	
	this.selectedCategories = {};
	
	this.selectedAdspaces = {};
	
	this.parentCategories = {};
	this.childCategories = {};
	
	this.parentAdspaces = {};
	this.childAdspaces = {};
	
	this.lastPoints = {};
	this.lastRequest = {};
	
	
	
	this.setRegion = function(argRegion)
	{
		this.region = argRegion;
		
		this.requestMarker();
	}
	
	this.getRegion = function()
	{
		return(this.region);
	}
	
	this.setRoute = function(argRoute)
	{
		this.route = argRoute;
		
		this.requestMarker();
	}
	
	this.getRoute = function()
	{
		return(this.route);
	}
	
	this.setMode = function(argMode)
	{
		this.mode = argMode;
		
		document.getElementById("stroer_distance").disabled = this.mode == "free";
		
		this.requestMarker();
	}
	
	this.getMode = function()
	{
		return(this.mode);
	}
	
	this.setSortOrder = function(argSortOrder)
	{
		this.sortOrder = argSortOrder;
		
		this.requestMarker();
	}
	
	this.getSortOrder = function()
	{
		return(this.sortOrder);
	}
	
	this.setArea = function(argArea)
	{
		this.area = argArea;
		
		document.getElementById("stroer_area_distance").disabled = ((this.area == "no") || (this.area == "view") || (this.area == "region"));
		
		var blendBoxes = {"stroer_modes":true, "stroer_adspaces":true, "stroer_categories":true};
		
		switch(this.area)
		{
			case "route":	blendBoxes["route"] = true; break;
			case "region":	blendBoxes["stroer_regions"] = true; break;
			case "address":	blendBoxes["searchaddress"] = true; break;
			case "uploaded":	blendBoxes["stroer_uploaded"] = true; break;
		}
		
		multislideAuto(blendBoxes);
		
		this.requestMarker();
	}
	
	this.getArea = function()
	{
		return(this.area);
	}
	
	this.setDistance = function(argDistance)
	{
		this.distance = argDistance * 1;
		var field = document.getElementById("stroer_distance");
		if(field)
		{
			if(field.value != this.distance)
			{
				field.value = this.distance;
			}
		}
		this.requestMarker();
	}
	
	this.getAreaDistance = function()
	{
		return(this.areaDistance);
	}
	
	this.setAreaDistance = function(argAreaDistance)
	{
		this.areaDistance = argAreaDistance * 1;
		var field = document.getElementById("stroer_area_distance");
		if(field)
		{
			if(field.value != this.areaDistance)
			{
				field.value = this.areaDistance;
			}
		}
		this.requestMarker();
	}
	
	this.getDistance = function()
	{
		return(this.distance);
	}
	
	this.downloadList = function()
	{
		var bounds = getCurrentMapExtent();
		var center = bounds.getCenterLonLat();
		
		var request =
		{
			"outf": "csv",
			"mode": this.mode,
			"area": this.area,
			"distance": this.distance,
			"areaDistance": this.areaDistance,
			"sortOrder": this.sortOrder,
			
			"region": this.region,
			
			"center_lon": center.lon,
			"center_lat": center.lat,
			
			"bound_lat_min": bounds.bottom,
			"bound_lat_max": bounds.top,
			
			"bound_lon_min": bounds.left,
			"bound_lon_max": bounds.right
		};
		
		var n = 0;
		for(var enKey in this.selectedCategories)
		{
			if(this.selectedCategories[enKey])
			{
				request["categories[" + (n++) + "]"] = enKey;
			}
		}
		
		var n = 0;
		for(var enKey in this.selectedAdspaces)
		{
			if(this.selectedAdspaces[enKey])
			{
				request["adspaces[" + (n++) + "]"] = enKey;
			}
		}
		
		var n = 0;
		for(var enKey in this.route)
		{
			/*if(this.route[enKey])
			{*/
				request["zroute[" + (n++) + "]"] = this.route[enKey];
			/*}*/
		}
		
		HaritaStroer.lastRequest = request;
		window.open(Sensus.urlMake("data/stroerpoints?" + $.param(request)));
	}
	
	this.requestMarker = function()
	{
		var str_box = document.getElementById("box_stroer_modes");
		if(!str_box)
		{
			return(false);
		}
		
		document.getElementById("stroer_download_list").disabled = true;
		
		var bounds = getCurrentMapExtent();
		var center = bounds.getCenterLonLat();
		
		var request =
		{
			"mode": this.mode,
			"area": this.area,
			"distance": this.distance,
			"areaDistance": this.areaDistance,
			"sortOrder": this.sortOrder,
			
			"region": this.region,
			
			"center_lon": center.lon,
			"center_lat": center.lat,
			
			"bound_lat_min": bounds.bottom,
			"bound_lat_max": bounds.top,
			
			"bound_lon_min": bounds.left,
			"bound_lon_max": bounds.right
		};
		
		var n = 0;
		for(var enKey in this.selectedCategories)
		{
			if(this.selectedCategories[enKey])
			{
				request["categories[" + (n++) + "]"] = enKey;
			}
		}
		
		var n = 0;
		for(var enKey in this.selectedAdspaces)
		{
			if(this.selectedAdspaces[enKey])
			{
				request["adspaces[" + (n++) + "]"] = enKey;
			}
		}
		
		var n = 0;
		for(var enKey in this.route)
		{
			/*if(this.route[enKey])
			{*/
				request["zroute[" + (n++) + "]"] = this.route[enKey];
			/*}*/
		}
		
		HaritaStroer.lastRequest = request;
		//alert(Sensus.urlMake("data/stroerpoints") + "?" + $.param(request));
		$.getJSON(Sensus.urlMake("data/stroerpoints"), request, this.updateMarker);
	}
	
	this.updateMarker = function(argData)
	{
	
		markers.clearMarkers();
		
		if(argData)
		{
			this.lastPoints = argData;
		}
		else
		{
			argData = this.lastPoints;
		}
		
		if(!argData["success"])
		{
			return(false);
		}
		
		var pointerUri = Sensus.urlMake("images/pointer.png");
		
		var count = 0;
		
		for(var enKey in argData["points"])
		{
			var item = argData["points"][enKey];
			
			var icon_url = Sensus.urlMake("icons/" + item.cid + ".png");
        
			count = count + 1;
			
			var text =
					"<div class=\"poimarker2\"> \
							<div align=\"center\"><img src=\"" + icon_url + "\" /></div> \
							<div align=\"center\">" + count + "</div> \
						</div>\
						<div class=\"omx_marker_pointer\"></div>";
						
			var lonlat = new OpenLayers.LonLat(parseFloat(item.lon), parseFloat(item.lat));
			lonlat = lonlat.transform(map.displayProjection, map.projection);
			var icon = new OpenLayers.Marker.TextIcon(text, {});
			
			var marker = new OpenLayers.Marker(lonlat, icon);
			marker.poi = item;

			// register event listener
			//marker.events.register('mousedown', marker, function(evt) { markerMouseDown(this, evt); OpenLayers.Event.stop(evt); });
			marker.events.register('mouseover', marker, function(evt) { markerMouseOver(this, evt); OpenLayers.Event.stop(evt); });
			marker.events.register('mouseout', marker, function(evt) { markerMouseOut(this, evt); OpenLayers.Event.stop(evt); });
			
			// add marker
			markers.addMarker(marker);
			
			// update marker icon offset (icon size can de determined only after marker has been added)
			icon.offset = new OpenLayers.Pixel(0, -icon.imageDiv.offsetHeight);
			icon.imageDiv.style.zIndex = '1000';
			icon.draw();
		}
		
		if(count > 0)
		{
			document.getElementById("stroer_download_list").disabled = false;
		}
		
	}
          
	
	
	this.loadAdspaces = function(argCid, argLevel)
	{
		
		if(argCid == 0)
		{
			$(this.containerAdspacesName).empty();
			$(this.containerAdspacesName).hide();
			$(this.containerAdspacesName).append("<div id=\"stroer_adspace_et_" + argCid + "\"></div>");
		}
		
		var numLoaded = 0;
		
		HaritaStroer.childAdspaces[argCid] = new Object();
		
		$.getJSON(Sensus.urlMake("data/scategories/" + argCid),
			function(argData)
			{
				if(argData.length)
				{
					$.each(argData,
						function(argKey, argVal)
						{
						
							$("#stroer_adspace_et_" + argCid).append("<div id=\"stroer_adspace_sl_" + argVal["cid"] + "\" class=\"sel_list_level_" + argLevel + " clickable\"><div style=\"float:right\"><input type=\"checkbox\" onchange=\"HaritaStroer.setAdspace(" + argVal["cid"] + ", this.checked);\" id=\"stroer_adspace_cb_" + argVal["cid"] + "\" /></div><span id=\"stroer_adspace_tg_" + argVal["cid"] + "\" class=\"clickable\">" + argVal["name"] + "</span></div><div id=\"stroer_adspace_et_" + argVal["cid"] + "\"></div>");
							HaritaStroer.selectedAdspaces[argVal["cid"]] = false;
							HaritaStroer.parentAdspaces[argVal["cid"]] = argCid;
							HaritaStroer.childAdspaces[argCid][argVal["cid"]] = argVal["cid"];
							
							var subLoaded = HaritaStroer.loadAdspaces(argVal["cid"], argLevel + 1);
							
							$("#stroer_adspace_et_" + argVal["cid"]).hide();
							$("#stroer_adspace_tg_" + argVal["cid"]).click(
								function()
								{
									$("#stroer_adspace_et_" + argVal["cid"]).slideToggle(200);
								});
							
							numLoaded++;
						});
				}
			});
		
		if(argCid == 0)
		{
			$(this.containerAdspacesName).fadeIn("slow");
		}
		
		return(numLoaded);
	}
	
	this.setAdspace = function(argCid, argStatus)
	{
		this.selectedAdspaces[argCid] = argStatus;
		
		var nowParent = this.parentAdspaces[argCid];
		
		if(nowParent)
		{
			document.getElementById("stroer_adspace_cb_" + argCid).checked = argStatus;
			
			var hasTrue = false;
			var hasFalse = false;
			
			for(var elKey in this.childAdspaces[nowParent])
			{
				if(this.selectedAdspaces[elKey])
				{
					hasTrue = true;
				}
				else
				{
					hasFalse = true;
				}
			}
			
			var parentStatus = true;
			
			if(hasTrue && !hasFalse)
			{
				parentStatus = true;
			}
			else
			{
				parentStatus = false;
			}
			
			this.selectedAdspaces[nowParent] = parentStatus;
			document.getElementById("stroer_adspace_cb_" + nowParent).checked = parentStatus;
			
		}
		else
		{
			var followUp = new Array();
			
			followUp.push(argCid);
			
			while(followUp.length > 0)
			{
				var nowCid = followUp.pop();
				this.selectedAdspaces[nowCid] = argStatus;
				document.getElementById("stroer_adspace_cb_" + nowCid).checked = argStatus;
				
				if(typeof(this.childAdspaces[nowCid]) == "object")
				{
					for(var elKey in this.childAdspaces[nowCid])
					{
						followUp.push(elKey);
					}
				}
			}
		}
		
		this.requestMarker();
		
	}
	
	this.loadCategories = function(argCid, argLevel)
	{
		
		if(argCid == 0)
		{
			$(this.containerCategoriesName).empty();
			$(this.containerCategoriesName).hide();
			$(this.containerCategoriesName).append("<div id=\"stroer_category_et_" + argCid + "\"></div>");
		}
		
		var numLoaded = 0;
		
		HaritaStroer.childCategories[argCid] = new Object();
		
		$.getJSON(Sensus.urlMake("data/categories/" + argCid),
			function(argData)
			{
				if(argData.length)
				{
					$.each(argData,
						function(argKey, argVal)
						{
						
							$("#stroer_category_et_" + argCid).append("<div id=\"stroer_category_sl_" + argVal["cid"] + "\" class=\"sel_list_level_" + argLevel + " clickable\"><div style=\"float:right\"><input type=\"checkbox\" onchange=\"HaritaStroer.setCategory(" + argVal["cid"] + ", this.checked);\" id=\"stroer_category_cb_" + argVal["cid"] + "\" /></div><span id=\"stroer_category_tg_" + argVal["cid"] + "\" class=\"clickable\">" + argVal["name"] + "</span></div><div id=\"stroer_category_et_" + argVal["cid"] + "\"></div>");
							HaritaStroer.selectedCategories[argVal["cid"]] = false;
							HaritaStroer.parentCategories[argVal["cid"]] = argCid;
							HaritaStroer.childCategories[argCid][argVal["cid"]] = argVal["cid"];
							
							var subLoaded = HaritaStroer.loadCategories(argVal["cid"], argLevel + 1);
							
							$("#stroer_category_et_" + argVal["cid"]).hide();
							$("#stroer_category_tg_" + argVal["cid"]).click(
								function()
								{
									$("#stroer_category_et_" + argVal["cid"]).slideToggle(200);
								});
							
							numLoaded++;
						});
				}
			});
		
		if(argCid == 0)
		{
			$(this.containerCategoriesName).fadeIn("slow");
		}
		
		return(numLoaded);
	}
	
	this.setCategory = function(argCid, argStatus)
	{
		this.selectedCategories[argCid] = argStatus;
		
		var nowParent = this.parentCategories[argCid];
		
		if(nowParent)
		{
			document.getElementById("stroer_category_cb_" + argCid).checked = argStatus;
			
			var hasTrue = false;
			var hasFalse = false;
			
			for(var elKey in this.childCategories[nowParent])
			{
				if(this.selectedCategories[elKey])
				{
					hasTrue = true;
				}
				else
				{
					hasFalse = true;
				}
			}
			
			var parentStatus = true;
			
			if(hasTrue && !hasFalse)
			{
				parentStatus = true;
			}
			else
			{
				parentStatus = false;
			}
			
			this.selectedCategories[nowParent] = parentStatus;
			document.getElementById("stroer_category_cb_" + nowParent).checked = parentStatus;
			
		}
		else
		{
			var followUp = new Array();
			
			followUp.push(argCid);
			
			while(followUp.length > 0)
			{
				var nowCid = followUp.pop();
				this.selectedCategories[nowCid] = argStatus;
				document.getElementById("stroer_category_cb_" + nowCid).checked = argStatus;
				
				if(typeof(this.childCategories[nowCid]) == "object")
				{
					for(var elKey in this.childCategories[nowCid])
					{
						followUp.push(elKey);
					}
				}
			}
		}
		
		this.requestMarker();
		
	}
	
	this.initAdspaces = function()
	{
		this.loadAdspaces(0, 0);
	}
	
	this.initCategories = function()
	{
		this.loadCategories(0, 0);
	}
	
	this.initModes = function()
	{
		this.mode = "free";
		this.distance = 1000;
		this.region = 0;
		this.route = [];
		this.sortOrder = "close";
	}
	
	this.initAreas = function()
	{
		this.area = "no";
		this.areaDistance = 100;
	}
	
	
	
	this.init = function()
	{
		this.containerModesName       = "stroer_modes";
		this.containerCategoriesName  = "stroer_categories";
		this.containerAdspacesName    = "stroer_adspaces";
		
		this.containerModes        = document.getElementById(this.containerModesName);
		this.containerCategories   = document.getElementById(this.containerCategoriesName);
		this.containerAdspaces     = document.getElementById(this.containerAdspacesName);
		
		if(this.containerModes)
		{
			this.containerModesName = "#" + this.containerModesName;
			this.initModes();
			this.initAreas();
		}
		else
		{
			this.containerModes = false;
		}
		
		if(this.containerCategories)
		{
			this.containerCategoriesName = "#" + this.containerCategoriesName;
			this.initCategories();
		}
		else
		{
			this.containerCategories = false;
		}
		
		if(this.containerAdspaces)
		{
			this.containerAdspacesName = "#" + this.containerAdspacesName;
			this.initAdspaces();
		}
		else
		{
			this.containerAdspaces = false;
		}
		
		return(true);
	}
	
}

