/**
 * ComedyandShareThis
 * @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) {
	this._element = instance;
	this._url = url;
	this._title = title;
	this._embed = embed;
	this._type = type;
	this._unbind();
	this._init();
}
/**
 * Prototype.
 */
ComedyShareThis.prototype = {
	_element:null,
	_url:'',
	_title:'',
	_embed:'',
	_type:'',
	_show:'',
	_sharethis:null,
	_clipboard:null,
	_embedClipboard:null,
	_instance:'',
	_hasMouse:false,
	_init:function() {
		
		var s = this;
		/* intialize sharethis */
		this._sharethis = SHARETHIS.addEntry({
			title:this._title,
			url:this._url
		});
		
		var embedButtonId = "embedcopy";
		var shareButtonId = "sharecopy";
		var embedCodeId = "embedCodeForShare";
		var id = "";
		if (this._element.attr("id")) {
			id = this._element.attr("id");
			id = id.replace("comedy_share", "");
			this._instance = id;
			embedButtonId = embedButtonId + this._instance;
			shareButtonId = shareButtonId + this._instance;
			embedCodeId = embedCodeId + this._instance;
		}

		/* add click events to elements */
		this._element.find('.facebook').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				if (s._type == 'poll')
					s.share('facebook', 'pollshare_fb');
				else
					s.share('facebook', 'share_fb');
			});
		});
		this._element.find('.twitter').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				if (s._type == 'poll')
					s.share('twitter', 'pollshare_twitter');
				else
					s.share('twitter', 'share_twitter');
			});
		});
		this._element.find('.digg').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				if (s._type == 'poll')
					s.share('digg', 'pollshare_digg');
				else
					s.share('digg', 'share_digg');
			});
		});
		this._element.find('.stumble').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				if (s._type == 'poll')
					s.share('stumbleupon', 'pollshare_stumble');
				else
					s.share('stumbleupon', 'share_stumble');
			});
		});
		this._element.find('.reddit').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {	
				if (s._type == 'poll')
					s.share('reddit', 'pollshare_red');
				else
					s.share('reddit', 'share_red');
			});
		});
		this._element.find('.buzz').each(function() {
			$(this).hover(function() {
				$(this).addClass("hover");
			}, function() {
				$(this).removeClass("hover");
			});
			$(this).click(function() {
				if (s._type == 'poll')
					s.share('gbuzz', 'pollshare_buzz');
				else				
					s.share('gbuzz', 'share_buzz');
			});
		});
		this._element.hover(function() {
			
			s._hasMouse = true;
			if (!$(this).is(".hover")) {
				var clipURL = s._url;
				if (clipURL.indexOf("?") > 0) clipURL = clipURL + "&";
				else clipURL = clipURL + "?";
				if (s._type == 'poll')
					clipURL = clipURL + "xrs=pollshare_copy";
				else
					clipURL = clipURL + "xrs=share_copy";
				var embedText = $("#"+embedCodeId).val();
				
				$(this).addClass("hover");
				
				s._clipboard = new ZeroClipboard.Client();
				s._clipboard.setHandCursor(true);
				s._clipboard.addEventListener('onComplete', function (a,b) {
					$("#"+shareButtonId).addClass("copied");
					if ($("#"+embedButtonId)) $("#"+embedButtonId).removeClass("copied");
				});
				s._clipboard.addEventListener('mouseDown', function(client) {
					s._clipboard.setText(clipURL);
				});
				
				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);
				
				if (s._embed != '') {
					s._embedClipboard = new ZeroClipboard.Client();
					s._embedClipboard.setHandCursor(true);
					s._embedClipboard.addEventListener('onComplete', function(a,b) {
						$("#"+embedButtonId).addClass("copied");
						$("#"+shareButtonId).removeClass("copied")
					});
					s._embedClipboard.addEventListener('mouseDown', function(client) {
						s._embedClipboard.setText(embedText);
					});
					s._embedClipboard.addEventListener('onMouseOver', function(client) {
						s._show = true;
						s._hasMouse = true;
						s._element.addClass("hover");
						$("#"+embedButtonId).addClass("hover");
					});
					s._embedClipboard.addEventListener('onMouseOut', function(client) {
						$("#"+embedButtonId).removeClass("hover");
						s._show = false;
					});
					s._embedClipboard.glue(embedButtonId);
				}
			}
		},function() {
			s._hasMouse = false;
			var el = $(this);
			setTimeout(function() {
				if (s._show) return;
				if (s._hasMouse) return;
				el.removeClass("hover");
				el.find('.copybutton').each(function() {
					$(this).removeClass("copied");
					$(this).removeClass("hover");
				});
				el.find('.embedbutton').each(function() {
					$(this).removeClass("copied");
					$(this).removeClass("hover");
				});
				if (s._clipboard) s._clipboard.destroy();
				if (s._embedClipboard) s._embedClipboard.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");
	},
	_unbind:function() {
		var s = this;
		s._element.unbind('hover');
		s._element.find('.button').each(function() {
			$(this).unbind('hover');
			$(this).unbind('click');
		});
		s._element.find('.copybutton').each(function() {
			$(this).unbind('hover');
			$(this).unbind('click');
		});
		s._element.find('.embedbutton').each(function() {
			$(this).unbind('hover');
			$(this).unbind('click');
		});
	},
	report:function(url,type,service) {
		if (this._type == 'poll') {
			var items = service.split('_');
			var evar19 = items[0] + '_' + this._title + '_' + items[1];
			mtvn.btg.Controller.sendLinkEvent({linkName:'Share Module',linkType:type,eVar9:pageName,eVar19:evar19,events:'event9'});
		} else {
			mtvn.btg.Controller.sendLinkEvent({linkName:'Share Module',linkType:type,eVar9:pageName,eVar19:service,events:'event9'});
		}
	}
};
