function setCookie(name, value, expires, path, domain, secure)
{
  var curCookie = name + "=" + escape(value) +
  ((expires) ? "; expires=" + expires.toGMTString() : "") +
  ((path) ? "; path=" + path : "") +
  ((domain) ? "; domain=" + domain : "") +
  ((secure) ? "; secure" : "");

  document.cookie = curCookie;
}

function getCookie(name)
{
  var prefix = name + "="
  var cookieStartIndex = document.cookie.indexOf(prefix)
  if (cookieStartIndex == -1)
  {
    return null
  }
  
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
  if (cookieEndIndex == -1)
  {
    cookieEndIndex = document.cookie.length
  }
  
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

var _requests = 0;

function nwShowAjaxProgress()
{
  if(!$('ajax-progress'))
  {
    $$('body')[0].insert(Builder.node('div', { id: 'ajax-progress', className: 'ajax-message' }, [
      Builder.node('div', { className: 'container' }, [
        Builder.node('div', { className: 'header' }, 'Идёт загрузка, пожалуйста, подождите...'),
        Builder.node('div', { className: 'body' }, [
          Builder.node('img', { src: '/static/img/progress.activity.gif' })
        ])
      ])
    ]));
    $('ajax-progress').hide();
  }
  
  if(_requests)
  {
    centerWindow('ajax-progress');
    $('ajax-progress').show();
  }
  else
  {
    $('ajax-progress').hide();
  }
}

function showProgress()
{
  _requests++;
  nwShowAjaxProgress();
}

function hideProgress()
{
  _requests--;
  nwShowAjaxProgress();
}

function showError(transport)
{
  if(!$('ajax-error'))
  {
    $$('body')[0].insert(Builder.node('div', { id: 'ajax-error', className: 'ajax-message' }, [
      Builder.node('div', { className: 'container' }, [
        Builder.node('div', { className: 'header' }, 'Произошла ошибка'),
        Builder.node('div', { className: 'body' }, [
          Builder.node('div', { id: 'ajax-error-body' }),
          Builder.node('div', { className: 'close' }, [
            Builder.node('a', { id: 'ajax-error-close', href: '#' }, 'Закрыть'),
          ])
        ])
      ])
    ]));
    
    $('ajax-error-close').observe('click', function () {
      $('ajax-error').hide();
    });
  
    $('ajax-error').hide();
  }
  
  $('ajax-error-body').update(transport.status);
  centerWindow('ajax-error');
  $('ajax-error').show();
}

function centerWindow(id)
{
  var left = Math.floor(($$('body')[0].getWidth() - $(id).getWidth()) / 2);
  $(id).setStyle({ 'left': left + 'px' });
}

function centerModalWindow(id)
{
  if(!$('overlay'))
  {
    $$('body')[0].insert(Builder.node('div', { id: 'overlay' }));
    $('overlay').hide();
  }
  
  if(Prototype.Browser.IE) $$('body')[0].insert($(id));
  
  centerWindow(id);
  $('overlay').setStyle({ 'height': $$('body')[0].getHeight() + 'px' });
}

function showModalWindow(id)
{
  centerModalWindow(id);
  $('overlay').observe('click', function (){
    hideModalWindow(id);
  });
  $('overlay').appear({ to: 0.8, duration: 0.5 });
  $(id).appear({ duration: 0.5 });
}

function hideModalWindow(id)
{
  Effect.multiple(['overlay', id], Effect.Fade, {duration: 0.5});
}

function submitSearch(url, id)
{
  document.location.href = url + '&' + $(id).serialize();
  return false;
}

function fireEvent(element,event){
  if (document.createEventObject){
    // dispatch for IE
    var evt = document.createEventObject();
    return element.fireEvent('on'+event,evt)
  }
  else{
    // dispatch for firefox + others
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent(event, true, true ); // event type,bubbling,cancelable
    return !element.dispatchEvent(evt);
  }
}

function nwAcronym(string)
{
  return string.substr(0,1);
}
