$(function() {

/* tooltips */
// $('.tooltip').tipsy({gravity: $.fn.tipsy.autoNS, fade: true});
    
$('#button-close-result').click(function() {
  $('#result').hide();
  return false;
});

$('#button-show-form-multiple').click(function() {
  $('#form-multiple').show();
  $('#url-multiple').focus();
  $('#result').hide();
  return false;
});

$('#button-close-form-multiple').click(function() {
  $('#form-multiple').hide();
  return false;
});

function trim(s, c) {
	return ltrim(rtrim(s, c), c);
}
 
function ltrim(s, c) {
	c = c || "\\s";
	return s.replace(new RegExp("^[" + c + "]+", "g"), "");
}
 
function rtrim(s, c) {
	c = c || "\\s";
	return s.replace(new RegExp("[" + c + "]+$", "g"), "");
}

$('#form-statistic').submit(function() {
  
  var url = $.trim($('#short-url').val());
  
  var matches = url.match(/^http:\/\/i2h\.de\/([a-z0-9+-].*)/i);

  if(matches && matches[1]) {
    var target = matches[1];
  }
  else {
    var matches = url.match(/^i2h\.de\/([a-z0-9+-].*)/i);

    if(matches && matches[1]) {
      var target = matches[1];
    }
  }
  
  if(target) {
    location.href = 'http://i2h.de/' + rtrim(target, '+') + '+';
  }

  return false;
});

$('#faq-button-submit').click(function() {
  var question = $('#faq-question');
  if(question.val().length > 0) {
    $.get('/ajax.php?method=faq-add&link_id=1&question=' + question.val());
    alert('Danke für deine Frage!');
    question.val('');
  }
  else {
    alert('Bitte gib eine Frage an!');
    question.focus();
  }
});

$('#load-visitor-domain').click(function() {
  $('#visitor-domain').load('/ajax.php?method=load-visitor-domain&link_id=' + this.rel);
  return false;
});

$('#load-visitor-country').click(function() {
  $('#visitor-country').load('/ajax.php?method=load-visitor-country&link_id=' + this.rel);
  return false;
});

$('#load-links-from-domain').click(function() {
  $('#visitor-from-domain').load('/ajax.php?method=load-links-from-domain&domain=' + this.rel);
  return false;
});

$('#load-links-to-domain').click(function() {
  $('#visitor-to-domain').load('/ajax.php?method=load-links-to-domain&domain=' + this.rel);
  return false;
});

/* mentions */
if(window.location.pathname.match(/\+$/)) {
  $.getJSON('http://search.twitter.com/search.json?callback=?&rpp=100&q=filter%3Alinks+"' + escape(location.href.substring(0, location.href.length -1))+'"',
    function(data) {
      if(data && data.results && data.results.length > 0) {
        var result = '';
      	for(var k=0; k<data.results.length; k++) {
          with(data.results[k]) {
            result += '<tr><td valign="top"><img width="48" style="padding:2px;border:1px solid #E0E0E0;" src="'+profile_image_url+'" /></td><td valign="top">' + text + '<br />Von <a href="http://twitter.com/' + from_user+'" target="_blank">' + from_user + '</a> am <a href="http://twitter.com/' + from_user+'/statuses/' + id + '" target="_blank">' + created_at.substr(5, 20) + '</a></td></tr>';
          }
        }
        if(result.length > 0) {
          result = '<table cellspacing="5" cellpading="4" border="0"><thead><tr><th colspan="2"><h2>Tweets über diesen Link</h2></th></tr></thead><tbody>' + result + '</tbody></table>'
          $('#mentions').html(result);
        }
      }
  });
}

/* $('table tbody tr td, table tbody tr th').css({'border-bottom':'1px dashed #333', 'padding':'5px 0 5px 0'});
$('tbody tr').hover(function(){
  $(this).addClass('hover');
},
function(){
  $(this).removeClass('hover');
});
*/
});