// $Id: articleShare.js,v 1.27 2008/03/11 21:32:18 prichardk Exp $ 

function showHideSwap (id1, id2) {
	if (document.getElementById) {
		if (document.getElementById(id1).className == 'hide' && document.getElementById(id2).className == 'post') {
			document.getElementById(id1).className = 'show';
			document.getElementById(id2).className = 'postFrame';
		} else {
       		     	document.getElementById(id1).className = 'hide';
            		document.getElementById(id2).className = 'post';
		}
	}
}

function articleShare(site) {
	var popUpUrl;
    var title = getShareHeadline();
    var description = getShareDescription();
	switch (site) {
	case "newsvine":
		popUpUrl = generatePasskeyURL('newsvine');
		var keywords = getShareSection(); 
		if(typeof(getShareSubSection) == 'function') {
			if(getShareSubSection() != '') {
				keywords = keywords + ',' + getShareSubSection();
			}
		}
		if (getShareKeywords() != '') {
			keywords = keywords + ',' + getShareKeywords();
		}
		postPopUp('http://www.newsvine.com/_wine/save?ver=2&popoff=0&aff=nytimes&t=' + keywords + '&e=' + description + '&h=' + title + '&u=' + popUpUrl, 'newsvine', 'toolbar=0,status=0,height=445,width=650,scrollbars=yes,resizable=yes');
		s_code_linktrack('Article-Tool-Share-Newsvine');
		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');
		s_code_linktrack('Article-Tool-Share-Facebook');
		break;
	case "digg":
		popUpUrl = generatePasskeyURL('digg');
		var topic = getShareSection();	
		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');
		s_code_linktrack('Article-Tool-Share-Digg');
		break;	
	case "permalink":
		popUpUrl = generatePasskeyURL('permalink');
		var section = getShareSectionDisplay();
		var byline = getShareByline();
		var pubdate = getSharePubdate();	
		postPopUp('http://www.nytimes.com/export_html/common/new_article_post.html?url=' + popUpUrl + '&title=' + title+ '&summary=' + description + '&section=' + section + '&pubdate=' + pubdate + '&byline=' + byline, 'permalink', 'toolbar=0,status=0,height=410,width=490,scrollbars=yes,resizable=no');
		s_code_linktrack('Article-Tool-Share-Permalink');
		break;	
	case "delicious":
		popUpUrl = generatePasskeyURL('delicious');
		postPopUp('http://del.icio.us/post?v=4&partner=nyt&noui&jump=close&url=' + popUpUrl + '&title=' + title + '&bodytext=' + description, 'delicious', 'toolbar=0,status=0,height=400,width=700,scrollbars=yes,resizable=no');
		s_code_linktrack('Article-Tool-Share-Delicious');
		break;			

	case "yahoobuzz":
	       popUpUrl = getShareURL();
		postPopUp( 
			   'http://buzz.yahoo.com/article/new_york_times/' + popUpUrl,
			   'yahoobuzz',
			   'toolbar=0,status=0,height=600,width=700,scrollbars=yes,resizable=no'
		);
		s_code_linktrack('Article-Tool-Share-YahooBuzz');
		break;			

	case "mixx":
		try {
		    var popUpUrl = getShareURL();
		    var passKey  = encodeURIComponent( '?' + getSharePasskey() );
		    var otherParams = 
			     '&title='       + getShareHeadline()
			   + '&description=' + getShareDescription()
			   + '&tags='        + getShareKeywords()
			   + '&partner='     + 'NYT';
		    postPopUp( 
			   'http://mini.mixx.com/submit/story'
			   + '?page_url='    + getShareURL()
			   + passKey 
			   + otherParams,
			   'mixx',
			   'toolbar=0,status=0,height=550,width=700,scrollbars=yes,resizable=no'
		    );
		} catch(e) {
		    postPopUp( 
			   'http://mini.mixx.com/submit/story'
			   + '?page_url='    + getShareURL()
			   + '&title='       + getShareHeadline()
			   + '&partner='     + 'NYT'
			   ,
			   'mixx',
			   'toolbar=0,status=0,height=550,width=700,scrollbars=yes,resizable=no'
		    );
		}
		s_code_linktrack('Article-Tool-Share-Mixx');
		break;			
	}
}

function generatePasskeyURL(tracker) {
	if(document.getElementById) {
		var passkey = getSharePasskey();
	
		// make sure passkey exists
		if(passkey != '') {
			if (decodeURIComponent(getShareURL()).indexOf('?') > 0) {
				return getShareURL() + encodeURIComponent('&' + passkey + '&partner=' + tracker + '&exprod=' + tracker);
			} else {
				return getShareURL() + encodeURIComponent('?' + passkey + '&partner=' + tracker + '&exprod=' + tracker);
			}
		} else {
			if (decodeURIComponent(getShareURL()).indexOf('?') > 0) {
				return getShareURL() + encodeURIComponent('&partner=' + tracker + '&exprod=' + tracker);
			} else {
				return getShareURL() + encodeURIComponent('?partner=' + tracker + '&exprod=' + tracker);
			}
		}
	}
}

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

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

function writePost() {
	
	if( document.createElement ){
		var parentElement = document.getElementById('toolsList');
		
		//create post li
		var postElement = document.createElement( "LI");
		postElement.className = "post";
		postElement.setAttribute("id", "post");
		// create post link
		var postLink = document.createElement("A");
		postLink.setAttribute("href", "#");
		//postLink.setAttribute("onClick", "showHideSwap(\'postList\', \'post\'); return false;");
		postLink.onclick = function () { showHideSwap('postList', 'post'); return false; };
		postLink.innerHTML = " Share";

		postElement.appendChild(postLink);
		
		// create unordered list for post items
		var postList = document.createElement("UL");
		postList.setAttribute("id","postList");	
		postList.className = "hide";
		
		//add post links
		addPosts(postList);
		
		postElement.appendChild(postList);
		parentElement.appendChild(postElement);
	}
}

function addPosts(shareList) {	

		if(document.getElementById) {	
			var sList;
				
			if(typeof(shareList)=='string') {
				sList = document.getElementById(shareList); 
				}
			
			else if(typeof(shareList)=='object') { 
				sList = shareList;}
			
			else {return false;}

//                   Note: delicious and newsvine commented-out intentionally, per Marko.
			addPostItem(sList, "digg", "javascript:articleShare('digg');", "Digg");
			addPostItem(sList, "facebook", "javascript:articleShare('facebook');", "Facebook");
			addPostItem(sList, "mixx", "javascript:articleShare('mixx');", "Mixx");
			addPostItem(sList, "yahoobuzz", "javascript:articleShare('yahoobuzz');", "Yahoo! Buzz");
			addPostItem(sList, "permalink", "javascript:articleShare('permalink');", "Permalink");
		}
}


