var $j = jQuery.noConflict();

$j(document).ready(function() { 


	/* --  INITIAL PROCESSING ---- */

	logo = $j('#logoImg');
	visionV = $j('#V');
	modelNav = $j('#model-nav');
	secondaryNav = $j('#secondary-nav');
	contentDiv = $j('#content');
	
	logo.css({'opacity':0});


	/* --  LOGO V animation ---- */

	visionV.css({'opacity':0}).animate({
			opacity: 1.0
			
		  }, 1500, 'easeInQuad', function() {
			
			// fade in is complete- now shrink it
			visionV.delay(200).animate({
				width:41
			  }, 500, 'easeInOutSine', function() {
			  
			  	// shrink is complete- slide left
				visionV.delay(200).animate({
						right:178
					  }, 1000, 'easeInOutSine', function() {
						// slide is complete- fade out
						visionV.delay(200).fadeOut(3000);
						logo.delay(200).animate({opacity:1.0}, 2000, 'easeInQuad');
		
					  });

			  });
		  
		  });
		  
	
	/* --  NAV animation ---- */
	
	modelNav.find('a').delay(5000).css('opacity',0).fadeInSequence(300, 50);
	secondaryNav.find('a').css('opacity',0).delay(6500).fadeInSequence(300, 50);



	/* --  HOME slideshow images resizing funcs - INITIAL ---- */

	rootElement = ("onorientationchange" in window) ? $j(document) : $j(window); // hack to acccount for iOS position:fixed shortcomings

	imgContainer = $j('#homepage-images'); // spec the container div
	slideshowImages = imgContainer.find('img');
	
	slideshowImages.filter(':odd').addClass('odd');
	
	
	

	/* --  DO EVERYTHING FUNCTION ---- */

	$j(window).resize(_adjustContainer);
	
	function _adjustContainer(fn) {
	
		//window.console.log('resizing');
	
		try {
		
			screenHeight = rootElement.height();
			screenWidth = rootElement.width();
			
			// adjust all the container elems
			imgContainer.css({
				height: (screenHeight-20)+'px',
				width: (screenWidth-20)+'px'
			});
			$j('#content').css({
				height: (screenHeight-80)+'px',
				width: (screenWidth-10)+'px'
			});
			$j('#footer-wrap').css({
				width: (screenWidth-10)+'px'
			});
			
			
							// bind the resizing for the slideshow images, iterate
							slideshowImages.each(function(e) {
							
									  var self = $j(this);
									  
									  
									 
									  var imgWidth = 1500; // couldnt make dynamic original width detection work- must always be 1500 wide
									  var imgHeight = 1000; // couldnt make dynamic original height detection work- must always be 1000 tall
									  
									  var imgRatio = 1.5;
									  var roundedRatio = 1.5;
									  
									  self.attr('rel', roundedRatio); // store ratio in rel attr
									  
									  var targetWidth = Math.round( (screenHeight-80)*roundedRatio );
									  var targetHeight = Math.round( screenHeight-80 );
									  
									  targetLeftPos = Math.round( (screenWidth/2) - (targetWidth/2) ); // no "var" makes it global - works because all imgs same size
									  
									  self.css({ 
												height: targetHeight+'px',
												width: targetWidth+'px',
												left: targetLeftPos+'px'
											});
											
									  if ( self.hasClass('odd') ) {  // move images to the right side, they will slide in from right
											self.css('left', screenWidth);
									  } else {
											self.css('left', '-'+targetWidth+'px');
									  } 
									  
									
							//}).hide();
							});
							
			
			
		} catch(err) {
			// IE7 seems to trigger _adjustBG before the image is loaded.
			// This try/catch block is a hack to let it fail gracefully.
		}
  
		// Executed the passed in function, if necessary
		if (typeof fn == "function") fn();
	}
	
	// CALL THE FUNCTION
	_adjustContainer();
	
	
	
	
	
	/* --  HOME SLIDESHOW SCRIPT ---- */
		
	setInterval( 
		function() {
		
			var targetWidth = Math.round( (screenHeight-80)*1.5 );
			var targetLeftPos = Math.round( (screenWidth/2) - (targetWidth/2) );
			
			imgContainer.find('img')
			//slideshowImages
					.eq(0) // find first image
					.fadeOut('slow', function() {
					
							  if ( $j(this).hasClass('odd') ) {  // move images to the right side, they will slide in from right
									$j(this).css('left', screenWidth)
							  } else {
									$j(this).css('left', '-'+targetWidth+'px')
							  } 
							  
					})
					.next('img')
					//.fadeIn()
					.show()
					.animate({
						left: targetLeftPos+'px'
					  }, 1000, 'swing' )
					.end()
					.appendTo(imgContainer);
			}, 6000);
			
	
	/* --  END SLIDESHOW SCRIPT ---- */
	




	
	
	
	
	
});
