/*
 * Social bookmarking functions
 * @author Lee Daffen lee.daffen@net-a-porter.com
 */

if(typeof NAP == "undefined") {
	var NAP = {};
}

NAP.addBookmark = function(site, delay) {
	/*
	 * $param site [string] 'nap' / 'outnet', defines the share links based on site
	 * $param delay [int] delay between mouse out and hiding the tool panel
	 *
	 * HTML required for bookmarking:
	 * <div id="add-bookmark-link"><a href="#">Share this page</a></div>
	 *
	 */

	// get some page information
	
	var href = window.location.protocol + "//" + window.location.hostname + "/" + channel + window.location.pathname;
	
	var url = encodeURIComponent(href),
		title = encodeURIComponent(document.title);

	/*
	 * $var BookmarkData [object]
	 * Reference object for all the available bookmark targets
	 *
	 * bookmarkName[string] = {
	 * 	   title: bookmark display name [string],
	 * 	   url: the bookmarking url of the target site [string]. can contain the vars {url} and {title} defined above,
	 * 	   windowWidth: width of the window to be opened [int],
	 * 	   windowHeight: height of the window to be opened [int],
	 * 	   site: comma delimited list of the sites to which the bookmark applies [string]. should match param site
	 * }
	 *
	 */

	var bookmarkData = {
		"delicious": {
			title: "<img src='/outnet/build/8.19.3/images/product_page/delicious.gif' title='delicious'>",
			url: "http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url="+url+"&amp;title="+title,
			windowWidth: 630,
			windowHeight: 500,
			site: "nap,outnet",
			scroll: 0
		},
		"facebook": {
			title: "<img src='/outnet/build/8.19.3/images/product_page/facebook.gif' title='facebook'>",
			url: "http://www.facebook.com/sharer.php?u="+url+"&t="+title,
			windowWidth: 630,
			windowHeight: 500,
			site: "nap,outnet",
			scroll: 0
		},
		"google": {
			title: "<img src='/outnet/build/8.19.3/images/product_page/google.gif' title='google'>",
			url: "http://www.google.com/bookmarks/mark?op=add&bkmk="+url+"&title="+title,
			windowWidth: 700,
			windowHeight: 600,
			site: "nap,outnet",
			scroll: 1
		},
		"twitter": {
			title: "<img src='/outnet/build/8.19.3/images/product_page/twitter.gif' title='twitter'>",
			url: "http://twitter.com/home?status="+url,
			windowWidth: 800,
			windowHeight: 600,
			site: "nap,outnet",
			scroll: 1
		}
	};

	var thisItemHTML,
		bookmarks = [];

	for(var key in bookmarkData) {
		var thisItem = bookmarkData[key];
		if(thisItem.site.toLowerCase().indexOf(site.toLowerCase()) != -1) {
			thisItemHTML = '<div class="'+key+' bookmark-link" onmouseover="NAP.bookmarkUtils.hilite(this)" onmouseout="NAP.bookmarkUtils.lolite(this)" onclick="NAP.bookmarkUtils.bookmarkThis(\''+key+'\')"><span>'+thisItem.title+'</span></div>';
			bookmarks.push(thisItemHTML);
		}
	}

	var html = ['<div id="add-bookmark-container" style="position:absolute;">',
					'<div id="add-bookmark-content">',
						'<div class="add-bookmark-list">',
							bookmarks.join(""),
						'</div>',
					'</div>',
				'</div>'].join("");

	function hideTool() {
		$("#add-bookmark-container").remove();
	}

	var hideInterval;

	$("#add-bookmark-link").hover(
		function() {
			// onmouseover
			clearInterval(hideInterval);
			$(html).appendTo($(this));
		},
		function() {
			// onmouseout
			hideInterval = setInterval(hideTool,delay);
		}
	).click(function(event) {
		event.preventDefault();
	});

	$(".bookmark-link").click(function(event) {
		event.stopPropagation();
		hideTool();
	});

	// lend some functions
	NAP.bookmarkUtils = {
		hilite: function(element) {
			element.style.backgroundColor = "#F2F2F2";
		},
		lolite: function(element) {
			element.style.backgroundColor = "transparent";
		},
		bookmarkThis: function(type) {
			var thisItem = bookmarkData[type];
			window.open(thisItem.url,"_blank","scrollbars="+thisItem.scroll+",toolbar=0,status=0,width="+thisItem.windowWidth+",height="+thisItem.windowHeight);
		},
		showHelp: function() {
			// show the 'what's this' help dialog
		},
		hideHelp: function() {
			// hide the 'what's this' help dialog
		}
	};
};