Asset.images([
  'textarea_focus.png', 
  'textarea_focus2.png', 
  'search_focus.png',
  'w_btn_focus.png',
  'input_focus.png',
  'input_focus2.png',
  'grab.cur',
  'grabbing.cur',
  'language_box.png',
  'my_menu_down.gif',
  'my_menu_left.gif',
  'my_menu_down.gif',
  'my_menu_left.gif',
  'trash.gif',
  'trash_over.gif',
  'trash_trans_01.png',
  'catbtn_hover.gif',
  'right_head_hover.gif',
  'header_profiles.jpg',
  'header_profiles_down.jpg',
  'header_profiles_down_active.jpg',
  'header_top.jpg',
  'header_work.jpg',
  'header_work_down.jpg',
  'header_work_down_active.jpg'
],{prefix:'/images/'})

// Language bar
// ==================================
var LanguageBox = Class.create()
LanguageBox.prototype = {
  initialize:function(e){
    Object.extend(e,this)
    Event.observe('language','mouseover', e.show_language_box)
    Event.observe('language_box','mouseout', e.hide_language_box.bind(e))
  },
  show_language_box:function(){
    $("language_box").show()
    return false
  },
  hide_language_box:function(event){
    if(this.is_really_outside(event)){
      $("language_box").hide()
  	}
    return false
  },
  is_really_outside:function(e){
      if (!e) var e = window.event;
  	var tg = (window.event) ? e.srcElement : e.target;
  	if (tg.nodeName != 'DIV') return false;
  	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
  	while (reltg != tg && reltg.nodeName != 'BODY')
  		reltg= reltg.parentNode
  	if (reltg== tg) return false;
  	return true
  }
}

// SearchBar
// ==================================
var SearchBar = Class.create()
SearchBar.prototype = {
  initialize:function(e){
    var search_text =  $(e.id+'_text')
    
      search_text.observe('focus', function(){
        e.removeClassName("search_blur"); 
        e.addClassName("search_focus")
      })
      search_text.observe('blur', function(){
        e.removeClassName("search_focus")
        e.addClassName("search_blur")
      })
    
  }
}

// Favourites
// ==================================

var Favourites = Class.create()
Favourites.prototype = {
  initialize:function(e,options){
    Object.extend(e,options)
    e.text_content = $$('#fav_head span').first()
    Object.extend(e,this)
    e.set_sortable(e)
    Droppables.add(e, {accept:'job_fav', hoverclass:'drop_job', onDrop:e.onDrop.bind(e)})
    Droppables.add("fav_head", {accept:'fav', hoverclass:'trash_over', onDrop:e.onTrashDrop.bind(e)})
    e.style.height = ($('content').scrollHeight-120)+'px'
    if($$('#favourites li').length != 0)$('favourite_drop').hide()
  },
  login_opened:function(opened){
    if(opened)this.style.height = ((1*this.style.height.split('px')[0]) - 216)+'px'
    else this.style.height = ((1*this.style.height.split('px')[0]) + 216)+'px'
  },
  set_sortable:function(e){
    e = this
    Sortable.create(e, {handle:'handle', onUpdate:function(){new Ajax.Request('/favourites;order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(e)})}})
  },
  show_favourite_drop:function(){
    if($$('#favourites li').length == 0)$('favourite_drop').show()
  },
  open:function(){
    $('favourite_drop').hide()
    this.text_content.innerHTML = this.drop_text
    this.addClassName("drag_job")
  },
  close:function(){
    this.text_content.innerHTML = this.idle_text
    this.removeClassName("drag_job")
    this.show_favourite_drop()
  },
  update_cookie:function(){
    Cookie.set('favs',$$('#favourites li').pluck('record_id').join(','),4)
  },
  onDrop:function(el){
    this.add(el.record_id)
  },
  create_thumb:function(id){
    var new_elem = document.createElement('li')
    this.appendChild(new_elem)
    new_elem.id = 'favourite_'+id
    new_elem.innerHTML = '<div class="handle"></div>'+this.loading_text
    new_elem.record_id = id
    new_elem.className = 'favourite_progress'
    return new_elem
  },
  add:function(id){
    var resident_elem = $$('#favourites li').find(function(e){ return e.record_id == id})
    if(resident_elem){
        resident_elem.visualEffect('highlight', {startcolor:'#ff9999'})
    }else{
      $('favourite_drop').hide()
      this.new_elem = this.create_thumb(id)
      this.update_cookie()
      Sortable.destroy(this)
      new Ajax.Request('/favourites/new', {
        onLoading:function(){
          this.text_content.innerHTML = this.loading_text
          this.set_sortable.bind(this)(this)
        }.bind(this),
        onComplete:function(){
          this.text_content.innerHTML = this.idle_text
          this.set_sortable.bind(this)(this)
          this.delay(100,function(){ this.update_cookie() })
        }.bind(this),
        onFailure:function(){this.new_elem.remove()}.bind(this),
        asynchronous:true, evalScripts:true, parameters:"job_id=" + id
      })
    }
  },
  // TRASH
  // ----------------------------
  tarsh_loading_queue:0,
  start_tarsh_loading:function(){
    $('fav_head').addClassName("trash_progress")
    this.tarsh_loading_queue++
  },
  finish_trash_loading:function(){
    this.tarsh_loading_queue--
    if(this.tarsh_loading_queue==0){
      $('fav_head').removeClassName("trash_progress")
      $("fav_head").removeClassName("trash")
      this.text_content.innerHTML = this.idle_text
      this.show_favourite_drop()
    }
  },
  onTrashDrop:function(el){
    el.remove()
    Sortable.destroy(this)
    this.update_cookie()
    this.start_tarsh_loading()
    new Ajax.Request('/favourites/'+el.record_id, {
      onLoading:function(){
        this.text_content.innerHTML = this.deleting_text
        this.set_sortable.bind(this)(this)
      }.bind(this),
      onFailiure:function(){this.show()}.bind(el),
      onComplete:this.finish_trash_loading.bind(this),
      asynchronous:true, evalScripts:true, method:'delete'
    })
  }
}
var FavouriteItem = Class.create()
FavouriteItem.prototype = {
  initialize:function(e){
    e.record_id = e.id.split('_')[1]
    Object.extend(e,this)
    if($(e.id+'_handle')){
      $(e.id+'_handle').observe('mousedown',function(){
        $("fav_head").addClassName("trash")
      })
      $(e.id+'_handle').observe('mouseup',function(){
        $("fav_head").removeClassName("trash")
      })
    }
  }
}

var Rejection = Class.create()
Rejection.prototype = {
  initialize:function(e,options){
    e.texts = options.content
    Object.extend(e,this)
    e.langs = $('lang_select')
    e.rejections = $('rejeciton_select')
    e.langs.observe('change',function(){
      var new_lang = this.getValue()
      var opts = $A(e.rejections.options).each(function(opt,i){
        opt.innerHTML = e.texts[i][new_lang].title
      })
    })
    $('insert_rejection').observe('click',function(){
      e.disapprove_reason.value = e.texts[e.rejections.selectedIndex][e.langs.getValue()].text
      e.disapprove_reason.focus()
      return false
    })
  }
}

var Login = Class.create()
Login.prototype = {
  collapse:function(){
    $(this.form).visualEffect('fade',{duration:0.4, afterFinish:function(){if(this.favourites)this.favourites.login_opened(0)}.bind(this)})
    this.removeClassName('menu_title_login_open')
  },
  restore:function(){
    if(this.favourites)this.favourites.login_opened(1)
    if(window.khtml)this.form.show()
    else this.form.visualEffect('appear',{duration:0.4, afterFinish:function(){ $('login_username').focus() }})
    this.addClassName('menu_title_login_open') 
  },
  initialize:function(e,form,options){
    Object.extend(e,this)
    e = $(e)
    
    e.form = form
    e.observe('click',function(){
      if(this.form.visible())this.collapse()
      else this.restore()
      return false
    }.bind(e))
    e.favourites = $('favourites')
    $('forgotten_password').observe('click',function(){
      login_or_email = window.prompt(options.forgotten_text,'')
      if(login_or_email)new Ajax.Request('/users;forgotten', {method:'get', asynchronous:true, evalScripts:true, parameters:"login_or_email=" + login_or_email})
      return false
    }.bind(e))
  }
}
