YUI({
    gallery: 'gallery-2010.05.26-19-47'
}).use('gallery-formmgr', 'gallery-overlay-extras',  'dd-constrain',  'dd-plugin', 'io-form', 'json', 'node', 'overlay', 'widget-anim', 'plugin','event-focus', function(Y)
{
	

						var loginOverlay = new Y.Overlay({
							
							srcNode     : '#loginOverlay',
							width		   : '450px',
							height      : 'auto',
							zIndex      : 20,
            			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
										pressedEscape : true
                			}}
 
            			]
 
												
			        });
					  
					  
					  
					  	Y.one('#show-loginOverlay').on('click', Y.bind(loginOverlay.show, loginOverlay));
				      Y.one('#hideloginOverlay').on('click', Y.bind(loginOverlay.hide, loginOverlay));
						
			
						
						(new Y.DD.Drag({
					
							node	: loginOverlay.get('boundingBox'),
							handles	: ['.yui3-widget-hd']
					
						})).plug(Y.Plugin.DDConstrained, { constrain2view : true });	
						
								
						
						Y.get('#hideloginOverlay').on('click', function(e){ 
							  loginOverlay.hide(); 
						 }); 	

	
	// 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.1.1 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) {
					 alert("Invalid json data");
				}
			  
			 
			 if (typeof json_data.errors != 'undefined' && json_data.errors == true) {
		   		
					
					f.enableForm();
					
					f.displayFormMessage('Passwort oder Benutzername sind falsch', true, true );
					f.displayMessage('#passwd', '', 'error');
					f.displayMessage('#handle', '', 'error');

						
			 }
			 

			  
			  if(json_data.success === true ){
			  
			  	  loginOverlay.hide();
				  f.populateForm();
							  
					window.setTimeout(function() {
						window.location = "admin.php"
					}, 500);
				  
				  
				  
			  
			  }
			  

				//Y.log("test "+ Y.dump(json_data));  
		}
	
	
		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');
 
	       //status display
 
			var changed_status = Y.one('#changed-status');
			Y.later(1000, f, function()
			{
				changed_status.set('innerHTML', this.isChanged() ? 'Ja' : 'Nein');
			},
			
			null, true);	
			
});
