/*
 * My own helper functions based on jQuery JavaScript library.
 * To avoid too big HTML files all helper functions, that are used on more
 * then one page, are placed here.
 */


/******************************************************************************
 * COMPACT SEARCH INPUT (used on every page) */

// search compact form element selectors
var searchLabelSelector = "div#search label";
var searchInputSelector = "div#search input#q";

$(document).ready(function()
	{
		// if javascript enabled, add over class to search label to make compact form
		$(searchLabelSelector).addClass("over");
		// toggle label visibity according to search input value.
		toggleSearchLabel(true);
		// add focus and blur events
		$(searchInputSelector).bind("focus", function(){toggleSearchLabel(false)});
		$(searchInputSelector).bind("blur", function(){toggleSearchLabel(true)});
		// focus input manually in safari
		$(searchLabelSelector).bind("click", function(){focusSearchInput()});
	}
);

// Fucuses the search input.
function focusSearchInput()
{
		$(searchInputSelector).focus();
}

// Shows/hides the search label in the compact form.
function toggleSearchLabel(visible)
{
	value = jQuery.trim($(searchInputSelector).val());
	if (visible && value === '')
	{
		$(searchLabelSelector).removeClass("shifted");
	}
	else
	{
		$(searchLabelSelector).addClass("shifted");
	}
}


/******************************************************************************
 * SLIDE EFFECTS (used on every page) */

// Definition of my jQuery menu slide functions.
jQuery.fn.menuSlideIn = function(speed, easing, callback)
{
	return this.animate({marginTop: "-1.2em", paddingTop: 0}, speed, easing, callback);
}
jQuery.fn.menuSlideOut = function(speed, easing, callback)
{
	return this.animate({marginTop: 0, paddingTop: "0.2em"}, speed, easing, callback);
}
jQuery.fn.sitemapSlideOut = function(speed, easing, callback)
{
	return this.animate({marginTop: "-0.7em", paddingTop: 0}, speed, easing, callback);
}

$(document).ready(function()
	{
		// remove do-not-animate CSS classes (do-not-animate class is used for CSS
		// hover effects when JavaScript is turned off)
		$('.do-not-animate').removeClass("do-not-animate");
		// add JavaScript slide efects to menu hover events
		$('#menu li:not(.active):not(#go-sitemap) a').hover
		(
			function ()
			{
				$(this).menuSlideOut(50);
			},
			function ()
			{
				$(this).menuSlideIn(400);
			}
		);
		// sitemap menu item slide effect
		$('#menu li:not(.active)#go-sitemap a').hover
		(
			function ()
			{
			  $(this).sitemapSlideOut(50);
			},
			function ()
			{
				$(this).menuSlideIn(400);
			}
		);
	}
);


/******************************************************************************
 * E-MAILS DE-OBFUSCATION (used on pages with obfuscated e-mails) */

// Reborns e-mail HTML code to useful version. The mail is obfuscated by
// Helper.secureMail PHP function.
function rebornMails()
{
	$(".eml").each(function()
			{
				// reborn displayed mail address
				html = $(this).html();
				replace = html.replace("(a)","@");
				$(this).html(replace);

				// on click reborn mail address in href
				$(this).bind("click", function(){
					href = $(this).attr("href");
					rep = href.replace("(a)","@");
					$(this).attr("href", rep);
					});
			}
			);
}

// Reborn e-mails when document is ready.
$(document).ready(function(){rebornMails()});
