/** 
 *  jquery.columnview-1.1.1.js
 *  
 *  Created by Chris Yates on 2009-02-26. 
 *  http://christianyates.com
 *  Copyright 2009 Christian Yates and ASU Mars Space Flight Facility. All rights reserved.

 *  Modified by Megan MacMurray - Core Industries 2010-02-04
 *  http://core-industries.com/

 *  Tested with Firefox 3.x, Safari 3.x,4.x, Internet Explorer 6.x,7.x, 8
 *  Dual licensed under MIT and GPL.
 *
 */
 
(function($){
  $.fn.columnview = function(){

    // Hide original list
    $(this).hide();
    
    // Create new top container from top-level LI tags
    var top = $(this).children('li');
    var container = $('<div/>').addClass('containerobj').attr('id','cv'+Math.floor(Math.random()*10e10)).insertAfter(this);
    var topdiv = $('<div class="top"></div>').appendTo(container);
    if($.browser.msie) { $('.top').width('200px'); } // Cuz IE don't support auto width
    $.each(top,function(i,item){
      var topitem = $(':eq(0)',item).clone().data('sub',$(item).children('ul')).appendTo(topdiv);
      if($(topitem).data('sub').length) {
        $(topitem).addClass('hasChildMenu');
        if($.browser.safari){
          $(topitem).css({'margin-right':'15px'});    
        }
      }
    });

    
    // Event handling functions
    $('a').live("click",function(){
	
      var container = $(this).parents('.containerobj');
	 

      // Handle clicks
      var level = $('div',container).index($(this).parents('div'));
      // Remove blocks to the right in the tree, and 'deactivate' other links within the same level
      $('div:gt('+level+')',container).remove();
      $('div:eq('+level+') a',container).removeClass('active').removeClass('inpath');
      $('.active',container).addClass('inpath');
      $(this).addClass('active');
      if($(this).data('sub').children('li').length) {
        // Menu has children, so add another submenu
        submenu(container,this);
      } else {
        // No children, just show text
        // var title = $('<a/>').text();
        // var featurebox = $('<div/>').html(title).addClass('feature').appendTo(container);
        // // Set the width
        // var remainingspace = 0;
        // $.each($(container).children('div').slice(0,-1),function(i,item){
        //   remainingspace += $(item).width();
        // });
        // var fillwidth = $(container).width() - remainingspace;
        // $(featurebox).css({'top':0,'left':remainingspace}).width(fillwidth).show();    
      }
      return false;
    }); 

    // Keyboard navigation
    $('a',container).live('keydown',function(key){
      switch(key.which){
        case(37): //left
          $(this).parent().prev().children('.active').focus().click();
          break;
        case(38): //up
          $(this).prev().focus().click();
          break;
        case(39): //right
          if($(this).hasClass('hasChildMenu')){
            $(this).parent().next().children('a:first').focus().click();            
          }
          break;
        case(40): //down        
          $(this).next().focus().click();
          break;
        case(13): //enter
          $(this).dblclick();
          break;
      }
    });

  }; 
  
  // Generate deeper level menus
  function submenu(container,item){
    var leftPos = 0;
    $.each($(container).children('div'),function(i,mydiv){
      leftPos += $(mydiv).width();
    });
    var submenu = $('<div/>').css({'top':0,'left':leftPos}).appendTo(container);
    if($.browser.msie) { $(submenu).width('200px'); } // Cuz IE don't support auto width
    var subitems = $(item).data('sub').children('li');
    $.each(subitems,function(i,subitem){
      var subsubitem = $(':eq(0)',subitem).clone().data('sub',$(subitem).children('ul')).appendTo(submenu);
      if($(subsubitem).data('sub').length) {
        $(subsubitem).addClass('hasChildMenu');
        if($.browser.safari){
          $(subsubitem).css({'margin-right':'15px'});    
        }
      }
    });
  }
  

  
})(jQuery);