// JavaScript Document
window.addEvent('domready', function() { 
	ObjectDock.init({
		elements: {
			'home': {
				icon: './_templates/_' + template + '/_images/dockRefresh.png',
				maxsize: 128,
				onclick: function(){
					window.location.reload();
				},
				proximity: 300,
				title: DockLang['reload']
			},
			'login': {
				icon: './_templates/_' + template + '/_images/dockKey.png',
				maxsize: 128,
				onclick: function(){
					var LoginForm = new Element('form', {
						'id': 'loginForm',
						'action': './?ajax=login',
						'method': 'post'
					}).set('html', '<table id="loginTable" cellpadding="0" cellspacing="0">' +
					'<tr>' +
					'<td>' +
					DockLang['username'] +
					'</td>' +
					'<td><input type="text" id="user" name="user" /></td>' +
					'</tr>' +
					'<tr>' +
					'<td>' +
					DockLang['password'] +
					'</td>' +
					'<td><input type="password" id="pass" name="pass" /></td>' +
					'</tr>' +
					'<tr>' +
					'<td colspan="2"><input type="submit" name="submit" id="submit"' +
					' value="' +
					DockLang['submit'] +
					'"/>' +
					'<div id="loginSpinner" style="display: none;"></div></td>' +
					'</tr>' +
					'</table>');
					
					HTMLBox.open({
						caption: DockLang['registered'],
						height: 140,
						element: LoginForm,
						onComplete: function(){
							$('loginForm').addEvent('submit', function(e){
								new Event(e).stop();
								
								$('loginSpinner').setStyle('display', 'inline');
								
								this.set('send', {
									url: './?ajax=login',
									method: 'post',
									onComplete: LoginCheck
								});
								this.send();
							});
							
							$('user').focus();
						},
						title: DockLang['login'],
						width: 334
					});
				},
				proximity: 300,
				title: DockLang['login']
			}
		},
		
		key: 'l',
		hide: true
	});
	
	// $('AuthForm').addEvent('submit', IFrameSubmit);
});

function LoginCheck(xml)
{
	$('loginSpinner').setStyle('display', 'none');
	if(xml == 'success')
	{
		HTMLBox.container.setStyle('background', 'url(_templates/_'+template+'/_images/dockAccepted.png) no-repeat center');
		HTMLBox.container.empty();
		setTimeout('window.location.reload()', 750);
	}
	else
	{
		switch(xml)
		{
			case 'password':
				{
					HTMLBox.container.setStyle('background', 'url(_templates/_'+template+'/_images/dockDenied.png) no-repeat center');
					
					$('pass').value = '';
					$('pass').focus();
					
					$('pass').setStyle('border', '1px solid #990000');
					$('user').setStyle('border', '1px solid #cccccc');
				}
				break;
			case 'username':
				{
					HTMLBox.container.setStyle('background', 'url(_templates/_'+template+'/_images/dockDenied.png) no-repeat center');
					
					$('user').value = '';
					$('user').focus();
					
					$('user').setStyle('border', '1px solid #990000');
					$('pass').setStyle('border', '1px solid #cccccc');
				}
				break;
		}
	}
}

function popout(url, width, height)
{
	var centerWidth = (window.screen.width - width) / 2;
    var centerHeight = (window.screen.height - height) / 2;

    newWindow = window.open(url, '', 'resizable=0,width=' + width + ',height=' + height + ',left=' + centerWidth + ',top=' + centerHeight);
	
	if(newWindow)
    	newWindow.focus();
	
    return false;
}

function IFrameSubmit(e)
{
	new Event(e).stop();
	
	$$('h1[class^=kop-]').destroy();
	$('mainContent').empty();
	
	new Element('iframe', {
		'id': 'urenReg',
		'name': 'urenReg',
		'src': 'about:blank',
		'frameBorder': 0,
		'styles': {
			'background': 'none',
			'border': 0,
			'height': 400,
			'width': '100%'
		}
	}).inject($('mainContent'));
	
	this.target = 'urenReg';
	this.removeEvent('submit', IFrameSubmit);
	
	this.submit();
}
