var bookmarkLinks = [{text: "MySpace", className: "myspace", href: "http://www.myspace.com/Modules/PostTo/Pages/?u=[URL]&t=[TITLE]&c=%20"},
					 {text: "Facebook", className: "facebook", href: "http://www.facebook.com/sharer.php?u=[URL]&t=[TITLE]"},
					 {text: "StumbleUpon", className: "stumbleupon", href: "http://www.stumbleupon.com/submit?url=[URL]&title=[TITLE]"},
					 {text: "Technorati", className: "technorati", href: "http://technorati.com/faves?sub=addfavbtn&add=[URL]"},
					 {text: "Reddit", className: "reddit", href: "http://reddit.com/submit?url=[URL]&title=[TITLE]"},
					 {text: "Digg", className: "digg", href: "http://digg.com/submit?url=[URL]&title=[TITLE]&media=News&topic=health&thumbnails=0"},
					 {text: "Del.icio.us", className: "delicious", href: "http://del.icio.us/post?v=4&noui&jump=close&url=[URL]&title=[TITLE]"}];

function createNode(type, attributeList, text) {
	var node = document.createElement(type);
	
	if ((type) && (typeof type == "string")) {
	
		if ((attributeList) && (typeof attributeList == "object")) {
			var keys = Object.keys(attributeList);
			var values = Object.values(attributeList);
			for (i = 0; i < keys.length; i++) {
				if (keys[i] == "className") 
					node.className = values[i];
				else
					node.setAttribute(keys[i],values[i]);
			}
		}
		
		if ((text) && (typeof text == "string")) {
			var txt = document.createTextNode(text);
			node.appendChild(txt);
		}
	}
	
	return node;
}

var pageHREF, pageTitle;
function createListItem(bookmark, bookmarkList, href, title) {
	var URL = bookmark.href;
	URL = URL.replace("[URL]",href);
	URL = URL.replace("[TITLE]",title);
	var listItem = createNode("li");
	var bookmarkLink = createNode("a", {href: URL, className: bookmark.className}, bookmark.text);
	Event.observe($(bookmarkLink), "click", function(e) { pageTracker._trackPageview("/share_this_page/"+this.className+"/page.html?url="+pageHREF); }); 
	listItem.appendChild(bookmarkLink);
	bookmarkList.appendChild(listItem);
}

function shareEntry(infoObj) {
	// generate main ul
	if (!$$("ul#"+infoObj.id.replace("share_","options_"))[0]) {
		var shareList = createNode("ul", {id: infoObj.id.replace("share_","options_"), className: "share_page"});
		
		// create bookmark list based on bookmarkLinks object
		bookmarkLinks.each( function(bookmark) { createListItem(bookmark, shareList, encodeURIComponent(infoObj.href), encodeURIComponent(infoObj.title)) } );
		
		// create what's this link
		var whatsThis = createNode("li", {className: "whatsThis"}, "What's This?");
		shareList.appendChild(whatsThis);
		
		Event.observe($(whatsThis), "click", function(e) { toggleWhatsThis("show"); }); 

		// div that encompasses then entire menu
		var shareContain = createNode("div", {id: infoObj.id.replace("share_","menu_"), className: "shareContain hidden"})
		shareContain.appendChild(shareList);
		
		// dropshadow
		var dropShadow = createNode("div", {id: infoObj.id.replace("share_","shadow_"), className: "dropShadow hidden"}," ");
		
		$("container").appendChild(shareContain);
		$("container").appendChild(dropShadow);
		
		// attach mouseout and over events for delayed hide of menu
		Event.observe($$("div#"+infoObj.id.replace("share_","menu_"))[0], "mouseout", function(e) { Event.stop(e); toggleDelay=setTimeout("toggleMenu('"+this.id.replace("menu_","share_")+"')",1000); });
		Event.observe($$("div#"+infoObj.id.replace("share_","menu_"))[0], "mouseover", function(e) { Event.stop(e); clearTimeout(toggleDelay); });
		
		Event.observe($$("a#"+infoObj.id)[0], "mouseout", function(e) { Event.stop(e); if (this.className.indexOf("shareThis_on") >= 0) toggleDelay=setTimeout("toggleMenu('"+this.id+"')",750); });
		Event.observe($$("a#"+infoObj.id)[0], "mouseover", function(e) { Event.stop(e); clearTimeout(toggleDelay); });
	}
}

function toggleWhatsThis(display) {
	var width=400;
	var height=250;
	var borderWidth=6
	
	var top=((document.viewport.getHeight()/2) - (height/2)) + document.viewport.getScrollOffsets().top;
	var left=(document.viewport.getWidth()/2) - (width/2);
	
	if (this.transBorder == undefined) {
	
		this.transBorder = createNode("div",{id: "transBorder"});
		this.whatsThisContain = createNode("div",{id: "whatsThisContain"});
		this.closeWhatsThis = createNode("span",{id: "closeWhatsThis"}, "Close");

		$(this.transBorder).setStyle({ width: (width+borderWidth*2)+"px",
									   height: (height+borderWidth*2)+"px",
									   top: (top - borderWidth)+"px",
									   left: (left - borderWidth)+"px"
									 });
		$(this.transBorder).hide();
		
		$(this.whatsThisContain).setStyle({ width: width+"px",
										  	height: height+"px",
											top: top+"px",
											left: left+"px"
										  });
		$(this.whatsThisContain).hide();
		
		$$("body")[0].insert({top: transBorder});
		$$("body")[0].insert({top: whatsThisContain});
		$$("div#whatsThisContain")[0].innerHTML='<iframe height="'+ (height-20) +'" frameborder="0" width="'+ width +'" scrolling="auto" id="whatsThisFrame" src="/whatsthis.html" border="0"/>';
		$$("div#whatsThisContain")[0].insert({top: this.closeWhatsThis});
		
		$$("iframe#whatsThisFrame")[0].setAttribute("border","0");
		$$("iframe#whatsThisFrame")[0].setAttribute("frameborder","0");
		
		Event.observe($("closeWhatsThis"), "click", function(e) { toggleWhatsThis("hide")  });
	}
	
	if (display == "show") {
		$("transBorder").show();
		$("whatsThisContain").show();
		
		Event.observe($(window), "scroll", function(e) { toggleWhatsThis("reposition")  });
		Event.observe($(window), "resize", function(e) { toggleWhatsThis("reposition")  });
		Event.observe($(document), "mousedown", function(e) { toggleWhatsThis("hide")  });
	}
	else if (display == "hide") {
		$("transBorder").hide();
		$("whatsThisContain").hide();
		
		Event.stopObserving($(window), "scroll", function(e) { toggleWhatsThis("reposition")  });
		Event.stopObserving($(window), "resize", function(e) { toggleWhatsThis("reposition")  });
		Event.stopObserving($(document), "click", function(e) { toggleWhatsThis("hide")  });
	}
	else {
		$(this.transBorder).setStyle({top: (top - borderWidth)+"px",
								  left: (left - borderWidth)+"px"
								});
		$(this.whatsThisContain).setStyle({ top: top+"px",
								  left: left+"px"
								});
	}
}

var toggleDelay;
function toggleMenu(id) {
	var shareThis = $$("a#"+id)[0];
	var linkDim = shareThis.getDimensions();
	
	var shareContain = $$("div#"+id.replace("share_","menu_"))[0];
	var dropShadow = $$("div#"+id.replace("share_","shadow_"))[0];
	positionMenu(shareContain,dropShadow,shareThis);

	shareContain.toggleClassName("hidden");
	dropShadow.toggleClassName("hidden");

	shareThis.toggleClassName("shareThis_on");
}

function positionMenu(shareContain,dropShadow,shareThis) {
	var x = shareThis.cumulativeOffset().left;
	var y = shareThis.cumulativeOffset().top + shareThis.getDimensions().height;

	shareContain.setStyle({
							top: y+'px',
							left: x+'px',
							visibility: "hidden",
							display: "block"
							});
	
	var width, height, top, left;
	width = shareContain.offsetWidth - 6;
	height = shareContain.offsetHeight - 6;
	
	top = y + 5;
	left = x + 7;
	
	dropShadow.setStyle({
							top: top+'px',
							left: left+'px',
							width: width+'px',
							height: height+'px'
							});
	shareContain.setStyle({
							visibility: "visible",
							display: ""
							});
	
}
// END BOOKMARK COMPONENET

document.observe('dom:loaded', function() {
	if($$('.pullquote').size() > 0) {
		$$('.pullquote').invoke('insert', {top: '<div class="t"><div></div></div>'});
		$$('.pullquote').invoke('insert', '<div class="b"><div></div></div>');
	}
	
	// create all of the share this links
	$$("div.entry").each(function(entry) {
		if ($$("div#"+entry.id+" div.entry-footer p")[0]) {
		pageHREF = $$("div#"+entry.id+" a.permalink")[0].href;
		pageTitle = $$("div#"+entry.id+" h3.entry-header a")[0] ? $$("div#"+entry.id+" h3.entry-header a")[0].innerHTML : $$("div#"+entry.id+" h3.entry-header")[0].innerHTML;
		
//		$$("div#"+entry.id+" p.feedburnerFlareBlock").invoke('insert', '<span> &#8226; </span>');
//		$$("div#"+entry.id+" p.feedburnerFlareBlock").invoke('insert', createNode("a", {href: pageHREF, title: pageTitle, className: "shareThis", id: "share_"+entry.id}, "Share This Post"));
		
			$$("div#"+entry.id+" div.entry-footer p")[0].insert('<span class="separator">|</span>');
			$$("div#"+entry.id+" div.entry-footer p")[0].insert(createNode("a", {href: pageHREF, title: pageTitle, className: "shareThis", id: "share_"+entry.id}, "Share This Post"));
		}
	});
	
	// create dropdown menus and event handler
	$$("a.shareThis").each( function(sharethis_instance) { 
		// shareEntry({href: sharethis_instance.href, title: sharethis_instance.title, id: sharethis_instance.id})
		
		Event.observe($$("a#"+sharethis_instance.id)[0], "click", function(e) {
			Event.stop(e);

			shareEntry({href: this.href, title: this.title, id: this.id})

			toggleMenu(this.id);
			clearTimeout(toggleDelay);

		});
	});
	
	var emailSubscriptionText = "Enter email address";
	
	$("email").value = emailSubscriptionText;
	
	Event.observe($("email"), "focus", function(e) { if (this.value == emailSubscriptionText) this.value = ""; });
	Event.observe($("email"), "blur", function(e) { if (this.value == "") this.value = emailSubscriptionText; });

	$$(".post-meta").each( function(category) {
		var blogCategory = category.innerHTML.replace(" ","");
		switch (blogCategory) {
			case "Policy" :
				$(category).addClassName("policy"); 
				break;
			
			case "Research" :
				$(category).addClassName("research"); 
				break;
			
			default :
				$(category).addClassName("new-media");
				break;
			}
		});


});