
//////
//
// Script configuration
//
//////

var sburl = encodeURIComponent(location.href);
var sbtitle = encodeURIComponent(document.title);
var sbscriptpath = sbparam_scriptpath;		/* Parameter passed to the script specifying where on the server the
											   script resides, relative to the document root. (Terminating '/' is
											   preferred, but not required.) */
var sblang = sbparam_lang;					/* Parameter passed to the script that determines what language to
											   use. Currently only English (en) and German (ger) are supported. */
var sbsmtpdomain = sbparam_smtpdomain;		/* Parameter passed to the script that specifies the officially registered
											   domain name of the site's SMTP server. */
var sbsitename = sbparam_sitename;			/* Parameter passed to the script that sets the name that should appear in
											   the "From" field for emails sent by the script. */
var sbsiteemail = sbparam_siteemail;		/* Parameter passed to the script that sets the contact address the script
											   should use as originator when sending mails. */
var sbvariant = sbparam_variant;			/* Parameter passed to the script for using different button designs.
											   For every variant, the classes ".sbc_button_<sbparam_variant>" and
											   ".sbc_button_hover_<sbparam_variant>" should be defined in
											   bookmarks.css */
var sbposoffset_x = sbparam_posoffset_x;	/* Parameter passed to the script for offsetting the automatically
											   chosen x-position of the bookmark popup */
var sbposoffset_y = sbparam_posoffset_y;	/* Parameter passed to the script for offsetting the automatically
											   chosen y-position of the bookmark popup */
var button_class = "sbc_button_"+sbvariant;
var button_class_hover = "sbc_button_hover_"+sbvariant;


//////
//
// Script functionality
//
//////

// Toggle function
function toggle_bookmarks()
{
	// This function essentially implements an FSM (Finite State Machine) for
	// controlling the showing/hiding of the bookmark div

	switch(toggle_bookmarks.state)
	{
		case 0:
		{
			// Fetch the height of the document without
			// the bookmarks displayed for later use
			siteheight = $("body").height();

			// Make the bookmarks visible and update the events
			$("#sbc_button").unbind('mouseenter', toggle_bookmarks);
			$("#sbc_bookmarks").css({"display":"block"});
			$("#sbc_bookmarks").mouseleave(toggle_bookmarks);

			// Change popup position if necessary
			anchor = $("#sbc_button").position();
			bookmark_height = $("#sbc_bookmarks").height();
			position_top = $("#sbc_bookmarks").offset().top;
			space_available = siteheight - position_top;
			if (bookmark_height > space_available)
			{
				newtop = anchor.top - bookmark_height;
				if (newtop > 1)
				{
					$("#sbc_bookmarks").css("top", newtop)
				}
				else
				{
					newtop = position_top - bookmark_height + space_available - 16;
					if (newtop > 1)
						$("#sbc_bookmarks").css("top", position_top - bookmark_height + space_available - 16);
					else
						/* DoNothing() */;
				}
			}

			// Init tabs
			$("#sbc_tab_bookmarks").removeClass("sbc_tab_bookmarks_active");
			$("#sbc_tab_email").removeClass("sbc_tab_email_active");
			if (switch_tabs.current == 0)
				$("#sbc_tab_bookmarks").addClass("sbc_tab_bookmarks_active");
			else
				$("#sbc_tab_email").addClass("sbc_tab_email_active");

			// Transit to state "idle, open"
			toggle_bookmarks.state = 1;
			break;
		}
		case 1:
		{
			// Make the bookmarks invisible and update the events
			$("#sbc_bookmarks").unbind('mouseleave', toggle_bookmarks);
			$("#sbc_bookmarks").css({"display":"none"});
			$("#sbc_button").mouseenter(toggle_bookmarks);

			// Transit to state "idle, closed"
			toggle_bookmarks.state = 0;
			break;
		}
	}
}
toggle_bookmarks.state = 0;			// Values: 0: idle, closed - 1: idle, open - 2: opening - 3: closing
toggle_bookmarks.expanded = 0;		// Ranges from 0 (fully closed) to 10 (fully opened)

// Tab switch function
function switch_tabs(target)
{
	// This function loads the content of the desired tab and inserts it into the
	// tab body. The elements that get removed are presserved so that they don't lose
	// their content when the tab is switched.

	switch(target)
	{
		case 0:
		{
			// We don't need to do anything if the Bookmarks tab is already open
			if (switch_tabs.current == 0) return;

			// Update tab area
			$("#sbc_tab_bookmarks").addClass("sbc_tab_bookmarks_active");
			$("#sbc_tab_email").removeClass("sbc_tab_email_active");

			// Exchange the content of the tab body
			$("#sbc_emailtable").css({"display":"none"});
			$("#sbc_bookmarktable").css({"display":"block"});

			// Update indicator of current tab
			switch_tabs.current = 0;
			break;
		}
		case 1:
		{
			// We don't need to do anything if the Email tab is already open
			if (switch_tabs.current == 1) return;

			// Update tab area
			$("#sbc_tab_email").addClass("sbc_tab_email_active");
			$("#sbc_tab_bookmarks").removeClass("sbc_tab_bookmarks_active");

			// Exchange the content of the tab body
			$("#sbc_bookmarktable").css({"display":"none"});
			$("#sbc_emailtable").css({"display":"block"});

			// Update indicator of current tab
			switch_tabs.current = 1;
			break;
		}
	}
}
switch_tabs.current = 0;			// Values: 0: bookmarks - 1: Email

function send_mail_finished()
{
	// Re-enable the send button
	$("#sbc_email_sending").css({"display":"none"});
	$("#sbc_email_send").css({"display":"block"});

	// Reactivate the mouse-leave event
	$("#sbc_bookmarks").mouseleave(toggle_bookmarks);

	alert(document.getElementById('sbc_ajaxdump').innerHTML);
}

function send_mail()
{
	var error_str = ""
	var error_count = 0;

	field_name = document.getElementById('sbc_email_name');
	field_address = document.getElementById('sbc_email_address');
	field_recipient = document.getElementById('sbc_email_recipient');
	field_message = document.getElementById('sbc_email_message');

	// Parse input
	email_name = encodeURIComponent(field_name.value);
	email_address = encodeURIComponent(field_address.value);
	email_recipient = encodeURIComponent(field_recipient.value);
	email_message = encodeURIComponent(field_message.value);

	// Prevent accidental double clicks
	$("#sbc_email_send").css({"display":"none"});
	$("#sbc_email_sending").css({"display":"block"});

	// Deactivate the mouse-leave event while the message is being sent
	$("#sbc_bookmarks").unbind('mouseleave', toggle_bookmarks);

	// Send mail
	$("#sbc_ajaxdump").load(sbscriptpath+'bookmarks_sendmail.php',
							{"email_name":email_name, "email_address":email_address,
							 "email_recipient":email_recipient, "email_message":email_message,
							 "sburl":sburl, "sbtitle":sbtitle, "sblang":sblang,
							 "sbsmtpdomain":encodeURIComponent(sbsmtpdomain),
							 "sbsitename":encodeURIComponent(sbsitename),
							 "sbsiteemail":encodeURIComponent(sbsiteemail)}, send_mail_finished);
}

// Process parameters that need special attention
if (sbscriptpath.length > 0)
{
	lastChar = sbscriptpath.charAt(sbscriptpath.length-1);
	if (lastChar != "/")
	{
		if (lastChar == "\\")
		{
			if (sbscriptpath.length == 1)
				sbscriptpath = '/';
			else
				sbscriptpath = sbscriptpath.substring(0, sbscriptpath.length-2) + '/'
		}
		else
			sbscriptpath = sbscriptpath + '/';
	}
}

// Insert script into the document
document.write('<div id="sbc_button" class="'+button_class+'"></div>');
document.write('<div id="sbc_bookmark_hook" style="display: inline; margin: 0; padding: 0; width: 0; height: 0; overflow: hidden; border: 0 none gray"></div>');
$("#sbc_bookmark_hook").load(sbscriptpath+'bookmarks.php', {"sbscriptpath":encodeURIComponent(sbscriptpath), "sburl":sburl, "sbtitle":sbtitle, "sblang":encodeURIComponent(sblang), "sbposoffset_x":encodeURIComponent(sbposoffset_x), "sbposoffset_y":encodeURIComponent(sbposoffset_y)});

// Init inserts
$(document).ready(function()
{
	// Init events
	$("#sbc_button").mouseenter(toggle_bookmarks);
	$("#sbc_button").click(toggle_bookmarks);
	$("#sbc_button").mouseover(function ()
	{
		$("#sbc_button").removeClass(button_class);
		$("#sbc_button").addClass(button_class_hover);
	});
	$("#sbc_button").mouseleave(function ()
	{
		$("#sbc_button").removeClass(button_class_hover);
		$("#sbc_button").addClass(button_class);
	});

	// Init functional CSS
	anchor = $("#sbc_button").position();
	$("#sbc_bookmarks").css({"position":"absolute"});
	$("#sbc_bookmarks").css("left", anchor.left);
	$("#sbc_bookmarks").css("top", anchor.top + $("#sbc_button").height());
});
