﻿// JavaScript functions for documents using the "subfront template"
// Last updated: 1/30/07


// insertAfter function
// This function operates similar to insertBefore except it allows you to insert content AFTER a given element
function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement);
	} else {
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

// Creates the print feature for story pages, and fires the browser's print option when that feature is clicked
function preparePrint() {
	// Test for the DOM
	if (!document.getElementById("toolbar")) return false;
	if (!document.createTextNode) return false;
	if (!document.createElement) return false;
	if (!window.print) return false;
	// Create the elements for the print feature on story pages
	var printericon = document.createElement("li");
	var printlink = document.createElement("a");
	printlink.setAttribute("id","printStory");
	printlink.setAttribute("title","Print this story");
	var printtext = document.createTextNode("Print story");
	printlink.appendChild(printtext);
	printericon.appendChild(printlink);
	// Insert the newly created elements into the document within the story toolbar
	var storytools = document.getElementById("toolbar");
	var toolitem = storytools.getElementsByTagName("li");
	insertAfter(printericon,toolitem[0]);
	// Impliment the browser's print function when the icon is clicked
	var print = document.getElementById("printStory");
	print.onclick = function() {
		window.print();
	}
	print.onkeypress = print.onclick;
}

// Creates the "Share story" feature for story pages, and reveals the icons when the feature is clicked
function sharethisStory() {
	// Test for the DOM
	if (!document.getElementById("toolbar")) return false;
	if (!document.createTextNode) return false;
	if (!document.createElement) return false;
	if (!document.getElementById) return false;
	// Create the list item for the share this story function
	var shareicon = document.createElement("li");
	var shareiconlink = document.createElement("a");
	shareiconlink.setAttribute("id","shareStory");
	shareiconlink.setAttribute("title","Share this story");
	var sharetext = document.createTextNode("Share story");
	shareiconlink.appendChild(sharetext);
	shareicon.appendChild(shareiconlink);
	// Insert the Share story element into the document
	var toolbar = document.getElementById("toolbar");
	var tools = toolbar.getElementsByTagName("li");
	insertAfter(shareicon,tools[1]);
	// Create the unordered list for the share this story menu items
	var sharestorymenu = document.createElement("ul");
	sharestorymenu.setAttribute("id","sharemenu");
	// Hide the share story menu before a user clicks on the button
	sharestorymenu.style.display = "none";
	// Insert it into the document after the toolbar
	insertAfter(sharestorymenu,toolbar);
	// If this feature needs to be amended, simply change the HTML below. At the time of this writing, we're using the following social networks: Digg, Magnolia, del.icio.us, Newsvine, Google, Yahoo!, Facebook, Reddit and Technorati
	sharestorymenu.innerHTML = "<li id=\"digg\"><a href=\"javascript:var%20m='';var%20d='No%20Description';var%20metas=document.getElementsByTagName('meta');for(count=0;count%3Cmetas.length;count++){if(metas[count].name=='description'){m=metas[count].content;}};if(m!=''){d=encodeURIComponent(m);}location.href='http://digg.com/submit?phase=2&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title)+'&bodytext='+d;\"><img src=\"/images/toolbar/icons/digg_icon.gif\" width=\"16\" height=\"16\" alt=\"Digg this story\" /> Digg</a></li><li id=\"magnolia\"><a href=\"javascript:var%20m='';var%20d='No%20Description';var%20metas=document.getElementsByTagName('meta');for(count=0;count%3Cmetas.length;count++){if(metas[count].name=='description'){m=metas[count].content;}};if(m!=''){d=encodeURIComponent(m);}location.href='http://ma.gnolia.com/bookmarklet/add?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&description='+d;\"><img src=\"/images/toolbar/icons/magnolia_icon.gif\" width=\"16\" height=\"16\" alt=\"Mark in Ma.gnolia\" /> Ma.gnolia</a></li><li id=\"delicious\"><a href=\"javascript:var%20m='';var%20d='No%20Description';var%20metas=document.getElementsByTagName('meta');for(count=0;count%3Cmetas.length;count++){if(metas[count].name=='description'){m=metas[count].content;}};if(m!=''){d=encodeURIComponent(m);}location.href='http://del.icio.us/post?v=3&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&amp;notes='+d;\"><img src=\"/images/toolbar/icons/delicious_icon.gif\" width=\"16\" height=\"16\" alt=\"Tag with del.icio.us\" /> del.icio.us</a></li><li id=\"newsvine\"><a href=\"javascript:u=location.href;h=document.title;void(window.open('http://www.newsvine.com/_wine/save?u='+encodeURIComponent(u)+'&h='+encodeURIComponent(h),'newsvine','toolbar=no,width=590,height=420'));\"><img src=\"/images/toolbar/icons/newsvine_icon.gif\" width=\"16\" height=\"16\" alt=\"Seed newsvine\" /> Newsvine</a></li><li id=\"google\"><a href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,d=a.open('http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+c(b.location)+'&title='+c(b.title),'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=420px,width=550px,resizable=1,alwaysRaised=1');a.setTimeout(function(){d.focus()},300)})();\"><img src=\"/images/toolbar/icons/google_icon.gif\" width=\"15\" height=\"16\" alt=\"Post to Google Base\" /> Google</a></li><li id=\"yahoo\"><a href=\"javascript:(function()%20%7Bwindow.open('http://myweb2.search.yahoo.com/myresults/bookmarklet?t='+encodeURIComponent(document.title)+'&d='+'&u='+encodeURIComponent(window.location.href)+'&ei=UTF-8','popup',%20'width=520px,height=420px,status=0,location=0,resizable=1,%20scrollbars=1,left=100,top=50',0);%7D)()\"><img src=\"/images/toolbar/icons/yahoo.gif\" width=\"14\" height=\"16\" alt=\"Add to Yahoo!\" /> Yahoo!</a></li><li id=\"facebook\"><a href=\"javascript:var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1164728347&u='+e(l.href)+'&t='+e(d.title);try{if(!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z){a=function(){if(!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=0,width=626,height=436'))l.href=f+p};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0)\"><img src=\"/images/toolbar/icons/facebook.gif\" width=\"16\" height=\"16\" alt=\"Share on Facebook\" /> Facebook</a></li><li id=\"reddit\"><a href=\"javascript:location.href='http://reddit.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)\"><img src=\"/images/toolbar/icons/reddit.gif\" width=\"16\" height=\"16\" alt=\"Submit this link to Reddit\" /> Reddit</a></li><li id=\"technorati\"><a href=\"javascript:location.href='http://www.technorati.com/search/'%20+%20document.location.href\"><img src=\"/images/toolbar/icons/technorati_icon.gif\" width=\"16\" height=\"14\" alt=\"Related at Technorati\" /> Technorati</a></li>";
	// Unhide the share story menu when the user clicks it, and hide it when clicked again
	shareiconlink.onclick = function() {
		if (sharestorymenu.style.display == "none") {
		sharestorymenu.style.display = "block";
	}	else {
		sharestorymenu.style.display = "none";
		}
	}
	shareiconlink.onkeypress = shareiconlink.onclick;
} 