var apiKey = 'f9932da7301e295da204d4e0e4384d0e';
var debug = false;


if($(document).context.body.id == 'home'){
 $('.item#email iframe').attr('src','mailchimp.html?v=2');
 $('.item#email iframe').attr('height','105');
$('#fb-like').html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2Fweeels.org&amp;layout=standard&amp;show_faces=false&amp;width=225&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:225px; height:35px;" allowTransparency="true"></iframe>');
}
/*
*/

function tell(t){
	if(debug == true && !!console){
		console.log(t);
	} else {
		// do nothing
	}
}
$("#fbSuggest").click(function(){
	fbSuggest_start();
});
$("#fbSuggestOpener").fancybox({
	'width':480,
	'height':520,
	'autoScale':false,
	'transitionIn':'none',
	'transitionOut':'none',
	'type':'iframe',
	'padding':0
});		
function fbSuggest(){
	tell('fired fbSuggest');
	$("#fbSuggestOpener").trigger('click');
}
function fbSuggest_start(){
	tell("fbSuggest_start");
	FB.init({apiKey:apiKey});
	FB.getLoginStatus(loginStatusResponseHandler);
}
function loginStatusResponseHandler(loginStatusResponse) {
	tell("getLoginStatus response:");
	tell(loginStatusResponse);	
	if (!loginStatusResponse.session) {
		tell("not logged in, beginning login");
		FB.login(function(loginResponse) {
			tell("loginResponse:");
			tell(loginResponse);
			if (loginResponse.session){				
				tell("loginResponse has session");
				// do something with session
				if (loginResponse.perms){
					tell("loginResponse session has permissions");
					// do something with permissions
					fbSuggest();
				} else {
					tell("loginResponse session DOES NOT HAVE permissions");
					// deal with lack of permissions
				}
			} else {			
				tell("loginResponse DOES NOT HAVE session");
				// deal with lack of sessions			
			}		
		},{perms:'read_stream,publish_stream'});
	} else {
		FB.api({
			method: 'fql.query',
			query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
		},function(response) {
		    var user = response[0];
		    tell('in session for: '+user.name);
		    fbSuggest();
		});
	}
};
$(document).context.URL.split('!')[1] == 'fbsuggest' ? $('a#fbSuggest').trigger('click') : false;
setTimeout(function(){
$(document).context.URL.split('!')[1] == 'subscribe' ? $('a#updates').trigger('click') : false;
},2500);
