var map;
var geocoder;
var last_id = -1;
 
function module_init() {
	if(GBrowserIsCompatible()) {
		geocoder = new GClientGeocoder();
	}	
	if(ft_id > 0) {
		show_details(ft_id);
	}
}

function show_details(id) {
	if(last_id > 0) {
		hide_details(last_id);
	}
	var id_to_hide = "ft_" + id;
	var id_to_show = "ft_full_" + id;
	$(id_to_hide).style.display = "none";
	$(id_to_show).style.display = "block";
	last_id = id;

	// google map
	if(map_create("gmap_" + id), "small") {
		get_company_location(id);
	}

	// Incrementing "view details" clicks count
	var url = "./index.php?mod=subcategories&method=listings_details_clicks&id=" + parseInt(id); 
	new Ajax.Request(url, {method: 'get'});
}

function hide_details(id) {
	var id_to_hide = "ft_full_" + id;
	var id_to_show = "ft_" + id;
	$(id_to_hide).style.display = "none";
	$(id_to_show).style.display = "block";
	last_id = -1;	
}

// Ajax request
function get_company_location(id) {
	var url = "./index.php?mod=subcategories&method=get_company_location&id=" + parseInt(id); 
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: get_info_success
	});
}

function get_info_success(transport) {
	if(transport.responseText != "Error") {
		showLocation(transport.responseText);
	}
}

/*** Google Map Code ***/
function map_create(container, size) {
	if(GBrowserIsCompatible()) {
		if(typeof(map) != "undefined") {
			delete map;
		}
		map = new GMap2($(container));
		map.my_type = size;
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl(true));
		return true;
	} else {
		return false;
	}
}
function addAddressToMap(response) {
	var magnifer = [1,3,5,6,8,12,13,14,15];
	if ( !response || response.Status.code != 200 ) {
		//document.getElementById( "err_message").innerHTML = "ERROR";
	} else {
		var place = response.Placemark[0];
		var latlng = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
		var marker = new GMarker(latlng);
		var adress = response.name.split(",");
		var decription = "", i;
		for(i = 0; i < adress.length; i++) {
			decription += (decription != "")? "<br />" + adress[i]: adress[i];  
		}
		map.setCenter(latlng);
		map.addOverlay(marker);
		marker.description = "<div class=\"description\">" + decription + "</div>";
		if(map.my_type == "large") {
			GEvent.addListener( marker, "mouseover",
				function() {
					this.openInfoWindowHtml(this.description);
				}
			);
		}
		map.setZoom(magnifer[place.AddressDetails.Accuracy])
	}
}

function showLocation(address) {
	//document.getElementById( "err_message").innerHTML = "";
	if ( geocoder ) {
		geocoder.getLocations(address, addAddressToMap);
	}
	return false;
}

function view_larger_map(id) {
	var strWindowFeatures = "width=602,height=440,menubar=0,location=0,resizable=0," +
		"scrollbars=0,status=0,dependent=1,titlebar=0";
	var url = "./index.php?mod=subcategories&method=gmap_popup&id=" + id;
    window.open(url, "", strWindowFeatures);
}
