(function( ) {

	// jSite object
	jSite = function(ID) {
		//config object
		this.config = {
            ID: ID ,
			socials: {}
		}
		//constructor object
		this.init = (function(){
			return this.config;		
		})();
	}
	

	
	 //////////////////
	// - SOCIALS  -  //______________________________


	// - SET social information about a social service
	jSite.prototype.setSocial = function(obj){
		this.config.socials[obj.social] = {};
		var soc = this.config.socials[obj.social];
		for(var i in obj){
			soc[i] = obj[i];
		}
		return this.config.socials[obj.social];
	}

	//fetch the actual social data
	jSite.prototype.getSocialData = function(obj){
		//check on location callback
		obj.callback	= (this[obj.callback])? this[obj.callback] : window[obj.callback]
       var element = $(this.config.ID +' '+ obj.element );
		var feedUrl = obj.feedUrl

		//force new data
		if(obj.force == true){
			$.get( feedUrl, function(e){
                obj.callback(element, e)
				self.config.socials[obj.social].data = e;

			});
		//check if new request is needed
		}else if(obj.feedUrl != this.config.socials[obj.social].feedUrl){
			this.config.socials[obj.social].feedUrl = obj.feedUrl;

			//get new data
			var self = this;
			
			$.get( this.config.socials[obj.social].feedUrl, function(e,a){
				self.config.socials[obj.social].data = e;
				obj.params.data = e;
				obj.callback(obj.element, e, self, obj.callback2, obj.params)
			});

		
		//if data is present and feedurl is the same then use old data.
		}else if(this.config.socials[obj.social].data != undefined || this.config.socials[obj.social].data != ''){
			obj.params.data = self.config.socials[obj.social].data;
			obj.callback(obj.element, self.config.socials[obj.social].data, obj.callback2, obj.params)
		}
	}
	
//////////////////
// - LAST.FM feed
	jSite.prototype.feedLastfm = function(obj){
		obj.feedUrl   = 'http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&limit=50&user='+this.config.socials.lastfm.user+'&api_key='+this.config.socials.lastfm.key;
		obj.social	  = 'lastfm'
		this.getSocialData(obj)
	}
		jSite.prototype.renderLastfm = function(element , e, self, callback, params){

			var data = $.xml2json(e).recenttracks.track;
			var str = '<ul>';
			for(var i=0; i< data.length; i++){
				str += (
					'<li class="message">'
					  +'<a target="blank" href="'+data[i].url+'"><img src="'+data[i].image[0]+'" alt="'+data[i].title+'" /></a>'
				      +'<h3 class="artist">'+data[i].artist+'</h3>'
				      +'<h4 class="album">album: '+data[i].album+'</h4>'
				      +'<a class="track" href="http://grooveshark.com/#/search/song?q='+data[i].artist+'+'+data[i].name+'" target="blank">song: '+data[i].name+'</a>'
				   +'</li>')

			}str+= "</ul>";
            $(self.config.ID).find(element).html(str);
/*
http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026
*/
		}
		
//////////////////
// - FLICKR feed
	jSite.prototype.feedFlickr = function(obj){
		obj.social	  = 'flickr'
		obj.feedUrl = buildProxyUrl({
			src		: 'http://api.flickr.com/services/feeds/photos_public.gne',
			vars	: '?id='+ this.config.socials.flickr.user+'&format=json'
		})
		this.getSocialData(obj)
	}
		jSite.prototype.renderFlickr = function(element , e, self, callback, params){
			
			var data = e.value.items[0].entry;
			var str = '<ul>';
			for(var i=0; i< data.length; i++){
				str += (
					'<li class="message">'
				      +'<h3 class="name">'+data[i].author.name+'</h3>'
				      +'<p><a target="blank" href="'+data[i].link[0].href+'" class="thumb" '
				      +'style="background:url('+data[i].link[2].href+') 50% 50% no-repeat;" title="'
				      +data[i].title+'"></a></p>'
				      +'</li>');
			}str+= "</ul>";
			
			if(window[callback])window[callback](params);
            $(self.config.ID).find(element).html(str);
            
		}

////////////////
// TWITTER feed
	jSite.prototype.feedTwitter = function(obj){
		obj.social	  = 'twitter'
		obj.feedUrl = buildProxyUrl({
			src		: 'http://twitter.com/statuses/user_timeline/',
			vars	: ''+ this.config.socials.twitter.user+'.json'
		})
		this.getSocialData(obj)
	}

	jSite.prototype.renderTwitter = function(element , e, self, callback, params){
		var data = e.value.items[0].json;
		
		var str = '<ul>';
		for(var i=0; i< data.length; i++){
			data[i].text = formatTwitterText(data[i].text);
			str += (
				'<li class="message">'
			   +'<h3 class="name">'+data[i].user.name+'</h3>'
			   +'<p>'+data[i].text+'</p>'
			   +'</li>');
		}str+= "</ul>";
		if(window[callback])window[callback](params);
		 $(self.config.ID).find(element).html(str);
	}

//////////////////
// Daily Mugshot feed
	jSite.prototype.feedDailymugshot = function(obj){
		obj.social	  = 'dailymugshot';
		obj.feedUrl = buildProxyUrl({
			src		: 'http://www.dailymugshot.com/rss/',
			vars	: ''+ this.config.socials.dailymugshot.type +'/'+ this.config.socials.dailymugshot.user
		})
		this.getSocialData(obj)
	}
	jSite.prototype.renderDailymugshot = function(element , e, self, callback, params){
		var data = e.value.items[0].channel.item // yahoo pipes
		var str = '<ul>'
				+ '<li>'
				+'<img src="'+data.link+'" title="'+data.title+'" />'
				//+'<p class="date caption">'+data.pubDate+'</p>'
				+'</li>'
				+'</ul>';
		if(window[callback])window[callback](params);
		$(self.config.ID).find(element).html(str);
	}

//Example: buildProxyUrl{type:'json',src:'',path:'',vars:''}
buildProxyUrl = function(obj){
	var proxyurl;
	obj.type = obj.type || 'json';
	obj.path = obj.path || '';
	obj.vars = obj.vars || '';
	if(obj.src == '')return false;
	proxyurl = 'http://pipes.yahoo.com/pipes/pipe.run?_id=2e67c87220c2bbc86e56faa24b4cb21b&_render='+obj.type+'&src='+obj.src+''+obj.path+''+obj.vars;
	return proxyurl;
}

renderList = function(obj){
	var str = ''
	for(var i in obj){
		str += (
			'<li class="'+i+'">'
		   	+'<h3>'+i+'</h3>'
		   	+'<p>'+obj[i]+'</p>'
		   +'</li>');
	}
	return str;
}

formatTwitterText = function(str){
	var h = str.split(' ');
	for(var i=h.length-1; i>=0; i--){
		if(h[i].substr(0,1) == '#'){
			h[i] = '<a target="blank" href="http://twitter.com/search?q='+h[i]+'">'+h[i].substr(1,h[i].length)+'</a>'
		}else if(h[i].substr(0,1) == '@'){
			h[i] = '<a class="user" target="blank" href="http://twitter.com/'+h[i]+'">'+h[i].substr(1,h[i].length)+'</a>'
		}else if(h[i].substr(0,4) == 'http' || h[i].substr(0,4) == 'www.'){
			h[i] = '<a target="blank" href="'+h[i]+'">'+h[i].substr(1,h[i].length)+'</a>'
		}
	}
	return h.join(' ')
}

//////////////////
// Nike +
	jSite.prototype.feedNikePlus = function(obj){
		
		obj.feedUrl = buildProxyUrl({
			src		: 'http://nikerunning.nike.com/nikeplus/v2/services/app/run_list.jsp',
			vars	: '&vars=?userID=1409766595'
		})

		obj.social	 = 'nikeplus';

		this.getSocialData(obj)
	}
		jSite.prototype.renderNikePlus = function(element , e, self, callback, params){
			var data = e.value.items // yahoo pipes

			//Runs
			var str;
				str="<div class='global'>"
				  //global stats
				  str += '<ul>';
				  str += renderList(e.value.items[0].runListSummary);
				  str += "</ul>";			
				str += "</div>";
			
			    // detailed run information.
				str +="<div class='runs'>"
			for(var i=e.value.items[0].runList.run.length-1; i >= 0;i--){

				str += '<ul>';
				str += renderList(e.value.items[0].runList.run[i]);
				str += "</ul>";
				
				//GPS
				var runGPS = buildProxyUrl({
					src		: 'http://nikerunning.nike.com/nikeplus/v2/services/app/get_gps_detail.jsp',
					vars	: '?_plus=true&id='+e.value.items[0].runList.run[i].id+'&format=json'
				})/*
				$.get( runGPS, function(e,a){
				var data = e.value.items;
				});*/
				
				str += "<div class='details'><ul><li><strong>*NOTE:</strong> implement GPS information:<p><em>"+runGPS+"</em></p><li></ul></div>"
			}
				str += "</div>";
			
			if(window[callback])window[callback](params);
            $(self.config.ID).find(element).html(str);
        }
	/*
nike ID: 1409766595
nike FEED: http://nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=<YOUR ID HERE>
run info: http://nikerunning.nike.com/nikeplus/v2/services/app/get_gps_detail.jsp?_plus=true&id=<YOUR RUN-ID HERE>&format=json
--
http://nikeplus.nike.com/nikeplus/v1/services/widget/get_public_run_list.jsp?userID=1409766595
http://nikerunning.nike.com/nikeplus/v2/services/app/get_gps_detail.jsp?_plus=true&id=299064666&format=json

http://pipes.yahoo.com/pipes/pipe.run
?_id=2e67c87220c2bbc86e56faa24b4cb21b
&_render=json
&src=


http://nikerunning.nike.com/nikeplus/v2/services/app/get_gps_detail.jsp&vars=?_plus=true&id=2043875054&format=json


*/

	window.jS = jSite;
})();

