$(document).ready(function() {
	
	$.ifixpng('../images/pixel.gif');
	//$('#header').ifixpng();
	//$('#mainNav > li > .linkWrapper > a').ifixpng();
	
	// Set up animation effect for link rollovers
	$('#mainNav > li > .linkWrapper').each(function(){
	
		$(this).append('<span class="highlight"></span>')
		.find('.highlight')
		.css({
			width: '0px',
			height: '0px',
			left: ($(this).width() / 2)+'px',
			top: ($(this).height() / 2)+'px',
			opacity: 1
		});
		
	});
	
	$('#mainNav > li > .linkWrapper > a').hover(
		function() {
			$(this).next('.highlight').stop().animate({
				width: ( $(this).parent().width() )+'px',
				height: ( $(this).parent().height() )+'px',
				top: '0px',
				left: '0px',
				opacity: 1
			}, 200, 'easeInOutSine');
		},
		function() {
			$(this).next('.highlight').stop().animate({
				width: '0px',
				height: '0px',
				top: ( $(this).parent().height() / 2 )+'px',
				left: ( $(this).parent().width() / 2 )+'px',
				opacity: 1
			}, 200, 'easeInOutSine');
		}
		
	);
	
	
	// Build bookmark bar
	
	function getSubmissionURL(siteKey) {
		var submitUrl = submitURLs[siteKey];
		var rxUrl = /\{\$url\}/;
		submitUrl = submitUrl.replace(rxUrl, escape(document.location));
 
		var title = document.title;
		var rxTitle = /\{\$title\}/;
		submitUrl = submitUrl.replace(rxTitle, escape(title));
 
		return submitUrl;
	}
	
	
	var services = [
		"delicious", "stumbleupon", "google", "yahoo",
		"furl", "thisnext", "wists"
	];
	
	var serviceTitles = {
		delicious:"del.icio.us",
		stumbleupon:"StumbleUpon",
		google:"Google Bookmarks",
		yahoo: "Yahoo! My Web",
		furl: "Furl",
		thisnext: "ThisNext",
		wists: "Wists"
	};
 
	var submitURLs = {
		delicious: "http://del.icio.us/post?url={$url}&title={$title}",
		stumbleupon: "http://www.stumbleupon.com/submit?url={$url}&title={$title}",
		google: "http://www.google.com/bookmarks/mark?op=edit&bkmk={$url}&title={$title}",
		yahoo: "http://myweb2.search.yahoo.com/myresults/bookmarklet?u={$url}&t={$title}",
		furl: "http://furl.net/storeIt.jsp?u={$url}&t={$title}",
		thisnext: "http://www.thisnext.com/pick/new/submit/bookmarklet/?url={$url}&name={$title}&_charset_=UTF-8",
		wists: "http://wists.com/r.php?c=null&r={$url}&title={$title}"
	};
	
	$('#bookmarks').append('<b id="bookmarkTrigger">Bookmark and Share</b><div id="bookmarkMenu"></div>').find('#bookmarkMenu').hide();

	for (var i = 0; i < services.length; i++) {
		var service = services[i];
		var tempLi = document.createElement('li');

		var tempA = document.createElement('a');
		tempA.href = getSubmissionURL(service);
		tempA.target = "_blank";

		var tempImg = document.createElement('img');
		tempImg.src = "images/bookmarking/" + service + ".png";
		tempImg.alt = serviceTitles[service];
		tempImg.title = serviceTitles[service];

		//tempA.appendChild(tempImg);
		//tempLi.appendChild(tempA);
		//toolbar.appendChild(tempLi);
		$('#bookmarkMenu').append(tempA).find('a:last')
		.append(tempImg)
		.append(' '+ serviceTitles[services[i]]);
	 };
	 
	var time = 250;
	var hideDelay = 500;
	var hideDelayTimer = null;
	var beingShown = false;
	var shown = false;
	 
	$('#bookmarks, #bookmarkMenu').mouseover(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		if (beingShown || shown) {
			// don't trigger the animation again
			return;
		} else {
			// reset position of info box
			beingShown = true;

			$('#bookmarkMenu').slideDown( time, function() {
				beingShown = false;
				shown = true;
			});
		}

		return false;
	}).mouseout(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		hideDelayTimer = setTimeout(function () {
			hideDelayTimer = null;
			
			$('#bookmarkMenu').slideUp( time, function () {
				shown = false;
				$(this).css('display', 'none');
			});

		}, hideDelay);

		return false;
	});
	
});
