var _isAdmin;
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19715160-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

$( function()
{
	$( 'input:submit, input:reset, button', 'form' ).button();
	$( '.leftPaneContainer' ).css({ 'min-height': $( '.rightPaneContainer' ).height() });
	$( '#navContainer a' ).hover( function() { $(this).parents( 'li:first' ).toggleClass( 'linkhover' ); } );
});

function ajaxUpdate( obj, waitText, ajaxFn )
{
	var l, t, a = {}, fnCoreComplete, fnUsrComplete;
	
	if( !obj.length )
		return false;
	
	if( !waitText )
		waitText = 'Please wait';
	
	if( !typeof( ajaxFn ) == 'object' )
		return false;
	
	fnCoreComplete = function()
	{
		window.clearInterval( t );
		l.remove();
		obj.fadeTo( 'fast', '1' );
	}
	
	fnUsrComplete = function() { return true; }
	
	for( var x in ajaxFn )
	{
		if( x == 'complete' )
			fnUsrComplete = ajaxFn[x];
		else
			a[x] = ajaxFn[x];
	}
	
	a['complete'] = function() { fnUsrComplete(); fnCoreComplete(); }
	
	l = $( '<div />' )
		.css({
			'position': 'absolute',
			'top': obj.position().top,
			'left': obj.position().left,
			'width': obj.width(),
			'height': obj.height(),
			'text-align': 'center',
			'z-index': '2',
			'display': 'none' })
		.append( $( '<h1 />' )
			.text( waitText )
			.data( 'original', waitText )
			.css({
				'position': 'relative',
				'top': ( obj.height() / 2 ) - 10,
				'font-size': '2.1em' }) )
		.appendTo( obj.parent() );
	
	l.fadeIn( '0' );
	obj.fadeTo( 'fast', '0.1', function()
	{
		t = window.setInterval( function()
		{
			var text = $( 'h1', l ).text();
			
			if( text.length > 13 )
				$( 'h1', l ).text( $( 'h1', l ).data( 'original' ) );
			else
				$( 'h1', l ).text( text + '.' );
		}, 200 );
		
		$.ajax( a );
	});
	
	return true;
}

function enableCAPTCHA( obj, key, options )
{
	var params = {
		theme: 'clean'
		};
		
	if( obj.jquery && obj.length == 0 )
	{
		alert( 'Unable to initiate reCAPTCHA object: container not found' );
		return;
	}
	
	for( x in options )
		params[x] = options[x];
		
	if( obj.jquery )
		obj = obj.get(0);
	Recaptcha.create( key, obj, params );
	$( obj ).parents( 'form' ).submit( formSubmitCB( function() { Recaptcha.reload(); } ) );
}

function _formSubmit( cb )
{	
	//alert( '[_formSubmit] CB: ' + typeof( cb ) + '\n' + cb );
	//var callback = ( typeof( cb ) == 'function' ) ? _clone( cb ) : function() {};
	cb = ( typeof( cb ) == 'function' ) ? cb : function() { return true; };
	if( typeof( _isAdmin ) == 'undefined' || !_isAdmin )
		_isAdmin = false;
	
	return function( e )
	{
		var f = $( e.target ), a;
		
		//alert( '[_formSubmitCallback] CB: ' + typeof( callback ) + '\n' + callback );
		//alert( '[_formSubmitCallback] CB: ' + typeof( cb ) + '\n' + cb );
		
		e.preventDefault();
		e.stopImmediatePropagation();
		a = {
				type: 'POST',
				url: 'ajax.php',
				data: ( _isAdmin ) ? f.serialize() + '&_adm=true' : f.serialize(),
				success: function( resp )
				{
					alert( processErr( resp ) );
					cb( resp, e );
				},
				error: function( req, err, e )
				{
					alert( err + '\n\n' + e );
				}
			}
		
		if( f.attr( 'enctype' ) )
			a[ 'contentType' ] = f.attr( 'enctype' );

		ajaxUpdate( f, 'Submitting', a );
		return false;
	}
}

function formSubmit( e )
{
	// This function ISNT redundant!
	// It came first, before I implemented callbacks
	// It can also be passed directly as an event callback,
	// whereas formSubmitCB needs to be called first and returns
	// the event callback
	var s = _formSubmit();
	s( e );
}

function formSubmitCB( callback )
{
	//alert( '[formSubmitCB] CB: ' + typeof( callback ) + '\n' + callback );
	return _formSubmit( callback );
}

function processErr( str )
{
	//return str;
	str = str.replace(/<br[^>]*>/g, '\n').replace(/<\/?p[^>]*>/g, '\n\n');
	str = str.replace(/<\/h[1-3]>/g, '\n').replace(/<\/div[^>]*>/g, '\n');
	str = str.replace(/&nbsp;/g, ' ');
	str = str.replace(/<[^>]+>/g, '');
	return str;
}

function reload()
{
	var h = location.href;
	location.href = h;
}
