// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/* 05/01/09
 * William Madden, will.madden@silverpond.com.au
 */

$(document).ready(function(){
  
  
  $(".portfolio .selector a").click(function(e) {
    
    // Get the current object and it's parent item
    var $this = $(this);
    var $item = $this.parents('.item');
    
    // Get the portfolio container
    var $portfolio = $(".portfolio");
    
    // If there is no portfolio container, exit
    if( $portfolio == null )
    {
      return;
    }
    
    // Find the name of project we want to highlight, the corresponding element
    // and the current project
    var new_project_name = $this.text().toLowerCase();
    // Replace all spaces with dots
    while( true )
    {
      var old = new_project_name;
      new_project_name = new_project_name.replace(" ", "_");
      if( old == new_project_name )
      {
        break;
      }
    }
    
    var $new_project = $portfolio.children("." + new_project_name);
    var $current_project = $portfolio.children(".current.project")
    
    // If the new project matches nothing, abort
    if( $new_project.length == 0 )
    {
      return;
    }
    
    // If it matches more than one thing, pick the first one
    if( $new_project.length > 1 )
    {
      $new_project = $new_project.eq(0);
    }
    
    // If the new project is the current project, abort
    if( $current_project.hasClass(new_project_name) )
    {
      return;
    }
    
    
    
    // Now highlight the new project link, hide the old project and show the
    // new project
    
    
    
    // Highlight the subnav link
    $item.siblings('.current').removeClass('current');
    $item.addClass('current');
    
    // Hide the current project
    $current_project.slideUp("normal");
    $current_project.removeClass("current");
    
    // Display the new project
    $new_project.slideDown("normal", function() {
      // Then set this subnav element as the currently highlighted one
      $this.addClass("current");
      $new_project.addClass("current");
    });
    
    e.preventDefault();
  });
  
})