/*************************
 * Navigation
 ************************/

var timer = null;

topNavBehavior = Behavior.create({
  initialize: function() {
    this.sub_menu = this.element.up('li').down('ul');
  },
  onmouseover: function(event) {
    clearTimeout(timer);
    hideAllNav();
    if(this.sub_menu) {
      this.sub_menu.show();
    }
  },
  onmouseout: function(event) {
    if(this.sub_menu) {
      timer = setTimeout(function(){
        this.hide();
      }.bind(this.sub_menu), 300);
    }
  },
  onclick: function(event) {
    Event.stop(event);
  }
});

subNavBehavior = Behavior.create({
  onmouseover: function(event) {
    clearTimeout(timer);
  },
  onmouseout: function(event) {
    timer = setTimeout(function(){
      this.hide();
    }.bind(this.element.up('ul')), 300);
  }
});

function hideAllNav() {
  $$('ul.candidates li ul, .upmenu ul li ul').each(function(sm) {
    sm.hide();
  });
}

logoBehavior = Behavior.create({
  onclick: function(event) {
    window.location = "/";
  }
});

Event.addBehavior({
  'ul.candidates li a.top_level' : topNavBehavior(),
  'ul.candidates li ul li a' : subNavBehavior(),
  '.upmenu ul li a.top_level' : topNavBehavior(),
  '.upmenu ul li ul li a' : subNavBehavior(),
  '#logo' : logoBehavior
});


/*****************************
 *  REGISTRATION FORM
 *****************************/

inputFieldBehavior = Behavior.create({
  onfocus: function(event) {
    field = this.element;
    if(descr = field.up(0).down('.field_description')){
      Effect.Appear(descr.identify(), { duration: 0.2 });
    }
  },
  onblur: function(event) {
    field = this.element;
    if(descr = field.up(0).down('.field_description')){
      Effect.Fade(descr.identify(), { duration: 0.2 });
    }
  }
});

subregionSelectBehavior = Behavior.create({
  onchange: function(event) {
    el = this.element;
    if(el.getValue() == '0') {
      $('city_selector').hide();
    } else {
      $('city_selector').show();
      $('city_selector').innerHTML = "<img src='/images/loading.gif' alt='завантаження' class='loading' />";
      new Ajax.Updater('city_selector', '/ua/user/registration/get_cities', {
        method: 'get',
        parameters: { sub_region: el.getValue() }
      });
    }
  }
});

regionSelectBehavior = Behavior.create({
  onchange: function(event) {
    el = this.element;
    if(el.getValue() == '0') {
      $('sub_region_selector').hide();
      $('city_selector').hide();
    } else {
      $('sub_region_selector').show();
      $('city_selector').hide();
      $('sub_region_selector').innerHTML = "<img src='/images/loading.gif' alt='завантаження' class='loading' />";
      new Ajax.Updater('sub_region_selector', '/ua/user/registration/get_sub_regions', {
        method: 'get',
        parameters: { region: el.getValue() },
        onComplete: function() {
          Event.addBehavior({
            '#sub_region_id' : subregionSelectBehavior()
          })
        }
      });
    }
  }
});

countrySelectBehavior = Behavior.create({
  onchange: function(event) {
    el = this.element;
    if(el.getValue() == '0') {
      $('region_selector').hide();
      $('sub_region_selector').hide();
      $('city_selector').hide();
    } else {
      $('region_selector').show();
      $('sub_region_selector').hide();
      $('city_selector').hide();
      $('region_selector').innerHTML = "<img src='/images/loading.gif' alt='завантаження' class='loading' />";
      new Ajax.Updater('region_selector', '/ua/user/registration/get_regions', {
        method: 'get',
        parameters: { country: el.getValue() },
        onComplete: function() {
          Event.addBehavior({
            '#region_id' : regionSelectBehavior()
          })
        }
      });
    }
  }
});


Event.addBehavior({
  '#registration_form input.text' : inputFieldBehavior(),
  '#country_id' : countrySelectBehavior()
});

/*****************************
 *  CANDIDATES SLIDER
 *****************************/

moreCandidatesBehavior = Behavior.create({
  onclick: function(event) {
    Event.stop(event);
    if($('more_candidates').visible()) {
      Effect.BlindUp('more_candidates', { duration: 0.3});
      this.element.toggleClassName('opened');
    } else {
      Effect.BlindDown('more_candidates', { duration: 0.3});
      this.element.toggleClassName('opened');
    }
  }
});

Event.addBehavior({
  '#show_more_candidates' : moreCandidatesBehavior()
});


/*****************************
 *  COMMENTS
 *****************************/

thumbUpBehavior = Behavior.create({
  onclick: function(event) {
  
  }
});

answerLinkBehavior = Behavior.create({
  onclick: function(event) {
    Event.stop(event);
    var comment_id = /\d+/.exec(this.element.up('li').identify());
    var action = $$('#add_comment form')[0].getAttribute('action');
    block = this.element.up('div.answer');
    form_el = document.createElement('form');
    text_el = document.createElement('textarea');
    text_el.setAttribute('name', 'comment[body]');
    form_el.appendChild(text_el);
    form_el.setAttribute('action', action);
    form_el.setAttribute('method', 'post');
    subm = document.createElement('input');
    subm.setAttribute('type', 'submit');
    subm.setAttribute('value', 'Відповісти');
    hidden = document.createElement('input');
    hidden.setAttribute('type', 'hidden');
    hidden.setAttribute('name', 'id_parent');
    hidden.setAttribute('value', comment_id);
    form_el.appendChild(hidden);
    form_el.appendChild(subm);
    block.appendChild(form_el);
    block.removeChild(this.element);
  }
});

deleteLinkBehavior = Behavior.create({
  onclick: function(event) {
    Event.stop(event);
    list = this.element.up('li');
    var comment_id = /\d+/.exec(list.identify());
    new Ajax.Request('/ua/comments/'+comment_id+'/destroy', {
      method: 'get',
      onSuccess: function() {
        msg = document.createElement('h5');
        msg.insert('deleted');
        this.childElements().each(function(_sibs) {
          if(_sibs.tagName == "DIV") {
            _sibs.remove();
          }
        });
        this.insertBefore(msg, this.firstChild);  
      }.bind(list)
    });
  }
});


Event.addBehavior({
  '#comments div.answer a' : answerLinkBehavior(),
  '#comments div.heading a.remove' : deleteLinkBehavior()
});

