$.extend({
	_enc_div: null,
	htmlenc: function(val) {
		if (this._enc_div == null) this._enc_div = $('<div />');
		this._enc_div.text(val);
		return this._enc_div.html().replace("\n", "<br />");
	},
	htmldec: function(val) {
		if (this._enc_div == null) this._enc_div = $('<div />');
		this._enc_div.html(val);
		return this._enc_div.text();
	},
	preload_images: function(images, pointer, callback) {
		if (images.length > pointer) {
			$('<img />').load(function() {
				$.preload_images(images, pointer + 1, callback);
			}).attr('src', images[pointer]);
		} else {
			callback();
		}
	}
});

var lock = false;

$(function() {
	if (lock) return;
	lock = true;
	
	/* align the top menu */
	var mi = 0;
	var mi_w = 0;
	while( $('div#calc_menuitem'+ mi).width() > 0 )
	{
		mi_w += $('div#calc_menuitem'+ mi).width();
		mi++;
	}
	
	var doc = $(document);
	var win = $(window);
	var bgHeight = Math.max( doc.height(), win.height() );
	var bg = $("div.category_bg");
	bg.css({
		'height': bgHeight +"px"
	});
	
	var space_w = 981 - mi_w;
	var space_w = Math.ceil( space_w / mi);
	for( var i=0; i<mi-1; i++ )
	{
		var space = $('td#menuspace'+ i);
		space.css( "width", space_w +"px" );
	}
	/* align the top menu */
	
	// setup the ajax event handler for processing indicator
	$('#ajax_communication_indicator').ajaxStart(function() {
		  $(this).show();
	}).ajaxStop(function() {
		  $(this).hide();
	});
});

function placeMailto( employeeId, span )
{
	if( employeeId > 0 && span )
	{
		var span = $( span );
		$.ajax(
		{
			type: 'POST',
			dataType: "json",
			url: globalBaseUrl +"public/get_employee_json/"+ employeeId +"/",
			data: null,
			success: function( data )
			{
				if( data.result == 1 )
				{
					span.html( '<a href="mailto:'+ data.employee.email +'">'+ data.employee.email +'</a>' );
					span.attr( 'onclick', '' );
				}
			},
			error: function( XMLHttpRequest, textStatus, errorThrown ) { alert('error get data: '+textStatus ); try {} catch (e) {} }
		});
	}
}
