function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) {
      c_start=c_start + c_name.length+1;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}

$(document).ready(function() {

  if (getCookie('city') == "") {
    $('#citywindow').fadeIn();
  }
  // City selector window
  $('#cityselect .selector').click(function() {
    $('#citywindow').fadeIn();
    return false;
  });
  $('#citywindow .close').click(function() {
    $('#citywindow').fadeOut();
    return false;
  });

  // Main menu effects
  $('#flashcontent_mmenu h1').bind('mouseover', function() {
    $(this).fadeOut('fast');
    $(this).next('ul').fadeIn();
  });
  $('#flashcontent_mmenu ul:not(.selected)').bind('mouseleave', function() {
    $(this).fadeOut('fast');
    $(this).prev('h1').fadeIn();
  });

  // Оформляем "полосатые" таблицы
  $('.stripy').each(function() {
    $(this).find('tr:even').addClass('even');
    $(this).find('tr:odd').removeClass('even');
  })

  // Поля форм
  formField = function(objid, formid, default_text) {
    this.obj = document.getElementById(objid);
    this.form = (formid == null) ? document.getElementById(formid) : null;
    this.default_text = default_text;
    this.state = { value: false };
    this.obj.value = this.default_text;
    this.obj.onfocus = function(state, obj) { return function() { if (!state.value) obj.value = ''; obj.className = 'text active'; } } (this.state, this.obj);
    this.obj.onblur  = function(state, obj, default_text) { return function() { if (!state.value) { obj.value = default_text; obj.className = 'text'; } } } (this.state, this.obj, this.default_text);
    this.obj.onkeyup = function(state, obj) {
      return function() {
        state.value = obj.value;
      }
    } (this.state, this.obj);
    if (this.form != null) this.form.onsubmit = function(state) { return function() { if (!state.value) return false; } } (this.state);
  }

  $("#connect-city-id").change(function(){
    $("#connect-street, #check_connection_street").flushCache();
    $("#connect-house, #check_connection_house").flushCache();
  });

  $("#connect-street, #check_connection_street").autocomplete( '/connect/streets', {
    max: 0,
    multiple: false,
    minChars: 2,
    extraParams: {
      city: function(){ return $('#connect-city-id option:selected').html() }
    }
  }).result(function(event, data) {
    $("#connect-house, #check_connection_house").flushCache();
  });

  $("#connect-house, #check_connection_house").autocomplete( '/connect/houses', {
    max: 0,
    multiple: false,
    minChars: 1,
    extraParams: {
      city: function(){ return $('#connect-city-id option:selected').html() },
      street: function(){ return $('#connect-street').val() ? $('#connect-street').val() : $('#check_connection_street').val()}
    }
  }).result(function(event, data) {
  });

})

