// sharetools.js
// functions for mailing, printing and social media sharing

// setting variable that defines if code is running on the live server
var liveServer = false;

function popEmailWin() {
	var win = window.open('/email-page.cfm','EmailSmallWindow','width=450,height=500,toolbar=no,status=no,left=100,top=100,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');
}

function popPrintWin(page) {
	var var_fontSize = document.getElementById("fontSize").value;
	var printParam = "?renderforprint=1&fontsize=" + var_fontSize;
	
	if (page.indexOf("?") != -1)
	{
		printParam = "&renderforprint=1&fontsize=" + var_fontSize;
	}
	var printUrl = page + printParam;
	var win = window.open(printUrl,'SmallWindow','width=620,height=480,resizable=yes,toolbar=yes,status=yes,left=30,top=30,directories=yes,scrollbars=yes,location=no,menubar=no');
}

function popVideoWin(videoID,videoTitle) {
	eval("window.open('/en/video/customcf/player.cfm?id="+videoID+"&title="+videoTitle+"','VideoWindow','width=404,height=393,toolbar=no,status=no,left=100,top=100,directories=no,scrollbars=no,location=no,resizable=no,menubar=no')");
}

function toggleDisplay(elemid) {
	if (document.getElementById) {
		if (document.getElementById(elemid).className == 'hide' || document.getElementById(elemid).style.display == 'none') {
			document.getElementById(elemid).className = 'show';
			document.getElementById(elemid).style.display = 'block';
		}
		else {
			document.getElementById(elemid).className = 'hide';
			document.getElementById(elemid).style.display = 'none';
		}
	}
}

function popShare(site) {
	var t = getShareTitle();
	var e = encodeURIComponent;
	var l = document.location;
	var u = l.href;
	var d = "";

	if ( document.getElementsByName('description').length > 0 )
		{ var d = document.getElementsByName('description')[0].content; }
	
	switch (site) {
		case 'delicious':
			var s = 'del.icio.us';
			var p = '/post';
			var q = 'v=4&noui&jump=close&url='+u+'&title='+t;
			break;
		case 'digg':
			var s = 'digg.com';
			var p = '/submit';
			var q = 'url='+u+'&title='+t+'&bodytext='+e(d)+'&topic=health';
			break;
		case 'facebook':
			var s = 'www.facebook.com';
			var p = '/sharer.php';
			var q = 'u='+u+'&t='+t;
			break;
		case 'google':
			var s = 'www.google.com';
			var p = '/bookmarks/mark';
			var q = 'op=edit&output=popup&bkmk='+u+'&title='+t;
			break;
		case 'reddit':
			var s = 'reddit.com';
			var p = '/submit';
			var q = 'url='+u+'&title='+t;
			break;
		case 'yahoo':		
			var s = 'bookmarks.yahoo.com';
			var p = '/toolbar/savebm';
			var q = 'u='+u+'&t='+t+'&opener=bm&ei=UTF-8';
			break;
	}

	var f = function(){
		dcsMultiTrack('WT.SiteName','SharpLive','DCS.dcssip',s,'DCS.dcsuri',p,'DCS.dcsqry',q,'WT.ti','ShareTool: '+site,'DCSext.sharedurl',u);
		if(!window.open('http://'+s+p+'?'+q,site,'toolbar=0,status=0,resizable=1,scrollbars=1,width=700,height=450'))
			u='http://'+e(s+p+'?'+q)
	}
	if (liveServer == false){
		alert("Not a live server. URL:\nhttp://"+s+p+'?'+q);
		return;	// return here so we don't call the functions during testing
	}
	else {
		if(/Firefox/.test(navigator.userAgent))
			setTimeout(f,0);
		else
			f();
	}
}

function populateShareContainer(isLiveServer) {
	if( document.createElement ){
		// setting global variable that indicates if it's the live server
		liveServer = isLiveServer;
		// get the container element
		var containerElem = document.getElementById("sharecontainer");
		
		// create the share box div
		var shareBoxElem = document.createElement("div");
		shareBoxElem.setAttribute("id", "sharebox");

		// create the share list
		var shareListElem = document.createElement("ul");
		shareListElem.setAttribute("id", "sharelist");

		// populate the share list
		populateShareList(shareListElem); // old ektron specific code had whatIsThisID as function argument, not needed now
		
		// append share list to share box
		shareBoxElem.appendChild(shareListElem);
		
		// add share box div to container
		containerElem.appendChild(shareBoxElem);
	}
}

function populateShareList(shareList) {
	// add list items
	 addShareListItem(shareList, "javascript:popShare('delicious');", "delicious", "del.icio.us");
	 addShareListItem(shareList, "javascript:popShare('digg');", "digg", "Digg");
	 addShareListItem(shareList, "javascript:popShare('facebook');", "facebook", "Facebook");
	 addShareListItem(shareList, "javascript:popShare('google');", "google", "Google");
	 addShareListItem(shareList, "javascript:popShare('reddit');", "reddit", "Reddit");
	 addShareListItem(shareList, "javascript:popShare('yahoo');", "yahoo", "Yahoo");
	 addShareListItem(shareList, "/news/social-bookmarking.cfm", "shareinfo", "What is this?");
}

function addShareListItem(shareList, link, classname, text) {
	if( document.createElement ){
		// create li element
		var listItemElem = document.createElement("li");
		listItemElem.className = classname;
		
		// create link
		var listItemLinkElem = document.createElement("a");
		listItemLinkElem.setAttribute("href", link);
		var listItemLinkText = document.createTextNode(text);
		listItemLinkElem.appendChild(listItemLinkText);

		// add link to li element
		listItemElem.appendChild(listItemLinkElem);
		
		// add li element to list
		shareList.appendChild(listItemElem);
	}
}