Common.prototype.contactListing = function() {
	var data = $.ofd(["id", "message", "email", "token"], "contactListing__");
	data.token = this.currentToken;
	$("div.contact-content form").html("<table style='width:100%; height:100%;'><tr><td valign='middle' align='center'>Sending this person a message...</td></tr></table>");
	this.emailListing(data);	
}


Common.prototype.disableListing_cb = function(e) {
	alert(e.message);
	if(!e.ok) {	
	} else {
		$.modal.close();
	}
	document.location = "index.php?page=listing_followup";
}


Common.prototype.emailListing_cb = function(e) {
	if(!e.ok) {
		alert("couldn't email"); return(false);
		$.modal.close();
	}
	else {
		$('#contact-container .contact-title').html("Success!");
		$("div.contact-content form").html("<table style='width:100%; height:100%;'><tr><td valign='middle' align='center'>Message sent.</td></tr></table>");
	}
	
	
}


Common.prototype.editListing_cb = function(e) {
	if(!e.ok) return(false);
	$.log(e.data, "", true);
	showEditListingModal("edit", e.data);
	for(i in e.data) {
		if(e.data.hasOwnProperty(i)) {
			$("#listing__" + i).val(e.data[i]);
		}
	}
}


Common.prototype.getNewToken = function(e) {
	x.getListingToken();
}

Common.prototype.getImageURLs_cb = function(e) {
	$.log(e, "imageList", true);
	//jonas, you can check the firebug console for details..
	//here is where you make the call to your image preloader
	//the image list (as firebug will show) is e.data..
	//you will want to comment out the first line of this function when you have it working.
	//D
}

Common.prototype.getListingToken_cb = function(e) {
	this.currentToken = e.data;
}

Common.prototype.newListing_cb = function(e) {
	
	//alert(e.message);
	if(!e.ok) {
		x.getListingToken();
		switch(e.data) {
		case  "badlocation" :
			$("#listing__zip, #listing__street").css("background", "#FFAAAA");
		}
	} else {
		$('#contact-container .contact-title').html("Success!");
		$("div.contact-content form").html("<table style='width:100%; height:100%;'><tr><td valign='middle' align='center'>Your listing has been created<p>" + e.message + "</p></td></tr></table>"); 
		//$.modal.close();
	}
}

Common.prototype.newGmarker = function(inListing) {	
	var listing = inListing;
	var point = new GLatLng(parseFloat(listing.y_lat), parseFloat(listing.x_long));
	var marker = new GMarker(point, {"title":listing.title});
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml($.snippet("listing_detail", listing));
	});
	return(marker);
	
}

Common.prototype.removeListing = function(id, s_confirm) {
	x.disableListing(id, s_confirm);
}


Common.prototype.search = function(inPage) {
		var searchParams = {"pageNum":0, "userAddress":$("#search__content").val(), "geoPoint":false};
		if(mapDefaultGeo) {
			searchParams.geoPoint = {"lat":mapDefaultGeo.lat(), "lng":mapDefaultGeo.lng()}
		}
		$.log(searchParams, "", true);
		searchParams.listing_type = [];
		if($("#search__gardeners").is(":checked")) {
			searchParams.listing_type.push($("#search__gardeners").val());
		}
		if($("#search__yards").is(":checked")) {
			searchParams.listing_type.push($("#search__yards").val());
		}
		if($("#search__mulch").is(":checked")) {
			searchParams.listing_type.push($("#search__mulch").val());
		}
		if($("#search__tools").is(":checked")) {
			searchParams.listing_type.push($("#search__tools").val());
		}
		if($("#search__aid").is(":checked")) {
			searchParams.listing_type.push($("#search__aid").val());
		}
		if(typeof(inPage) != "undefined") {
			searchParams.pageNum = inPage;
		}
		if(searchParams.userAddress == "enter address or zip here") {
			searchParams.userAddress = "";
		}
		this.searchListings(searchParams);
}


Common.prototype.searchListings_cb = function(e) {
	if(!e.ok) return(false);
	this.searchResults = e.data;
    var bounds = map.getBounds();
    
    var icoFarmer = new GIcon();
    icoFarmer.image = 'img/icons/mapicon.gardener.png';
	icoFarmer.printImage = 'img/icons/mapicon.gardener.printImage.gif';
	icoFarmer.mozPrintImage = 'img/icons/mapicon.gardener.mozPrintImage.gif';
	icoFarmer.iconSize = new GSize(40,40);
	icoFarmer.shadow = 'img/icons/mapicon.gardener.shadow.png';
	icoFarmer.transparent = 'img/icons/mapicon.gardener.transparent.png';
	icoFarmer.shadowSize = new GSize(60,40);
	icoFarmer.printShadow = 'img/icons/mapicon.gardener.printShadow.gif';
	icoFarmer.iconAnchor = new GPoint(20,40);
	icoFarmer.infoWindowAnchor = new GPoint(20,0);
	icoFarmer.imageMap = [25,0,27,1,27,2,27,3,26,4,27,5,31,6,32,7,33,8,32,9,33,10,34,11,35,12,36,13,37,14,38,15,38,16,37,17,29,18,29,19,28,20,28,21,27,22,26,23,26,24,25,25,25,26,25,27,25,28,25,29,25,30,25,31,25,32,25,33,25,34,25,35,26,36,26,37,26,38,26,39,15,39,14,38,14,37,15,36,15,35,15,34,15,33,15,32,14,31,14,30,14,29,14,28,14,27,13,26,12,25,11,24,10,23,9,22,8,21,7,20,6,19,5,18,5,17,4,16,3,15,1,14,1,13,1,12,1,11,2,10,3,9,4,8,14,7,16,6,19,5,19,4,19,3,19,2,19,1,19,0];
    
    var icoGarden = new GIcon();
	icoGarden.image = 'img/icons/mapicon.yard.png';
	icoGarden.printImage = 'img/icons/mapicon.yard.printImage.gif';
	icoGarden.mozPrintImage = 'img/icons/mapicon.yard.mozPrintImage.gif';
	icoGarden.iconSize = new GSize(40,40);
	icoGarden.shadow = 'img/icons/mapicon.yard.shadow.png';
	icoGarden.transparent = 'img/icons/mapicon.yard.transparent.png';
	icoGarden.shadowSize = new GSize(60,40);
	icoGarden.printShadow = 'img/icons/mapicon.yard.printShadow.gif';
	icoGarden.iconAnchor = new GPoint(20,40);
	icoGarden.infoWindowAnchor = new GPoint(20,0);
	icoGarden.imageMap = [22,0,23,1,24,2,26,3,27,4,28,5,29,6,29,7,37,8,37,9,36,10,36,11,36,12,36,13,36,14,36,15,36,16,36,17,36,18,35,19,35,20,35,21,35,22,35,23,35,24,35,25,35,26,35,27,35,28,34,29,34,30,35,31,35,32,35,33,35,34,35,35,35,36,35,37,35,38,34,39,3,39,3,38,3,37,3,36,3,35,3,34,3,33,3,32,3,31,4,30,6,29,7,28,7,27,8,26,8,25,8,24,9,23,9,22,10,21,10,20,10,19,11,18,11,17,11,16,11,15,11,14,11,13,12,12,9,11,9,10,10,9,11,8,12,7,13,6,14,5,15,4,16,3,17,2,19,1,20,0];

	var icoMulch = new GIcon();
	icoMulch.image = 'img/icons/mapicon.mulch.png';
	icoMulch.printImage = 'img/icons/mapicon.mulchprintImage.gif';
	icoMulch.mozPrintImage = 'img/icons/mapicon.mulch.mozPrintImage.gif';
	icoMulch.iconSize = new GSize(35,35);
	icoMulch.shadow = 'img/icons/mapicon.mulch.shadow.png';
	icoMulch.transparent = 'img/icons/mapicon.mulch.transparent.png';
	icoMulch.shadowSize = new GSize(53,35);
	icoMulch.printShadow = 'img/icons/mapicon.mulch.printShadow.gif';
	icoMulch.iconAnchor = new GPoint(18,35);
	icoMulch.infoWindowAnchor = new GPoint(18,0);
	icoMulch.imageMap = [16,1,20,2,21,3,21,4,24,5,25,6,28,7,28,8,28,9,28,10,31,11,31,12,31,13,31,14,31,15,31,16,32,17,33,18,34,19,34,20,34,21,33,22,33,23,33,24,33,25,31,26,33,27,33,28,33,29,32,30,24,31,22,32,11,33,9,34,9,34,8,33,4,32,2,31,2,30,3,29,2,28,2,27,0,26,0,25,0,24,2,23,0,22,0,21,0,20,0,19,2,18,2,17,2,16,2,15,2,14,4,13,6,12,4,11,4,10,6,9,7,8,7,7,7,6,8,5,11,4,14,3,14,2,15,1];
	
	var icoTools = new GIcon();
	icoTools.image = 'img/icons/mapicon.tools.png';
	icoTools.printImage = 'img/icons/mapicon.tools.printImage.gif';
	icoTools.mozPrintImage = 'img/icons/mapicon.tools.mozPrintImage.gif';
	icoTools.iconSize = new GSize(35,35);
	icoTools.shadow = 'img/icons/mapicon.tools.shadow.png';
	icoTools.transparent = 'img/icons/mapicon.tools.transparent.png';
	icoTools.shadowSize = new GSize(53,35);
	icoTools.printShadow = 'img/icons/mapicon.tools.printShadow.gif';
	icoTools.iconAnchor = new GPoint(18,35);
	icoTools.infoWindowAnchor = new GPoint(18,0);
	icoTools.imageMap = [24,0,25,1,25,2,25,3,26,4,34,5,34,6,34,7,34,8,34,9,34,10,34,11,34,12,33,13,33,14,32,15,31,16,30,17,29,18,28,19,28,20,28,21,29,22,29,23,29,24,27,25,26,26,24,27,23,28,21,29,13,30,13,31,12,32,11,33,9,34,7,34,6,33,6,32,6,31,6,30,6,29,7,28,7,27,8,26,9,25,10,24,9,23,8,22,8,21,7,20,5,19,5,18,3,17,3,16,2,15,2,14,1,13,1,12,0,11,0,10,0,9,1,8,2,7,4,6,5,5,6,4,7,3,9,2,11,1,14,0];
	
	var icoAid = new GIcon();
	icoAid.image = 'img/icons/mapicon.aid.png';
	icoAid.iconSize = new GSize(50,40);
	icoAid.shadow = 'img/icons/mapicon.aid.shadow.png';
	icoAid.shadowSize = new GSize(74,40);
	icoAid.iconAnchor = new GPoint(18,35);
	icoAid.infoWindowAnchor = new GPoint(30,0);
	icoAid.imageMap = [38,0,40,1,42,2,46,3,46,4,47,5,47,6,46,7,45,8,30,9,30,10,30,11,31,12,32,13,32,14,33,15,34,16,34,17,36,18,37,19,37,20,39,21,40,22,40,23,41,24,42,25,45,26,47,27,49,28,49,29,49,30,49,31,49,32,49,33,48,34,48,35,47,36,45,37,42,38,40,39,25,39,23,38,21,37,19,36,18,35,16,34,15,33,13,32,12,31,11,30,9,29,8,28,7,27,6,26,5,25,5,24,4,23,3,22,3,21,2,20,2,19,1,18,1,17,0,16,0,15,0,14,0,13,0,12,1,11,2,10,2,9,3,8,3,7,4,6,5,5,5,4,6,3,6,2,7,1,8,0,38,0];
    
    map.clearOverlays();
	map.setCenter(new GLatLng(e.data.center.y_lat, e.data.center.x_long), 12);
    //G_DEFAULT_ICON.image = "img/icon.gardener.png";
	//$.log(e,"and lo, there was a response",true);
	for(i in e.data.listings) if(e.data.listings.hasOwnProperty(i)) {
			var listing = e.data.listings[i];
			if(listing.title.length == 0) listing.title = "listing";
			var point = new GLatLng(parseFloat(listing.y_lat), parseFloat(listing.x_long));
			
			switch(listing.listing_type) {
			case "farmer" :
				listing.iconURL = "img/icons/mapicon.gardener.png";
				var marker = new GMarker(point, {"title":listing.title, "icon":icoFarmer});
				break;
			case "tools" : 
				listing.iconURL = "img/icons/mapicon.tools.png";
				var marker = new GMarker(point, {"title":listing.title, "icon":icoTools});
				break;
			case "mulch" : 
				listing.iconURL = "img/icons/mapicon.mulch.png";
				var marker = new GMarker(point, {"title":listing.title, "icon":icoMulch});
				break;
			case "aid" :
				listing.iconURL = "img/icons/mapicon.aid.png";
				var marker = new GMarker(point, {"title":listing.title, "icon":icoAid});
				break;
			default :
				listing.iconURL = "img/icons/listingsicon.yard.png";
				var marker = new GMarker(point, {"title":listing.title, "icon":icoGarden});
			}
			map.addOverlay(marker);
			//$.log(listing, "before making detail snippet", true);
			marker.bindInfoWindowHtml($.snippet("listing_detail", listing));
			listing.marker = marker;
	}
	
	var html = $.snippet("listing_search_results", {"list":e.data.listings});
	$("#searchResultsPanel").html(html);
	
	$.listingcount = $(e.data.listings).size();
	
	if( $.listingcount <= 4){
		if ($.cookie('noresults') == null){
			$('.nolistingsbox').css('display','block');
		};
		$(".nolistingsbox span.listingcount").html($.listingcount);	
	} else {
		$('.nolistingsbox').css('display','none');	
	}

}


Common.prototype.saveListing_cb = function(e) {
	alert(e.message);
	if(!e.ok) {
		switch(e.data) {
		case  "badlocation" :
			$("#listing__zip, #listing__street").css("background", "#FFAAAA");
		}
	} else {
		$.modal.close();
	}
}

Common.prototype.showMarker = function(inID) {
	//$.log(this.searchResults.listings, "", true);
	for(i in this.searchResults.listings) if(this.searchResults.listings.hasOwnProperty(i)) {
			if(this.searchResults.listings[i].id == inID) {
				this.searchResults.listings[i].marker.openInfoWindowHtml($.snippet("listing_detail", this.searchResults.listings[i]));
				
			}
	}
}
