//CVS:       $Id: main.js,v 1.13 2012/02/22 17:19:13 cvsdevel Exp $
//Title:     main.js
//Version:   1.02
//Copyright: Copyright (c) 2012
//Author:    Donovan Mueller
//Company:   Rhino Internet

/**
 * Main JavaScript controller for the main Rhino Internet website.
 *
 * <p>
 * <b>Changelog:</b><pre>
 *  1.00  DDM 2012/01/12  Created.
 *  1.01  DDM 2012/02/13  Added client comp list functionality.
 *  1.02  DDM 2012/02/22  Added auto scrolling and hiding for Jobs page.
 * </pre>
 *
 * @author  DDM
 * @version 1.02
 */

;(function main($){$(function onDOMReady(){
   // Open external links in new windows
   $('a[rel=external]').attr('target', '_blank');
   
   
   /// Carousel
   var promoCarousel = $('#bd > .promos');
   promoCarousel.donutSlider({ frameSelector: '#bd > .promos'
                             , listSelector:  '.items'
                             , delay: 10000
                             , transEasing: 'easeOutQuad' });
   // Center position markers
   promoCarousel.children('.pos_markers').each(function eachPromoCarousel(){
      var $this    = $(this)
        , maxwidth = $this.offsetParent().outerWidth()
        , width    = 0;
      
      $this.children().each(function(){ width += $(this).outerWidth(true); });
      
      $this.css({ width: width + 'px'
                , right: 'auto'
                , left:  (width > maxwidth) ? 0 : (maxwidth/2 - width/2) });
   });


   /// Navigation animation
   var nav = $('#nav')
     , navlist = nav.children('ul');
  
   function showNav(event) {
      if (event && event.type === 'click') {
         showNav.clicked = true;
         
         if (navlist.position().left < -40) {
            event.preventDefault();
         }
      }
      clearTimeout(hideNav.timeout);
      nav.width(770);
      navlist.stop().animate({left: -40}, 600, 'easeOutQuint');
   }
   
   function hideNav(event) {
      if (event && event.type !== 'click') {
         if (showNav.clicked) return;
         hideNav.timeout = setTimeout(function(){ hideNav(false); }, 1300);
         return;
      }
      navlist.stop().animate( {left: -660}, 1200, 'easeOutQuint'
                            , function(){ nav.width(130); } );
      showNav.clicked = false;
   }
   
   hideNav.timeout = setTimeout(function() { hideNav(false); } , 700);
   
   navlist.find('a').click(function onNavLinkClick(){
      if (navlist.position().left < -40) {
         event.preventDefault();
         event.stopImmediatePropagation();
      }
   });
   nav.hover(showNav, hideNav);
   nav.click(function onNavClick(event){
      if (event.target.nodeName !== "NAV" && event.target.nodeName !== "UL") {
         return true;
      }
      
      if (nav.width() <= 130) {
         showNav(event);
      } 
      else {
         hideNav(event);
      }
   });


   /// Extranet Comp List
   $('table.comps tr.past').each(function eachCompsTable(){
      var $row   = $(this)
        , active = $row.prev()
        , next   = $row.next()
        , btn, past;
      
      // Mark it if its the last of a "past" set.
      if (next.hasClass('past') === false) {
         $row.addClass('last');
         next.addClass('after_past');
      }
      
      // Stop if it's just another past row
      if (active.hasClass('past')) return; // next
      
      active.addClass('has_past');
      $row.addClass('first');
      
      btn = $('<button class="toggle_past">Past</button>');
      // btn needs to an extra container to support absolute positioning 
      // relative to the current row.
      $('<div class="toggle_past_btnbox"></div>')
         .append(btn)
         .appendTo(active.children('td.posted'));
      
      past = active.nextUntil(':not(.past)');
      past.hide();
      btn.data('past_comps', past);
      
      btn.click(function togglePastComps(){
         var $btn = $(this)
           , past = $btn.data('past_comps');
         
         past.toggle();
         $btn.toggleClass('expanded');
      });
   });
   
   /// Jobs page
   var jobs = $('article.job');
   $('div.job_board > ul a').click(function onJobClick(event){
      event.preventDefault();
      var job = jobs.filter($(this).attr('href'));
      if ($.browser.msie && $.browser.version < 9) {
         // Occasionally formatting of jobs get screwed up in IE < 9 when using
         // the slide animations.
         jobs.not(job).fadeOut();
         job.not(':visible').fadeIn();
      }
      else {
         jobs.not(job).slideUp();
         job.not(':visible').slideDown();
      }
      setTimeout(function jumpToJob(){
         job.donutScroll(null, function updateURL() {
            window.location.hash = job.attr('id');
         });
      }, 600);
   });
}); /* on DOM ready */ })(jQuery);
