$(function($) {
  
  //--------------------------------------------------------------------------
  // TABS (Homepage)
  //--------------------------------------------------------------------------
  $.tabs = function() {
  }
  
  $.extend($.tabs, {
        
    tab_names: [],
    
    init: function(ids){
      tab_names = ids
      this.deactivate_all();
      this.activate(ids[0])
    },
    
    activate: function(element){
      this.deactivate_all();
      $("#"+element+"_tab").addClass('active')
      $("#"+element+"_content").show();      
    },
    
    deactivate_all: function(){
      for(var i=0; i<tab_names.length; i++)
      {
        $("#"+tab_names[i]+"_tab").removeClass('active')
        $("#"+tab_names[i]+"_content").hide(); 
      }
    }
  })

  //--------------------------------------------------------------------------
  // TABS (Maps)
  //--------------------------------------------------------------------------
  $.tabsmaps = function() {
  }
  
  $.extend($.tabsmaps, {
        
    tab_names: [],
    
    init: function(ids){
      tab_names = ids
      this.deactivate_all();
      this.activate(ids[0])
    },
    
    activate: function(element){
      this.deactivate_all();
      $("#"+element+"_tab").addClass('active')
    },
    
    deactivate_all: function(){
      for(var i=0; i<tab_names.length; i++)
      {
        $("#"+tab_names[i]+"_tab").removeClass('active')
      }
    }
  })

  //--------------------------------------------------------------------------
  // More Dropdown
  //--------------------------------------------------------------------------

  $.fn.expand_collapse_link = function() {
    return this.each(function(){
      $.listing.init($(this));
    });
  }

  $.listing = function() {
  }

  $.extend($.listing, {

    init: function(element){
      element.data('collapsed', false);

      element.find('.collapse').bind("click", function(){
        element.find('.expanded').hide();
        element.find('.collapsed').show();
        element.data('collapsed', true);
      })

      element.find('.expand').bind("click", function(){
        element.find('.expanded').show();
        element.find('.collapsed').hide();
        element.data('collapsed', false);
      })

      return this;
    }
  })
  
  //--------------------------------------------------------------------------
  // Listing
  //--------------------------------------------------------------------------
  $.fn.expand_collapse_link = function() {
    return this.each(function(){
      $.listing.init($(this));
    });
  }
  
  $.listing = function() {
  }
  
  $.extend($.listing, {
        
    init: function(element){
      element.data('collapsed', false);
  
      element.find('.collapse').bind("click", function(){
        element.find('.expanded').hide();
        element.find('.collapsed').show();
        element.data('collapsed', true);
      })

      element.find('.expand').bind("click", function(){
        element.find('.expanded').show();
        element.find('.collapsed').hide();
        element.data('collapsed', false);
      })
      
      return this;
    }
  })
  
  //--------------------------------------------------------------------------
  // Favorite
  //--------------------------------------------------------------------------
  
  $.fn.favorite_link = function() {
    return this.each(function(element){
      $this = $(this)
      var fav = $.favorite.init($this);
      fav.write_html();
    });
  }
  
  $.favorite = function(element) {
  }
  
  $.favorite.link = {
  
    add: function(id) {
      $this = $("#listing_"+id)
      var fav = $.favorite.init($this);
      fav.add();
      fav.write_html();
    },
  
    remove: function(id) {
      $this = $("#listing_"+id)
      var fav = $.favorite.init($this);
      fav.remove();
      fav.write_html();
    }
  }
  
  /*
  * Public, $.favorite methods
  */

  $.extend($.favorite, {
    
    id: null,
    element: null,
    
    init: function(elem){
      element = elem;
      id = element.attr("id").split('_')[1]
      return this;
    },
    
    write_html: function() {
      if(this.is_favorite() == false)
      {
        element.find('.favorite').html("<a href='#' onclick='$.favorite.link.add("+id+");return false;'>Add To Favorites</a>");
      }else{
        element.find('.favorite').html("<a href='#' onclick='$.favorite.link.remove("+id+");return false;'>Remove From Favorites</a>");
      }
    },
    
    update_count: function(num)
    {
      var current_count = Number($("#favorite_count").html());
      $("#favorite_count").html(current_count + num)
    },
    
    remove: function() {
      this.update_count(-1)
      var cookie = $.cookie('_rennie_favorites');
      if(cookie != undefined){
        var new_ids = []
        var ids = cookie.split(',');
        for(var i=0; i<ids.length; i++)
        {
          if(ids[i] != id)
          {
            new_ids.push(ids[i])
          }
        }
        $.cookie('_rennie_favorites', new_ids.join(","),{ path: "/" });
        // If we are on the favorites page, hide this.
        if($('body#favorites').length > 0){element.hide()};
      }
    },
    
    add: function() {
      this.update_count(1)
      var cookie = $.cookie('_rennie_favorites');      
      if(cookie != undefined){
        var ids = cookie.split(',');
        ids.push(id)
      }else{
        var ids = [id]
      }
      $.cookie('_rennie_favorites', ids.join(","),{ path: "/" });
    },
    
    is_favorite: function()
    {
      var cookie = $.cookie('_rennie_favorites');
      if(cookie != undefined){
        var ids = cookie.split(',')
        return ($.inArray(id, ids) != -1) ? true : false;
      }else{
        return false
      }
    }
  })  
  
  $('.controls,.controls .links').css('cursor', 'pointer')

  $('.listing.wrapper').mouseenter(function() {
    $(this).find('.links').show();
    return false;
  });

  $('.listing.wrapper').mouseleave(function() {
    $(this).find('.links').hide()
    return false;
  });
  
  $('.mainnav .more').mouseenter(function(){
    $('ul#dropdown_more').show();
    $(this).css({'background':'#2670b4'});
  })
  $('.mainnav .more').mouseleave(function(){
    $("ul#dropdown_more").hide();
    $(this).css({'background':'none'});
  })

  $('.subnav .more').mouseenter(function(){
    $('ul#resources_more').show();
    $(this).css({'background':'#2670b4'});
  })
  $('.subnav .more').mouseleave(function(){
    $("ul#resources_more").hide();
    $(this).css({'background':'none'});
  })
   
})


$(function(){
  $('#filter_sumbit').hide();
  $('.listing').favorite_link();
  $('.listing').expand_collapse_link();
  $('.expand_listings').click(function(){
    $('.listing').find('.expanded').show();
    $('.listing').find('.collapsed').hide();
    $('.listing').data('collapsed', false);
    return false;
  })
  $('.collapse_listings').click(function(){
    $('.listing').find('.collapsed').show();
    $('.listing').find('.expanded').hide();
    $('.listing').data('collapsed', true);
    return false;
  })
});

// TODO - namespace this under Utils
function current_year(){
  var d = new Date();
  return d.getFullYear();
}

function to_currency(num)
{
  var n = num.toString()
  var count = 0;
  var s = [];
  for(var i =(n.length-1); i >= 0; i--)
  {
    s.unshift(n.charAt(i));
    count += 1;
    if(count%3==0 && i != 0)
    {
      s.unshift(",");
    }
  }
  s.unshift("$");
  s = s.join("");
  return s
}
