function isEmptyTab( element ) {
  return element.hasClass( "tab_field_empty_left") || element.hasClass( "tab_field_empty_right");
}

function selectPortfolioMenuTab(closed) {
  var link = jQuery("#tabbed_portfolio_link_" + (closed ? "closed" : "active"));
  link.click();
}
  
function selectPortfolioMenuItem(selectedTab) {
  if( isEmptyTab( selectedTab )) {
    // don't do anything if an empty tab is selected
    return;
  }
  
  var selectedSymbol = jQuery.trim(selectedTab.text());
  var associatedAccordionPane = selectedTab.parent().parent().next();
  var associatedContent = jQuery('#' + selectedSymbol);

  if (!associatedAccordionPane.is(':visible')) {
    //collapse any other visible accordion panes
    jQuery('div.accordion_pane:visible').slideUp('normal');
    //open selected accordion pane
    associatedAccordionPane.slideDown('normal');
  }
  
  // collapse sibling content
  associatedContent.siblings().each( function() {
    jQuery(this).fadeOut( 'fast');
  });
  // open my content
  jQuery('a.tab_tool.selected').toggleClass('selected');
  associatedContent.find('a.tab_tool').slice(0,1).toggleClass('selected');
  associatedContent.fadeIn( 'slow');
  
  // remove selected class from other tabs
  jQuery('li.tab_field').each( function() {
    otherTab = jQuery(this);
    jQuery.each([ "green", "red", "green_closed", "red_closed", "empty_left", "empty_right" ], function(i, name) {
      if( otherTab.hasClass( 'tab_field_selected_' + name )) {
        otherTab.removeClass( 'tab_field_selected_' + name );
      }
    });
  });

  // add selected class to selected tab
  jQuery.each([ "green", "red", "green_closed", "red_closed" ], function(i, name) {
    if ( selectedTab.hasClass( 'tab_field_' + name ) || selectedTab.hasClass( 'tab_field_' + name + "_animated" )) {
      selectedTab.addClass( 'tab_field_selected_' + name );
    }
  });

  // add selected class to adjacent empty tab, if there is one
  var adjacentTab = jQuery(selectedTab.siblings()[0]);
  if( isEmptyTab( adjacentTab )) {
    var adjacentSide = "right";
    if( selectedTab.hasClass( "tab_field_red")) {
      adjacentSide = "left";
    }
    adjacentTab.addClass( "tab_field_selected_empty_" + adjacentSide );
  }
}

function showInDynamicPlaceHolder(aTag){
  jQuery.ajax({
    url: aTag.attr("href"),
    complete: function(request){
      if(jQuery('.dynamic_place_holder').is(':visible')){
        jQuery( '.dynamic_place_holder:visible' ).hide(); 
        jQuery( '#dynamic_content_container' ).show();
      }
      jQuery('#dynamic_content').html(request.responseText);
    },
    error: function(request) {}
  });
}

function initMenus() {
  jQuery('li.tab_field').click( function() {
    var clicked = jQuery(this);
    if(!clicked.hasClass("disabled")){
      selectPortfolioMenuItem(clicked, null);
      showInDynamicPlaceHolder( clicked.find( "a" ) );
    }else{
      document.location.href = clicked.find( "a" ).attr( "href" );
    }
    return false;
  });
  
  jQuery(".tab_tool.remote-call").click(function(){
    showInDynamicPlaceHolder( jQuery(this) );
    jQuery('a.tab_tool.selected').toggleClass('selected'); 
    jQuery(this).toggleClass('selected')
    return false;
  });
}

function handleChange(e, ui) {
    var maxScroll = $(".scroller").attr("scrollHeight") -
                    $(".scroller").height();
    $(".scroller").animate({ scrollTop: - ui.value *
     (maxScroll / 100)}, 1000);
}

function handleSlide(e, ui) {
    var maxScroll = $(".scroller").attr("scrollHeight") -
                    $(".scroller").height();
    $(".scroller").attr({ scrollTop: - ui.value * 
      (maxScroll / 100) });
}