function initLogin()
{
	jQuery.each($('.login-form'), function(i,  form)
	{
		new LoginForm(form);
	});
}
/* CLEAR INPUT */

var formLogin_my_p_ID = 0;
var formLogin_from_p_ID = 0;
var formLogin =
{
  _options :
  {
    onSuccess: function()
    {
      
    },
    onFailure: function()
    {
      
    }
  },
	sendRequest : function(form_id, options)
  {
    formLogin._options = jQuery.extend(formLogin._options, options);
    
		f=document.getElementById(form_id);
		u_userName=f.elements['u_userName'].value;
		u_pwd=f.elements['u_pwd'].value;
		script=f.elements['script'].value;
		formLogin_my_p_ID=f.elements['p_ID'].value;
		formLogin_from_p_ID=f.elements['from_p_ID'].value;
		if (console) console.log(script);
		$("#loginNoticeContent #error_message").hide();
		$.getJSON("/index/index/",{returnie:'json',u_userName:u_userName,u_pwd:u_pwd,script:script},formLogin.readResponse);
	},
	readResponse : function(data, textStatus){
		if (console) console.log(data,"|",textStatus);

		if(data.response.status===1)
    {
			u_UID=data.response.auth.u_UID;
			tb_remove();

      formLogin._options.onSuccess.apply(formLogin, arguments);
      
      if (formLogin_from_p_ID > 0)
      {
        // da muss mindestens u_UID durchgeschleift werden! TODO
				// document.location.href='/index/index?p_ID='+formLogin_from_p_ID;

          switch(formLogin_my_p_ID)
          {
            // R12 SMARTTIMES ANMELDUNG
            case '307':
            case '309':
            case '954':
              document.location.href='/index/index?p_ID='+formLogin_from_p_ID+"&u_UID="+u_UID;
              break;

            // R10 FORUM
            // BEITRAG SCHREIBEN; USER LOGIN "ON THE FLY"
            case '124':
              var mb_UID = null;
              try
              {
                // mb_UID für redirect holen
                mb_UID = data.response.entries.mb_UID;
              }
              catch (e)
              {
                if(typeof console !== 'undefined')
                {
                  console.log(e);
                }
                mb_UID = null;
              }

              if(mb_UID > 0)
              {
                 document.location.href='/index/index?p_ID='+formLogin_my_p_ID+"&mb_UID="+mb_UID;
              }
              else
              {
                document.location.href='/index/index?p_ID=122';
              }
              break;
          }

			}

		}else{
			$("#loginNoticeContent #error_message").show();
      formLogin._options.onFailure.apply(formLogin, arguments);
			}
		}
}

var LoginForm = function()
{
	this.initialize.apply(this, arguments);
}

LoginForm.prototype = jQuery.extend(LoginForm.prototype,
{
	initialize: function(form)
	{
		this._form = form;
		this._observeUsernameField();
		this._observePasswordField();
	},
	_observeUsernameField : function()
	{
		$('.clearMeFocus', this._form).focus(function()
		{
			if($(this).val()==$(this).attr('title')){
				$(this).val('');
			}
		});
		$('.clearMeFocus', this._form).blur(function(){
			if($(this).val()==''){
				$(this).val($(this).attr('title'));
			}
		});
	},
	_observePasswordField: function()
	{
		var self = this;

		// toggle inputs on focus
		$('.clearMePwdLabel', this._form).focus(function()
		{
			$('.clearMePwdLabelContainer', self._form).css('display','none');
			$('.clearMePwdContainer', self._form).css('display', 'inline');
			$('.clearMePwd', self._form).focus();
		});

		// if field is empty afterward, toggle inputs again
		$('.clearMePwd', this._form).blur(function()
		{
			if($(this).val()=='')
			{
				$('.clearMePwdContainer', self._form).css('display','none');
				$('.clearMePwdLabelContainer', self._form).css('display','inline');
			}
		});
	},
	sendRequest : function()
	{
		var u_userName = f.elements['u_userName'].value;
		var u_pwd = f.elements['u_pwd'].value;
		var script=f.elements['script'].value;
		$("#loginNoticeContent #error_message").hide();

		$.getJSON("/index/index/",{returnie:'json',u_userName:u_userName,u_pwd:u_pwd,script:script},formLogin.readResponse);
	},
	readResponse : function(data, textStatus){
		if (console) console.log(data,"|",textStatus);

		if(data.response.status===1){
			u_UID=data.response.auth.u_UID;
			tb_remove();
		}else{
			$("#loginNoticeContent #error_message").show();
		}
	}
});
