
function initFeed ($service, $id, $feed) {
	var feed = new google.feeds.Feed($feed);
	feed.setNumEntries(3);
	feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
	feed.load(function(result) {
		if (!result.error) {
			for (var i = 0; i < result.feed.entries.length; i++) {
				var entry = result.feed.entries[i];
				if (entry.title == '') {
					continue;
				}
				// what is it?
				switch ($service) {
					default :
						/*var video = jQuery(entry.xmlNode).find('[nodeName=media:content]').attr('url');
						if (video) {
							var tube = '<video width="320" height="265" src="' + video + '">';
							jQuery('#' + $id).append(tube);
						}*/
						var thumbnail = jQuery(entry.xmlNode).find('[nodeName=media:thumbnail]').attr('url');
						if (thumbnail) {
							jQuery('#' + $id).append('<a href="'+entry.link+'"><img src="'+thumbnail+'" alt="" title="'+entry.title+'" class="thumbnail" /></a>');
						} else {
							jQuery('#' + $id).append('<a href="'+entry.link+'" class="quiet">'+entry.title+'</a>');
							jQuery('#' + $id).append('<br />');
						}
						/*if (entry.title != entry.content) {
							jQuery('#' + $id).append(' - <span class="quiet">'+entry.contentSnippet+'</span>');
						}*/
				}
			}
		}
	});
}

function showMap (id, address) {
	// defaults
	var options = {
		zoom: 5,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	};
	// lat,lng ?
	var latlng = address.match(/([\-\d\.]+)\,([\-\d\.]+)/);
	if (latlng) {
		var latlng = new google.maps.LatLng(latlng[1], latlng[2]);
		options['center'] = latlng;
		options['zoom'] = 16;
	}
	// draw map
	var map = new google.maps.Map(document.getElementById(id), options);
	// set marker or if no lat,lng lookup address
	if (latlng) {
		var marker = new google.maps.Marker({
			position: latlng,
			map: map
		});
	} else {
		// mark address
		var geocoder = new google.maps.Geocoder();
		geocoder.geocode( {'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
					map.setCenter(results[0].geometry.location);
					// show the marker if it looks accurate
					if (address.match(/([\s\,])/)) {
						var marker = new google.maps.Marker({
							map: map, 
							position: results[0].geometry.location
						});
					}
				}
			} else {
				jQuery("#map").remove();
			}
		});
	}
}

function block (id, obj) {
	jQuery(obj).click(function() { return false; });
	jQuery.ajax({ url: jQuery(obj).attr('href') });
	jQuery("#" + id).slideUp();
}

function follow (obj) {
	jQuery(obj).click(function() { return false; });
	jQuery.ajax({ url: jQuery(obj).attr('href') });
	//jQuery(obj).siblings(".follow").andSelf().toggle();
	jQuery(obj).addClass('fg-button-icon-right');
	jQuery(obj).append('<span class="ui-icon ui-icon-check"></span>');
}

function spam ($username) {}

function altprofile (service, id, username) {
	switch (service) {
		case 'twitter' :
			jQuery.getJSON("http://twitter.com/users/show.json?screen_name=" + username + "&callback=?", function (data) {
				jQuery("#" + id).append('<a href="http://konfect.dev/explore/http://twitter.com/'+ username +'"><img src="'+ data.profile_image_url +'"></a>');
				//jQuery("#" + id).append(data.name);
			});
			break;
	}
}

var vcard = [];
var microformat = {
	addPair : function (id, name, value) {
		if (!vcard[id]) {
			vcard[id] = [];
		}
		if (!vcard[id][name]) {
			vcard[id][name] = [];
		}
		vcard[id][name][vcard[id][name].length] = value;
	},
	yq : function (id, url) {
		var q = 'select * from microformats where url = "' + url + '"';
		var yUrl = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURI(q) + '&format=json&callback=?';
		jQuery.getJSON(yUrl, function (data) {
			// Inspired by http://github.com/shapeshed/hcardme
			jQuery(data.query.results.result.feed.adjunct).each(function(adjunct){
				if (this.id == "com.yahoo.page.uf.hcard") {
					// Parse items
					jQuery(this.item).each(function(){
						jQuery(this.type.item).each(function(){
							switch (this.rel) {
								case "vcard:org":
									microformat.addPair(id, this.rel, this.type.meta.content);
									break;    
								case "vcard:adr":
									jQuery(this.type.meta).each(function(meta) {
										microformat.addPair(id, this.property, this.content);
									});
									break;
							}
						});
						// Parse meta items
						jQuery(this.type.meta).each(function(){
							microformat.addPair(id, this.property, this.content);
						});
					});
				}
			});
			microformat.makeCard(id);
		});
	},
	makeCard : function (id) {
		if (!vcard[id]) {
			return;
		}
		if (vcard[id]['vcard:org']) {
			jQuery('#' + id + ' .org').append(vcard[id]['vcard:org'][0]);
		}
		if (vcard[id]['vcard:fn']) {
			jQuery('#' + id + ' .fn').append(vcard[id]['vcard:fn'][0]);
		}
		if (vcard[id]['vcard:title']) {
			jQuery('#' + id + ' .title').append(vcard[id]['vcard:title'][0]);
		}
		jQuery('#' + id).show();
	}
}

function facebook () {
	var api = FB.Facebook.apiClient;
	api.requireLogin(function(exception){
		location.href = "/socialcake/facebook/login";
	});
}

function installOpenSearch () {
	if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
		window.external.AddSearchProvider("http://konfect.com/opensearch.xml");
	} else {
		alert("This plugin requires a browser that supports OpenSearch.");
	}
}


window.onload = function () {
	if (typeof jQuery != 'undefined') {
		jQuery('a[href][rel*=external]').each(function(i){
			this.target = "_blank";
		});
	}
};
