/*
 * Harpoon URL Management
 */

var HARPOON = {};
HARPOON.magicAssets = {};
HARPOON.magicAssets.easing = 'easeOutQuint';
HARPOON.magicAssets.itemWidth = 730;

(function($) {
  
  $(document).ready(function() {
    
    var selectors = [
      '#header ul.navi li a[rel!="external"]',
      '#header h1 a',
      '.item.director a'
    ];
    
    $('body').delegate( selectors.join(','), 'click', function(e) {
      e.preventDefault();

      ROUTER.navigate( ( Backbone.history._hasPushState ? '/restructure' : '' ) + $(this).attr('href'), true );

      HARPOON.navi.refresh();
    });
    
    $('a[rel="external"]')
      .attr('target', '_blank');

  });

})(jQuery);

(function($) {
  
  $(document).ready(function() {
    
    var navigation = new views['navigation']({el: '#header ul.navi'});
    navigation.render();

    HARPOON.navi.bind();
    HARPOON.navi.refresh();

  });


})(jQuery);


(function($) {
  
  /*
   * GF: Navi
   */

  HARPOON.navi = {};

  HARPOON.navi.directors = {};
  HARPOON.navi.directors.show = function( $base ) {
    $base.find('.tooltip')
      .stop(true,true)
      .css({'opacity':0,'marginTop': -10})
      .show()
      .animate({'opacity':1,'marginTop': 0}, 100, HARPOON.magicAssets.easing, function() {

        $(this)
          .removeClass('busy');

      });
  };

  HARPOON.navi.directors.hide = function( $base ) {
    $base.find('.tooltip')
      .stop(true,true)
      .animate({'opacity':0,'marginTop': -10}, 100, HARPOON.magicAssets.easing, function() {

        $(this)
          .css({'opacity':0,'marginTop': 0})
          .hide();

      });
  };

  HARPOON.navi.directors.test = function() {
    for (var i=1; i < 10; i++) {
      
      setTimeout(function() {
        HARPOON.navi.directors.show();
      }, i*1000);

      setTimeout(function() {
        HARPOON.navi.directors.hide();
      }, (i*1000)+500);
      
    };
  };

  HARPOON.navi.bind = function() {

    $('ul.navi .directors')
      .bind('mouseenter', function() {
        $(this).addClass('active');
        
        HARPOON.navi.directors.show( $(this) );
      })
      .bind('mouseleave', function() {
        $(this).removeClass('active');
        
        HARPOON.navi.directors.hide( $(this) );
      });
    
    $('ul.navi .directors').each(function() {
    
      var glyph_width = 8;
      var max_length = 0;
    
      $(this).find('.tooltip ul li a').each(function() {
        max_length = ( max_length < $(this).text().trim().length ? $(this).text().trim().length : max_length );
      });
      
      var width = max_length * glyph_width;
      var w_padding_width = width + 20;
      
      $(this).find('.tooltip')
        .width( w_padding_width )
        .css({
          'left': -1 * ( (w_padding_width/2) - 50 )
        });
    
      $(this).find('.tooltip ul').width( width );
    
    });
    
  };

  HARPOON.navi.refresh = function() {
    
    $('#header ul.navi a.active').removeClass('active');
    
    if( HARPOON.stage.activeDirector ) {
      $('#header ul.navi a[href*="' + HARPOON.stage.activeDirector.director + '"]').addClass('active');
    } else if( Backbone.history.getFragment() != '/' ) {
      $('#header ul.navi a[href$="' + Backbone.history.getFragment() + '"]').addClass('active');
    }

    $('#header ul.navi li.directors:has(li a.active) > a').last().addClass('active');
    
  };

})(jQuery);

(function($){
  
  /*
   * GF: Stage
   */

  HARPOON.stage = {};
  HARPOON.stage.show = function() {

    $('#body .stage')
      .show();

  };

  HARPOON.stage.hide = function() {

    $('#body .stage')
      .hide();
    
  };

  // HARPOON.stage.test = {};
  // HARPOON.stage.test.show_hide = function() {
  //   for (var i=1; i < 3; i++) {
      
  //     setTimeout(function() {
  //       HARPOON.stage.hide();
  //     }, i*1000);

  //     setTimeout(function() {
  //       HARPOON.stage.show();
  //     }, (i*1000)+500);
      
  //   };
  // };

  // HARPOON.stage.test.go = function() {
    
  //   for (var i=0; i < $('.stage .inside .item').length; i++) {
      
  //     setTimeout(function() {
  //       HARPOON.stage.item.go( '+1' );
  //     }, (i+1)*1000);
      
  //     setTimeout(function() {
  //       HARPOON.stage.item.go( '-1' );
  //     }, (i+$('.stage .inside .item').length+1)*1000);

  //   };
  // };

  HARPOON.stage.item = {};
  HARPOON.stage.item.current = function( elem ) {
    
    if( !HARPOON.stage.item._current && HARPOON.stage.item._current != 0 ) {
      var scrollLeft = $('.stage').scrollLeft();
      
      HARPOON.stage.item._current = Math.floor( scrollLeft / HARPOON.magicAssets.itemWidth );
    }
    
    return ( elem ? $('.stage .inside .item:eq(' + HARPOON.stage.item._current + ')') : HARPOON.stage.item._current );
    
  };

  HARPOON.stage.item.go = function(i) {

    // Check if helpers are being used
    
    if( typeof(i) == 'string' ) {
      if( i.substr(0,1) == '+' ) {
        i = HARPOON.stage.item.current() + parseInt( i.substr(1) );
      } else if( i.substr(0,1) == '-' ) {
        i = HARPOON.stage.item.current() - parseInt( i.substr(1) );
      }
    }
    
    // checks & balances
    
    var total_items = $('.stage .inside .item').length;
    
    if( i < 0 ) { i = 0; }
    if( i > (total_items-1) ) { i = total_items-1; }

    // Do we have anything to do?
    if( HARPOON.stage.item._current == i ) {
      return false;
    }
    
    // stop the video from playing
    HARPOON.videos.pause( HARPOON.stage.item.current(true) );
    
    // Animation

    $('.stage')
      .stop(true,true)
      .animate({'scrollLeft': HARPOON.magicAssets.itemWidth*i}, 300, HARPOON.magicAssets.easing);

    // Do the fading out
    
    HARPOON.stage.item.current(true)
      .removeClass('active')
      .find('.overlay')
        .stop(true,true)
        .css('display','block')
        .animate({'opacity':0.6}, 300, HARPOON.magicAssets.easing);
    
    HARPOON.stage.thumbs( HARPOON.stage.item.current(), false );
    
    HARPOON.stage.item._current = i;

    if( HARPOON.stage.item.current(true).find('.title-strip').html() ) {
      ROUTER.setTitle( HARPOON.stage.item.current(true).find('.title-strip').html() );
    }
    
    // Do the fading in
    
    HARPOON.stage.item.current(true)
      .addClass('active')
      .find('.overlay')
        .stop(true,true)
        .animate({'opacity':0}, 300, HARPOON.magicAssets.easing, function() {
          $(this)
            .css('display', 'none');
        });
    
    HARPOON.stage.thumbs( HARPOON.stage.item.current(), true );
    
    if( HARPOON.stage.activeDirector ) {
      var id = HARPOON.stage.item.current(true).attr('id');
      ROUTER.navigate( ROUTER.getDirectorUrl() + id );
    }
    
  };

  HARPOON.stage.registerItems = function( n, itemWidth ) {
    
    if( n ) {
      items = n;
    } else {
      items = $('.stage .inside .item').length;
    }
    
    $('.stage .inside')
      .width( items * (itemWidth || HARPOON.magicAssets.itemWidth) );
    
  };

  HARPOON.stage.thumbs = function( i, active ) {
    
    if( !i ) { i = HARPOON.stage.item.current(); };
    
    var $thumbs = $('#body .thumbs');
    
    $thumbs.find('a:eq(' + i + ') img')
      .stop(true,true)
      .animate({
        'opacity': ( active ? 1 : 0.6 )
      }, 300, HARPOON.magicAssets.easing)
      .each(function() {
      
        if( active ) {
          $(this).addClass('active');
        } else {
          $(this).removeClass('active');
        }
      
      });
    
    return $thumbs;
  };

  HARPOON.stage.background = function(url) {
    
    var $background = $('#body .background');
    
    $background
      .children()
      .remove();
    
    $('<img />')
      .hide()
      .load(function() {
        var src = $(this).attr('src');
        
        var $d = $('<div />')
          .appendTo( $background )
          .css({
            'position': 'absolute',
            
            'top': 0,
            'left': 0,
            
            'width': '100%',
            'height': '100%',
            
            'display': 'block',
            'opacity': 0,
            
            'background': 'url("' + src + '")'
          })
          .animate({
            'opacity': 1
          });
          
      })
      .attr('src', url)
      .appendTo( $background );
    
  };

  HARPOON.stage.bind = function() {
    
    $('#body .overlay a.previous')
      .unbind('click')
      .bind('click', function() {
        HARPOON.stage.item.go('-1');
      
        return false;
      });
    
    $('#body .overlay a.next')
      .unbind('click')
      .bind('click', function() {
        HARPOON.stage.item.go('+1');
      
        return false;
      });
    
    $(document)
      .unbind('keydown')
      .bind('keydown', 'left', function() {
        $('#body .overlay a.previous').trigger('click');
        
        return false;
      })
      .bind('keydown', 'right', function() {
        $('#body .overlay a.next').trigger('click');
        
        return false;
      });
    
    $('#body .stage .item')
      .unbind('click.center')
      .bind('click.center', function() {
        
        var $this = $(this);
        var _index = $('#body .stage .item').index( $this );
        
        HARPOON.stage.item.go( _index );
        
      });
    
    $('#body .stage .item.director')
      .unbind('click.center');

    $('#body .stage .item.director .director')
      .unbind('mouseenter')
      .bind('mouseenter', function() {

        var $this = $(this);
        
        $this
          .find('a span.name')
            .stop(true,true)
            .fadeTo(300,1,HARPOON.magicAssets.easing);

      })
      .unbind('mouseleave')
      .bind('mouseleave', function() {

        var $this = $(this);
        
        $this
          .find('a span.name')
            .stop(true,true)
            .fadeTo(300,0,HARPOON.magicAssets.easing);

      });
    
    // thumbs
    
    $('#body .thumbs a').each(function(n) {
      
      $(this)
        .unbind('click')
        .bind('click', function() {
          HARPOON.stage.item.go( n );
          return false;
        })
        
        .unbind('hover')
        .hover(function() {
          
          var $img = $(this).find('img');
          var _active = $img.hasClass('active');
          if( !_active ) {

            $img
              .stop(true,true)
              .fadeTo(0,0.8);
              
          }
          
        }, function() {
        
          var $img = $(this).find('img');
          var _active = $img.hasClass('active');
          if( !_active ) {
            $img
              .stop(true,true)
              .fadeTo(0,0.6);
          }
        
        });
      
    });
    
    // center the bio text
    
    HARPOON.stage.centerBio();
    
  };

  HARPOON.stage.onload = function() {
    HARPOON.stage.item.go( 0 );
  };

  HARPOON.stage.centerBio = function() {

    $('.item.text').each(function() {

      var $item = $(this);
      var $content = $item.find('.content');

      $item.css('padding-top', 0);

      $item
        .css({'paddingTop': ( $item.outerHeight() - $content.outerHeight() ) / 2 });

    });

  };

  HARPOON.stage.unload = function() {
    
    $('.stage .inside').fadeOut();
    $('#body > .title-strip').delay(500).fadeOut();
    $('.overlay').delay(500).fadeOut();
    $('.thumbs').delay(500).fadeOut();

    $('.background div').delay(800).fadeOut();
    
    $('#body').addClass('unload');
    
    $(document).unbind('keydown');
    
  };

  HARPOON.stage.reload = function() {
    
    $('.stage .inside').fadeIn();
    $('#body > .title-strip').delay(500).fadeIn();
    $('.overlay').delay(500).fadeIn();
    $('.thumbs').delay(500).fadeIn();
    
    $('#body').removeClass('unload');
    
  };

  HARPOON.stage.position = function() {
    
    $(window)
      .unbind('resize')
      .bind('resize', function() {
        
        var _top = ( $(window).height() / 2 ) - 266;
        if( _top <= 141 ) {
          $('#body').addClass('stick');
        } else {
          $('#body').removeClass('stick');
        }
        
        var max_height = 756;
        
        /*
        if( !$('#thumbs').is(':visible') ) {
          max_height = 691;
          $('#footer').addClass('no-thumbs');
        } else {
          $('#footer').removeClass('no-thumbs');
        }
        */
        
        var _height = $(window).height();
        if( _height <  max_height ) {
          $('#footer').addClass('stick');
        } else {
          $('#footer').removeClass('stick');
        }
        
      })
      .trigger('resize');
    
  };

  HARPOON.stage.rapidUnload  = function() {
    
    $('.stage .inside').fadeOut(0);
    $('#body > .title-strip').fadeOut(0);
    $('.overlay').fadeOut(0);
    $('.thumbs').fadeOut(0);
    
    $('#body').addClass('unload');
    
  };

})(jQuery);

(function($) {
  
  /*
   * GF: Videos
   */

  HARPOON.videos = {};
  HARPOON.videos.setup = function() {
    
    if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || navigator.userAgent.match(/iPad/i)) {
      
      $('.stage .item.video')
        .each(function() {
          var $elem = $(this);
          
          $elem
            .find('img.video-thumb,.loading')
              .remove();

          var $iframe_code = $elem.find('.vimeo-code');
          $elem.append( $iframe_code.val() );
          
        });
      
      $('.stage .item.video .tooltip').remove();
      
      return false;
      
    }
    
    $('.stage .item.video')
      .unbind('click.video')
      .bind('click.video', function() {
        
        var $this = $(this);
        var _index = $('.stage .item').index( $this );
        
        if( HARPOON.stage.item.current() == _index ) {
          
          HARPOON.videos.play( HARPOON.stage.item.current(true) );
          
        }
        
      })

      /*
      .bind('mousemove', function(e) {
        var offset = 10;
        
        var top = e.pageY - $(this).offset().top;
        var left = e.pageX - $(this).offset().left;

        var $tooltip = $(this).find('.tooltip');
        
        var max_left = $(this).width() - $tooltip.width() - (offset*2);
        var max_top = $(this).height() - $tooltip.height() - (offset*2);
        
        if( left > max_left ) {
          $tooltip.addClass('left');
        } else {
          $tooltip.removeClass('left');
        }
        
        if( top > max_top ) {
          $tooltip.addClass('top');
        } else {
          $tooltip.removeClass('top');
        }
        
        $tooltip
          .css({
            'top': ( top + offset ) + 'px',
            'left': ( left + offset ) + 'px'
          });
      })
      */
      
      .unbind('hover')
      .hover(function() {
        var $elem = $(this);
        var _index = $('.stage .item').index( $elem );
        
        if ( HARPOON.stage.item.current() == _index ) {

          $elem
            .addClass('hover')
            .find('.overlay')
              .stop(true,true)
              .fadeTo(300,0.2,HARPOON.magicAssets.easing);
          
          /*  
          setTimeout(function() {
            if( $elem.hasClass('hover') ) {
              $elem
                .find('.tooltip')
                  .fadeTo(300,1,HARPOON.magicAssets.easing);
            }
          }, 500);
          */

        } else {
        
          $elem
            .find('.overlay')
              .stop(true,true)
              .fadeTo(300,0.3,HARPOON.magicAssets.easing);
        
        }
          
      }, function() {
        var $elem = $(this);
        var _index = $('.stage .item').index( $elem );
        
        if ( HARPOON.stage.item.current() == _index ) {

          $elem
            .find('.overlay')
              .removeClass('hover')
              .stop(true,true)
              .fadeTo(1000,0,HARPOON.magicAssets.easing);
          
          /*
          $elem
            .find('.tooltip')
              .fadeTo(0,0);
          */

        } else {
        
          $elem
            .find('.overlay')
            .stop(true,true)
            .fadeTo(300,0.6,HARPOON.magicAssets.easing);
        
        }
            
      });
    
  };

  HARPOON.videos.play = function( elem ) {
    var $elem = elem;
    
    HARPOON.videos._video = $elem;
    
    if( $elem.hasClass('video') && !$elem.hasClass('play') ) {
      $elem
        .addClass('play');
      
      $elem
        .find('.loading')
          .stop(true,true)
          .fadeTo(300,1,HARPOON.magicAssets.easing);
      
      var $iframe_code = $elem.find('.vimeo-code');
      $elem.append( $iframe_code.val() );

      $iframe = $elem.find('iframe');

      $iframe.bind('load', function() {

        $elem
          .find('img.video-thumb')
            .add( $elem.find('.loading') )
            .delay(1500)
            .stop(true,true)
            .fadeTo(
              1500,
              0,
              HARPOON.magicAssets.easing,
              function() {
                $(this).hide();
              }
            );

      });

      _gaq.push(['_trackEvent', 'Play', 'Video Play', window.location.hash]);
    }
  };

  HARPOON.videos.pause = function( elem ) {
    var $elem = elem;
        
  /*
    $elem
      .removeClass('hover')
      .find('.tooltip')
        .stop(true,true)
        .fadeTo(0,0);
  */
    
    if( $elem.hasClass('video') && $elem.hasClass('play') ) {
      $elem.removeClass('play');
      
      var $iframe = $elem.find('iframe');
      
      if( $iframe.length > 0 ) {
        
        $iframe.remove();
        
        $elem
          .find('img.video-thumb')
            .stop(true,true)
            .fadeTo(0,1)
            .show();
          
      }
    }
  };

  HARPOON.videos.test = function() {
    
    $('.stage .item.video')
      .eq(2)
      .trigger('click')
      .trigger('click');
    
  };

})(jQuery);
