/**
 * Standard login window widget
 * Requires std-login.css style sheet
 * SVN: $Id: login.js 6163 2008-08-14 14:36:31Z mjezierski $ 
 */
JMF.Widgets.Login = function(props) {
	props = props || {};
	props.attributes = props.attributes || {};
	props.attributes['class'] = props.attributes['class'] || 'std-lgm';
	
	props.headClass  = props.headClass  || 'std-lghd';
	props.hcBtnClass = props.hcBtnClass || 'std-lgcb';  
	props.cntClass   = props.cntClass   || 'std-lgcnt';
	props.bBarClass  = props.bBarClass  || 'std-lgbn';
	
	JMF.Widgets.Modal.call(this,props);
	
	this.head.DOMRef.appendChild(JMF.$H.create());
	
   this.content.iHTML(
      '<h1 class="std-lgw">Zaloguj się</h1>'+
      '<div style="overflow:hidden">'+
      '<div class="std-lgw-l">'+
         '<h2 class="std-lgw">Nick</h2>'+
         '<input id="std-lgwl" class="std-lgwi" type="text" tabindex="1" />'+
         '<a class="std-lgw" target="_blank" href="rejestracja.html">Zarejestruj się</a>'+
      '</div>'+
      '<div class="std-lgw-r">'+
         '<h2 class="std-lgw">Hasło</h2>'+
         '<input id="std-lgwp" class="std-lgwi" type="password" tabindex="2"/>'+
         '<a class="std-lgw" target="_blank" href="/haslo.html">Zapomniałem hasła</a>'+
      '</div>'+
      '</div>'+
      '<span id="std-lgw-err" class="std-lgw-err" style="display:none"></span>' +
      '<span class="std-lgw"><input style="display:none;vertical-align:middle" id="std-lgws" type="checkbox" tabindex="3" /></span>');

   var instance = this;
     
   this.logini = this.content.DOMRef.getElementById('std-lgwl');
   this.passi  = this.content.DOMRef.getElementById('std-lgwp');
   this.ssli   = this.content.DOMRef.getElementById('std-lgws');

   this.errmsg =   this.content.DOMRef.getElementById('std-lgw-err');

/*
   this.logini.addListener('focus',function(e){
   	JMF.$H(JMF.$E(e).target).css('background-color:#FEFFCE');
   });
*/   
   this.logini.addListener('blur',function(e){
      JMF.$H(JMF.$E(e).target).css('background-color:#FFFFFF');
   });
/*
   this.passi.addListener('focus',function(e){
      JMF.$H(JMF.$E(e).target).css('background-color:#FEFFCE');
   });
*/   
   this.passi.addListener('blur',function(e){
      JMF.$H(JMF.$E(e).target).css('background-color:#FFFFFF');
   });
   
   this.passi.addListener('keyup',function(e){
      if(JMF.$E(e).keyCode == 13) {
      	instance.submit();
      }
   });
   
   this.buttonbar.iHTML(
      '<input id="std-lgwbc" class="std-lgwbc" type="button" value="Anuluj"/>'+
      '<input id="std-lgwbo" class="std-lgwbo" type="button" value="OK"/>');
   
   this.btnCn = this.buttonbar.DOMRef.getElementById('std-lgwbc');
   this.btnOk = this.buttonbar.DOMRef.getElementById('std-lgwbo'); 

   this.btnCn.addListener('click',function(e){
   	JMF.$E(e).stopPropagation();
   	e.preventDefault();
   	instance.cancel();
   });
   
   this.btnOk.addListener('click',function(e){
      JMF.$E(e).stopPropagation();
      e.preventDefault();
      instance.submit();
   });
} ;

JMF.inherit(JMF.Widgets.Login,JMF.Widgets.Modal);

JMF.Widgets.Login.prototype.show = function() {
	this.errmsg.css('display:none');
	this.logini.value = '';
	this.passi.value = '';
	JMF.Widgets.Modal.prototype.show.call(this);
   
   //this.logini.value = decodeURIComponent((((JMF.Cookie.get('onet_uoi')||'').match(/l=([^&]*)&/)||['',''])[1]));
	this.logini.focus();
};

JMF.Widgets.Login.prototype.displayError = function(errText,field) {
   this.errmsg.iHTML(
      '<img src="/_g/popover/2/w.gif" style="width:15px;height:15px;vertical-align:middle;margin-right:0.5em" />'+
      errText);	
   
   this.errmsg.css('display:block');
   
   if(field) {
      field.css('background-color:#FEFFCE');
   	field.focus();
   }
};

JMF.Widgets.Login.prototype.cancel = function() {
	this.dispatchControllerEvent('loginWin_cancelled');
	JMF.Widgets.Modal.prototype.cancel.call(this);
};

JMF.Widgets.Login.prototype.submit = function() {
   var isValid = false;
   if(!this.logini.value.length) {
	   this.displayError('Wpisz poprawny adres e-mail',this.logini);
   } else if(!this.passi.value.length) {
   	this.displayError('Wpisz hasło',this.passi);
   } else {
   	isValid = true;
   }
   
   if(!isValid) {
   	return;
   }
   
   this.wait = this.wait || new JMF.Widgets.Wait();
   this.wait.show();
   this.hide();
   this.passi.blur();
   this.logini.blur();
   this.dispatchControllerEvent(JMF.Events.create('loginWin_request',{login:this.logini.value,pass:this.passi.value,ssl:this.ssli.checked}));
};

JMF.Widgets.Login.prototype.onLogin = function() {
	this.wait.hide();
	this.dispatchControllerEvent('loginWin_logged');
};

JMF.Widgets.Login.prototype.onLoginError = function() {
	this.wait.hide();
	var lv = this.logini.value;
	var pv = this.passi.value;
	this.show();
	this.logini.value = lv;
	this.passi.value  = pv;
	this.displayError('Wpisz poprawne hasło lub sprawdź adres e-mail',this.passi);
};


JMF.Widgets.Login.InfoWindow = function(props) {
   props = props || {};
   props.attributes = props.attributes || {};
   props.attributes['class'] = props.attributes['class'] || 'std-lgm';
   
   props.headClass  = props.headClass  || 'std-lghd';
   props.hcBtnClass = props.hcBtnClass || 'std-lgcb';  
   props.cntClass   = props.cntClass   || 'std-lgcnt';
   props.bBarClass  = props.bBarClass  || 'std-lgbn';
   
   JMF.Widgets.Modal.call(this,props);
   
   this.head.DOMRef.appendChild(JMF.$H.create());

   this.content.iHTML(
      '<h1 class="std-lgw">' +
         '<img src="/_g/popover/2/i.gif" style="width:15px;height:15px;vertical-align:middle;margin-right:0.5em" />'+
         'Czym jest Onet e-mail'+
      '</h1>'+
      '<p class="std-lgw-iw">Do serwisów onetu możesz się zalogować używając adresu e-mail założonego w OnetPoczcie</p>'+
      '<p class="std-lgw-iw">Możesz uzyć adresow z różnych domen dostępnych w onecie, m.in.: @onet.pl, @poczta.onet.pl, @op.pl, @buziaczek.pl, @autograf.pl, i innych.<br />' +
      'Jeśli nie masz jeszcze konta w OnetPoczcie - możesz je <a class="std-lgw" href="http://poczta.onet.pl/oferta/20856,rezerwuj.html" target="_blank">załozyć teraz</a>.</p>'+
      '<p class="std-lgw-iw-gr">Pamiętaj! Logując się uzywaj pełnego adresu e-mail (np. julia.nowak@onet.pl)</p>');

   this.buttonbar.iHTML(
      '<input id="std-lgwbo" class="std-lgwbo" type="button" value="OK"/>');
   
   var instance = this;
   this.btnOk = this.buttonbar.DOMRef.getElementById('std-lgwbo'); 
   this.btnOk.addListener('click',function(e){
      JMF.$E(e).stopPropagation();
      e.preventDefault();
      instance.cancel();
   });
};

JMF.inherit(JMF.Widgets.Login.InfoWindow,JMF.Widgets.Modal);

JMF.Widgets.Login.InfoWindow.show = function() {
   if(!JMF.Widgets.Login.InfoWindow.instance) {
   	JMF.Widgets.Login.InfoWindow.instance = new JMF.Widgets.Login.InfoWindow(); 
   }
   JMF.Widgets.Login.InfoWindow.instance.show();	
};

