// Load Options
var loadPlacesFlag	= true;
var loadRefsFlag	= true;
var loadStreetsFlag	= false;

// Ajax URLs
var main_search_ajax_url = ROOT + "ajax/map.search.place.php";
var places_ajax_url 	 = ROOT + "ajax/places.php";
var streets_ajax_url	 = ROOT + "ajax/getStreets.php";

// Icons
var message_icon   = "<img src='"+ROOT+"images/icons/comment.png' alt=' '/> ";
var generic_icon   = "<img src='"+ROOT+"images/markers/mini/mini_green_Z.png' alt=' '/>";
var street_icon    = "<img src='"+ROOT+"images/icons/map.png' alt=' '/>";
var reference_icon = "<img src='"+ROOT+"images/reference_icon.png' alt=''/>";


// Bits of text
var text_loading 		= " Se incarca ...";
var text_title_suffix	= " - Click pentru detalii";
var text_idle			= message_icon + "Click pe pictograme (" + generic_icon + ") pentru mai multe detalii despre local.";

var text_not_found		= "Nu am gasit niciun rezultat."

// Loaders
var white_bg_loader = "<img src='"+ROOT+"images/ajax-loader_bg-FFF-fg-FF6600.gif' alt=' '/>";
var _333_bg_loader  = "<img src='"+ROOT+"images/ajax-loader-bg_333-fg-FF6600.gif' alt=' '/>";

// Helper
var jsv0		   = "javascript:void(0)";

// Color
var streets_highlight_color = "#FF9900";

$(function(){
	// Debugging
	if(typeof(loadFirebugConsole) != "undefined")
		window.loadFirebugConsole();
	
	
	// Event associations
	
	$(".places_clear_button").click(clearPlaceSearch);
	
	$(".streets_clear_button").click(clearStreetSearch);
	/*
	$(".advanced_search_button").click(togglePlacesFilter);
	*/
	// Initialisers
	
	// Private initialisation variables
	var places_div_id   	= "map_container_inner";


	// Map Options
	var map_options = {
		continuousZoom  : true,
		scrollWheelZoom : true,
		startLat		: ((typeof(start_lat) == "undefined")?default_lat:start_lat),
		startLng		: ((typeof(start_lng) == "undefined")?default_lng:start_lng),
		startZoom		: ((typeof(start_zoom) == "undefined")?17:start_zoom),
		afterLoad 		: function(map){
							if(loadPlacesFlag) 
								map.addPlaces(places_ajax_url, places_options);
								
							if(street_search)
								filterStreets();
							},
		zoomEnd			: updateFilterLinks,
		moveEnd			: updateFilterLinks
		};


	// Places Options
	var places_options = {
		ajaxOptions : {
						f : "map"
						},
		afterLoad 	: function(map){
						updateStatus(text_idle);
						placeSearchIndicator(false);
						streetSearchIndicator(false);
						if(filtered)
							togglePlacesFilter();
						else
							if(main_search)
								mainSearch();

						
						
						if(loadStreetsFlag){
							updateStatus(text_idle);
							map.addStreets(streets_ajax_url, street_options);
						}else{
								
							}
						},
		getIcon 	: function(item, icon){
						if(typeof(item.categories) != "undefined" && typeof(item.categories[0]) != "undefined"){
							var first	= item.categories[0];
							var categ	= places_categories[item.categories[0].toString()];
							if( categ.color && categ.letter){
								var image = icon.image =  ROOT + "images/markers/mini/mini_"  + categ.color + "_" + categ.letter + ".png";
								item.icon_img = "<img src='"+image+"' alt=''/>";
								}
							}else{
								// some contingency in case no category is provided by icon
								// but it could be re-imlpemtened in here
								}
						return icon;
						},
		getTitle	: function(item, title){
						if(typeof(item.categories) != "undefined" && typeof(item.categories[0]) != "undefined"){
							var first	= item.categories[0];
							var categ	= places_categories[first];
							title 	= categ.name + " - " + title + text_title_suffix;
							}
						return title;
						},
		
		// event handles
		mouseout	: function(){
						updateStatus(text_idle);
						},
		mouseover	: function() {
						updateStatus(this.item.icon_img + " " + this.item.name + " " + text_title_suffix);
						},
						
		click		: function(){
						// Already have info
						var loading  = "<div class='infoWindow'>";
							loading += "<h2>"  + this.item.name  + "</h2>";
							loading += "<br/>" + white_bg_loader + text_loading + "</div>";
							
						this.openInfoWindowHtml(this.html?this.html:loading);
						if(this.html) 
							return;
						
						// Load info from get
						if(!this.loading){
							this.loading = true;
							var me = this;
							
							$.get(
								ROOT + "ajax/places.php",
								{
									f	: "single_html",
									id	: me.item.id
								},
								function(place_data_html){
									me.closeInfoWindow();
									var html = "<div class='infoWindow'>" + place_data_html +"</div>";
									me.html = html;
									me.openInfoWindowHtml(html);
									}
								);
							}
						},
		dblclick	: function(){
						document.location = this.item.url;
						},
		filterOpts	: { 
						onInit	: filtered,			  // these variables are defined in layout/places_adv_search_box.php
						check	: placesAdvCondition, // these variables are defined in layout/places_adv_search_box.php
						handle	: function(results, map){
									handlePlaceResults(results,map);
									updateFilterLinks();
									return false;
									}
						}
		
		};
	
	// Street Options
	var street_options = {
		ajaxOptions : {
						for_maps : "true"
						},
		afterLoad	: function(){
						streetSearchIndicator(false);
						if(!main_search)
							updateStatus(text_idle);
						

						},
		mouseover	: function(){
						var item = this.item;
						var status = "";
						status += street_icon + " <a href='#'>"+ item.n + "</a>";
						status += ", "+zones[item.z];
						status += " ("+this.getParsedLength()+")";
						status += (item.b)?" Autobuze: "+item.b:"";
						updateStatus(status);
						this.highlight(streets_highlight_color);
						},
		mouseout	: function(){
						updateStatus(text_idle);
						this.unHighlight();
						}
		}
		
	// Referenced Landmarks Options
	
	var ref_options = {
		//event
		click	: function(){
					document.location = ROOT + this.item.url;
					},
					
		getTitle	: function(item, title){
						title = "<a href='"+item.url+"'>"+title+"</a>";
						return title;
						},
		afterLoad	: function(map, controller){
						var refs_class = "refResult";
						var html = controller.getHTML(refs_class);
						
						$("#shortcuts").html(html);
							
						$("#shortcuts a."+refs_class).click(function(){
							var id = $(this).attr("id").split("_")[1];
							var m = places_map._controllers.refs.getMarkerByID(id);
							places_map.panTo(m.getLatLng());
							});
						}
		}
	// Results 
	rc = new resultsController("results");
	
	
	// Map
	places_map = new Map(document.getElementById(places_div_id), map_options);
	

	if(loadRefsFlag) 
		places_map.addRefPlaces(refLandmarks, ref_options);  //or url
	
	//toggleOptions();
	});


function toggleOptions(){
	var t = $("#advanced_options");
	if(!t.is(":visible"))
		t.fadeIn("normal");
	else
		t.fadeOut("normal");
	}
	



function navShow(class_ie_name, menu_item){
	$(".advanced_content > div:visible").slideUp();
	$("."+class_ie_name).slideDown();
	$("#advanced_options_inner ul.nav li a").removeClass("sel");
	$(menu_item).addClass("sel");
	}
	
$(function(){
	$("#btn_options_toggle").click(toggleOptions);
	
	$("#btn_adv_search").click(function(){navShow("advanced_search_div",this)});
	$("#btn_legend").click(function(){navShow("legend", this)});
	$("#btn_visibility").click(function(){navShow("visibility",this)});
	$("#btn_export").click(function(){navShow("export", this)});
	$("#btn_social").click(function(){navShow("social", this)});
	});
