num = 0;
flashing = [];
focused = false;
tabMessage = '';
baseTitle = '';

function addComponent(component_name, id, component_type, plural_name, limit) {
  list_id = '#' + component_type + '_list'
  // check by id if component's been added already - thus it's renaming; delete it first
  $j('.cmp' + component_type + id).remove()
        
  if (component_name != null && $j(list_id + ' li').length < limit) {
    num = num + 1;
    if ($j(list_id).html().indexOf(component_name) == -1) {
          
      delete_js = '$j(\'.com' + num + '\').remove(); $j(\'#chk' + component_type + id + '\').removeAttr(\'checked\'); return false;';
      component_html = '<li class="cmp' + component_type + id + ' com' + num +  '"> <a class="delete_image com" onclick="' + delete_js + '"> <img src ="/images/ze-delete-tag.png"> </a>' + component_name + '</li>';
      $j(list_id).append(component_html);

      field_html = '<input class="cmp' + id + ' com' + num + '" type="hidden" name="'+ plural_name + '[]" value="' + id + '"></input>'
      $j('#listing_form').append(field_html);
    }
  }
}
  
function remove_on_click_outside(element)
{
  $j(document).click(function(e) {
    if ($j(e.target).parents().index(element) == - 1 && e.target != element)
      $j(element).remove();
  })
}
  
  
function hide_on_click_outside_if_visible(id)
{
  $j(document).click(function(e) {
    if ($j('#' + id + ':visible').length != 0) {
      element = $j('#' + id + ':visible')[0]
      if ($j(e.target).parents().index(element) == - 1 && e.target != element) {
        $j(element).hide();
      }
    }
  })
}

function flashEffects() {
  flash_element = $('flash')
  if(!flash_element.empty()) {
    flash_element.hide()
    new Effect.Appear('flash', { 
      duration: 2.0
    })
    setTimeout(function() { 
      new Effect.Fade('flash',{
        duration: 2.0
      })
    }, 8000);
  }
}

function toggleChatBody(chat_id) {
  chat_identifier = '#chat_' + chat_id
  $j(chat_identifier).removeClass('unread');
  
  $j(chat_identifier + ' .user-link').toggle()
  $j(chat_identifier + ' div.chat').toggle();
  $j(chat_identifier + ' .chat-form').toggle();
}

function addScrollToChat() {
  if ($j('#messages').height() >= 500 && !$j('#messages').parent().hasClass('conversation')) {
    $j('#messages').parent().addClass('conversation')
  }
}

function scrollDownChat(chat_id) {
  addScrollToChat()
  $j(chat_id).attr('scrollTop', function() { 
    return this.scrollHeight
  })
}

function pulsateNewChatMessage(chat_id) {
  window.setInterval(function() {
    new Effect.Highlight('user_' + chat_id, {
      startcolor: '#191919',
      endcolor: '#f88017'
    })
  }, 2000)
}

function initWindowFocus(){
  $j([window, document]).blur(function(){
    focused = false;
  }).focus(function(){
    focused = true;
  });
}

function initTabFlashing(){
  baseTitle = document.title;
  setTimeout("showTabMessage()", 1000);
}

function showTabMessage(){
  if(focused) tabMessage = '';
  if(tabMessage != ''){
    if(document.title == baseTitle){
      document.title = tabMessage; document.title;
    }else{
      document.title = baseTitle; document.title;
    }
  }else{
    document.title = baseTitle; document.title;
  }
  setTimeout("showTabMessage()", 1000);
}

function setTabMessage(m){
  tabMessage = m;
}

function initBlinking()
{
  window.setInterval(function(){
    $j(".blinking a").animate({
      opacity: 0
    }, {
      duration: 500
    }).animate({
      opacity: 1
    }, {
      duration: 500
    })
  }, 2000);
}

function initForms()
{
  $j('form').bind('submit', function() {
    $j(this).find('.unfocused').val('');
  })

  $j('#city_board_wrapper').click(function(e) {
    e.stopPropagation();
    return false
  })

  $j(document).click(function() {
    if ($j('#city_board').css('display') != 'none') {
      $j('#city_board').hide();
    }
  })
  
}

function facebook_share(u, t) {
  window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), "sharer", "'toolbar=0,status=0,width=626,height=436′")
  return false;
}

function reset_search_criteria() {
  $j('#refine_search :input[type="checkbox"]').each(function() {
    $j(this).attr('checked', false)
  })
  Custom.clearAll()
  $j('#refine_search select').each(function() { 
    this.value = -1; this.onchange()
  })
  $('search_form').onsubmit()
}

function hide_chat_list() {
  $j('.chat_list_item').hide();
  _top = $j('#zelocal_online').css('position') == 'fixed' ? window.innerHeight - 48 : -83;
  $j('#zelocal_online').css({
    top: _top
  });
}

function show_chat_list() {
  $j('.chat_list_item').show();
  _top = $j('#zelocal_online').css('position') == 'fixed' ? window.innerHeight - 48 : -83;
  $j('#zelocal_online').css({
    top: _top - $j('.chat_list_item').length * 48
  });
}

function toggle_chat_list() {
  if ($j('.chat_list_item:visible')[0]) {
    hide_chat_list()
  } else {
    show_chat_list()
  }
}

var footer_offset_left, chat_offset_left;
function visible_messenger(init)
{
  offset = window.pageYOffset + window.innerHeight
  footer_offset = $j('#footer').position().top

  _top = 0
  if ($j('.chat_list_item:visible').length > 0)
  {
    _top = $j('.chat_list_item').length * 48
  }
  
  if(footer_offset <= offset)
  {
    $j('#zelocal_online').css({position: 'absolute', top: -(83 + _top), left: chat_offset_left - footer_offset_left, right: null})
  }
  else
  {
    $j('#zelocal_online').css({position: 'fixed', top: window.innerHeight - (48 + _top), left: chat_offset_left})
  }
}
window.onscroll = function() { visible_messenger(false); }
window.onresize = function() { visible_messenger(false); }
