 
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;
    var title = sharedTitle;
    var description = sharedDescription;
	switch (site) {
	case "newsvine":
		popUpUrl = generatePasskeyURL('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":
		popUpUrl = generatePasskeyURL('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":
		popUpUrl = generatePasskeyURL('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":
		popUpUrl = generatePasskeyURL('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":
		popUpUrl = generatePasskeyURL('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":
		popUpUrl = generatePasskeyURL('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;
	}
}

function generatePasskeyURL(tracker) {
	if(document.getElementById) {
		var url = getShareURL();
		return url;
		//if (decodeURIComponent(url).indexOf('?') > 0) {
		//	return url + encodeURIComponent('&partner=' + tracker + '&exprod=' + tracker);
		//} else {
		//	return url + encodeURIComponent('?partner=' + tracker + '&exprod=' + tracker);
		//}
	}
	return "";
}

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 != "") {
			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) {
	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", "#");
		postLink.onclick = function () { showHideSwap('postList', "bookmark_img", contextPath, gnf); return false; };
		postLink.innerHTML = " Compartir";

		// create an image
		var img = document.createElement("img");
		img.className = "article_toolbar_icon";
		img.setAttribute("src", contextPath + "/docroot/dulcinea/img/icons/icon_" + gnf + "_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 = "show";
		
		//addEmailThis(postList, contextPath, targetURL, gnf);
		
		//add post links
		addPosts(postList, contextPath);

		postElement.appendChild(postList);
	}
}


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(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('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('facebook');", "Facebook", contextPath + "/docroot/dulcinea/img/icons/icon_facebook.png");
			addPostItem(sList, "javascript:articleShare('newsvine');", "Newsvine", contextPath + "/docroot/dulcinea/img/icons/icon_newsvine.png");
			addPostItem(sList, "javascript:articleShare('stumbleUpon');", "StumbleUpon", contextPath + "/docroot/dulcinea/img/icons/icon_stumbleupon.png");
			addPostItem(sList, "javascript:articleShare('addthis');", "Addthis",  contextPath + "/docroot/dulcinea/img/icons/icon_addthis.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));
}
