$(document).ready(function() {	
	$(".comedy_share").each(function() {
		var title = $(this).attr("postTitle");
		var url =  $(this).attr("url")
		var embed = $(this).attr("embed");
		var type = $(this).attr("type");
		var post = $(this).attr("post");
		return new ComedyShareThis($(this), url, title, embed, type, post);
	});
});

/**
 * ComedyShareThis
 * @param instance CSS element.
 * @param url Share element.
 * @param title Title.
 * @param embed Embed code.
 * @param type Content type.
 */
function ComedyShareThis(instance,url,title,embed,type,post) {
	this._element = instance;
	this._url = url;
	this._title = title;
	this._embed = embed;
	this._type = type;
	this._post = post;
	this._init();
}
/**
 * Prototype.
 */
ComedyShareThis.prototype = {
	_element:null,
	_post:'',
	_url:'',
	_title:'',
	_embed:'',
	_type:'',
	_show:false,
	_sharethis:null,
	_clipboard:null,
	_hasMouse:false,
	_init:function() {
		var s = this;
		/* intialize sharethis */
		this._sharethis = SHARETHIS.addEntry({
			title:this._title,
			url:this._url
		});
		
		var postURL = this._url;
		if (postURL.indexOf("?") > 0) postURL = postURL + "&";
		else postURL = postURL + "?";
		postURL = postURL + "xrs=share_copy";
		var shareButtonId = "copybutton";
		if (this._post)
			shareButtonId = shareButtonId + this._post;
		
		// If we're in exploded view, create the clipboard here.
		if (s._element.is(".exploded")) {
			shareButtonId = "copybutton";
			s._clipboard = new ZeroClipboard.Client();
			s._clipboard.setHandCursor(true);
			s._clipboard.addEventListener('onComplete', function (a,b) {
				$("#"+shareButtonId).addClass("copied");
			});
			s._clipboard.addEventListener('mouseDown', function(client) {
				s._clipboard.setText(postURL);
			});
			s._clipboard.addEventListener('onMouseOver', function(client) {
				$("#"+shareButtonId).addClass("hover");
			});
			s._clipboard.addEventListener('onMouseOut', function(client) {
				$("#"+shareButtonId).removeClass("hover");
			});
			s._clipboard.glue(shareButtonId);
			
			$("#" + shareButtonId).mouseover(function() {
				s._clipboard.reposition();
			});
		}
		
		/* add click events to elements */
		this._element.find('.facebook').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				s.share('facebook', 'share_fb');
			});
		});
		this._element.find('.twitter').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				s.share('twitter', 'share_twitter');
			});
		});
		this._element.find('.digg').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				s.share('digg', 'share_digg');
			});
		});
		this._element.find('.stumble').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				s.share('stumbleupon', 'share_stumble');
			});
		});
		this._element.find('.reddit').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {	
				s.share('reddit', 'share_red');
			});
		});
		this._element.find('.buzz').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				s.share('gbuzz', 'share_buzz');
			});
		});
		
		this._element.hover(function() {
			s._hasMouse = true;
			if (!$(this).is(".hover") && !$(this).is(".exploded")) {
				var postURL = s._url;
				if (postURL.indexOf("?") > 0) postURL = postURL + "&";
				else postURL = postURL + "?";
				postURL = postURL + "xrs=share_copy";
				
				$(this).addClass("hover");
				s._clipboard = new ZeroClipboard.Client();
				s._clipboard.setHandCursor(true);
				s._clipboard.addEventListener('onComplete', function (a,b) {
					$("#"+shareButtonId).addClass("copied");
				});
				s._clipboard.addEventListener('mouseDown', function(client) {
					s._clipboard.setText(postURL);
				});
				s._clipboard.addEventListener('onMouseOver', function(client) {
					s._show = true;
					s._hasMouse = true;
					s._element.addClass("hover");
					$("#"+shareButtonId).addClass("hover");
				});
				s._clipboard.addEventListener('onMouseOut', function(client) {
					$("#"+shareButtonId).removeClass("hover");
					s._show = false;
				});
				s._clipboard.glue(shareButtonId);
			}
		},function() {
			s._hasMouse = false;
			var el = $(this);
			if (el.is(".exploded")) return;
			setTimeout(function() {
				if (s._show) return;
				if (s._hasMouse) return;
				el.removeClass("hover");
				el.find('.copybutton').each(function() {
					$(this).removeClass("copied");
					$(this).removeClass("hover");
				});
				if (s._clipboard) s._clipboard.destroy();
			}, 200);
		});
	},
	share:function(service,xrs) {
		var s = this;
		var url = this._url;
		if (url.indexOf("?") > 0)
			url = url + "&xrs=" + xrs;
		else
			url = url + "?xrs=" + xrs;
		url = encodeURIComponent(url);
		shareURL = "http://wd.sharethis.com/api/sharer.php?destination="+service + "&url="+url+"&title="+this._title+"&publisher="+SHARETHIS.fpc+"&sessionID="+SHARETHIS.sessionID+"&source=button";
		// Send the link event.
		s.report(url, 'o', xrs);
		window.open(shareURL,"sharethis");
	},
	report:function(url,type,service) {
		mtvn.btg.Controller.sendLinkEvent({linkName:'Share Module',linkType:type,eVar9:pageName,eVar19:service,events:'event9'});
	}
};
