$(document).ready(function(){
  
  //
  // Show post form once topic title is clicked
  //
  // $(".post-form .form").hide();
  // $(".post-form.opened .form").show();
  // function populateTopic(selector, defvalue) {
  //   $(selector).each(function() {
  //     if($.trim(this.value) == "") {
  //       this.value = defvalue;
  //     }
  //   });
  //   $(selector).focus(function() {
  //     if(this.value == defvalue) {
  //       this.value = "";
  //       // $("input#s").removeClass("inactive");
  //       // $("input#s").addClass("active");
  //       $(".post-form .topic input").addClass("active");
  //       $(".post-form .form").slideDown();
  //     }
  //   });
  // }
  // populateTopic('.post-form .topic input', 'Click here to get started...');
  
  
  $(".new-topic .post-form").hide();
  $(".new-topic .show-form").click(function () {
    // $(".new-topic .show-form").fadeOut(300);
    // slideUp(150);
    $(".new-topic").addClass("active");
    $(".new-topic .show-form").animate({opacity: 'hide', height: 'hide'}, '100'); 
    $(".new-topic .post-form").animate({opacity: 'show', height: 'show'}, '100'); 
    // slideDown(250);
    // .fadeIn(1000);
    // return false;
    // $(this).unbind('keydown', 'esc', fn);
  });
  

  
  
  //
  //  Search Box
  //  NOTE:  This is the same code as above, but targeting a different element
  //  Would be better if this were a single function that hides and shows different text
  //
  //	http://docs.jquery.com/Tutorials:5_Quick_jQuery_Tips
  //
	function populateElement(selector, defvalue) {
	  $(selector).each(function() {
	    if($.trim(this.value) == "") {
	      this.value = defvalue;
	    }
	  });
	  $(selector).focus(function() {
      if(this.value == defvalue) {
        this.value = "";
			  $("input#s").removeClass("inactive");
        $("input#s").addClass("active");
	    }
	  });
	  $(selector).blur(function() {
      if($.trim(this.value) == "") {
        this.value = defvalue;
				$("input#s").removeClass("active");
				$("input#s").addClass("inactive");
      }
    });
  }
  populateElement('input#s', 'Search the forums...');
  
  //
  // Changes the ugly timestamps into friendly ones
  //
  
  //function parse_times() {
    var seconds;
    $("span.time_since_in_seconds").each(function (i,el) {
      seconds = $(el).html();
      $(el).parent().text(friendly_time(seconds));
    });
  //}
  
  
  //
  //  Toggle user profile box (needs lots of dev)
  //  NOTE:  Image could use parallax scrolling, and popup needs to spawn close to the link that calls it
  //
  // $(".show-profile").click(function () {
  //   $(".popup.profile").toggle();
  //   return false;
  // });
  //
  //  http://www.fullimpact.net/blog/index.php/2008/05/09/jquery-bubble-popup/
  //
  $("a.show-profile").bind("click", function(e) {
    
    var usr = this.className.split(' ')[1];
    popup = $("#"+usr+"-popup");
    
    $(".popup.profile").animate({opacity: 'hide'}, '5'); 
    
    popup.css({
            left:0,//$(this).offset().left,
            top:$(this).offset().top-popup.height()
        // }).show();
        }).animate({opacity: 'show'}, '25');
    e.stopPropagation(); // Stops the following click function from being executed
    $(document).one("click", function(f) {
        // $(".popup.profile").hide();
        $(".popup.profile").animate({opacity: 'hide'}, '25'); 
    });
    return false;
  });
  
  $("ul.menu a").bind("click", function(e) {
    $(".popup.profile").animate({opacity: 'hide'}, '25');
  });
  
  
  
  //
  //  Makes AJAX request to set instant subscription notifications
  //
  
  $("input.instant").bind("click", function(e) {
    var s_id = parseInt(this.parentNode.id);
    if ($(this).attr('checked')) {
      $.get("/forums/subscriptions/"+s_id+"/enable_instant")
    }
    else {
      $.get("/forums/subscriptions/"+s_id+"/disable_instant")
    };
  });
  
  //
  // Accordion for discovering topics
  //
  
  $("#newest").accordion({ autoHeight: false, active: Math.floor(Math.random()*3) });
  
});


function friendly_time(seconds) {
  seconds = parseInt(seconds);
  if(seconds < 60)
  {
    return "seconds ago";
  }
  else if(seconds < 3600){
    return parseInt(seconds/60)+" minutes ago";
  }
  else if(seconds < 7200) {
    return "1 hour ago"
  }
  else if(seconds < 86400) {
    return parseInt(seconds/3600)+" hours ago";
  }
  else if(seconds< 172800) {
    return "1 day ago"
  }
  else if(seconds < 2592000){
    return parseInt(seconds/86400)+" days ago";
  }
  else if(seconds < 5184000) {
    return "1 month ago"
  }
  else {
    return parseInt(seconds/2592000)+" months ago";
  }
}