var Homepage = (function() {

  var input_product;

  // Init homepage stuff
  function init() {

    $(document).ready(function(){
      
      input_product = $("#rs_input_product");

      setSearchType($("#rs_search_type").val(), 0);
      //alert($("#search_type").val());

        

      $("input.location").focus(function()
      {
        if ($(this).val() == $(this)[0].title)
          {
            $(this).removeClass("defaultLocationTextActive");
            $(this).val("");
          }
      });
	
      $("input.location").blur(function()
      {
        if ($(this).val() == "")
          {
            $(this).addClass("defaultLocationTextActive");
            $(this).val($(this)[0].title);
          }
      });

      $("input.location").focus();
      $("input.location").blur(); 

    });
    
  }
  
  /* Public functions */

  function has_term(){
    var input_product_val = input_product.val();
    return ((input_product_val == "cd") || 
            (input_product_val == "special-cd") || 
            (input_product_val == "jumbo-cd") ||
            (input_product_val == "business-cd") ||  
            (input_product_val == "ira-cd"));
  }

  function toggle_term_field(speed){
    var term_field = $("#term_field");

    if (has_term()) {
      $("#term_field:hidden").show(speed);
    } else {
      $("#term_field:visible").hide(speed);
    }
  }

  function setSearchTypeTab(search_type) {
    setSearchType(search_type, 200);
    updateSearchTypeCookie(search_type);
  }

 

  // Set the product select dropdown with last value
  function initProductDropdown() {
    var product_val = input_product.val();
    var selected_option = $("div.product_dropdown div.elem[title='"+product_val+"']");

    var selected_text = selected_option.text();
    $(".product_dropdown .elem.selected").removeClass("selected highlighted");
    selected_option.addClass("selected highlighted");

    $(".product_dropdown .dropdown_inner").text(selected_text);

    //alert(selected_option.html());
    //alert(product_val);
    //alert(document.getElementById("product").value);
    toggle_term_field(0);
  }

  // Set the term select dropdown with last value
  function initTermDropdown() {
    var term_val = $("#rs_input_term").val();
    var selected_option = $(".term_dropdown div.elem[title='"+term_val+"']");

    var selected_text = selected_option.text();
    $(".term_dropdown .elem.selected").removeClass("selected highlighted");
    selected_option.addClass("selected highlighted");

    $(".term_dropdown .dropdown_inner").text(selected_text);

  }


  // Helper functions
  function setSearchType(search_type, speed) {	
    if (speed == null) {
      speed = 200;
    }

    $("#main-search-nav a").removeClass('selected');
    $("#main-search-nav a").addClass('inactive');
    $("#search_description span").hide();
    switch(search_type) {
    case 'local':
      // $("#search_description").hide(fast);
      //$("#search_description").text('Compare rates from local banks in your area');
      $("#search_description span.local").show()
        $("#location_field").show(speed);
      $("#rs_search_type").val('local');
      $("#main_search_nav_local").addClass('selected');
      $("#main_search_nav_local").removeClass('inactive');
      break;
    case 'online':

      //$("#search_description").hide(speed);
      //$("#search_description").text('Compare rates that you can apply for online');
      $("#search_description span.online").show()
        $("#location_field").hide(speed);
      $("#rs_search_type").val('online');
      $("#main_search_nav_online").addClass('selected');
      $("#main_search_nav_online").removeClass('inactive');
      break;
    case 'all':
      //$("#search_description").show(speed);
      //$("#search_description").text('Search for both online rates and local rates');
      $("#search_description span.all").show()
        $("#location_field").show(speed);
      $("#rs_search_type").val('all');
      $("#main_search_nav_all").addClass('selected');
      $("#main_search_nav_all").removeClass('inactive');
      break; 
    }
  

  }

  function updateSearchTypeCookie(search_type) {
  
    var prefs_cookie = JSONCookies.load('prefs');
    if (!prefs_cookie) {
      prefs_cookie = {};
    }
    prefs_cookie.search_type = search_type;
    JSONCookies.create('prefs', prefs_cookie, 7);
  
    //JSONCookies.update('prefs','search_type', search_type, 7);

  }

  return {
    init: init,
    has_term: has_term,
    toggle_term_field: toggle_term_field,
    setSearchTypeTab: setSearchTypeTab,
    initProductDropdown: initProductDropdown,
    initTermDropdown: initTermDropdown

  }
  

})();

