var id_login_form = 'userLogin';
var login_url = 'index.php?id=22&type=109';
var id_retrieve_form = 'userLostLogin';

var class_login = 'loading';
var class_error = 'error';

/*
+++++++++++++++++++++++++++++++++
Init Ende
+++++++++++++++++++++++++++++++++
*/

/*
window.addEvent('domready', function(){
	aktive();
});
*/

window.addEvent('load', function(){
	ak();
});

// request login form
function ak() {

  var list = $$('#rohr li a');

  list.each(function(element) {

    if (element.getProperty('id') == 'showUserLogin') {
      
      element.removeProperty('href');

      var parentElement = element.getParent('li');
      var loginContainer = new Element('ul', { 'styles': { 'opacity':'0', 'display':'none' } });
      var loginContainerInner = new Element('li', { 'id':'userLoginContainerWrap' });
    	var informant = new Element ('div', {id: 'log_res'});

      parentElement.grab(loginContainer);
      loginContainer.grab(loginContainerInner);
    	loginContainerInner.grab(informant);


      new Request({
        method:'get',
        url:login_url,
        onRequest: function() {
    			informant.addClass(class_login);
          informant.set('html', 'Request started'); 
        },
        onFailure: function(xhr) {
        	informant.set('html', 'The request to ' + login_url + ' failed. ');
        },
        onSuccess: function(responseText) {
           
          responseText = wrapMyForm(responseText);

        	loginContainerInner.set('html', responseText);

          $('resLoginCreate').grab(informant);

          informant.set('html', '');
          informant.removeClass(class_login);
          
          FancyForm.add($$('#top input'));

        	aktive();
          addEffects();

        }
      }).send();
    }
  });
};

//stop the submit, enable ajax request
function aktive() {


      if (! $('log_res') && $('resLoginCreate') ) {
      	var informant = new Element ('div', {id: 'log_res'});
        $('resLoginCreate').grab(informant);
      } else if ($('log_res'))  {
      	var informant = $('log_res');
      } else {
      }




	if($(id_login_form)) {
//    var userGUI = $$('#rohr div.userLoginCreate');
    var userGUI = $('userMenu').getParent('div');

		//$(id_login_form).removeEvent('submit');
		$(id_login_form).addEvent('submit', function(e) {

  		// Prevents the default submit event from loading a new page.
  		e.stop();

			informant.addClass(class_login);
      informant.set('html', '<h2>please wait...</h2>');

   // Set the options of the form's Request handler.  
           // ("this" refers to the $('login') element).  
       this.set('send', {
                  onComplete: function(response) {
  
                        informant.removeClass(class_login);
                        informant.addClass(class_error);

                         if( response.test('logged in') )
                         {  //Login erfolgreich

                              //userGUI.set('html', response );
                              informant.set('html', '<h1>Login sucessfull</h1><br /><p>Please wait while loading the Premium Area, you may reload the Page manually or simply <a href="Premium.3.0.html">click here</a></p>');
                              informant.getElement('a').addEvent ('click', function() { window.location.reload() } );
                              window.location.href = "Premium.3.0.html";  
                         }
                         else
                         {

                              //$('resLoginCreate').set('html', response );
                              //The last line also overwrites the informant
                              informant.set('html', '<h1>Please check your supplied credentials</h1><br /><p><a href="#">Click here to return to Login</a></p>');
                              informant.getElement('a').addEvent ('click', function() { this.removeClass(class_login) } );
                              informant.addEvent ('click', function() {
                                this.removeClass(class_error) 
                              } );
                              informant.setStyle ('cursor', 'pointer' );

                         }

                         aktive();
                  }
        });

        $('top').setStyle ('z-index', '');
    		this.send();

  	});
	}
	
	if($(id_retrieve_form)) {

		$(id_retrieve_form).addEvent('submit', function(e) {

  		// Prevents the default submit event from loading a new page.
  		e.stop();

			informant.addClass(class_login);
      informant.set('html', '<h2>please wait...</h2>');

   // Set the options of the form's Request handler.  
   // ("this" refers to the $('login') element).  
       this.set('send', {


                  onComplete: function(response) {
                  informant.removeClass(class_login);
                  informant.addClass(class_error);
                  informant.addEvent ('click', function() {
                    this.removeClass(class_error) 
                  } );
                  informant.setStyle ('cursor', 'pointer' );
                  
//                  myResponse = '<h1>' + response + '</h1><br /><p>Your password has been sent to the email address you supplied.<br />Clicking here shows the login form</p>';
                  myResponse = '<p>Your password has been sent to the email address you supplied.<br />Clicking here shows the login form</p>';
//                  informant.set('html', '<div id="error" style="margin: 20px;">' + myResponse + '</div>');
                  informant.set('html', myResponse );

                	aktive();
                  }
        });

        $('top').setStyle ('z-index', '');
    		this.send();

  	});
	}
	
};

function wrapMyForm(element) {
returnText = '<div id="userLoginContainer" class="formOuter"><div class="formArrow"></div><div class="formTop"><div class="corner formTopLeft"></div><div class="corner formTopRight"></div><div class="formTopMiddle"></div></div><div class="formRight"><div class="formLeft"><div id="resLoginCreate" class="formCenter">';
returnText = returnText + element;
returnText = returnText + '</div> </div> </div> <div class="formBottom"> <div class="corner formBottomLeft"></div> <div class="corner formBottomRight"></div> <div class="formBottomMiddle"></div> </div> </div>';

return returnText;
};


function addEffects() {

//Container für den der z-index bei hover gesetzt wird, IE<7
this.topContainer = $$('#top');
var informant = $('log_res');
//Ort zum Einfügen der Objekte


var list = $$('#rohr li');

  list.each( function(element) {
    if (myUL = $(element).getElement('ul')) {
//    if ($(element).getElement('ul')) {
//      myUL = $(element).getElement('ul');
       
      var myFx = new Fx.Morph(myUL, {
        fps: 30,
        unit: 'px',
        duration:400,
        link:'cancel',
        transition: Fx.Transitions.Quad.easeOut,
        onComplete: function() {
          if ($('user').get('value')=="") {
            $('user').focus();
          }
/*
          if ($('log_res').hasClass(class_error)) {
            $('log_res').removeClass(class_error);
          }
*/
        }
      });
                
      myUL.setStyle('opacity', 0);
      myUL.setStyle('display', 'none');
    
      element.addEvent('click', function(){
        topContainer.setStyle ('z-index','2');

        myFx.element.setStyle('display', 'block');
        myFx.element.setStyle('visibility', 'visible');

        myFx.start({'opacity':'1'});
      });
  
      element.addEvent('mouseleave', function(){
        
        myFx.start({ 'opacity': '0' });
        topContainer.setStyle ('z-index','');

      });
    }     
  }); // End each (LIST)


};

function urlSetTypeNum (myURL,type_content) {

    myURL = myURL.toString();

    //var myURL = (window.location).toString();

    //remove ?&logintype=logout from URL
    if (myURL.match(/\?&logintype=logout/i)) {
      myURL = myURL.replace(/\?&logintype=logout/g, '');
    }
    
    //replace typeNum in URL
    if (myURL.match(/\.0\.html/i)) {
      myURL = myURL.replace(/\.0\./g, '.' + type_content + '.');
    } else if (myURL.match(/\//)) {
      if (myURL.match(/\?/)) {
        myType = '&type=';
      } else {
        myType = '?type=';
      }
      myURL = myURL + myType + type_content;
    } else {
    console.warn (myURL + ' was not matched');
    }
    //console.info (myURL);
    return myURL;
};
