document.domain = "irishcentral.com";

//document.write("<script src='http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php' type='text/javascript'></sc"+"ript>");


var slFacebookProxy = function(){
	this.ApiKey = "180a28a07e7113a4b55b3846045a29b6";
	this.Templates = {
							'ArticleComment' : '50971911585'			};
	
	this.connectStatus = null;
};

slFacebookProxy.prototype.loadScript = function(url, callback) {
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.charset = 'utf-8';
	if (callback)
		script.onload = script.onreadystatechange = function() {
			if (script.readyState && script.readyState != 'loaded' && script.readyState != 'complete')
				return;
			script.onreadystatechange = script.onload = null;
			callback();
		};
	script.src = url;
	document.getElementsByTagName('head')[0].appendChild (script);
}

slFacebookProxy.prototype.init = function(callback){
	// If no API key, do nothing, call callback immediately.
	if(!this.ApiKey || this.ApiKey == ""){
		if(callback) callback();
		return;
	}
	
	var me = this;
	//Crazy hack for removing document.write
	if(!document.getElementById("FB_HiddenContainer")){
		try{
			if(!window.document.body){
				document.write('<div style="position: absolute; top: -10000px; left: -10000px; width: 0px; height: 0px;" id="FB_HiddenContainer"></div>');
			}
			else{
				var fbHiddenDiv = document.createElement("div");
				fbHiddenDiv.id = "FB_HiddenContainer";
				fbHiddenDiv.style.position = "absolute";
				fbHiddenDiv.style.left = "-10000px";
				fbHiddenDiv.style.top = "-10000px";
				fbHiddenDiv.style.width = "0px";
				fbHiddenDiv.style.height = "0px";
				window.document.body.insertBefore(fbHiddenDiv, window.document.body.firstChild);
			}
		}
		catch(e){
			setTimeout(function(){
				me.init(callback);
			}, 100);
			return;
		}	
	}
	
	// Race condition where Fb isn't loaded yet...
	if(!window.FB){
		this.loadScript('http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php', function(){
			me.init(callback);
		});
		return;
	}

	if(!this.initialized){
		this.initialized = true;
		FB.init("180a28a07e7113a4b55b3846045a29b6", "/templates/FacebookXd.html");
	}
		
	FB.ensureInit(function(){
		// Get current status
		FB.Connect.get_status().waitUntilReady(function( status ){
			me.connectStatus = status;

			if(callback){
				callback();
			}
		});
	});
};

slFacebookProxy.prototype.linkImages = function(images, url){
	var fb_images = [];
	for(i=0; i<images.length; i++){
		fb_images.push({'src': images[i], 'href': url});
		
	}
	return fb_images;
};

slFacebookProxy.prototype.trunc = function(text, maxLen){
	if(!maxLen){
		maxLen = 200;
	}
	
	if(text.length > maxLen){
		text = text.substring(0, maxLen);
		text = text + "...";
	}
	
	return text;
};

slFacebookProxy.prototype.submitFeedStory = function(template_name, variables, callback){
	if(!this.ApiKey || this.ApiKey == ""){
		if(callback) callback();
		return;
	}
	
	var template = this.Templates[template_name];
	if(!template){
		throw("Bad template ID: " + template_name);
	}
	
	var me = this;
	this.init(function(){
		FB.Connect.showFeedDialog(template, variables, null, "", FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect, callback);
	});
};

slFacebookProxy.prototype.submitArticleComment = function(title, url, excerpt, comment_body, images, callback){
	var me = this;
	variables = {
		'Title' : title,
		'URL' : url,
		'Excerpt' : me.trunc(excerpt),
		'Comment_Body' : me.trunc(comment_body),
		'images' : me.linkImages(images, url)
	};
	
	this.submitFeedStory("ArticleComment", variables, callback);
};

slFacebookProxy.prototype.getUrlParameters = function(url){
	var ret = {};
	var params = null;
	
	var startPos = url.indexOf('?');
	if(startPos >= 0){
		var endPos = url.indexOf('#');
		if(endPos < startPos){
			endPos = url.length;
		}
		
		params = url.substring(startPos+1, endPos);
		
		var splitParams = params.split("&");
		for(i=0; i<splitParams.length; i++){
			var kvp = splitParams[i].split('=');
			if(kvp[0]){
				ret[kvp[0]] = kvp[1] || "";
			}
		}
	}
	
	return ret;
};

slFacebookProxy.prototype.showInviteFriendsDialog = function(title, header, invite_text, next_page, type, accept_text, accept_url){
	var params = this.getUrlParameters(next_page);
	this.init(function(){
	    var the_fbml = "";
		the_fbml += '<fb:fbml>';
		the_fbml += '	<fb:request-form style="width: 630px; height: 560px;" action="' + next_page + '" method="GET" invite="true" type="' + type + '" content="' + invite_text + ' <fb:req-choice url=\'' + accept_url + '\' label=\'' + accept_text + '\'/>">';
				
		// add input params
		for(var i in params){
			the_fbml += '<input type="hidden" name="'+i+'" value="'+params[i]+'">';
		}
				
		the_fbml +=	'		<fb:multi-friend-selector showborder="false" bypass="cancel" actiontext="' + header + '"/>';
		the_fbml += '	</fb:request-form>';
		the_fbml += '</fb:fbml>';

		var dlg = new FB.UI.FBMLPopupDialog(title, the_fbml);
		dlg.setContentWidth(630);
		dlg.setContentHeight(560);
		dlg.set_placement(FB.UI.PopupPlacement.center);
		dlg.show();
	});
};

slFacebookProxy.prototype.isLoggedIn = function(){
	return (this.connectStatus != FB.ConnectState.userNotLoggedIn);
};

slFacebookProxy.prototype.currentUserFacebookId = function(){
	//return this.currentUserId;
	if(FB.Facebook.apiClient.get_session()){
		return FB.Facebook.apiClient.get_session().uid;
	}
		
	else return null;
};

slFacebookProxy.prototype.isConnectedUser = function(){
	return (this.connectStatus == FB.ConnectState.connected);
};

slFacebookProxy.prototype.connectEnabled = function(){
	return (this.ApiKey != "");
};

slFacebookProxy.prototype.renderFbml = function(){
	FB.XFBML.Host.parseDomTree();
};

slFacebookProxy.prototype.onLogin = function(callback){
	this.init(function(){
		FB.Facebook.get_sessionWaitable().waitUntilReady( callback );
	});
};

slFacebookProxy.prototype.queryConnectStatus = function(callback){
	this.init(function(){
		FB.Connect.get_status().waitUntilReady( callback );
	});
};

slFacebookProxy.prototype.login = function(callback){
	var me = this;
	this.init(function(){		
		FB.Connect.requireSession(function(){
			FB.Connect.get_status().waitUntilReady(function( status ){
				me.connectStatus = status;
				
				if(callback){
					me.onLogin(callback);
				}
			});
		});
	});
};

slFacebookProxy.prototype.logout = function(callback){
	this.init(function(){
		FB.Connect.logout(callback);
	});
};

var slFB = null;
// Initialize facebook API
slFB = new slFacebookProxy();
