function moving(e, that)
{
	var tooltip_w = $j(that).find('div.tooltip').width() / 2;
  var x = e.pageX - $j(that).offset().left;
	var mouse_x = x - tooltip_w;
	var tooltip_width = tooltip_w * 2;
	var limit = $j(that).width() - tooltip_width;

  if (mouse_x < 45) {
    mouse_x = 45;
  }
  if (mouse_x > limit) {
    mouse_x = limit;
  }

  $j(that).find('div.tooltip').css('left', mouse_x); // top: in css file
}

function hovering(e, that)
{
  var x = e.pageX;
  var y = e.pageY;
  var pop_w = $j(that).width() / 2;
  var pop_h = $j(that).height() + 2;
  $j(that).css({'left': x - pop_w, 'top': y - pop_h})
}

$j(document).ready(function() {
	$j('ul.listings li').hover(function(e) {
    moving(e, this)
    $j(this).find('div.tooltip').end().addClass('hover');
	}, function() {
		$j(this).removeClass('hover');
	}).bind("mousemove", function(e) {
    moving(e, this)
  });

	$j('ul.listings li div.tooltip').hover(function() {
		$j(this).parent().unbind('mousemove');
	}, function() {
		$j(this).parent().bind("mousemove", function(e) {
      moving(e, this)
    });
	});

  $j('a.tooltip').hover(function(e) {
    id = $j(this).attr('href')
    hovering(e, id)
    $j(id).show()
  }, function() {
    id = $j(this).attr('href')
    $j(id).hide()
  }).bind("mousemove", function(e) {
    id = $j(this).attr('href')
    hovering(e, id)
  })
  
  $j('a.flag').click(function(e) {
    y = e.pageY - $j('#flag_dialog').height() - 30
    x = e.pageX - $j('#flag_dialog').width() / 2
    $j('#flag_dialog').toggle().css({top: y, left: x})
  })
})

/*function changePurposePickerColor(color_class) {
  $('purpose_picker').className = "line " + color_class
}

$j(document).ready(function() {
	$j('div.video embed').attr({wmode: 'opaque', width: '100%'});
  $j('#posted-by').mouseover(function() {
    $j('div.user-line div.tooltip').show().end().mouseout(function() {
      $j('div.user-line div.tooltip').hide().mouseover(function(){
		    $j(this).show();
      });
    });
  });
	$j('#insert_image').click(function() {
		$j('#insert_image_box').show(); return false;
	});
  	$j('#insert_video').click(function() {
		$j('#insert_video_box').show(); return false;
	});
	$j('img.flag').click(function() {
		$j(this).next('span.flag-as').toggle();
	});
	
  initQMark();

  $j('li.schedule_list_item, div.friend-name').hover(
    function(e) {
      var x = e.pageX - this.offsetLeft;
      var y = e.pageY - this.offsetTop;
      var pop_w = $j(this).find('div.pop').width() / 2;
      var pop_h = $j(this).find('div.pop').height() + 5;
      $j(this).css('background-color','#fffaea');
      $j(this).find('div.pop').css({'left': x - pop_w, 'top': - pop_h}).show();
    }, function() { $j(this).find('div.pop').hide();
    $j(this).css('background-color','transparent');
 });
	
$j('div.zelocals-list ul li').hover(
    function(e) {
	 var users_online = document.getElementById('zelocals_online').offsetLeft;
      var x = e.pageX - users_online;
      var y = e.pageY - this.offsetTop;
      var pop_w = $j(this).find('div.pop').width() / 2;
      var pop_h = $j(this).find('div.pop').height() + 5;
      $j(this).css('background-color','#444');
      $j(this).find('div.pop').css({'left': x - pop_w, 'top': - pop_h}).show();
    }, function() { $j(this).find('div.pop').hide();
    $j(this).css('background-color','transparent');
 });	
	
});

function initQMark(){
	$j('div.q-mark').hover(
	    function(e) {
	      var x = e.pageX - this.offsetLeft;
	      var y = e.pageY - this.offsetTop;
	      var pop_w = $j(this).find('div.pop').width() / 2;
	      var pop_h = $j(this).find('div.pop').height() + 10;
	      $j(this).find('div.pop').css({'left': x - pop_w, 'top': - pop_h}).show();
	    }, function() {	$j(this).find('div.pop').hide(); }
	  );
}

function checkAddConditions(form_to_submit, check_listing_name) {
  list_title = $('list_title').value
  if(check_listing_name && list_title.empty()) {
    $('flash').update("<div class='flash error'><div>Please type in listing title first.</div></div>")
    flashEffects()
  } else {
    $('explicit_listing_title').value = list_title
    form_to_submit.onsubmit()
  }
}
*/