// Show/hide for the Climate Innovation Index page

$(function () {
      var tabs = $('.cii_nav>li');
      var tab_container = $('.cii_nav');
      var panels = $('.cii_home_main');
      var quotes = $('.cii_quote');
      var move_left = $('.quote_left a');
      var move_right = $('.quote_right a');
      var current_quote_index = 0;
      var current_panel_index = 0;
      var in_tab_container = false;
      
      move_left.click(
	  function (e) {
	      e.preventDefault();
	      change_quote(-1);
	  }
      );

      move_right.click(
	  function (e) {
	      e.preventDefault();
	      change_quote(1);
	  }
      );

      tab_container.hover(
	  function () {
	      in_tab_container = true;
	  },
	  function () {
	      in_tab_container = false;
	      change_panel(0);
	  }
      );

      var change_panel = function (new_panel_index) {
	  panels.eq(current_panel_index).hide();
	  panels.eq(new_panel_index).show();
	  current_panel_index = new_panel_index;
      };

      var panel_changer = function (new_panel_index) {
	  return function () {
	      change_panel(new_panel_index);
	  };
      };

      tabs.each(
	  function (i, tab) {
	      tab = $(tab);
	      tab.mouseover(panel_changer(i + 1));
	      //tab.mouseout(panel_changer(0));
	  }
      );	  

      var change_quote = function (offset) {
	  var new_quote_index = (current_quote_index + offset) % quotes.length;
	  if (new_quote_index < 0) {
	      new_quote_index += quotes.length;
	  }
	  quotes.eq(current_quote_index).hide();
	  quotes.eq(new_quote_index).show();
	  current_quote_index = new_quote_index;
      };

      var init = function () {
	  panels.slice(1).hide();
	  quotes.slice(1).hide();
      };

      init();
});
