/*!
 * Goodar
 * @author    Vyacheslav Voronchuk
 * @date      25.12.2009 
 * Copyright(c) 2009, by Vyacheslav Voronchuk
 */

Ext.onReady(function() 
{
  if(Ext.isIE6)
  {
	var oBody = Ext.getBody();
	oBody.setStyle({ background: 'black url(http://rghost.ru/1201427/image.png) center center' });
	oBody.dom.innerHTML = ['<div style="background: red; color: white; width: 100%; height: 50px; position: absolute; top: 50%; left:0; line-height: 50px; font-size: 15pt; z-index: 9001">',
	  '<center>',
		'Вы используете устаревший браузер. Вы можете <a href="http://www.microsoft.com/windows/internet-explorer/worldwide-sites.aspx">обновить его</a> или использовать <a href="http://getfirefox.com">альтернативный</a>.',
	  '</center>',
	'</div>'].join('');
	return false;
  }
	
  //отключаем форму логина, если она не нужна
  if(Ext.get('auth'))
  {    
    //форма входа
    var oAuthForm = new Goodar.Form
    ({
      id: 'auth',
      url: '/module/auth/check',
      validateType: 'tooltip',
            
      onSuccess: function(oResponse)
      {
        this.normalSubmit = true;
        this.submit();
      },
      
      onFailure: function(oResponse)
      {
        switch(oResponse.errors.code)
        {        
          case 60005:
            this.setErrorLabel('password', oResponse.errors.title);
            break;
            
          default:
            this.setErrorLabel('login', oResponse.errors.title);
            break;
        }
      }
    });
    oAuthForm.form.dom['url'].value = document.location.href;
    
    
    //вход на сайт
    Ext.fly('auth_submit').on('click', function (event) 
    {
      Goodar.Manager.getCmp('auth').submit();
    });  
    //вход на сайт по enter
    Ext.fly('auth').on('keypress', function (event, element) 
    {
      if(event.getCharCode() == 13)
        Goodar.Manager.getCmp('auth').submit();
    });
  }
  
  
  //поисковый модуль
  var SearchQuery = function () 
  {
    Goodar.request
    ({
      url: '/module/search/analysis',
      params: 
      {
        search: document.getElementById('search_query').value
      },
      onSuccess: function()
      {
        document.location.href = '/module/search/found';
      },
      onFailure: function(oResponse)
      {       
        var sError = '';
        if(!oResponse)
          sError = 'Нет соединения с сервером';
        else
          sError = oResponse.msg;
                  
        var oItem = document.getElementById('search_query');
        Ext.DomHelper.append(oItem.parentNode, { tag: 'div', cls: 'errortooltip', id: 'label-search_query', html: sError });
        oLabel = Ext.get('label-search_query');
        oLabel.ghost.defer(2000, oLabel, ['b', { remove: true }]);
      }
    });
  };
  
  //нажатие на кнопку поиска
  Ext.fly('search_button').on('click', SearchQuery);
  //поиск по enter
  Ext.fly('search').on('keypress', function (event, element) 
  {
    if(event.getCharCode() == 13)
      SearchQuery.call(this);
  });
});


