var origin = {
  contactPanels: function () {
    var singlePanelVisibleOnly = true;
    $(".panels .panel .area").hide();
    
    $(".panels h2 a").click(function () {
      if (singlePanelVisibleOnly) {
        if (!$(this).parents(".panel").hasClass("on")) {
          $(".panels .panel.on .area").slideUp("fast").parent(".panel").removeClass("on");
        }
        $(this).parents(".panel").find(".area").slideDown("fast").parents(".panel").addClass("on");
      }
      else {
        if ($(this).parents(".panel").hasClass("on")) {
          $(this).parents(".panel").find(".area").slideUp("fast").parents(".panel").removeClass("on");
        }
        else {
          $(this).parents(".panel").find(".area").slideDown("fast").parents(".panel").addClass("on");
        }
      }
      return false;
    });
    if (location.hash.substring(1) == "all") {
      $(".panels .panel").addClass("on").find(".area").show();
    }
    else if (location.hash.substring(1).length && $(".panel" + location.hash).length) {
      $(".panel" + location.hash).addClass("on").find(".area").show();
    } else {
      $(".panel:first").addClass("on").find(".area").show();
    }
  },

  // Used by make_tips_dynamic() to position and toggle tip
  //
  toggleTip: function(tip_id, speed) 
  {
    tip_button = jQuery("#"+tip_id);
    tip_content = jQuery("#"+tip_id+"_tip");
  
    if( tip_button.hasClass("dd_tip_show") ) {
      tip_button.html("Show tip");
      tip_button.removeClass("dd_tip_show");
      tip_content.fadeOut(speed);
    } else {
      // Hide all other tips
      //
      jQuery(".dd_tip").html("Show tip");
      jQuery(".dd_tip").removeClass("dd_tip_show");
      jQuery(".dd_tip_content").fadeOut(speed);
      
      // Position tip content next to tip button
      //
      var button_position = tip_button.position();
      var tip_left = button_position.left + 40;
      var tip_top = button_position.top - tip_content.outerHeight();
      
      tip_content.css({'left': tip_left + 'px', 'top': tip_top + 'px'});
      
      // Show this
      //
      tip_button.html("Hide tip");
      tip_button.addClass("dd_tip_show");
      tip_content.fadeIn(speed);
    }
  },
  
  // Make .dd_tip / .dd_tip_content pair into button and popup tip
  //
  make_tips_dynamic: function(speed)
  {
    $(".dd_tip_content").hide();
    // Add a close button to each tip content and make each one position:absolute
    //
    $(".dd_tip_content").each(function(){
      $(this).addClass('dd_tip_content_dynamic');
      var close_button = $('<a/>')
        .attr('href', '#')
        .addClass('dd_tip_close_button')
        .append('close')
        .click(function(event){
          var tip_id = $(this).closest(".dd_tip_content").attr('id').replace(/_tip/, '');
          origin.toggleTip(tip_id, speed);
          event.preventDefault();
        });
      $(this).prepend(close_button);
    });
    
    $(".dd_tip_content").draggable();
  
    $(".dd_tip").each( function() {
      $(this).show().click(function(event){
        origin.toggleTip($(this).attr("id"), speed);
      });
    });
  }

};

// Stop calls to console.log from breaking IE
if (typeof console == "undefined") {var console = {};} if (typeof console.log == "undefined") {console.log = function(){};}

