function social_widget(widget_id){
	/**
	 *	Internal Variables
	 */
	var widget_id = widget_id;
	var analytics = ToolbarAnalytics.getAnalytics(widget_id);

	//	Default time out
	var timeout = 50;

	//	Allows for functions to wait for their needed variable values to return from ajax calls
	var variables_initialized = false;

	//	Makes sure retweet_box only loads once
	var retweet_box_initialized = false;
	var retweet_box_loaded = false;

	//	Tracks the current state of the mouseover
	var state = "inactive";

	//	Twitter id
	var twitter_id = "0";

	/**
	 *	Variables retreived via ajax
	 */
	var session_id = "";
	var twitter_api_version = "";
	var twitter_api_key = "";
	var retweet_text = "";
	var park_id = "";
	var publisher_id = "";
	var hash_type = "";
	var user_id = "";
	var impression = "";
	var ad_id = "";
	var hash = Toolbar.getHash();

	//	Twitter Anywhere
	var twitter = new Twitter("anywhere");

	var post_drop = function () {update_hover(); init_retweet_box();};
	var pre_drop = function (){ $("#iframe").animate({"padding-top": "+=94px"}, "slow");};
	
	var post_raise = function () {update_hover();};
	var pre_raise = function (){$("#iframe").animate({"padding-top": "-=94px"}, "slow");};
	
	var my_dropdown = DropdownManager.get_drop_raise(widget_id, post_drop, pre_drop, post_raise, pre_raise, false);

	/**
	 *	Condition function used by loop
	 *
	 *	@return true if assetize variables have been initialized, false otherwise
	 */
	var condition_function = function (){
		return variables_initialized;
	};

	/**
	 *	Async loop helper
	 *
	 *	@param	callback function
	 */
	var loop = function (callback){
		async_loop(
			condition_function,
			callback,
			timeout
		);
	};

	/**
	 *	Sets object id as catalyst for drop down
	 *	@param object_id
	 */
	var set_as_dropdown = function(object_id){
		$(object_id).mouseover(function(){
			state = "active";
			update_hover();
		}).mouseout(function(){
			state = "inactive";
			update_hover();
		}).click(function (){
			dropdown();
		});
	};

	/**
	 *	Send info to servers just by giving url
	 *	@param url
	 */
	var send = function(url){
		$.ajax({
			type: "GET",
			contentType: "application/x-www-form-urlencoded",
			url: url
		});
	};

	/**
	 *	Keeps track of dropdown triggers
	 */
	var update_hover = function(){
		//	Blocks till the dropdown is done sliding
		var color = "#FFFFFF";
		if (state == "active")
			color = "#97D884";

		direction = "up";
		message = "less";

		if($("#" + widget_id + "_dropdown").is(":hidden")){
			message = "more";
			direction = "down";
		}

		$("#recent-viewers-pictures").css("color", color);
		$("#more-less-text").css("color", color);
		$("#more-less-text").html(message);
		$("#more-less-image").attr("src", "/tb/widgets/social/arrow" + "_" + direction + "_" + state + ".png");
	};

	/**
	 *	Sets and performs the dropdown logic and actions
	 */
	var dropdown = function() {
		sliding = true;
		if($("#" + widget_id + "_dropdown").is(":hidden") && !$("#" + widget_id).is(":hidden"))
			my_dropdown.drop();
		else
			my_dropdown.raise();		
	};

	/**
	 *	Retreives retweet-box from twitter and displays it
	 */
	var init_retweet_box = function() {
		if (!retweet_box_initialized){
			retweet_box_initialized = true;
			loop (
				function (){
					/**
					 *	In case real tweetbox times out we load our own
					 */
					setTimeout(
						function (){
							if(retweet_box_loaded)
								return;
							retweet_box_loaded = true;

							$("#retweet-box").hide("fast", function (){
								$('#ontwitter').click(function() {
									analytics.custom("retweet",{'0':park_id});
									window.open ("http://twitter.com/home?status="+$('#fortwitter').attr('value'));
						    	});
								$("#fortwitter").val(retweet_text);
								$("#internal-retweet-box").show();
								analytics.load("internal-retweet-box");
							});
						},
						15000
					);

					twitter.set_retweet_box(
						"#retweet-box",
						retweet_text,
						function (plaintext_tweet, html_tweet) {
							store_tweet(plaintext_tweet, html_tweet);
							analytics.custom("tweet", {'0':plaintext_tweet});
						},
						function (){
							// Lets our tweet box know not to load
							retweet_box_loaded = true;
							$("#retweet-box-image").remove();
						},
						function(){
						}
					);
					store_retweet_box();
				}
			);
		}
	};

	/**
	 *	Initialize user pictures in toolbar
	 */
	var update_recent_viewers_pictures = function() {
		loop (
			function (){
				$.ajax({
					type: "GET",
					url: "/clickdatausers/getRecentViewersPictures/?session_id="+session_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id,
					success: function(html){
						$("#recent-viewers-pictures").html(html);
						twitter.set_hovercards("#recent-viewers-pictures");
					}
				});
			}
		);
	};

	/**
	 *	Initialize suggestions in dropdown
	 */
	var update_suggestions = function() {
		loop (
			function (){
				$.ajax({
					type: "GET",
					url: "/clickdatausers/getSuggestions/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash,
					success: function(html){
						$("#suggestions").html(html);
					}
				});
			}
		);
	};

	/**
	 *	Initialize recent viewer's pictures and names in drop down
	 */
	var update_recent_viewers = function() {
		loop (
			function (){
				$.ajax({
					type: "GET",
					url: "/clickdatausers/getRecentViewers/?session_id="+session_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id,
					success: function(html){
						$("#recent-viewers").html(html);
						twitter.set_hovercards("#recent-viewers");
						twitter.set_connect(
							"#anywhere_signin_image",
							function(){},
							function(){}
						);
						twitter.set_connect("#anywhere_signin_link",function(){},function(){});
					}
				});
			}
		);
	};


	/**
	 *	Stores user information on server
	 *	@param T.current_user
	 */
	var store_twitter_info = function (current_user) {
		loop (
			function () {
				var profile_background_color = encodeURIComponent(current_user.data("profile_background_color"));
				var description = encodeURIComponent(current_user.data("description"));
				var profile_text_color = encodeURIComponent(current_user.data("profile_text_color"));
				var followers_count = encodeURIComponent(current_user.data("followers_count"));
				var lang = encodeURIComponent(current_user.data("lang"));
				var time_zone = encodeURIComponent(current_user.data("time_zone"));
				var utc_offset = encodeURIComponent(current_user.data("utc_offset"));
				var friends_count = encodeURIComponent(current_user.data("friends_count"));
				var profile_link_color = encodeURIComponent(current_user.data("profile_link_color"));
				var statuses_count = encodeURIComponent(current_user.data("statuses_count"));
				var created_at = encodeURIComponent(current_user.data("created_at"));
				var following = encodeURIComponent(current_user.data("following"));
				var favourites_count = encodeURIComponent(current_user.data("favourites_count"));
				var profile_sidebar_fill_color = encodeURIComponent(current_user.data("profile_sidebar_fill_color"));
				var contributors_enabled = encodeURIComponent(current_user.data("contributors_enabled"));
				var notifications = encodeURIComponent(current_user.data("notifications"));
				var protected = encodeURIComponent(current_user.data("protected"));
				var profile_image_url = encodeURIComponent(current_user.data("profile_image_url"));
				var geo_enabled = encodeURIComponent(current_user.data("geo_enabled"));
				var profile_background_image_url = encodeURIComponent(current_user.data("profile_background_image_url"));
				var profile_sidebar_border_color = encodeURIComponent(current_user.data("profile_sidebar_border_color"));
				var url = encodeURIComponent(current_user.data("url"));
				var location = encodeURIComponent(current_user.data("location"));
				var name = encodeURIComponent(current_user.data("name"));
				var screen_name = encodeURIComponent(current_user.data("screen_name"));
				var id = encodeURIComponent(current_user.data("id"));
				var verified = encodeURIComponent(current_user.data("verified"));
				var profile_background_tile = encodeURIComponent(current_user.data("profile_background_tile"));

				send("/clickdatausers/addtwitteranywhere/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&id="+id+"&name="+name+"&screen_name="+screen_name+"&profile_background_color="+profile_background_color+"&description="+description+"&profile_text_color="+profile_text_color+"&followers_count="+followers_count+"&lang="+lang+"&time_zone="+time_zone+"&utc_offset="+utc_offset+"&friends_count="+friends_count+"&profile_link_color="+profile_link_color+"&statuses_count="+statuses_count+"&created_at="+created_at+"&following="+following+"&favourites_count="+favourites_count+"&profile_sidebar_fill_color="+profile_sidebar_fill_color+"&contributors_enabled="+contributors_enabled+"&notifications="+notifications+"&protected="+protected+"&profile_image_url="+profile_image_url+"&geo_enabled="+geo_enabled+"&profile_background_image_url="+profile_background_image_url+"&profile_sidebar_border_color="+profile_sidebar_border_color+"&url2="+url+"&location="+location+"&verified="+verified+"&profile_background_tile="+profile_background_tile);
			}
		);
	};

	/**
	 *	Store the retweet box being generated
	 */
	var store_retweet_box = function () {
		loop(
			function (){
				send("/clickdatausers/addretweetbox/?transaction_type=toolbar_plugin&session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id);
			}
		);
	};

	/**
	 *	POST user details to database
	 */
	var store_not_connected = function() {
		loop (
			function () {
				send("/clickdatausers/addnotloggedin/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&accounttype=twitter");
			}
		);
	};

	/*
	 *	Store the contents of the tweet
	 */
	var store_tweet = function (plaintext_tweet, html_tweet) {
		loop (
			function (){
				send("/clickdatausers/addtweetfromtweetbox/?session_id="+session_id+"&user_id="+user_id+"&park_id="+park_id+"&publisher_id="+publisher_id+"&hash_type="+hash_type+"&hash="+hash+"&twitter_id="+twitter_id+"&plaintext_tweet="+encodeURIComponent(plaintext_tweet)+"&html_tweet="+encodeURIComponent(html_tweet));
			}
		);
	};

	/**
	 *	Initializes variebles with AJAX call
	 */
	var initalize_variables = function (){
		$.ajax({
			type: "GET",
			url: "/" + Toolbar.getController() + "/social/" + Toolbar.getHash(),
			success: function(data){
	        	//	Change to JSON return
	        	try {
	        		//	Throw in error checking to make sure the variables are "clean" ie. no url injection
		        	park_id = data['park_id'];
					user_id = data['user_id'];
					publisher_id = data['publisher_id'];
					retweet_text = data['retweet_text'];
					ad_id = data['ad_id'];
					ad_details = data['ad_details'];
					hash_type = data['hash_type'];
					impression = data['impression'];
					session_id = data['session_id'];
					title = data['title'];
					variables_initialized = true;
				}catch (err){
					$("#recent-viewers-pictures").html("Error Connecting To Visibli");
				}
			}
		});
	};


	/**************************************************
	 *		READY FUNCTION
	 **************************************************/

	/**
	 *	Sets up all the variables, meta tags and script tags needed for the widget to operate.
	 *	@IMPORTANT	Program only calls init() if all set ups were successfull.
	 */
	jQuery(document).ready(function() {
		initalize_variables();

		set_as_dropdown("#recent-viewers-pictures");
		set_as_dropdown("#more-less");
		set_as_dropdown("#hide");

		twitter.set_connect(
			"#twitter-connect",
			function(current_user){
				twitter_id = current_user.data("id");
				update_recent_viewers();
				update_recent_viewers_pictures();
				store_twitter_info (current_user);
				$("#twitter-connect").hide("fast");
			},
			function(){}
		);

		update_hover();
		update_suggestions();
		update_recent_viewers();
	    update_recent_viewers_pictures();

		//	Tracks variable initialization on google
		loop (
			function (){
				analytics.custom("impression",{'0':'impression','1':ad_id, '2':park_id});
			}
		);

		//	Tracks social widget load on toolbar plugin
		analytics.load("social");

	});


}
