function getShareURL(target, title, url) {
	var encUrl = encodeURIComponent(url);
	var encTitle = encodeURIComponent(title);
	switch (target) {
		case "twitter":
			return "http://twitter.com/home?status=" + encodeURIComponent(title + ": " + url);
		case "delicious":
			return "http://delicious.com/save?url=" + encUrl + "&title=" + encTitle;
		case "facebook":
			return "http://www.facebook.com/sharer.php?u=" + encUrl + "&t=" + encTitle;
		case "digg":
			return "http://digg.com/submit?phase=2&url=" + encUrl + "&title=" + encTitle;
		case "google":
			return "http://www.google.com/bookmarks/mark?op=add&bkmk=" + encUrl + "&title=" + encTitle;
		case "hyves":
			return "http://hyves-share.nl/button/tip/?title=" + encTitle + "&body=" + encodeURIComponent("[url=" + url + "]" + title + "[/url]");
		case "default":
			return null;
	}
}

function initialiseSocialBookmarkingDraggable() {
	/* drag effect for social bookmarking */
	var title = window.document.title;
	var createTip = function(e) {  
  		//create tool tip if it doesn't exist  
  		($("#tip").length === 0) ? $("<div>").html("<span>Versleep dit icoon om te delen<\/span><span class='arrow'><\/span>").attr("id", "tip").css({ left:e.pageX + 30, top:e.pageY - 16 }).appendTo("body").fadeIn("fast") : null;
	};
	
	$("#shareLink")
		.bind("mouseenter", createTip)
		.mousemove(function(e) {
			//move tooltip
			$("#tip").css({ left:e.pageX + 30, top:e.pageY - 16 });
		})
		.mouseleave(function() {
			// remove tooltip
			$("#tip").remove();
		})
		.draggable({
			//create draggable helper
			helper: function() {
				return $("<div>").attr("id", "helper").html("<span>" + title + "</span>").appendTo("body");
			},
			cursor: "pointer",
			cursorAt: { left: -10, top: 20 },
			zIndex: 99999,
			//show overlay and targets
			start: function() {
				$("<div>").attr("id", "overlay").css("opacity", 0.7).appendTo("body");
				$("#tip").remove();
				$(this).unbind("mouseenter");
				$("#targets").css("left", ($("body").width() / 2) - $("#targets").width() / 2).slideDown();
			},
			//remove targets and overlay
			stop: function() {
				$("#targets").slideUp();
				$(".share", "#targets").remove();
				$("#overlay").remove();
				$(this).bind("mouseenter", createTip);
			}
		});
	
	//make targets droppable
	$("#targets li").droppable({
		tolerance: "pointer",
		//show info when over target
		over: function() {
			$(".share", "#targets").remove();
			$("<span>").addClass("share").text("Deel via " + $(this).attr("id")).addClass("active").appendTo($(this)).fadeIn();
		},
		drop: function() {
			var id = $(this).attr("id"),
			currentUrl = window.location.href;
			window.open(getShareURL(id, title, currentUrl));
		}		  
	});
}

function initialisePortfolioDetailsCycle() {
	/* cycle effect for portfolio details */
	if ($().cycle) {
		var links = $("#portfolioList a");
		
		// determine starting slide if #hash is present
		var startingSlide = 0;
		if (window.location.hash) {
			$("#portfolioList a").each(function() {
				if (this.hash == window.location.hash) {
					startingSlide = links.index(this);
				}
			});
		}
		
		$("#portfolioDetails").cycle({
			fx: "uncover",
			direction: "right",
			timeout: 0,
			easing: 'easeInOutBack',
			startingSlide: startingSlide
		});
		
		links.click(function() {
			$("#portfolioDetails").cycle(links.index(this));
			window.location.hash = this.hash;
			return false;
		});
	}
}

$(document).ready(function(){

	/* sIFR code (fancy typography) */
    $.sifr({
		build: 436,
		version: 3,
		path: "resources/",
		save: true
	});
    $("h2, #news>h3").sifr({
		font: "myriadpro-lightsemicn.swf"
	});
	
	/* open external and PDF links in new window */
	$("a[rel='external'],a[href$='.pdf']").click(function() {
		window.open(this.href);
		return false;
	});
	
	/* fancy hover fade effect for site menu */
	if ($.support.opacity && !$.browser.mozilla) {
		$("#navigation").oneTime(2000, function() {
			$(this).fadeTo("slow", 0.5);
		}).hover(function() {
			$(this).stopTime();
			$(this).fadeTo("fast", 1.0);
		}, function() {
			$(this).oneTime(2000, function() {
				$(this).fadeTo("slow", 0.5);
			});
		});
	}
	
	/* roundabout effect for portfolio excerpt */
	if ($().roundabout) {
		$("#portfolioExcerpt>ul").roundabout().everyTime(3000, function() {
			$(this).roundabout_animateToNextChild();
		});
	}
	
	/* accordion effect for page content section */
	if ($().accordion) {
		var makeAutoHeight = false;
		if ($.browser.msie && parseInt($.browser.version) == 7) {
			makeAutoHeight = true;
		}
		
		$(".accordion").accordion({
			header: "h3",
			autoHeight: makeAutoHeight,
			navigation: true
		});
		
		$(".accordion h3").click(function() {
			window.location.hash = $("a", this)[0].hash;
		});
		
		$(".accordion a[href*=#]:not(.header)").click(function() {
			var headerElem = $(".accordion a[name=" + this.hash.substring(1) + "]").parent();
			$(".accordion").accordion("activate", headerElem);
		});
	}
	
	/* scrollpane effect for page content section */
	if ($().jScrollPane) {
		$(".scrollpane").jScrollPane({
			scrollbarWidth: 11,
			scrollbarMargin: 10
		});
	}
	
	initialisePortfolioDetailsCycle();
	initialiseSocialBookmarkingDraggable();
});