var first_load = true;

function callback(hash)
{
  var url = "";
  // hash doesn't contain the first # character.
  if(hash) {
    url = unparse_params(hash);
    if(first_load){
  	  /*document.location.href=url; // tidy up the url if its a paste link, for example
      return;*/ 
      // decided not to do this in the end
  	}
  } else if(!first_load) {
    url = document.location.href; 
  }
  
  if(url){

      $("#podcast_loading").stop(true);
  	  $("#podcast_loading").fadeIn(500);
  	  $("#podcast_list").stop(true);
  	  $("#podcast_list").animate({opacity: 0.5}, 500);
    	$.get(url, {ajax:1}, function(data, textStatus){
    	   if(textStatus == "success"){
    	     pageTracker._trackPageview(); // track page view on google analytics
    	   }
    	   $("#podcast_list").html(data.podcasts);
    	   $("#podcast_page_links").html(data.page);
    	   $("#podcast_order_links").html(data.order);
    	   do_ajax_clicks();
    	   $("#podcast_list").stop(true);
    	   $("#podcast_list").animate({opacity: 1.0}, 500);
	   //$("#podcast_list").css("opacity", 500);
         $("#podcast_loading").stop(true);
    	   $("#podcast_loading").fadeOut(500);
    	
    	}, "json");
    
  }
  
  first_load = false;
  
}

function unparse_params(param_str){

  var params = param_str.match(/([a-z]*)\|([0-9]*)/);
  if(params[1] == null || params[1] == "") params[1] = "recent";
  if(params[2] == null || params[2] == "") params[2] = 1;
  
  return "?o="+params[1]+"&p="+params[2];
}

function parse_params(hash){

  var params = hash.match(/\?o=([a-z]*)(&p=([0-9]*))?/);
  if(params[1] == null || params[1] == "") params[1] = "recent";
  if(params[3] == null || params[3] == "") params[3] = 1;
  return params[1]+"|"+params[3];
}


function do_ajax_clicks(){

  /*if($.browser.msie){
    // don't use ajax for ie - its slooooooow
  } else {*/
    $("a[rel='history']").unbind();
    $("a[rel='history']").click(function(){
      var hash = this.href.replace(/^.*#/, '');	
      $.history.load(parse_params(hash));
      return false;
    });
  //}
  
  $("a.podcast_more").click(function(){
  
    var m = $(this).attr("id").match(/more_(.*)/);
    var id = m[1];
    
    var hidden = false;
    var link = $(this);
    
    /*$(".prog_"+id+".podcast_item_hidden").each(function(){
      $(this).removeClass("podcast_item_hidden");
      $(this).addClass("podcast_item_shown");
      hidden = true;
      $(link).html("Hide...");
    });
    if(!hidden){
      var count=0;
      $(".prog_"+id+".podcast_item_shown").each(function(){
        $(this).removeClass("podcast_item_shown");
        $(this).addClass("podcast_item_hidden");
        count++;      
      });
      $(link).html(count+" more...");
    }*/
    var more_items = $(this).parents(".podcast_item").next(".podcast_more_items");
    if(more_items.css("display") == "none"){
      more_items.slideDown();
      $(this).html("Hide...");
    } else {
      more_items.slideUp();
      $(this).html((more_items.find(".podcast_item").length)+" more...");
    }
  
    return false;
  
  });  

}

$(document).ready(function(){

  $.history.init(callback);
  do_ajax_clicks();

});
