var $bbmBar, $bbmBox, bbmTO;
var bbmBarH = 16;
var bbm_ie6;

$(document).ready(function() {

	$("head").append("<link rel=\"stylesheet\" type=\"text/css\" href=\"/includes/bookmark/bbm.css\">");
	bbm_ie6 = ($.browser.msie && $.browser.version == 6.0);
	
	$bbmBar = $("#bbmBar");
	
	$bbmBar.click(function(){return false;});
	$bbmBar.hover(
		function() {bbmShowBox()},
		function() {bbmTO = window.setTimeout(bbmHideBox, 500)}
		);
	
});

function bbmShowBox() {
	
	var offset, $win;
	
	if(!$bbmBox) bbmAddBox();
	window.clearTimeout(bbmTO);	
	
	$win = $(window);	
	offset = $bbmBar.offset();
	offset.top += bbmBarH;	

	// Check if box would be clipped by bottom of viewport...
	if((offset.top+$bbmBox.outerHeight()) > ($win.scrollTop()+$win.height())) {
		offset.top -= bbmBarH+$bbmBox.outerHeight();
	}
	// Check if box would be clipped by right of viewport...
	if((offset.left+$bbmBox.outerWidth()) > ($win.scrollLeft()+$win.width())) {
		offset.left -= $bbmBox.outerWidth()-$bbmBar.width();
	}
	
	if(bbm_ie6)	$("select").css("visibility", "hidden");
	
	$bbmBox.css({top: offset.top, left: offset.left}).show();
	
}

function bbmHideBox() {
	$bbmBox.hide();
	if(bbm_ie6)	$("select").css("visibility", "visible");
}

function bbmAddBox() {

	$("body").append("<div id='bbmBox'><h6>Bookmark</h6>\
		<div class='bbmCol'>\
			<a href='#fav'>Favourites</a>\
			<a href='#delicious'>Delicious</a>\
			<a href='#google'>Google</a>\
			<a href='#live'>Live</a>\
			<a href='#twitter'>Twitter</a>\
		</div>\
		<div class='bbmCol'>\
			<a href='#prt'>Print</a>\
			<a href='#digg'>Digg</a>\
			<a href='#myspace'>MySpace</a>\
			<a href='#facebook'>FaceBook</a>\
			<a href='#stumbleupon'>StumbleUpon</a>\
		</div>\
		<div class='bbmProp'><!--IE 6--></div>\
	</div>");
	
	$bbmBox = $("#bbmBox");
	
	$("#bbmBox a").each(function () {
		var clss = $(this).attr("href");
		clss = clss.substring(clss.indexOf("#")+1);
		$(this).attr("class", "bbmL-" + clss);
	});
	
	$("#bbmBox a").click(function(){
		bbmDoIt(this);
		return false;
	});
	
	$bbmBox.hover(
		function() {window.clearTimeout(bbmTO)},
		function() {bbmTO = window.setTimeout(bbmHideBox, 500) }
		);
	
}

function bbmDoIt(a) {

	var $a = $(a);
	var href = $a.attr("href");
	href = href.substring(href.indexOf("#")+1);
	
	bbmHideBox();
	
	switch(href) {
		case "fav": 
			// alert(/chrome/.test(navigator.userAgent.toLowerCase()));
			// alert(navigator.userAgent.toLowerCase());

			if(/chrome/.test(navigator.userAgent.toLowerCase())) {
				alert("Press <Control>+D to bookmark in Chrome");
			} else if($.browser.safari) {
				alert("Press <Control>+D to bookmark in Safari");				
			} else if(window.sidebar) {
				window.sidebar.addPanel(document.title, document.URL, "");
			} else if(window.external) {
				window.external.AddFavorite(document.URL, document.title);
			} else if(window.opera) {
				alert("Press <Control>+D to bookmark in Opera");
			} else {
				alert("Please use the \"Bookmark\" or \"Favourites\" function in your browser");
			}
		
			break;
			
		case "prt":
			window.print();
			break;
			
		default:
			// External bookmark
			window.open("http://www.boosey.com/includes/bookmark/bbmExternal.asp?site=" + href + "&url=" + encodeURIComponent(document.URL) + "&title=" + encodeURIComponent(document.title) , "bbmExternal");
			break;
	}

}