TF7.events.SPLASH_INTRO_COMPLETE = 'splashIntroComplete';

// ====================================================================
// @HomeSection plugin
// ====================================================================
(function($){
  
  var HomeSection = function(options, elem) {
    
    var $this = $(elem),
				$body = $('body'),
				$copyLines = $('#content p'),
				_badIE = $.browser.msie == true && $.browser.version < 9;
				
    // ----------------------------------------------------------------
    
    function _init() {
			
			debug.log("HomeSection :: initialized");
			
			// setup properties for build animation
			// var destX = 50;
			var destX = 0;
			$copyLines.css({
        visibility: 'hidden',
			  marginLeft: destX
			});

			$body.bind(TF7.events.SPLASH_INTRO_COMPLETE, _doHomeBuild);

		  $('#background-layers').homeBackground();
		
   		$.cookie('lastVisited', 'home');

 		}; // _init()
    	
		// ----------------------------------------------------------------
		
		function _doHomeBuild() {
			debug.log("HomeSection :: _doHomeBuild");
			
			if (TF7.isFirstVisit) {
				
				if(_badIE) {
				  
  				$.doTimeout( 250, function() {
            $copyLines.css({
              visibility: 'visible'
            });
    			  _homeBuildComplete();
  				});
				  
				} else {
				  
  				$.doTimeout( 250, function() {
            $copyLines.css({
              visibility: 'visible',
              opacity: 0
            }).each(function(i){
    				  var $copyLine = $(this);
    				  if(i < $copyLines.length - 1) {
    				    $copyLine.stop().animate({opacity: 1}, 1250, 'easeOutQuad')
    			    } else {
    			      $copyLine.stop().animate({opacity: 1}, 1250, 'easeOutQuad', _homeBuildComplete)
    			    }
    				});

  				});
				  
				}
				
				
			} else {
			  
			  $copyLines.css({
			    visibility: 'visible',
			    marginLeft: 0
			  });
				
				_homeBuildComplete();
			}
						
		};
		
		// ----------------------------------------------------------------
		
		function _homeBuildComplete() {
			TF7.navController.transitionIn();
			TF7.footerController.transitionIn();
		};
		
    _init();
    
    return {
      
    };
    
  };
  
  // ==================================================================
  
  $.fn.homeSection = function(options) {
    
    var pluginName = "HomeSection";
    
    options = $.extend({}, $.fn.homeSection.defaults, options);
    
    return this.each(function(){
      
      var $this = $(this);
      
      if($this.data(pluginName)) {
        return;
      }
      
      // No JavaScript plugin instance exists, so add it as data attribute.
      var pluginInstance = new HomeSection(options, this);
      $this.data(pluginName, pluginInstance);
      
    });
    
  };
  
  // ==================================================================
  
  $.fn.homeSection.defaults = {
    
  };
  
})(jQuery);


// ====================================================================
// @HomeBackground plugin
// ====================================================================
(function($){
  
  var HomeBackground = function(options, elem) {
    
    var $this = $(elem),
        $win = $(window),
				$body = $('body'),
        $bgImage = $('#background-image'),
				$htGrid = $('#ht-grid'),
				$splash247Holder = $('#splash-247'),
        $splash247 = $('#splash-247 img'),
				splash247Rect = null,
				screenRect = HEGA.rectangle(0, 0, $win.width(), $win.height()),
				_badIE = $.browser.msie == true && $.browser.version < 9;
    
    
    // ----------------------------------------------------------------
    
    function _init() {
      debug.log("HomeBackground :: initialized");
      
			splash247Rect = HEGA.rectangle(0, 0, 1608, 1052);

			// setup animated properties
			$bgImage.css({
				opacity:0
			});
			
			$htGrid.css({
				opacity:0
			});
			
			$splash247.css({
				opacity:0
			});
			
			
			// only do the intro animation if this is the first time visiting page
			if (TF7.isFirstVisit) {
				
				_doSplashIntro();
				
			} else {
				
				$bgImage.css({
					opacity:1
				});
				
				$htGrid.css({
					opacity:0.5
				});
					
				$splash247.addClass('hidden');
				$body.trigger(TF7.events.SPLASH_INTRO_COMPLETE);
			}
			
			
			$win.bind('resize', _onResize);
      _onResize();
			
    }; // _init()
    
    // ----------------------------------------------------------------
    
		function _doSplashIntro() {
			var delay = 0;
			
			$splash247Holder.detach().appendTo($('#wrapper'));
			
			$.doTimeout( delay, function(){
				$bgImage.stop().animate({opacity:1}, 1500, 'easeOutSine');
			});
			
			$.doTimeout( delay, function(){
				$htGrid.stop().animate({opacity:0.5}, 1500, 'easeOutSine');
			});
			delay += 550;
			
			$.doTimeout( delay, function(){
			  $splash247.css({visibility: 'visible'});
				$splash247.stop().animate({opacity:0.3}, 1250, 'easeOutSine');
			});
			delay += 2500;
			
			$.doTimeout( delay, function(){
				$splash247.stop().animate({opacity:0}, 750, 'easeOutSine', _doSplashIntroComplete);
			});
		}
		
		// ----------------------------------------------------------------
		
		function _doSplashIntroComplete() {
			$splash247.addClass('hidden');
			
			$body.trigger(TF7.events.SPLASH_INTRO_COMPLETE);
		}
		
		// ----------------------------------------------------------------
		
    function _onResize() {
      var w = $win.width();
      var h = $win.height();
      
			screenRect.setWidth(w * 0.99);
			screenRect.setHeight(h * 0.99);
			
			if (!$splash247.hasClass('hidden')) {
				// resize splash text to always fit the visible area
				splash247Rect.copy(HEGA.ratioUtil.scaleToFit(splash247Rect, screenRect, false));
				$splash247.css({
		      width:splash247Rect.getWidth(),
		      height:splash247Rect.getHeight()
		    });
			}
			
    };

		// ----------------------------------------------------------------
    
    _init();
    
    return {
      
    };
    
  };
  
  // ==================================================================
  
  $.fn.homeBackground = function(options) {
    
    var pluginName = "HomeBackground";
    
    options = $.extend({}, $.fn.homeBackground.defaults, options);
    
    return this.each(function(){
      
      var $this = $(this);
      
      if($this.data(pluginName)) {
        return;
      }
      
      // No JavaScript plugin instance exists, so add it as data attribute.
      var pluginInstance = new HomeBackground(options, this);
      $this.data(pluginName, pluginInstance);
      
    });
    
  };
  
  // ==================================================================
  
  $.fn.homeBackground.defaults = {
    showSplash: false
  };
  
})(jQuery);

