 
var sharedTitle;
var sharedDescription;
var sharedKeywords;

function showHideSwap (id1, imgId, contextPath, gnf) {
	if (document.getElementById) {
		if (document.getElementById(id1).className == 'hide') {
			document.getElementById(id1).className = 'show';
			document.getElementById(imgId).setAttribute("src", contextPath + "/docroot/dulcinea/img/icons/icon_" + gnf + "_minus.png");
		} else {
	     	document.getElementById(id1).className = 'hide';
			document.getElementById(imgId).setAttribute("src", contextPath + "/docroot/dulcinea/img/icons/icon_" + gnf + "_plus.png");
		}
	}
}

function articleShare(site) {
    var popUpUrl = getShareURL();
    var title = sharedTitle;
    var description = sharedDescription;
	switch (site) {
	case "newsvine":
		var keywords = sharedKeywords;
		postPopUp('http://www.newsvine.com/_wine/save?ver=2&popoff=0&t=' + keywords + '&e=' + description + '&h=' + title + '&u=' + popUpUrl, 'newsvine', 'toolbar=0,status=0,height=445,width=650,scrollbars=yes,resizable=yes');
		break;
	case "facebook":
		postPopUp('http://www.facebook.com/sharer.php?u=' + popUpUrl + '&t=' + title, 'facebook', 'toolbar=0,status=0,height=436,width=646,scrollbars=yes,resizable=yes');
		break;
	case "digg":
		postPopUp('http://digg.com/remote-submit?phase=2&url=' + popUpUrl + '&title=' + title + '&bodytext=' + description, 'digg', 'toolbar=0,status=0,height=450,width=650,scrollbars=yes,resizable=yes');
		break;	
	case "delicious":
		postPopUp('http://del.icio.us/post?v=4&noui&jump=close&url=' + popUpUrl + '&title=' + title + '&bodytext=' + description, 'delicious', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
		break;			
	case "addthis":
		postPopUp('http://www.addthis.com/bookmark.php?url=' + popUpUrl + '&title=' + title, 'addthis', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
		break;
	case "stumbleUpon":
		postPopUp('http://www.stumbleupon.com/submit?url=' + popUpUrl + '&title=' + title, 'stumbleUpon', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
		break;
	case "diigo":
		postPopUp('http://www.diigo.com/post?url='+popUpUrl+'&title='+title, 'diigo', 'toolbar=0,status=0,height=700,width=1000,scrollbars=yes,resizable=no');
		break;
	case "yahoobuzz":
		// fall-back behavior.  At least it'll just open the page in a new window/tab, but not downsized.
		postPopUp( 'http://buzz.yahoo.com/submit?submitUrl=' + popUpUrl, 'yahoobuzz', 'scrollbars=yes,resizable=yes');
		break;
	}
}

function postPopUp(url, name, params) {
	var win = window.open(url, name, params);
}

function addPostItem(parentElement, post_link, text, imgSrc) {
	if( document.createElement ){
		var postItem = document.createElement("LI");
		postItem.className = "article_toolbar_item";
		
		var itemLink = document.createElement("A");
		itemLink.setAttribute("href", post_link);
		itemLink.setAttribute("onClick", post_link);
		itemLink.innerHTML = text;

		// create an image
		if (imgSrc != null && imgSrc != "") {
			var img = document.createElement("img");
			img.className = "article_toolbar_icon";
			img.setAttribute("src", imgSrc);
			//img.setAttribute("width", 14);
			//img.setAttribute("height", 14);
			img.setAttribute("border", "0");
			itemLink.appendChild(img);
		}
	
		postItem.appendChild(itemLink);
		parentElement.appendChild(postItem);
	}
}

function writePost(gnf, contextPath, title, desc, targetURL, keywords, canHide, isHidden) {
	if( document.createElement ){
		sharedTitle = encodeURIComponent(title);
		sharedDescription = encodeURIComponent(desc);
		sharedKeywords = encodeURIComponent(keywords);

		var postElement = document.getElementById('common_share_links');
		
		// create post link
		var postLink = document.createElement("A");
		postLink.setAttribute("href", "#");
		if (canHide)
			postLink.onclick = function () { showHideSwap('postList', "bookmark_img", contextPath, gnf); return false; };
		postLink.innerHTML = " Share";

		// create an image
		var img = document.createElement("img");
		img.className = "article_toolbar_icon";
		img.setAttribute("src", contextPath + "/docroot/dulcinea/img/icons/icon_" + gnf + "_" + (canHide && isHidden ? "plus" : "minus") + ".png");
		img.setAttribute("border", "0");
		img.setAttribute("id", "bookmark_img");
		img.setAttribute("alt", "Click to expand: Popular Social-bookmarking tools");

		postElement.appendChild(postLink);
		postLink.appendChild(img);
		
		// create unordered list for post items
		var postList = document.createElement("UL");
		postList.setAttribute("id","postList");	
		postList.className = canHide && isHidden ? "hide" : "show";
		
		//addEmailThis(postList, contextPath, targetURL, gnf);
		
		//add post links
		addPosts(gnf, postList, contextPath);
		postElement.appendChild(postList);

		if (canHide == false && gnf != "guides")
			instrumentYahooBuzzBadge();
	}
}

function instrumentYahooBuzzBadge() {
	if ( document.getElementById('yahooBuzzBadge') ) {
		// already instrumented. Abort.
		return;
	}

	var list = document.getElementById('postList');
	if ( !list ) {
		// haven't created the list yet. Abort.
		return;
	}

	//createYahooBuzzOnLoad();

	var postLinks = list.getElementsByTagName('a');
	for ( var i = 0, l = postLinks.length; i < l; i ++ ) {
		var href = postLinks[i].href;
		href = href.toLowerCase();
		if ( href.indexOf('buzz') !== -1 && href.indexOf('yahoo') !== -1 ) {
			// works whether it's the real URL or the javascript: with yahoobuzz in the call.
			var badge = postLinks[i];
			badge.id = 'yahooBuzzBadge';
		}
	}

	// Y! Buzz requires its own script be included dynamically like this ...
	var artUriYahoo = decodeURIComponent(getShareURL());
	document.write('<script type="text/javascript">yahooBuzzArticleId = window.location.href;</script>' +
			'<script badgetype="text" src="http://d.yimg.com/ds/badge2.js">'
			+ artUriYahoo
			+ '</script>'
			);
}

function createYahooBuzzOnLoad() {
	// An event listener is attached once the badge is loaded so that 
	// function itself is separated from the dom ref via a closure to prevent IE 6 memory leakage.
	window.yahooBuzzOnLoad = function () {
		return function () {
			if(typeof(window['yahooBuzzOnLoad'])!='function') {
				window.yahooBuzzOnLoad=null; // clean up the global.
			}
			var badge = document.getElementById('yahooBuzzBadge-form');
			if ( badge ) {
				if ( badge.addEventListener ) {
					badge.addEventListener('click', onclick, true);
				} else if ( badge.attachEvent ) {
					badge.attachEvent('click', onclick);
				} else {
					// can't put an onclick directly, or it'll break the badge.
					// This is an edge case, so losing non-mousers is not very significant.
					badge.onmousedown = onclick;
				}
			}
		}
	}();

};

function addEmailThis(shareList, contextPath, targetURL, gnf) {
	if( document.createElement ){
		var postItem = document.createElement("LI");
		postItem.className = "article_toolbar_item";

		var itemLink = document.createElement("A");
		itemLink.setAttribute("href", "mailto:?subject=An interesting article from findingDulcinea&body=Hi,%0D%0A%0D%0AI just found an interesting entry on http://findingDulcinea.com. Thought you'd like to look at it too.   %0D%0A%0D%0Ahttp://www.findingDulcinea.com" + targetURL);
		//itemLink.setAttribute("onClick", post_link);
		itemLink.innerHTML = "Email This";

		var img = document.createElement("img");
		img.className = "article_toolbar_icon";
		img.setAttribute("src", contextPath + "/docroot/dulcinea/img/icons/icon_" + gnf + "_email.png");
		
		itemLink.appendChild(img);
		postItem.appendChild(itemLink);
		shareList.appendChild(postItem);
	}
}


function addPosts(gnf, shareList, contextPath) {	

		if(document.getElementById) {	
			var sList;
				
			if(typeof(shareList)=='string') {
				sList = document.getElementById(shareList); 
				}
			
			else if(typeof(shareList)=='object') { 
				sList = shareList;}
			
			else {return false;}
	
			//addPostItem(sList, "javascript:articleShare('addthis');", "Addthis",  contextPath + "/docroot/dulcinea/img/icons/icon_addthis.png");
			if (gnf == "guides") {
				addPostItem(sList, "javascript:articleShare('delicious');", "Del.icio.us", contextPath + "/docroot/dulcinea/img/icons/icon_delicious.png");
				addPostItem(sList, "javascript:articleShare('digg');", "Digg", contextPath + "/docroot/dulcinea/img/icons/icon_digg.png");
				addPostItem(sList, "javascript:articleShare('diigo');", "Diigo",  contextPath + "/docroot/dulcinea/img/icons/icon_diigo.jpg");
				addPostItem(sList, "javascript:articleShare('facebook');", "Facebook", contextPath + "/docroot/dulcinea/img/icons/icon_facebook.png");
			} else {
				addPostItem(sList, "javascript:articleShare('digg');", "Digg", contextPath + "/docroot/dulcinea/img/icons/icon_digg.png");
				addPostItem(sList, "javascript:articleShare('facebook');", "Facebook", contextPath + "/docroot/dulcinea/img/icons/icon_facebook.png");
				addPostItem(sList, "javascript:articleShare('stumbleUpon');", "StumbleUpon", contextPath + "/docroot/dulcinea/img/icons/icon_stumbleupon.png");
			}
		}
}

// Current Page Reference
// copyright Stephen Chapman, 1st Jan 2005
function getURL(uri) {
	uri.dir = location.href.substring(0, location.href.lastIndexOf('\/'));
	uri.dom = uri.dir; if (uri.dom.substr(0,7) == 'http:\/\/') uri.dom = uri.dom.substr(7);
	uri.path = ''; var pos = uri.dom.indexOf('\/'); if (pos > -1) {uri.path = uri.dom.substr(pos+1); uri.dom = uri.dom.substr(0,pos);}
	uri.page = location.href.substring(uri.dir.length+1, location.href.length+1);
	pos = uri.page.indexOf('?');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	pos = uri.page.indexOf('#');if (pos > -1) {uri.page = uri.page.substring(0, pos);}
	uri.ext = ''; pos = uri.page.indexOf('.');if (pos > -1) {uri.ext =uri.page.substring(pos+1); uri.page = uri.page.substr(0,pos);}
	uri.file = uri.page;
	if (uri.ext != '') uri.file += '.' + uri.ext;
	if (uri.file == '') uri.page = 'index';
	uri.args = location.search.substr(1).split("?");
	return uri;
}


function getShareURL() {
	var uri = new Object();
	var URL = getURL(uri);
	return encodeURIComponent(URL.dir + "/" + URL.file);
}

function getSharePubdate() {
	 var today=new Date();
	 return encodeURIComponent(today.getMonth()+1+"/"+today.getDate()+"/"+(today.getYear() + 1900));
}
