//Added method to the Array function to return the element number if found and returns -1 if not found
Array.prototype.exists = function(o) {
for(var i = 0; i < this.length; i++)
   if(this[i] === o)
	 return i;
return -1;
}




//
//Toggle Services on and Off
//
function toggleService(svc) {
	if (myServices.exists(svc) != -1) {
		//The service is currently on... so let's turn it off
		myServices.splice(myServices.exists(svc),1);
		$("a.f-" + svc).css("background-position","0px -32px");
	} else {
		//The service is currently off... so let's turn in ON
		myServices.splice(0,0,svc);
		$("a.f-" + svc).css("background-position","0px 0px");
	}
	//alert(myServices);
}//end of the toggleService function




/*
 * JavaScript Pretty Date
 * Copyright (c) 2008 John Resig (jquery.com)
 * Licensed under the MIT license.
 */

// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(date_str){
  var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," ");
  var seconds = ((new Date - new Date(time)) / 1000) + 14400;
  var token = 'ago', list_choice = 1;
  if (seconds < 0) {
    seconds = Math.abs(seconds);
    token = 'from now';
    list_choice = 2;
  }
  var i = 0, format;
  while (format = time_formats[i++]) if (seconds < format[0]) {
    if (typeof format[2] == 'string')
      return format[list_choice];
    else
      return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token;
  }
  return time;
};
var time_formats = [
  [60, 'just now', 1], // 60
  [120, '1 minute ago', '1 minute from now'], // 60*2
  [3600, 'minutes', 60], // 60*60, 60
  [7200, '1 hour ago', '1 hour from now'], // 60*60*2
  [86400, 'hours', 3600], // 60*60*24, 60*60
  [172800, 'yesterday', 'tomorrow'], // 60*60*24*2
  [604800, 'days', 86400], // 60*60*24*7, 60*60*24
  [1209600, 'last week', 'next week'], // 60*60*24*7*4*2
  [2419200, 'weeks', 604800], // 60*60*24*7*4, 60*60*24*7
  [4838400, 'last month', 'next month'], // 60*60*24*7*4*2
  [29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
  [58060800, 'last year', 'next year'], // 60*60*24*7*4*12*2
  [2903040000, 'years', 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12
  [5806080000, 'last century', 'next century'], // 60*60*24*7*4*12*100*2
  [58060800000, 'centuries', 2903040000] // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100
];





//
//Setup the scrollable plugin for jQuery
//
function setScroller(){
	//Note: the current size of a scrolling bit of content is 150px + 30px spacing
	scrollWidth = 179 * parseInt((document.body.clientWidth - 30) / 180); 
	
	$("div.scrollable").css("width",scrollWidth);
	$("div.about").css("width",document.body.clientWidth);

	xyzWidth = parseInt((document.body.clientWidth - $("div.elements").children("div").width())/2);
	//change "about" elements to width of the page
	$("div.elements").children("div").each(function(i) {
		$(this).css("padding-left",xyzWidth);
		$(this).css("padding-right",xyzWidth);
	} );
}//end function setScroller






//
// The core friendFeed() function was adapted from this script by john girvin, june 2008 and is in the public domain
//
function friendFeed(){
	//clear the existing contents and put in place a loader
	xOffset = parseInt($("#content").width()/2)-80; // we subtract the width of the graphic
	yOffset = parseInt($("#content").height()/2)-46; // we subtract the height of the graphic
	$("#box-container").empty();
	$("#noresults").hide();
	$("#loader").show();
	$("#loader").css("margin-left",xOffset);
	$("#loader").css("margin-top",yOffset);
	
	
    // friendfeed: number of entries to retrieve
    var num = 50;


	if(terms =='' && myServices == ''){
		//user is just trying to view some items, possibly items of a specific type
		var JSONstring = 'http://friendfeed.com/api/feed/user/' + usr + '?num=' + num + '&service=' + myServices.toString() + '&callback=?';
	} else {
		//user is trying to search so let's build the search AJAX string
		//alert("searching for \""+terms+"\" on the following services:"+myServices.toString());
		var JSONstring = 'http://friendfeed.com/api/feed/search?q=' + terms + '%20from:' + usr + '%20service:' + myServices.toString() + '&num='+ num + '&callback=?';
	}


    // call friendfeed api
    $.getJSON(JSONstring,
	//$.getJSON('http://friendfeed.com/api/feed/search?q=modea,from:davidcatalano,service:facebook&callback=?',

    // build content from api results
    function(data) {
        // reference container to hold generated content
        var container = $('#box-container');

		//hide the loader
		$("#loader").hide();
		
		
		var itemCount =0;
        // loop for each friendfeed entry retrieved
        $.each(data.entries,
        function(i, entry) {
            // ignore entry if it is marked as 'hidden'
            if (entry.hidden != true) {

				// reference the service details
				var svc = entry.service;

				// established the updated ago language
				     if (svc.name == "Twitter") { historicalSN = "tweeted"; }
				else if (svc.name == "delicious") { historicalSN = "bookmarked"; }
				else if (svc.name == "blog") { historicalSN = "blogged"; }
				else if (svc.name == "YouTube") { historicalSN = "faved"; }
				else if (svc.name == "SmugMug") { historicalSN = "uploaded"; }
				else if (svc.name == "Digg") { historicalSN = "dugg"; }
				else if (svc.name == "Facebook") { historicalSN = "updated"; }
				else { historicalSN = ""; }
				
				//If the service is smugmug then replace the "title" with the actual image
				if (svc.name == "SmugMug") {entry.title = "<img src=\"" + entry.media[0].content[0].url + "\" />";}
				if (svc.name == "YouTube") {entry.title = entry.title + "<br/><img src=\"" + entry.media[0].thumbnails[0].url + "\" />";}
				
				
				var t = '';
				t += '<div class="box"><img src="lifestreamed/images/item-';
				t += svc.name;
				//alert(svc.name);
				t += '.png" width="30" height="30" alt="" class="icon"/><p>';
				t += '<a target="_new" href="'+entry.link+'">';
				t += entry.title;
				t += '</a>';
				t += '</p><span class="age">'
				t += historicalSN + ' ';
				t += prettyDate(entry.published);
				t += '</span></div>';
				container.append(t);
				
				itemCount ++;
            }
        });
		if(itemCount == 0) {
			$("#noresults").show();
			$("#noresults").css("margin-left",xOffset);
			$("#noresults").css("margin-top",yOffset);
		}
    });
}//end of friendFeed function