
/* */

function StopsInfo() {
	//this.maps_file = "/server/maps"; //content[city].data + "maps2.txt";
	this.maps_file = "/cache/maps2.txt"; //content[city].data + "maps2.txt";
	this._data = {maps:{}};
	this.ready = false;
}

StopsInfo.prototype.getDB = function(db) {
    return this._data[db] || {};
}

StopsInfo.prototype.get = function(db, key) {
    if(db in this._data) {
		if(key in this._data[db]) {
			return this._data[db][key];
		}
	}
	return {};
}

StopsInfo.prototype.execute = function(arg, fnc) {
    if(this.ready) {
	    //fnc(this, arg);
	    fnc(arg);
	}
	else {
	    if(this.load_started) return;
		this.load_started = true;
	    this.load(arg, fnc);
	}
}

/*
StopsInfo.prototype.executeOnPolylinesAll = function(arg, fnc) {
    if(arg.directions.length == 0) {
	    fnc(arg);
	}
	else {
	    var directions_id = arg.directions.pop();
        
		CDownloadUrl("get", pc.content[pc.city].data + directions_id + ".txt", function (doc) {
	        var lines = doc.split("\n");
		    arg.data[directions_id] = {levels:lines[0], line:lines[1]};
			
			pc.getSI().executeOnPolylinesAll(arg, fnc);
		});
	}
}



StopsInfo.prototype.executeOnPolylines = function(arg, fnc) {

    CDownloadUrl("get", pc.content[pc.city].data + arg.directions.main + ".txt", function (doc) {
	     var lines = doc.split("\n");
		 arg.polylines = {main: {levels:lines[0], line:lines[1]}};
		 
		 if(arg.directions.opposite) {
              CDownloadUrl("get", pc.content[pc.city].data + arg.directions.opposite + ".txt", function (doc) {
			       var lines = doc.split("\n");
				   arg.polylines.opposite = {levels:lines[0], line:lines[1]};
				   fnc(arg);
			  });
		 }
		 else {
		     fnc(arg);
		 }
    });
}

StopsInfo.prototype.getPointNeighbours = function(lat, lng) {
	var maxwalk = 5.0;
	if(maxwalk_el = document.getElementById("search_maxwalk_input")) {
	    maxwalk = parseFloat(maxwalk_el.value, 10);
	}
	//maxwalk = maxwalk/1000.0;
	
    var stops = [];
	for(var id in this._stops_data) {
	    var stop_data = this.getStopData(id);
		var name = stop_data.name;
		var fchar = id.charAt(0);
		if(fchar == "@" || fchar == "A") continue;
		
		//console.log(typeof point1.lat, typeof point2.lat);
	    var distance = distPythagor(lat, lng, stop_data.lat, stop_data.lng);
	    //var distance = point1.distanceFrom(point2);
		//console.log("distance: ", distance);
		if(distance > maxwalk) continue;
		stops.push(id);
	}
	
	
	return stops;
}
*/

StopsInfo.prototype.load = function(arg, onload) {
	var si = this;
		
	//CDownloadUrl("get", si.maps_file, function (doc) {
	$.ajax({url: si.maps_file,
			cache: false,
			dataType: "text",
			success: function(doc){
            //var today = toDay();
            
            var lines = doc.split("\n"); // split the document into lines

			
			var id, title, author, year, region, scale, contour, nr, file, width, height, bounds, rotate;
			title = author = year = region = country = scale = contour = nr = bounds = rotate = description = "";
			
			var best_version = false;
			
            var key_list = lines[0].split(';');
			var key_index_map = {};
			for (var i=0; i<key_list.length; i++) {
			    key_index_map[key_list[i].trim()] = i;
			}

		
	        for (var i=1; i<lines.length; i++) {
                if (lines[i].length > 1) {
			        var parts = lines[i].split(';'); // supjaustome eilutę
					

					//console.log(parts);
					var new_title = parts[key_index_map["title"]];
					if(new_title != title) best_version = true; // pirmas nauju pavadinimu
					else best_version = false;
					
					id = parts[key_index_map["id"]];
					title = new_title || ""; //title;
					author = parts[key_index_map["author"]] || ""; //author;
					year = parts[key_index_map["year"]] || ""; //year;
					region = parts[key_index_map["region"]] || ""; //region;
					scale = parts[key_index_map["scale"]] || ""; //scale;
					contour = parts[key_index_map["contour"]] || ""; //contour;
					country = parts[key_index_map["country"]] || "LTU";
					nr = parts[key_index_map["nr"]] || ""; //nr;
					file = parts[key_index_map["file"]];
					width = parts[key_index_map["width"]];
					height = parts[key_index_map["height"]];
					
					bounds = parts[key_index_map["bounds"]];
					bounds = bounds ? bounds.split(",").map(function(x) {return parseFloat(x)}) : "";
					rotate = parts[key_index_map["rotate"]].trim();
					description = parts[key_index_map["description"]].trim();
					
					//console.log(title.replace(/%sc%/g, ";"),author.replace(/%sc%/g, ";"), year );

					
    			    si._data.maps[id] = {
					    id       : id
				      , title    : title.replace(/%sc%/g, ";")
				      , author   : author.replace(/%sc%/g, ";")
				      , year     : year
					  , country  : country
				      , region   : region
				      , scale    : scale
				      , contour  : contour
				      , nr       : nr.replace(/%sc%/g, ";") 
				      , file     : file
				      , width    : parseInt(width, 10)
				      , height   : parseInt(height, 10)
				      , bounds   : bounds
				      , rotate   : rotate ? parseFloat(rotate): 0.0
					  , description : description.replace(/%sc%/g, ";")
				    }; 

					if(best_version) { si._data.maps[id].best_version = true;}
					//console.log(si._data.maps[id]);
		        }
            }

			si.ready = true;
			
			if((typeof onload == "function") && (onload != pages[pc._current_page_name].ondataload)) {
				onload(arg);
			} else { // cia buvo reikalingas, kad jeigu neuzhkrovus duomenu pereinu i kita taba - o paskui gryzhtu ir ten nezinia kas ivyke... todel reikia perkrauti...
				pages[pc._current_page_name].ondataload();
			}
	}
	});
}

