YUI({
	gallery: 'gallery-2011.08.24-23-44'
}).use(
	'gallery-formmgr', 
	'gallery-overlay-extras',  
	'dd-constrain',  
	'dd-plugin', 
	'io-form', 
	'json', 
	'node',
	'overlay', 
	'widget-anim', 
	'plugin',
	'event-focus',
function(Y){


Y.on('domready', function(){ 

	var loginOverlay = new Y.Overlay({						
		srcNode   : '#loginOverlay',
		width	  : '350px',
		height    : 'auto',
		zIndex	  : 100,
		centered  : true,
		constrain : true,
		render    : true,
		visible   : false,
		
		plugins   : [
			{ fn: Y.Plugin.OverlayModal },
			{ fn: Y.Plugin.OverlayKeepaligned },
			{ fn: Y.Plugin.OverlayAutohide, cfg: {
				focusedOutside : false,  // disables the Overlay from auto-hiding on losing focus
				clickedOutside : false
		        }}
		]							
	});


	Y.one('#hideloginOverlay').on('click', function(e){ 
		loginOverlay.hide(); 
	}); 	

	Y.one('#show-loginOverlay').on('click', function(e){ 
		loginOverlay.show(); 
		f.initFocus();		// only do this for one form on a page
	});
						
	Y.on('click', function(){
		f.populateForm();	// default_value_map + values in markup	
		f.enableForm();
	},
	'#reset');
			
			
	
	(new Y.DD.Drag({
					
		node	: loginOverlay.get('boundingBox'),
		handles	: ['.yui3-widget-hd']
					
	})).plug(Y.Plugin.DDConstrained, { constrain2view : true });	
						

	// create form
 
	var f = new Y.FormManager('login',{ status_node: '#form-status'});
	
	f.registerButton('#hideloginOverlay');   
	f.registerButton('#send');
	//f.registerButton('#reset');
	
	f.prepareForm();
	f.initFocus();		// only do this for one form on a page
	
	
	Y.FormManager.Strings ={
		validation_error:     'Bitte überprüfen sie die rot markierten Pflichtfelder.',
		required_string:      ' ',
		required_menu:        'This field is required. Choose a value from the pull-down list.',
		length_too_short:     'Enter text that is at least {min} characters or longer.',
		length_too_long:      'Enter text that is up to {max} characters long.',
		length_out_of_range:  'Enter text that is {min} to {max} characters long.',
		integer:              'Zahl eingeben: ',
		integer_too_small:    'Enter a number that is {min} or higher (no decimal point).',
		integer_too_large:    'Enter a number that is {max} or lower (no decimal point).',
		integer_out_of_range: 'Postleitzahl {min} und {max}.',
		decimal:              'Enter a number.',
		decimal_too_small:    'Enter a number that is {min} or higher.',
		decimal_too_large:    'Enter a number that is {max} or lower.',
		decimal_out_of_range: 'Enter a number between or including {min} and {max}.'
	};


	//########################################
	// YUI ASYNC REQUEST --> yui3.3 IO
	//########################################
	
	
		var handleStart = function(id, a) {
			//Y.log("io:start firing.", "info", "example");
			//output.set("innerHTML", "<li>Loading news stories via Yahoo! Pipes feed...</li>");
		}
	
		var handleSuccess = function(id, o, a) {
		
				var id = id; // Transaction ID.
				var data = o.responseText; // Response data.
	
				try {
					 var json_data = Y.JSON.parse(data);
				}
				catch (o) {
					 
				}
			  			Y.log("test "+ Y.dump(data));  
			 
			 if (json_data.status != 200) {
		   		
					
					f.enableForm();
					
					f.displayFormMessage('Passwort oder Benutzername sind falsch', true, true );
					f.displayMessage('#passwd', '', 'error');
					f.displayMessage('#handle', '', 'error');

						
			 }else{
				 
				  loginOverlay.hide();
				  f.populateForm();
							  
					window.setTimeout(function() {
						window.location = "admin.php"
					}, 500);
				  
				 
				 }
			 

	
		}
	
	
		var handleComplete = function(id, o, a) {
		
		}
		
		var handleFailure = function(id, o, a) {
		
		}
	
	
		var cfg = {
			method: 'GET',
			data: 'action=authentication',
			form: {
				id: 'login',
				useDisabled: true
			},
			on: {
					start: handleStart,
					complete: handleComplete,
					success: handleSuccess,
					failure: handleFailure
				},
			arguments: {
				start:    'foo',
				complete: 'bar',
				end:      'baz'
			}
	
		};
	

			function send() {

				f.validateForm();

				if(f.hasErrors()){
				
				}else{
			
					Y.io('/admin.php', cfg);
					f.disableForm();
				
				}	

			}

 
			// example actions
 			Y.on('click', send, "#send", this);	
			Y.on('click', function(){ f.validateForm()}, '#validate');
 			Y.on('click', function(){ f.populateForm()}, '#reset');
 			Y.on('click', function(){ f.clearForm()}, '#clear');
 	
 });
			
});

