SG.namespace("SG.cms.share");

SG.cms.share.Network = function(name, serviceUrl, text, target) {
	this.init(name, serviceUrl, text, target);
};

SG.cms.share.Network.prototype = {
	name : null,
	serviceUrl: null,
	text: document.title,
	target: window.location.href,

	init : function(name, serviceUrl, text, target) {
		this.name = name;
		this.serviceUrl = serviceUrl;
		if(text) {
			this.text = text;
		}
		if(target) {
			this.target = target;
		}

		this.generateServiceUrl();
		this.addOnClickHandler();
	},

	generateServiceUrl : function() {
		this.serviceUrl = this.serviceUrl.replace('{{url}}', encodeURIComponent(this.target)).replace('{{text}}', encodeURIComponent(this.text));
	},

	addOnClickHandler : function() {
		var that = this;
		document.getElementById(this.name).onclick = function() {
			window.open(that.serviceUrl, 'share');
			return false;
		};
	}
};
