/* START spaghetti, undocumented js */
(function($) {
    
    var container, parts, windowHeight, documentHeight;
    
    var DISTANCE = 2000,
        COMPETENT = (!$.browser.msie || $.browser.version > 7);
    
    function init() {
    
        container = $('#parts');
        parts = container.find('img');
        windowHeight = $(window).height();
	   
	  
      /*  
        $('#header').css('height', windowHeight - 50);
        $('#footer').css('height', Math.max(windowHeight / 2, $('#footer .inner').height()) + 'px');
       
        heading = $('#header h1');
        //headerThreshold = $('#footer').offset().top + 50 - heading.outerHeight();
        finalHeadingPos = windowHeight / 2 - heading.height() - 50;
         heading.css({
            position: 'fixed',
            top: 0,
            left: leftMargin + 'px'
        });*/
        documentHeight = $('body').height();
        
       
        
        var threshold = container.offset().top;
        var count = parts.length;
        
	 
        parts.each(function(i) {
            var me = $(this),
                posLeft = (parseInt(me.css('marginLeft')) + me.width() / 2) / container.width(),
                posTop = (parseInt(me.css('marginTop')) + me.height() / 2) / container.height();
            
            $(this).data('coords', {
                top: DISTANCE * posTop,
                left: DISTANCE * (posLeft - 0.5) * 4,
                origLeft: posLeft,
                origTop: posTop,
                angle: (posLeft > 0.5 ? -1 : 1) * Math.round(parseInt(me.css('marginTop')) / container.height() * 60 + 0),
                threshold: threshold + parseInt(me.css('marginTop'))
                //threshold: (threshold * (i + 1) / count + threshold) / 2
			
            }); 

        });
          
	   
        parts.each(function() {
            var part = $(this),
                origLeft,
                origTop;
            part.draggable({
                start: function() {
                    part.data('moved', true);
                },
                scroll: true,
                stop: function() {
                    part.css({
                        
                    });
                }
            });
        });       
    };
    
      
	   
    function ease(value, max) {
        return Math.sin((value / max - 1) * Math.PI / 2) * max + max;
    };
    
    
    var locked = false;
    
    function position() {
        var scrollTop = $(window).scrollTop();
       
	  
	  	 if ( scrollTop < 1000 && scrollTop > 4000) {
            	locked = true;	
		  }
		  else if (scrollTop > 1100 && locked) {
				locked = false;
				parts.each(function(i) {
				var coords = $(this).data('coords');
				$(this).stop().animate({
					top: coords.top + 'px',
					left: coords.left + 'px'
				}, 1000);
			});
				  
		 }
				
				
	    if (locked) {
            parts.css({
                top: 0,
                left: 0,
                '-webkit-transform': 'rotate(0)',
                '-moz-transform': 'rotate(0)'
            });

        }
        else {
            
            // Bring in left-side parts from the left and vice versa, the further from
            // the center, the further out the part starts. Parts at the top come in
            // horizontally, further down bring them in on an angle.        
            parts.each(function(i) {
              var coords = $(this).data('coords'),
                    factor = Math.max(0, coords.threshold - scrollTop) / coords.threshold,
                    easedFactor = ease(factor, 1),
                    deg = Math.round(coords.angle * easedFactor),
                    rotate = 'rotate(' + deg + 'deg)';
                
                if ($(this).data('moved')) {
                    var func = 'css';
                    $(this).data('moved', false);
                }
                else {
                    var func = 'css';
                }
                $(this).stop()[func]({
                    //opacity: 1 - easedFactor,
                    top: coords.top * easedFactor + 'px',
                    left: coords.left * easedFactor + 'px',
                    '-webkit-transform': rotate,
                    '-moz-transform': rotate
                });
            });
            
        }  
		  
       
        
        
       /* var headingFactor = ease(scrollTop / (documentHeight - windowHeight), 1);
        heading.css({
            top: finalHeadingPos * Math.max(headingFactor * 4 - 3, 0) + 'px'
        });
        
        $('#footer .inner').css({
            top: 20 * ease(documentHeight - windowHeight - scrollTop, windowHeight) + 'px'
        });
         */ 
    };
    
    function touchInit() {
        
    };
    
    $(function() {
       
            
            if (Modernizr.touch) {
                touchInit();
            }
            else {
                init();
                $(window).resize(function() {
                    init();
				
                    if (COMPETENT) {
                        position();
                    }
                });
                if (COMPETENT) {
                    $(window).scroll(position);
                    position();
				
                }
            }
              
    });
    

  
})(jQuery);
/* END spaghetti, undocumented js */


