var icons_per_row	= 5		// Change this to adjust how many icons will be displayed in one row

var icon_size		= 65	// style: width
var icon_spacing	= 20	// style: margin * 2

var box_size = icons_per_row * icon_size + icons_per_row * icon_spacing + 35;

function placeButton( name, img_name, url_pattern ) {
	var resulting_url = url_pattern.replace( "__URL__", encodeURI( location.href ) );
	resulting_url = resulting_url.replace( "__TITLE__", "AddressDoctor" );
	
	document.write( "<a class=\"share_button\" href=\"", resulting_url, "\" target=\"_blank\" onclick=\"toggleShareBox();\">" );
	document.write( "<img class=\"sb_image\" src=\"/share/icons/", img_name, "\" width=\"40\" height=\"40\" alt=\"", name, "\" /><br />" );
	document.write( "<span class=\"sb_title\">", name, "</span>" );
	document.write( "</a>" );
}

function getScrollXY() {
	var scrX = 0, scrY = 0;
	
	if ( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrY = window.pageYOffset;
		scrX = window.pageXOffset;
	} else if ( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrY = document.body.scrollTop;
		scrX = document.body.scrollLeft;
	} else if ( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrY = document.documentElement.scrollTop;
		scrX = document.documentElement.scrollLeft;
	}
	
	return [scrX,scrY];
}

function updateShareBoxPos() {
	scrollpos = getScrollXY();
	
	
	background = document.getElementById( "sp_background" );
	foreground = document.getElementById( "sp_foreground" );
	
	background.style.height = document.body.scrollHeight;
	background.style.width = document.body.scrollWidth;
	
	foreground.style.height = document.body.clientHeight;
	foreground.style.width = document.body.clientWidth;
	foreground.style.left = scrollpos[0];
	foreground.style.top = scrollpos[1];
}

function showShareBox() {
	updateShareBoxPos();
	box = document.getElementById( "share_popup" ).style.display = "block";
}

function hideShareBox() {
	box = document.getElementById( "share_popup" ).style.display = "none";
}

function toggleShareBox() {
	if ( document.getElementById( "share_popup" ).style.display == "none" )
		showShareBox();
	else
		hideShareBox();
}

function hideShareBoxOnEsc( evt ) {
	if ( !evt )
		evt = window.event;
	if ( evt.which )
		key = evt.which;
	else if ( evt.keyCode )
		key = evt.keyCode;
	else
		return;
	
	if ( key == 27 ) // ESC
		hideShareBox();
}

function setShareBoxSize( size ) {
	document.getElementById( "sp_box" ).style.width = box_size + "px";
}

window.onresize = updateShareBoxPos;
window.onscroll = updateShareBoxPos;

document.onkeydown = hideShareBoxOnEsc;