function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function insertAfter( referenceNode, newNode )
{
    referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}

var pageName = document.getElementById('pageName');
var content_text = document.getElementById('content_text');
if (pageName)
{
	var socialDiv = document.createElement('div');
	socialDiv.id = 'social';
	content_text.insertBefore(socialDiv, content_text.firstChild);
	document.getElementById('social').setAttribute("style", "margin-top:10px;height:30px;margin-left:6px;margin-bottom:10px;");
	
	document.write('<div id="fb-root"></div>');
	document.write('<scr'+'ipt src="http://connect.facebook.net/en_US/all.js#appId=184772921571925&amp;xfbml=1"></scr'+'ipt>');
	document.write('<scr'+'ipt src="http://platform.twitter.com/widgets.js" type="text/javascript"></scr'+'ipt>');
	document.write('<scr'+'ipt type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4dd190dc2dc61e02"></scr'+'ipt>');

	var addthis_config = {"data_track_clickback":true};
	var socialDiv = document.getElementById('social');

	// Twitter Share Button
	socialDiv.innerHTML += '<div style="float: left; margin-right: 10px;"><a href="http://twitter.com/share?text=Check out this page on tbc.edu!&count=none" class="twitter-share-button">Tweet</a></div>';
	// AddThis Sharing Toolbox
	socialDiv.innerHTML += '<div class="addthis_toolbox addthis_default_style " style="float: left; margin-right: 10px;"><a class="addthis_counter addthis_pill_style"></a></div>';
	// Facebook Like Button
	socialDiv.innerHTML += '<fb:like href="http://www.facebook.com/trinityrescue" send="false" layout="button_count" width="150" show_faces="false" font="arial"></fb:like></div>';
}

var fb_publisher = {
	init: function()
	{
		try {if (fb_publish_info) fb_publisher.publish();}
		catch(err) {return false;}
	},
	publish: function()
	{
		FB.ui(
			{
				method: 'feed',
				name: fb_publish_info.name,
				link: fb_publish_info.link,
				picture: fb_publish_info.picture,
				caption: fb_publish_info.caption,
				description: fb_publish_info.description,
				message: fb_publish_info.message
			},
			function(response) {
				if (response && response.post_id) {
				} else {
					return false;
				}
			}
		);
	}
}

//addLoadEvent(fb_publisher.init);

var publishPrompt = {
	init: function()
	{
		try {if (fb_publish_info) publishPrompt.create();}
		catch(err) {return false;}
	},
	create: function()
	{
		var publishDiv = document.createElement('div');
		publishDiv.setAttribute('id', 'publish_div');
		if (document.getElementById('social')) {
			insertAfter(document.getElementById('social'), publishDiv);
			publishDiv.innerHTML =
				'<div style="width: 580px;height: 120px;background-color: #fdf7df;margin-bottom: 10px;border: solid 1px;padding:10px;margin-top: 1em;">'+
				'<h3>Share This on Facebook!</h3>'+
				'<textarea id="fb_message" style="height: 26px;font-family: Arial;padding: 10px;width: 558px;overflow:auto;">'+
					fb_publish_info.message+
				'</textarea>'+
				'<a href="#" id="fb_publish_button" style="background: #3B5999; color: #FFF;display: block;padding: 7px;width: 150px;text-align: center;text-decoration: none;font-size: 12px;float: right;margin: 10px 0 0 0;border:outset 1px;font-weight: bold;">Preview Your Post!</a>'+
				'</div>';
		} 
		document.getElementById('fb_message').onchange = function() {fb_publish_info.message = this.value;};
		document.getElementById('fb_publish_button').onclick = function() {fb_publisher.publish();};
	}
}

addLoadEvent(publishPrompt.init);
