var discoveryShare = {
	defaultAddThisUser: "dnisites",
	defaultButtonText : "Share",
	defaultHeading : "Bookmark & Share",
	defaultServices : "facebook,twitter,myspace,google,stumbleupon",
	
	addEvent : function(element, type, func, bubble){
		if (element.addEventListener) {
			element.addEventListener(type, func, bubble);
		}
		else {
			if (element.attachEvent) {
				element.attachEvent("on" + type, func);
			}
		}
		return;
	},
	
	getShareButtons : function() {
		var shareButtons = new Array();
		var divs = document.getElementsByTagName("div");
		for (var x = 0; x < divs.length; x++) {
			if (divs[x].className && divs[x].className.indexOf("discovery-share-button") != -1) 
				shareButtons[shareButtons.length] = divs[x];
		}
		var anchors = document.getElementsByTagName("a");
		for (var x = 0; x < anchors.length; x++) {
			if (anchors[x].className && anchors[x].className.indexOf("discovery-share-button") != -1) 
				shareButtons[shareButtons.length] = anchors[x];
		}
		return shareButtons;
	},
	
	processShareButtons : function(e){
		var shareButtons = discoveryShare.getShareButtons();
		if (shareButtons.length > 0) {
			discoveryShare.addCSSFile();
			for (var x = 0; x < shareButtons.length; x++) {
				if (shareButtons[x].className.indexOf("discovery-share-button-style") != -1) {
					if (shareButtons[x].innerHTML == "") {
						shareButtons[x].innerHTML = (_shareDetails["button-text"] && _shareDetails["button-text"].length > 0)? _shareDetails["button-text"] : discoveryShare.defaultButtonText;
					}
				}
				var shareButton = shareButtons[x];

				if (!(shareButton.getAttribute("share:processed") == 'true')) {
					discoveryShare.addEvent(shareButton, "click", function(e){
						var el;
						if (e.target)
							el = e.target;
						else
							el = e.srcElement;
						var url = el.getAttribute("share:url");
						if (url == "") {
							url = null;
						}
						discoveryShare.createSharePopupMarkup(url);
						discoveryShare.displayShareDialog();
					}, false);
					shareButton.setAttribute("share:processed", "true");
				}
			}
		}
		return;
	},
	
	addCSSFile: function() {
		var domain = window.location.protocol + "//" + window.location.host;
		
		var styleSheetUrl = "/resources/igroups/css/share/discovery-share.css",
			localStyleSheetUrl = "/localresources/interestgroups/css/share/discovery-share.css",
			links = document.getElementsByTagName("link"),
			hasStyleSheet = false;
		
		var possibleHrefs = [styleSheetUrl, localStyleSheetUrl, domain + styleSheetUrl, domain + localStyleSheetUrl]; // firefox returns path qualified with domain, webkit doesnt
		
		linksLoop:
		for (var x = 0; x < links.length; x++){
			hrefLoop:
			for(var y = 0, href; href = possibleHrefs[y]; y++){
				if (links[x].href == href) {
					hasStyleSheet = true;
					break linksLoop;
				}
			}
		}
		if (!hasStyleSheet) {
			var cssFile = document.createElement("link");
			cssFile.setAttribute("href", styleSheetUrl);
			cssFile.setAttribute("rel", "stylesheet");
			cssFile.setAttribute("type", "text/css");
			document.getElementsByTagName("head")[0].appendChild(cssFile);
			
			var localCssFile = document.createElement("link");
			localCssFile.setAttribute("href", localStyleSheetUrl);
			localCssFile.setAttribute("rel", "stylesheet");
			localCssFile.setAttribute("type", "text/css");
			document.getElementsByTagName("head")[0].appendChild(localCssFile);
		}
		return;
	},
	
	displayShareDialog : function(e) {
		document.getElementById("sharing-container").style.display = "block";
		var fullHeight = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;
		var fullWidth = document.body.parentNode.scrollWidth;
		var sharingModalPanel = document.getElementById("sharing-modal-panel");
		sharingModalPanel.style.height = document.body.scrollHeight + "px";
		sharingModalPanel.style.width = fullWidth + "px";
		
		var sharingDialogContainer = document.getElementById("sharing-dialog-container");
		sharingDialogContainer.style.top = ((fullHeight - sharingDialogContainer.offsetHeight) / 2) + "px";
		sharingDialogContainer.style.left = ((fullWidth - sharingDialogContainer.offsetWidth) / 2) + "px";
		return;
	},
	
	
	closeShareDialog : function(e){
		var sharingContainer = document.getElementById("sharing-container");
		sharingContainer.parentNode.removeChild(sharingContainer);
		return;
	},
	
	
	createSharePopupMarkup : function(url){
	
		var sharingContainer = document.createElement("div");
		var sharingModalPanel = document.createElement("div");
		var sharingDialog = document.createElement("div");
		var sharingDialogTop = document.createElement("div");
		var sharingDialogBottom = document.createElement("div");
		var sharingDialogBorder = document.createElement("div");
		var headingPanel = document.createElement("div");
		var servicePanel = document.createElement("div");
		var leftColumn = document.createElement("div");
		var rightColumn = document.createElement("div");
		
		sharingContainer.id = "sharing-container";
		sharingModalPanel.id = "sharing-modal-panel";
		sharingDialog.id = "sharing-dialog";
		sharingDialogTop.id = "sharing-dialog-top";
		sharingDialogBottom.id = "sharing-dialog-bottom";
		sharingDialogBorder.id = "sharing-dialog-container";
		headingPanel.id = "heading-panel";
		servicePanel.id = "service-panel";
		leftColumn.id = "left-column";
		rightColumn.id = "right-column";
		
		var headingText = document.createElement("div");
		headingText.id = "heading-text";
		headingText.innerHTML = (_shareDetails["heading-text"] && _shareDetails["heading-text"].length > 0) ? _shareDetails["heading-text"] : discoveryShare.defaultHeading;
		var closeButton = document.createElement("div");
		closeButton.id = "close-button";
		headingPanel.appendChild(headingText);
		headingPanel.appendChild(closeButton);
		sharingDialog.appendChild(headingPanel);
		
		var addthisUser = (_shareDetails["addthis-user"] && _shareDetails["addthis-user"].length > 0) ? _shareDetails["addthis-user"] : discoveryShare.defaultAddThisUser,
			serviceList = ((_shareDetails["service-list"] && _shareDetails["service-list"].length > 0)  ? _shareDetails["service-list"] : discoveryShare.defaultServices);
		
		serviceList += ",email,print,favorites";
		
		var services = serviceList.split(',');

		for (var x = 0 ; x <services.length; x++) {
			var serviceButtonContainer = document.createElement("div");
			serviceButtonContainer.className = "service-button-container";
			var serviceButton = document.createElement("a");
			serviceButton.className = "addthis_button_" + services[x] + " addthis_32x32_style";
			if (url) {
				serviceButton.setAttribute("addthis:url", url);
			}
			serviceButton.setAttribute("addthis:username", addthisUser);
			serviceButton.innerHTML = addthis.util.getServiceName(services[x]);
			
			var type = "";
			switch(services[x]){
				case "facebook":
				case "email":
				case "twitter":
					type = services[x];
			}
					
			if(type){
				var self = this;
				(function(t){
					self.addEvent(serviceButton, "click", function(){
						discoveryShare.sendReport(t);
					});
				}(type));
			}
			
			serviceButtonContainer.appendChild(serviceButton);
			if (x < (services.length / 2)) {
				leftColumn.appendChild(serviceButtonContainer);
			}
			else {
				rightColumn.appendChild(serviceButtonContainer);
			}
			discoveryShare.addEvent(serviceButton, "click", discoveryShare.closeShareDialog, false);
		}
		
		
		servicePanel.appendChild(leftColumn);
		servicePanel.appendChild(rightColumn);
		sharingDialog.appendChild(servicePanel);
		sharingDialogBorder.appendChild(sharingDialogTop);
		sharingDialogBorder.appendChild(sharingDialog);
		sharingDialogBorder.appendChild(sharingDialogBottom);
		sharingContainer.appendChild(sharingModalPanel);
		sharingContainer.appendChild(sharingDialogBorder);
		document.body.appendChild(sharingContainer);
		
		discoveryShare.addEvent(sharingModalPanel, "click", discoveryShare.closeShareDialog, false);
		discoveryShare.addEvent(closeButton, "click", discoveryShare.closeShareDialog, false);
		
		addthis.toolbox("#service-panel");
		return;
	},
	
	sendReport : function(type){
		var report = $TORA("PAGE").getProperty("pageReport");
		if (report) {
			var evInfo = {};
			evInfo.srcType = "social";
			evInfo.srcElement = window.document.body;
			evInfo.reportType = "SOCIAL";
			
			switch (type) {
				case "email": evInfo.typeCode = 1;
					break;
				case "facebook": evInfo.typeCode = 2;
					break;
				case "twitter": evInfo.typeCode = 3;
					break;
			}
			report.send("omnitureEvent", null, evInfo);
		}
	},
	
	share : function(url) {
		discoveryShare.addCSSFile();
		discoveryShare.createSharePopupMarkup(url);
		discoveryShare.displayShareDialog();
		return;
	}
}

var _shareDetails;

if (typeof(shareDetails) == 'undefined') {
	_shareDetails = {};
}
else
{
	_shareDetails = shareDetails;
}

discoveryShare.addEvent(window, "load", discoveryShare.processShareButtons, false);



