/**
 * Global scripts
 *
 * @author    Aliance spb
 * @mailto    aliance@morgenshtern.com
 * @copyright Morgenshtern clan, 2004
 */

window._alert = window.alert;
window.alert = function( text )
{
	$.floatbox(
		{
			content: text,
			fade: true
		}
	);
}

$( function()
	{
		$( '#authorization-submit-button' ).live( 'click', function()
			{
				if ( $( '#nick' ).val() == '' )
				{
					alert( 'Внимание! Произошла ошибка!<br />Вы не заполнили поле <strong>логин</strong>.' );
				}
				else if ( $( '#password' ).val() == '' )
				{
					alert( 'Внимание! Произошла ошибка!<br />Вы не заполнили поле <strong>пароль</strong>.' );
				}
				else
				{
					$.post( '/ajax/authorization', { nick: $( '#nick' ).val(), pass: $( '#password' ).val() }, function( response, status ) 
						{
							if ( status == 'success' )
							{
								if ( response.error == '' )
								{
									var auth_block = $( '<div id="authorization-success"></div>' );
									$( '<div id="greeting"></div>' ).html( response.nick )
													.appendTo( auth_block );
									$( '<div id="exit"></div>' ).append( $( '<a href=""></a>' ).html( 'Выход' ) )
												    .appendTo( auth_block );
									$( '#logo-middle-right' ).empty().append( auth_block );
								}
								else
								{
									alert( 'Внимание! Произошла ошибка!<br />' + response.error );
								}
							}
							else
							{
								alert( 'Внимание! Произошла ошибка!<br />Не удалось подключиться к серверу. Попробуйте повторить попытку позже.' );
							}
						}, 'json' );
				}
			}
		);
		$( '#exit > a' ).live( 'click', function( evt )
			{
				$.ajax(
					{
						async: false,
						url: '/ajax/logout',
						success: function( response, status )
							{
								if ( status == 'success' )
								{									
									$( '#logo-middle-right' ).html( '<fieldset><legend>Авторизация</legend><form id="authorization-form" action="" method="post"><label>Логин: <input type="text" id="nick" name="nick" value="" /></label><br /><label>Пароль: <input type="password" id="password" name="password" value="" /></label><br /><a href="" id="authorization-forget-password-link">Забыли пароль?</a> <img id="authorization-submit-button" src="http://images.morgenshtern.com/enter.gif" alt="Авторизация" title="Войти" width="44" height="18" /></form></fieldset>' );
								}
								else
								{
									location.href = '/index/logout';
								}
							}
					}
				);
				evt = evt || window.event;
				evt.cancelBubble = true;
				evt.stopPropagation();
				return false;
			}
		);
		$( 'a.external' ).live( 'click', function( evt )
			{
				evt.preventDefault();
				window.open( $( this ).attr( 'href' ) );
			}
		);
		$( '#ajax-cron-button' ).click( function()
			{
				var field = $( '#ajax-cron-field' );
				var val = field.val();
				if ( val != '' )
				{
					$.post( '/ajax', { nick: val }, function() { field.val( '' ) } );
				}
			}
		);
		$( 'div.expand' ).click( showHide );
	}
);

function getElementPosition( elemId )
{
    var elem = document.getElementById( elemId );
	
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;
	
    var l = 0;
    var t = 0;
	
    while ( elem )
    {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return { 'left': l, 'top': t, 'width': w, 'height': h };
}

// текст "Показать/спрятать"
function showHide( event )
{
	var $this = $( this );
	var $text = $this.text();
	if ( $text == 'Показать скрытый текст' )
	{
		$this.text( 'Cкрыть текст' );
	}
	else if ( $text == 'Cкрыть текст' )
	{
		$this.text( 'Показать скрытый текст' );
	}
	$this.next().slideToggle( 'slow' );
}
