/**
 * create an alert from js
 * @param	msg	string	the alert message
 */
function newAlert(msg)
{
	// if ie6, position it
	if ( 'absolute' == $('#topAlert').css('position') ) $('#topAlert').css('top', $(document).scrollTop() );
	
	$('#topAlert')
	.html('<p>' + msg + '</p>')
	.showAlert();
}

$.fn.showAlert = function(){
	return this.each(function(){
		var self = this;
		
		var text = $(self).text();
		var aText = text.split(' ');
		var ms = aText.length * 500;

		$(self).faderEffect({
			callback: function(){
				setTimeout(function(){
					$(self).faderEffect({
						fadeType: 'fadeOut',
						count: 1
					});
				}, ms);
			} // callback
		});
	});
};

$.fn.faderEffect = function(options){
	options = jQuery.extend({
		fadeType: 'fadeIn', // or fadeOut
		count: 3, // how many times to fadein
		speed: 500, // speed of fadein
		callback: false // call when done
	}, options);
	
	return this.each(function(){
		
		// if we're done, do the callback
		if (0 == options.count) 
		{
			if ( $.isFunction(options.callback) ) options.callback.call(this);
			return;
		}
		
		var callAgain = function(){
			options.count = options.count - 1; // countdown
			$(this).faderEffect(options);
		};

		// fade, and call again
		switch(options.fadeType)
		{
		case 'fadeIn':
			// hide so we can fade in
			if ( $(this).is(':visible') ) $(this).hide();
			$(this).fadeIn(options.speed, callAgain);
			break;
		case 'fadeOut':
			// show so we can fade out
			if ( !$(this).is(':visible') ) $(this).show();
			$(this).fadeOut(options.speed, callAgain);
			break;
		default:
		 // nothing
		}
	});
}

function formElementError (field, message)
{
	$('#' + field).addClass('error');
	$('[for=' + field + ']')
	.append( '<span class="error">' +  message + '</span>');
}

// resets a form, including restoring the button
$.fn.resetForm = function(){
	return this.each(function(){
		$('.buttonMessage', this)
		.prev()
		.show()
		.end()
		.remove();
		
		$(this)[0].reset();
	});
}
var sessionTimer;
var sessionTime = 24*60*1000; // in ms, total time minus 5 minutes

function sessionWarning()
{
       tb_show('Your session is about to expire!',
'#TB_inline?height=300&width=300&inlineId=timeoutWarningWrapper&modal=true',
false);
       sessionTimer = setTimeout(sessionLogout, 5*60*1000); // 5 minutes
}

function sessionLogout()
{
       window.location = '/blankslate/logout';
}
$(document).ready(function(){
	
	$('#topAlert').each(function(){
		if ( $('p, ul', this).length > 0 ) 
		{
			$(this).showAlert();
		}
	});

	$('button, input[type=button], input[type=submit]').live('click', function(){
		if ( '' != $(this).attr('lang') )
		{
			$(this).hide().after('<b class="buttonMessage">' + $(this).attr('lang') + '</b>');
		}		
	});
	

$('#timeoutWarning input').click(function(){
       clearTimeout(sessionTimer);
       sessionTimer = setTimeout(sessionWarning, sessionTime); // 25 minutes
       $.get('/blankslate/secure/refreshSession.htm');
       tb_remove();
});


	
});
