// Instance switcher
Rimory.inst = {};
Rimory.activate_state = function (state, args) {
	try { 
		if(Rimory.map.state == state || state == false) {
			if(Rimory.inst[Rimory.map.state].end()) {
				$("#menu li.app").filter(".down").removeClass("down");
				Rimory.map.state = "_basic";
				$("#b_app").addClass("down");
				Rimory.inst[Rimory.map.state].view_change();
				document.location.hash = "#";
			}
			return;
		}
		if(Rimory.inst[state] && (Rimory.map.state == "_basic" || Rimory.inst[Rimory.map.state].end(true)) ) { 
			$("#menu li.app").filter(".down").removeClass("down");
			if(!$("#menu").find("a[href*=/app/" + state + "]").parent().addClass("down").size()) $("#b_app").addClass("down");
			Rimory.map.state = state;
			if(state != "_basic") document.location.hash = "#/app/" + state;
			if(!args) args = [];
			Rimory.inst[Rimory.map.state].start.apply(Rimory.inst[Rimory.map.state], args);
		}
	} catch (e) { Rimory.error("Недефиниран state: `" + state + "`"); }
};

// Resig's Simple Inheritance
(function() {
	var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
	this.Class = function(){};
	Class.extend = function(prop) {
		var _super = this.prototype;
		initializing = true;
		var prototype = new this();
		initializing = false;
		for (var name in prop) {
			prototype[name] = typeof prop[name] == "function" &&
			typeof _super[name] == "function" && fnTest.test(prop[name]) ?
			(function(name, fn){
				return function() {
					var tmp = this._super;
					this._super = _super[name];
					var ret = fn.apply(this, arguments);       
					this._super = tmp;
					return ret;
				};
			})(name, prop[name]) :
			prop[name];
		}
		function Class() {
			if ( !initializing && this.init )
				this.init.apply(this, arguments);
		}
		Class.prototype = prototype;
		Class.constructor = Class;
		Class.extend = arguments.callee;
		return Class;
	};
}) ();


// BASIC APPLICATION CLASS
var application = Class.extend({
	init : function(p, h) {
		this.page = p ? p : false;
		this.hide = h ? true : false;
	},
	start : function() {
		if(this.page == false) this.page = "/app/" + Rimory.map.state + "/";
		Rimory.map.hide_bubble();
		var _this = this;
		//$("#map_form .apps ul li").slideUp();
		$("#map_form .loading").show();

			if($("#map_form").is(":hidden")) {
				$("#map_form").show();
			}

		$("#map_form .app").load(this.page, function () {
			_this.form_loaded();

			$("#app_title").html($("a[href*=/app/" + Rimory.map.state + "]").text());

			$("#map_form .loading").hide();
			$("#map_form .app").show();
				//$("#app_title").html("<a href='#' onclick='Rimory.activate_state(false); return false;'>" + $("#app_title").text() + "</a>&nbsp;&raquo;&nbsp;" + $("a[href*=/app/" + Rimory.map.state + "]").text());
			//});
		});
	},
	end : function(keep) {
		/*
		$("#map_form .app").slideUp(function () {
			$(this).html("");
			$("#app_title").html($("#app_title a").html());
			$(".apps ul li").slideDown();
		});
		*/
		if(!keep) $("#map_form").hide();
		$("#map_form .app").html("");

		$("#map_form .app").hide().html("");
		$("#app_title").html($("#app_title a").html());
		//$(".apps ul li").slideDown();
		Rimory.map.state = "_basic";
		return true;
	},
	map_click : function (lat, lng, title, icon, id, opts, group, mrk) {
		// is marker or location
		if(!id && group != "search") return;
		var url = id ? "/poi_summary/" + id : "/location_summary/" + lat + "," + lng; 
		Rimory.map.show_bubble(new GLatLng(lat,lng), { 
			title		: lat + "<br />" + lng, 
			url			: url, 
			callback	: function () { 
				if(id && title) $("#addr").html(title);
				else {
					Rimory.map.geocoder.getLocations(new GLatLng(lat,lng), function(data) {
						if(data.Status.code == 200) { $("#addr").html(data.Placemark[0].address); }
					}); 
				}
			}
		});
	},
	view_change : function () {
		if(!this.hide) {
			var url = "/update_view/";
			var tmp = Rimory.map.instance.getBounds();
			url += tmp.getSouthWest().lat() + "," + tmp.getSouthWest().lng() + ",";
			url += tmp.getNorthEast().lat() + "," + tmp.getNorthEast().lng();
			var str = "0";
			$("ul.context").find("a.checked[id^='tip']").each(function () { str += "," + $(this).attr("rel"); });
			url += "/" + str;
			var str = "0";
			$("ul.context").find("a.checked[id^='m']").each(function () { str += "," + $(this).attr("rel"); });
			url += "/" + str;
			var str = "-1";
			$("ul.context").find("a.checked[id^='cv']").each(function () { str += "," + $(this).attr("rel"); });
			url += "/" + str;

			var _this = this;

			Rimory.map.getHash();
			$.getJSON(url, function (data) {
				if(!_this.mgr) _this.mgr = new MarkerManager(Rimory.map.instance);
				_this.mgr.clearMarkers();
				try { Rimory.map.rem_marker_group("view"); } catch(e) { };
				var batch = [];
				for(i in data) {
					batch.push(Rimory.map.add_marker(data[i].pos[1], data[i].pos[0], data[i].name, data[i].icon, data[i].id, false, "view"));
				}
				_this.mgr.addMarkers(batch, 2);
				_this.mgr.refresh();
				if(Rimory.hash && Rimory.hash.poi) {
					setTimeout(function () {
						Rimory.inst._basic.map_click(Rimory.hash.poi.lat,Rimory.hash.poi.lng,'',false,Rimory.hash.poi.id);
						delete Rimory.hash.poi;
					}, 500);
				}
			});
		}

		if(!Rimory.search) {
			var url = "/places/";
			var tmp = Rimory.map.instance.getBounds();
			url += tmp.getSouthWest().lat() + "," + tmp.getSouthWest().lng() + ",";
			url += tmp.getNorthEast().lat() + "," + tmp.getNorthEast().lng();
			$("#view_panel > .content").load(url);
		}
	},
	form_loaded : function (obj) {
		;
	}
}); 

Rimory.inst._basic = new application();
Rimory.map.state = "_basic";
$(function () {
	if(document.location.hash.indexOf("app") != -1) {
		var a = document.location.hash.toString();
		a = a.replace("#/app/","");
		Rimory.activate_state(a);
	}
});