//Steischränzer Constants on Facebook
var uid = "188159274535503";
var uaccess_token = "175113549234164|eagts9fMxk7sW2OjuYv5_bQJ9LI";
var fbAlbumActivated = "0";
var fbEventActivated = "0";
var fbVereinActivated = "0";


//Facebook Feed Parser
function fbFeed(){

	//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    var url = "https://graph.facebook.com/"+uid+"/feed?limit=10&access_token="+uaccess_token+"&callback=?&date_format=U";
			
		//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
		$.getJSON(url,function(json){
		    var html = "<ul>";
				//loop through and within data array's retrieve the message variable.
		    	$.each(json.data,function(i,fb){
				if (typeof fb.message != 'undefined') {
		      		html += "<li><img src='http://graph.facebook.com/"+uid+"/picture?type=square' class='fbpicture'><div class='fbmessage'>" + fb.message + "<br>" + relativeTime(fb.created_time*1000)+"</div></li>"; 
				}
		    	});
		    html += "</ul>";


			//A little animation once fetched
			$('.facebookfeed').animate({opacity:0}, 500, function(){
					$('.facebookfeed').html(html);
																  });
		    $('.facebookfeed').animate({opacity:1}, 500);
		});
};
//End Facebook Feed Parser

	//Facebook Event Parser
function fbEvent(){
	//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    //var url = "https://graph.facebook.com/"+uid+"/feed?limit=5&access_token="+uaccess_token+"&callback=?&date_format=U";
	var url = "https://graph.facebook.com/"+uid+"/events?access_token="+uaccess_token+"&callback=?&date_format=U";
	var eventurl = "http://www.facebook.com/event.php?eid=";
	fbEventActivated = "1";

	var Wochentag = new Array("Sonntag", "Montag", "Dienstag", "Mittwoch",
					  "Donnerstag", "Freitag", "Samstag");
					  
	var Monat = new Array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni",
				  "Juli", "August", "September", "Oktober", "November", "Dezember");
	
	//Initialisiere Zeitzonen Detection
	var timezone = jstz.determine_timezone();
	
	//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
	$.getJSON(url,function(json){
	
	//sort json data by start_time ascending
	json.data.sort(sort_by('start_time', false, parseInt));
		//table header
		var html = "<h2>Challenges</h2><table id='table_fbevents'><tr><th>Challenge</th><th>Ort</th><th>Zeit</th></tr>";
			//loop through and within data array's retrieve the message variable.
			$.each(json.data,function(i,fb){
			if (timezone.dst()) {
				var myDate = new Date((fb.start_time-32400)*1000);	
			} else {
				var myDate = new Date((fb.start_time-28800)*1000);
			}
				//Erstelle richtige Minuten						
				var MinAusgabe = ((myDate.getMinutes() < 10) ? "0" + myDate.getMinutes() : myDate.getMinutes());
				//Generiere Output
				html += "<tr><td><a href=http://www.facebook.com/event.php?eid="+fb.id+" target=_blank>"+fb.name +"</td><td>" +fb.location+"</td><td>"+Wochentag[myDate.getDay()]+" "+myDate.getDate()+". "+Monat[myDate.getMonth()]+" "+myDate.getFullYear()+" "+myDate.getHours()+":"+MinAusgabe+"</td></tr>"; 
			});
		html += "</table>";

			//A little animation once fetched
			$('.facebookevent').animate({opacity:0}, 500, function(){
					$('.facebookevent').html(html);
																  });
		    $('.facebookevent').animate({opacity:1}, 500);
		});
};
//End Facebook Event Parser

	//Facebook Album Parser
function fbAlbum(){

	//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    //var url = "https://graph.facebook.com/"+uid+"/feed?limit=5&access_token="+uaccess_token+"&callback=?&date_format=U";
	var url = "https://graph.facebook.com/"+uid+"/albums?limit=5&access_token="+uaccess_token+"&callback=?&date_format=U";
	fbAlbumActivated = "1";
		
		//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
		$.getJSON(url,function(json){
		    var html = "<h2>Gallery</h2><ul>";
				//loop through and within data array's retrieve the message variable.
		    	$.each(json.data,function(i,fb){
		      		html += "<li><a href='"+fb.link+"' target='blank'><img src='http://graph.facebook.com/"+fb.cover_photo+"/picture?type=album' class='fbeventpicture'></a><a href='"+fb.link+"' class='fbeventlink' target='blank'>" + fb.name + "</a><br>Letzte &Auml;nderung: " + relativeTime(fb.created_time*1000)+"</li>"; 
		    	});
		    html += "</ul>";


			//A little animation once fetched
			$('.facebookalbum').animate({opacity:0}, 500, function(){
					$('.facebookalbum').html(html);
																  });
		    $('.facebookalbum').animate({opacity:1}, 500);
		});
};
//End Facebook Album Parser

//Facebook Info Parser
function fbVerein(){

	//Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
    //var url = "https://graph.facebook.com/"+uid+"/feed?limit=5&access_token="+uaccess_token+"&callback=?&date_format=U";
	var url = "https://graph.facebook.com/"+uid+"?access_token="+uaccess_token;
	fbVereinActivated = "1";
		
		//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
		$.getJSON(url,function(json){
		    var html = "<h2>Verein</h2><ul>";
				//loop through and within data array's retrieve the message variable.
		    	$.each(json.data,function(i,fb){
		      		html += "<p>"+ fb.name +"/p>"; 
		    	});
		    html += "";


			//A little animation once fetched
//			$('.facebookverein').animate({opacity:0}, 500, function(){
//					$('.facebookverein').html(html);
//																  });
//		    $('.facebookverein').animate({opacity:1}, 500);
		});
};
//End Facebook Info Parser
	
//sort function for events
// Sort by price high to low
//homes.sort(sort_by('price', true, parseInt));
// Sort by city, case-insensitive, A-Z
//homes.sort(sort_by('city', false, function(a){return a.toUpperCase()}));

var sort_by = function(field, reverse, primer){

   reverse = (reverse) ? -1 : 1;

   return function(a,b){

       a = a[field];
       b = b[field];

       if (typeof(primer) != 'undefined'){
           a = primer(a);
           b = primer(b);
       }

       if (a<b) return reverse * -1;
       if (a>b) return reverse * 1;
       return 0;

   }
}


function relativeTime(time){
	
	// Adapted from James Herdman's http://bit.ly/e5Jnxe
	
	var period = new Date(time);
	var delta = new Date() - period;

	if (delta <= 10000) {	// Less than 10 seconds ago
		return 'Just now';
	}
	
	var units = null;
	
	var conversions = {
		Millisekunde: 1,		// ms -> ms
		Sekunde: 1000,		// ms -> sec
		Minute: 60,			// sec -> min
		Stunde: 60,			// min -> hour
		Tage: 24,			// hour -> day
		Monate: 30,			// day -> month (roughly)
		Jahre: 12			// month -> year
	};
	
	for (var key in conversions) {
		if (delta < conversions[key]) {
			break;
		}
		else {
			units = key;
			delta = delta / conversions[key];
		}
	}
	
	// Pluralize if necessary:
	
	delta = Math.floor(delta);
	if (delta > 1) { 
		units += 'en'; 
		}
	return ["vor", delta, units, ].join(' ').replace('ee','e');
	
}	

