//******************************************************************************************//
//******************************************************************************************//
// Bookmark and Share Scripts
//******************************************************************************************//
//******************************************************************************************//

function addBookmarkHover(id) {
	var elem = GetElementById(id);
	if (elem) {
		elem.className += (elem.className.length > 0 ? " " : "") + "sfhover";
	}
}

function removeBookmarkHover(id) {
	var elem = GetElementById(id);
	if (elem) {
		elem.className = elem.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
	}
}

//******************************************************************************************//
//* The javascript class that handles showing and hiding the bookmark/share "window" by
//* applying and removing the "sfhover" CSS class to the LI element.
//******************************************************************************************//
if (typeof CDC == "undefined") var CDC = new Object();
if (typeof CDC.Share == "undefined") CDC.Share = new Object();
CDC.Share = function() {
}
CDC.Share.prototype = {
	timeout : null,
	showPopup : function() {
		clearTimeout(this.timeout);
		if (this.className.indexOf("sfhover") < 0) {
			var command = "addBookmarkHover('" + this.id + "')";
			this.timeout = setTimeout(command, 300);
		}		
	},
	hidePopup : function() {
		if (this.className.indexOf("sfhover") < 0) {
			clearTimeout(this.timeout);
		} else {
			var command = "removeBookmarkHover('" + this.id + "')";
			this.timeout = setTimeout(command, 200);
		}
	}
}

//******************************************************************************************//
//* A function that iterates through the collection of LI items with a class of "share" and
//* sets the event handlers for the onmouseover and onmouseout events.
//******************************************************************************************//
shareHover = function() {
	var shareNavLIs = getElementsByClass("share", null, "li");
	if (shareNavLIs && shareNavLIs.length > 0) {
		for (var liIndex = 0; liIndex < shareNavLIs.length; liIndex++) {
			var shareNavLi = shareNavLIs[liIndex];
			if (shareNavLi) {
				var shareClass = new CDC.Share(shareNavLi);
				shareNavLi.onmouseover = shareClass.showPopup;
				shareNavLi.onmouseout = shareClass.hidePopup;
			}
		}
	}
}

//******************************************************************************************//
//* A function that iterates through the collection of LI items with a class of "share" and
//* sets the event handlers for the onfocus and onblur events.  These handlers are needed
//* to support keyboard-driven navigation of the bookmark/share functionality.
//******************************************************************************************//
mcAccessible = function() {	
	var shareNavLIs = getElementsByClass("share", null, "li");
	if (shareNavLIs && shareNavLIs.length > 0) {
	
		for (var liIndex = 0; liIndex < shareNavLIs.length; liIndex++) {
			
			var shareNavLi = shareNavLIs[liIndex];
			if (shareNavLi) {
			
				shareNavLi.onfocus = function() {
					// Add sfhover class if it doesn't already exist.
					if (this.className.indexOf("sfhover") < 0)
						this.className += (this.className.length>0 ? " " : "") + "sfhover";
				}
				shareNavLi.onblur = function() {
					// Remove sfhover class.
					this.className = this.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
				}

				var mcEls = shareNavLi.getElementsByTagName("A");
				for (var i = 0; i < mcEls.length; i++) {
					mcEls[i].onfocus = function() {
						if (this.className.indexOf("sffocus") < 0)
							this.className += (this.className.length>0 ? " " : "") + "sffocus"; //a:focus
						this.parentNode.className += (this.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < a:focus
						if (this.parentNode.parentNode.parentNode.nodeName == "LI") {
							this.parentNode.parentNode.parentNode.className += (this.parentNode.parentNode.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < ul < li < a:focus
							if (this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
								this.parentNode.parentNode.parentNode.parentNode.parentNode.className += (this.parentNode.parentNode.parentNode.parentNode.parentNode.className.length > 0 ? " " : "") + "sfhover"; //li < ul < li < ul < li < a:focus
							}
						}
					}
					mcEls[i].onblur = function() {
						this.className = this.className.replace(new RegExp("( ?|^)sffocus\\b"), "");
						this.parentNode.className = this.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
						if (this.parentNode.parentNode.parentNode.nodeName == "LI") {
							this.parentNode.parentNode.parentNode.className = this.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
							if (this.parentNode.parentNode.parentNode.parentNode.parentNode.nodeName == "LI") {
								this.parentNode.parentNode.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)sfhover\\b"), "");
							}
						}
					}
				}
			}
		}
	}
}

//
// The accessibility script and hoaver script is being loaded for all
// browsers since we are relying on javascript for the delayed hover effects.
//
// thanks http://www.brothercake.com/site/resources/scripts/onload/ for event handler logic.
//
if (window.addEventListener) {// gecko, safari, konqueror and standard
	window.addEventListener('load', mcAccessible, false); 
	window.addEventListener('load', shareHover, false);
}
else if (document.addEventListener) { // opera 7
	document.addEventListener('load', mcAccessible, false);
	document.addEventListener('load', shareHover, false);
}
else if (window.attachEvent) { // win/ie
	window.attachEvent('onload', shareHover);
	window.attachEvent('onload', mcAccessible);
} else { // mac/ie5
	if (typeof window.onload == 'function') {
		var existing = onload;
		window.onload = function() {
						existing();
						shareHover();
						mcAccessible();
		}
	} else {
		window.onload = function() {
						shareHover();
						mcAccessible();
		}
	}
}

var cdcShareWindowObjectReference;

//******************************************************************************************//
//* A function that opens the selected bookmark/share website and passes the URL and title
//* using the API for that site.
//******************************************************************************************//
function openBookmarkSite(sitename) {
	var title = escape(document.title);
	var url = escape(location.href);
	if (sitename.toLowerCase() == "favorites") {
		if (window.sidebar) { // firefox
			window.sidebar.addPanel(document.title, location.href, "");
		} else if (document.all) { // IE
			external.AddFavorite(location.href, document.title);
		} else if (window.opera && window.print) { // opera
			var elem = document.createElement('a');
			elem.setAttribute('href', url);
			elem.setAttribute('title', title);
			elem.setAttribute('rel', 'sidebar');
			elem.click();
		}
	}
	else
	{
		var bookmarkURL;
		if (sitename.toLowerCase() == "delicious") {
			bookmarkURL = "https://secure.del.icio.us/login?url=" + url + "&title=" + title + "&noui&jump=close&partner=%5Bpartner%5D&v=4";
		}
		else if (sitename.toLowerCase() == "digg") {
			bookmarkURL = "http://digg.com/submit?url=" + url + "&title=" + title + "&media=news";
		}	
		else if (sitename.toLowerCase() == "facebook") {
			bookmarkURL = "http://www.facebook.com/sharer.php?u=" + url + "&t=" + title;
		}	
		else if (sitename.toLowerCase() == "google") {
			bookmarkURL = "http://www.google.com/bookmarks/mark?op=add&bkmk=" + url + "&title=" + title;
		}
		else if (sitename.toLowerCase() == "yahoo-myweb") {
			bookmarkURL = "http://myweb.yahoo.com/myresults/bookmarklet?&ei=UTF-8&u=" + url + "&t=" + title;
		}
		else if (sitename.toLowerCase() == "technorati") {
			bookmarkURL = "http://technorati.com/faves?sub=favthis&add=" + url;
		}
		cdcShareWindowObjectReference = window.open(bookmarkURL, 
			"_blank",  "height=500,width=720,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	}
}
