function ieHover(_list) {
	if($.browser.msie && $.browser.version < 7){
		$(_list).live('mouseover', function() {
			$(this).addClass('hover');
		}).live('mouseout', function() {
			$(this).removeClass('hover');
		});
	}
}

hover = function() {
	var nav = document.getElementById("list");
	if(nav){
		var nodes = nav.getElementsByTagName("li")
		for (var i=0; i<nodes.length; i++) {
			nodes[i].onmouseover=function() {
				this.className+=" hover";
			}
			nodes[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover"), "");
			}
		}
	}
	var _el = document.getElementsByTagName('tr')
	for (var i=0; i<_el.length; i++) {
		_el[i].onmouseover=function() {
			this.className+=" hover";
		}
		_el[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" hover"), "");
		}
	}
}

function getSocialLinkContainerContent() {
	//sorry, javascript doesnt like line breaks
	return '<div class="shareLinksContainer"><a id="socialClose" href="#">close</a><div class="socialShareTitle">Share This Page</div><div class="shareColumn"><div class="shareItem"><a id="shareItemFacebook" class="shareItemLink" href="http://www.facebook.com" target="_blank">Facebook</a></div><div class="shareItem"><a id="shareItemTwitter" class="shareItemLink" href="http://twitter.com" target="_blank">Twitter</a></div><div class="shareItem"><a id="shareItemMyspace" class="shareItemLink" href="http://www.myspace.com" target="_blank">Myspace</a></div></div><div class="shareColumn"><div class="shareItem"><a id="shareItemDigg" class="shareItemLink" href="http://www.digg.com" target="_blank">Digg</a></div><div class="shareItem"><a id="shareItemStumbleUpon" class="shareItemLink" href="http://www.stumbleupon.com" target="_blank">StumbleUpon</a></div><div class="shareItem"><a id="shareItemDelicious" class="shareItemLink" href="http://delicious.com/save" target="_blank">Del.icio.us</a></div></div><div class="clearBoth"></div></div>';	
}

if (window.attachEvent) window.attachEvent("onload", hover);

$(document).ready(function() {
	
	$('.openInNewWindow').live('click', function (){
		window.open(this.href);
		return false;
	});
	
	ieHover('ul.navigation > li, div.dropDown');
	
	function openSocialMediaSite(siteType, location) {
		var title = document.title;
		var url = window.location.href;
		var content;
		if ($('.websiteMessageIntro').length > 0) {
			content = url + $('.websiteMessageIntro').text();
		} else {
			content = url + ' I\'m finding ways to raise awareness and funds to cure kids\' cancer!';
		}
		var newWindowTitle = '';
		var opts = '';
		
		switch (siteType) {
			case 1:
				//facebook
				var socialUrl = 'http://www.facebook.com/sharer.php?u=' + encodeURIComponent(url)
				+ '&amp;t=' + encodeURIComponent(title);
				newWindowTitle = 'sharer';
				opts = 'toolbar=0,status=0,width=626,height=436';
				break;
			case 2:
				//twitter
				var socialUrl = 'http://twitter.com/home?status=' + encodeURIComponent(content);
				break;
			case 3:
				//myspace
				var socialUrl = 'http://www.myspace.com/index.cfm?fuseaction=postto&' + 't=' + encodeURIComponent(title) + '&c=' + encodeURIComponent(content) + '&u=' + encodeURIComponent(url) + '&l=' + location;
				break;
			case 4:
				//digg
				var socialUrl = 'http://digg.com/submit?url=' + encodeURIComponent(url) + '&amp;title=' + encodeURIComponent(title) + '&amp;bodytext=' + encodeURIComponent(content) + '&amp;media=news&amp;topic=' + location;
				break;
			case 5:
				//stumbleupon
				var socialUrl = 'http://www.stumbleupon.com/submit?url=' + encodeURIComponent(url) + '&amp;title=' + encodeURIComponent(title);
				break;
			case 6:
				//delicious
				var socialUrl = 'http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(url)+'&amp;title='+encodeURIComponent(title);
				newWindowTitle = 'delicious';
				opts = 'toolbar=no,width=550,height=550';
				break;
		}
		
		if (!opts) {
			window.open(socialUrl);
		} else {
			window.open(socialUrl, newWindowTitle, opts);
		}
	}
	
	if ($('a.share').length > 0) {
		$('#socialClose').live('click', function () {
			$('.shareLinksContainer').slideUp(function() {
				$('.shareLinksContainer').remove();
			});
			return false;
		});
		
		$('.shareItemLink').live('click', function() {
			var siteType = '';
			var location = '';
			
			if ($(this).attr('id') == 'shareItemFacebook') {
				//content generated by Meta tags.  See sharepage.phtml
				siteType = 1;
			} else if ($(this).attr('id') == 'shareItemTwitter') {
				siteType = 2;
			} else if ($(this).attr('id') == 'shareItemMyspace') {
				siteType = 3;
				location = '1';
			} else if ($(this).attr('id') == 'shareItemDigg') {
				siteType = 4;
				location = 'health';
			} else if ($(this).attr('id') == 'shareItemStumbleUpon') {
				siteType = 5;
			} else if ($(this).attr('id') == 'shareItemDelicious') {
				siteType = 6;
			}
			
			openSocialMediaSite(siteType, location);
			return false;
		});
		
		$('a.share').click(function () {
			var link;
			var linkContainer;
			var marginTop;
			var paddingTop;
			if ($('.shareLinksContainer').length > 0) {
				$('.shareLinksContainer').slideUp(function() {
					$('.shareLinksContainer').remove();
				});
			} else {
				link = $(this)
				link.parent().css('position', 'relative');
				//generate the container contents
				linkContainer = $(getSocialLinkContainerContent());
				//get the padding and margin of the link as an integer
				marginTop = (isNaN(parseInt(link.css('margin-top').replace('px', '')))) ? 0 : parseInt(link.css('margin-top').replace('px', ''));
				paddingTop = (isNaN(parseInt(link.css('padding-top').replace('px', '')))) ? 0 : parseInt(link.css('padding-top').replace('px', ''));
				//position the container under the link
				linkContainer.css({'top': link.position().top + marginTop + paddingTop + link.height() - 1 + 'px', 'left': link.position().left + 'px'});
				//insert the container into the DOM
				link.after(linkContainer);
				linkContainer.slideDown();
			}
			return false;
		});
	}
	
	
});