var tunemyfeed = {
	
	init : function () {
		this.getsong();
		this.fbconnect();
		this.stats();
		this.postback();
		this.spotifylinks();
	},
	
	fbconnect : function () {
		FB.init("beab2efbe052caa904e61c56f20cdb96", "/xd_receiver.htm", { permsToRequestOnConnect : "publish_stream" });
	},
	
	spotifylinks : function () {
		$("a[rel=spotify]").click(function(){
			spotifyWindow = window.open(this.href);	
			setTimeout('spotifyWindow.close()', 1000);
			return false;
		});
	},
	
	postback : function () {
		$('.postback a').click(function(){
			var fbid = $(this).parent().parent().parent().parent().attr('id').replace('status-','');
			var _thisparent = $(this).parent();
			var url = _thisparent.find('.url').text();
			_thisparent.html('POSTING TO FACEBOOK');
			$.ajax({
			  	url: '/ajax/setstatus',
				async: true,
				data: ({
					fbid: fbid,
					url: url
				}),
				type: "POST",
				success: function(result) {
					_thisparent.html('POSTED!');
				}
			});
			return false;
		});
	},
	
	getsong : function () {
		var items = 5;
		var countrycode = $("body").attr('id').replace('country-','');
		var counter = 0;
		$('#feed-items li').each(function(){
			phrase = $(this).find('.status').text();
			var liid = $(this).attr('id');
			$.ajax({
			  	url: '/ajax/getsong',
				async: false,
				data: ({
					phrase: phrase,
					country: countrycode
				}),
			 	type: "POST",
			  	success: function(translation) {
					if(translation.length>1){
						$('#'+liid).find('.result').html(translation);
						$('#'+liid+" .trackinfo").hover(
						  	function () {
						    	$(this).addClass("hover");
						  	},
						  	function () {
						    	$(this).removeClass("hover");
						  	}
						);						
					}else{
						$('#'+liid).remove();
					}
			  	},
				timeout: 8000
				
			});
		});
		$('#feed-items li').each(function(){
			counter++;
			if(counter>items){
				$(this).remove();
			}
		});
		$('#results-header').fadeOut('300',function(){
			$(this).html($('#results-header-final').html());
			$('#results-header').fadeIn('800');	
		});
		$('#results').fadeIn();
		$('#loader').fadeOut();
	},
	
	stats : function () {
		$.ga.load('UA-12750399-1');
	}
};
$(document).ready(function() {
	tunemyfeed.init();
});




/*!
 * http://www.shamasis.net/projects/ga/
 * Refer jquery.ga.debug.js
 * Revision: 13
 */
(function($){$.ga={};$.ga.load=function(uid,callback){jQuery.ajax({type:'GET',url:(document.location.protocol=="https:"?"https://ssl":"http://www")+'.google-analytics.com/ga.js',cache:true,success:function(){if(typeof _gat==undefined){throw"_gat has not been defined";}t=_gat._getTracker(uid);bind();if($.isFunction(callback)){callback(t)}t._trackPageview()},dataType:'script',data:null})};var t;var bind=function(){if(noT()){throw"pageTracker has not been defined";}for(var $1 in t){if($1.charAt(0)!='_')continue;$.ga[$1.substr(1)]=t[$1]}};var noT=function(){return t==undefined}})(jQuery);