Cufon.replace('.logo span, h1, h2, h3, h4 , h5, h6, .learnmore, .fresh_button, .comment-reply-link, .slide_button, .page_title .bold p');
//Cufon.now();
// analytics
var google_analytics = $('meta[name=google-analytics]').attr('content');
var mixpanel         = $('meta[name=mixpanel]').attr('content');
var trackable        = true; //$('meta[name=trackable]').attr('content');
var hostname         = window.location.hostname.replace("www.","").toLowerCase();
var commentable      = window.location.pathname.split("/")[1].match(/(blog)/) != null;

if (trackable) {
  $.trackPage(google_analytics);
  $.mixpanel.setup(mixpanel);
}

$(document).ready(function() {
  Cufon.replace('.logo span, h1, h2, h3, h4 , h5, h6, .learnmore, .fresh_button, .comment-reply-link, .slide_button, .page_title .bold p');
  $("a").each(function(index, element) {
    var item    = $(element);
    var href    = item.attr("href").toLowerCase();
    var remote  = (href.indexOf("http://") !=- 1 && href.indexOf(hostname) == -1);
    if (remote) {
      item.addClass("external");
    } else {
      item.addClass("internal");
    }
  });
  
  $("a").click(function() {
    var element = $(this);
    var external = element.hasClass("external");
    var next = element.attr("href");
    if (!external) {
      next = "http://" + window.location.host + next;
    }
    var current = window.location.href;
    $.mixpanel.track("link", {next:next, current:current, external:external});
    $.trackEvent("link", "goto", next, current, external);
  });
  
  function githubRepo(element) {
    var element = $(element);
    var href    = element.attr("href");
    var match   = href.match(/^http:\/\/(?:www\.)?github\.com\/([^\/]+)\/([^\/]+)$/);
    if (!match)
      return;
    var user    = match[1];
    var repo    = match[2];
    if (!(user && repo))
      return;
    
    element.addClass("github-repository");
    
    var url = "http://github.com/api/v2/json/repos/show/" + user + "/" + repo + "?callback=?";
    
    $.getJSON(url, function(data) {
      var repo_data = data.repository;
      if (repo_data) {
        element.html("<span>" + element.html() + "</span><sup class='subtext'> (" + repo_data.watchers.toString() + " watchers)</sup>");
        $("a.github-repository").css("text-decoration", "none !important");
        $("a.github-repository sup.subtext").css("opacity", "0.5 !important");
        $("a.github-repository span").css("text-decoration", "underline !important");
      }
    });
  }
  
  // github links
  $(".entry-content a").each(function(index, element) {
    githubRepo($(element));
  });
  
  // slideshow
  if ($('.slideshow').get(0)) {
    $('.slideshow').after("<div id='nav'>").cycle({
      fx: 'fade',
      timeout: 3000,
      pager: '#nav',
      speed: 1000,
      pagerEvent: 'click',
      pauseOnPagerHover: true,
      cleartypeNoBg: true
    });
  } else {
  }
  
  $("#ss-form").ajaxForm({
    beforeSend: function() {
      return false;
    },
    success: function(responseText, statusText, xhr, $form) {
      alert("Thanks for filling it out!\n\nMaybe one day we'll make a chart of the responses.");
    }
  });
  
  $(".ss-form-entry").each(function() {
    var context = $(this);
    var required = $(".ss-required-asterisk", context).get(0) != null;
    if (required) {
      $("textarea, input, select", context).addClass("required");
    }
  });
  
  // construct jquery-validate.js validation rules
  var rules = {};      
  $(".ss-form-entry .required").each(function() {
    var element = $(this);
    var name = element.attr("name");
    rules[name] = "required";
  });
  // setup validation
  $("#ss-form").validate({
    rules: rules,
    errorPlacement: function(error, element) {
      var entry = element.parents(".ss-item");
      entry.addClass("errorbox-bad");
			error.insertAfter(entry.find(".ss-q-title"));
		}
  });
  
  $("a[rel='tag']").each(function(index, element) {
    element = $(element);
    var tag_count = parseInt(element.attr("data-count"));
    if (tag_count) {
      var font_size = parseInt(element.css("font-size"));
      element.css("font-size", font_size + (2 * tag_count)); 
    }
  });
  
  $("a.comment-count").css("visibility", "visible !important").disqusLink("viatropos");
  
  $('#disqus_thread').disqus({
    domain: "viatropos",
    title: document.title,
    message: $('meta[name=description]').attr('content'),
    developer: window.location.hostname == "localhost" ? 1 : 0,
    show_count: true,
    prettify: true,
    markdown: true,
    iframe_css: "http://viatropos.com/stylesheets/disqus.css",
    ready: function() {
      $("#dsq-new-post h3").text("Thoughts?");
      var title = $("#dsq-comments-title h3");
      var text = title.text();
      var number = parseInt(text.match(/(\d+)/)[1]);
      var message = "<span>These comments are <a href='http://daringfireball.net/projects/markdown/dingus' target='_blank'>Markdown compatible</a>!  Feel free to write in Markdown.</span>";
      $(".dsq-post-footer").prepend(message);
      Cufon.replace("#dsq-comments-title h3, #dsq-new-post h3, h3.dsq-h3-reactions");
      $(".dsq-post-edit").wrap("<span class='dsq-comment-footer-reply'></span>");
      $("#dsq-show-more-reactions .dsq-button-small").wrap("<span class='dsq-comment-footer-reply'></span>");
      var height = $("#disqus_thread").height();
      $("#disqus_thread").height(height).css({display:"block !important", position:"relative", top:0});
      $("#disqus_thread").height("auto");
      $("a.comment-count").css("visibility", "visible !important");
    },
    added: function(comments) {
      Cufon.replace("#dsq-comments-title h3, #dsq-new-post h3");
    }
  });
  
//  $("a.dsq-comment-count").disqusLinks("viatropos");
  
  $("pre").addClass("prettyprint");
  prettyPrint();
  
  $(".body a").hover(function() {
    $(this).stop().animate({opacity:.7}, 100);
  }, function() {
    $(this).stop().animate({opacity:1}, 100);
  });
  
  $("#select-a-post").change(function() {
    window.location.href = $(this).val();
  });
  
/*  
  $(window).click(function() {
    var url = "http://spreadsheets.google.com/a/viatropos.com/formResponse?formkey=dGdOSnNOamFMUjliMzBtWnpKQWQtaUE6MQ";
    $.ajax({
      url: url,
      method: "GET",
      data: {"entry.0.single":"Im a key!!!", "entry.1.single":"Im a value!!!"},
      success: function(data, success) {
      },
      error: function(data) {
      }
    })
    return false;
  })
*/  
});
