var RTR_ArticleURL = location.href;
if (location.href.search("\\?") != -1) {
	RTR_ArticleURL = location.href.split("?")[0];
}

function shareDelicious(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://del.icio.us/post?v=4&noui&jump=close&url='+encodeURIComponent(RTR_ArticleURL+"?rpc=28")+'&title='+encodeURIComponent(arr_article[0])+'&notes='+encodeURIComponent(arr_article[1])+'&tags=news', 540, 500, 1, 'deliciousPopup');
}

function shareFacebook(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://www.facebook.com/sharer.php?u='+encodeURIComponent(RTR_ArticleURL+"?rpc=60")+'&t='+encodeURIComponent(arr_article[0]), 626, 436, 1, 'facebookPopup');
}

function shareNewsvine(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://www.newsvine.com/_wine/save?aff=reuters&h='+encodeURIComponent(arr_article[0])+'&t=world-news&u='+encodeURIComponent(RTR_ArticleURL+"?rpc=63")+'&e='+encodeURIComponent(arr_article[1]), 650, 445, 1, 'newsvinePopup');
}

function shareYahoo(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://myweb2.search.yahoo.com/myresults/bookmarklet?t='+encodeURIComponent(arr_article[0])+'&u='+encodeURIComponent(RTR_ArticleURL+"?rpc=46")+'&ei=UTF-8'+'&d='+encodeURIComponent(arr_article[1])+'&tag=news', 620, 540, 1, 'yahooPopup');
}

function shareMixx(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://www.mixx.com/submit/story?title='+encodeURIComponent(arr_article[0])+'&page_url='+encodeURIComponent(RTR_ArticleURL+"?rpc=62")+'&description='+encodeURIComponent(arr_article[1]), 1024, 640, 1, 'mixxPopup');
}

function shareDigg(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')
		commonPopup('http://digg.com/remote-submit?phase=2&url='+encodeURIComponent(RTR_ArticleURL+"?rpc=64")+'&title='+encodeURIComponent(arr_article[0])+'&bodytext='+encodeURIComponent(arr_article[1])+'&topic=world_news', 540, 500, 1, 'diggPopup');
}

function shareLinkedIn(arr_article) {
	if (typeof(arr_article[0]) != 'undefined')		
		commonPopup('http://www.linkedin.com/shareArticle?mini=true&url='+encodeURIComponent(RTR_ArticleURL+"?rpc=59")+'&title='+encodeURIComponent(arr_article[0])+'&summary='+encodeURIComponent(arr_article[1])+'&source=Reuters', 520, 570, 1, 'linkedinPopup');
}

var arrShareLinks = new Array(
	"Delicious",
	"javascript:shareDelicious();",
	"Digg",
	"javascript:shareDigg();",
	"Facebook",
	"javascript:shareFacebook();",
	"Mixx",
	"javascript:shareMixx();",
	"LinkedIn",
	"javascript:shareLinkedIn();",
	"Yahoo!",
	"javascript:shareYahoo();"
);

var shareOverlayFocus = false;
var shareInterval;

// document.getElementById("shareLink").onclick = showShareOverlay;

function trimTools(strText) {
	return strText.replace(/\\./,"").replace("!","").replace(" ","");
}

function parseShareName(strWord) {
	if (strWord == "Delicious")
		strWord = "Del.icio.us";
	return strWord;
}

function tryShareOverlay() {
	if (!document.getElementById("shareContainer")) {
		var shareDiv = document.createElement("div");
		shareDiv.id = "shareContainer";
		shareDiv.className = "hidden";
		var objShareDiv = document.getElementById("atools").insertBefore(shareDiv, document.getElementById("autilities"));
	}
	if (document.getElementById("shareContainer").innerHTML == '') {
		showShareOverlay();
	} else {
		hideShareOverlay();
	}
}

function showShareOverlay() {
	var strShareOverlay = '<div id="shareTab"></div>';	
	strShareOverlay += '<div id="shareShadow"><div id="shareContents">';	
	for (i=0; i<arrShareLinks.length; i++) {
		if ((i%2) == 0) {
			strShareOverlay += '<div class="shareTool">';
			strShareOverlay += '<a id="' + trimTools(arrShareLinks[i]).toLowerCase() + 'Link" href="' + arrShareLinks[i+1] + '">' + parseShareName(arrShareLinks[i]) + '</a>';
			strShareOverlay += '</div>';
		}
	}
	strShareOverlay += '<div id="shareWhat"><a href="javascript:commonPopup('+"'/tools/share',	540, 600, 1, 'sharePopup'"+');">(what is this?)</a></div>';
	strShareOverlay += '</div></div>';
	strShareOverlay += '<div id="shareCloseButton"><a href="javascript:hideShareOverlay();"></a></div>';
	if (document.getElementById) {
		var intShareX = document.getElementById("shareLink").offsetLeft - 6;
		var intShareY = document.getElementById("shareLink").offsetTop + document.getElementById("shareLink").offsetHeight - 2;		
		document.getElementById("shareContainer").innerHTML = strShareOverlay;
		document.getElementById("shareContainer").className = "";
		document.getElementById("shareContainer").style.cssText = "position:absolute; top:" + intShareY + "px; left:" + intShareX + "px;";
	}
}

function hideShareOverlay() {
	if (document.getElementById) {
		document.getElementById("shareContainer").innerHTML = "";
		document.getElementById("shareContainer").className = "hidden";
		document.getElementById("shareContainer").style.cssText = "";
		shareOverlayFocus = false;
	}
}


function commonPopup(url, width, height, toolsInd, wname)
{
    var options = "width=" + width + ",height=" + height + ",top=" + ((screen.height - height) / 4).toString() + ",left=" + ((screen.width - width) / 2).toString();

    switch (toolsInd)
    {
        case 1:
            options += ",toolbar=no,status=no,resizable=no,scrollbars=yes";
            break;
        case 2:
            options += ",menubar=yes,toolbar=yes,status=yes,resizable=yes,location=yes,scrollbars=yes";
            break;
        case 3:
            options += ",top=50,left=50,resizable=yes,scrollbars=yes,status=no,menubar=no,toolbar=no,location=yes";
            break;
        case 4:
            options += ",top=50,left=50,resizable=yes,scrollbars=no,status=no,menubar=no,toolbar=no,location=yes";
            break;            
        default:
            //do nothing
            break;
    }

    if (!wname)
    {
        wname = "reutersPopup";
    }

    popupWindow = window.open(url, wname, options);

    if (popupWindow)
    {
        popupWindow.focus();
    }
}
