
if(cubic == undefined){
	var cubic = {}; // package definition
}


// ============================================
// create application
cubic.parallax = function(){
	// ============================================
    // private variables
	var _sections = ["schools","offsite","community","business"];
	var _positions = {};
	//var _currentHash = "";
	
	// ============================================
    // private functions
	
	// ========================
	var getPosition = function(pName){
		if(_positions[pName]){
			return _positions[pName];
		}else{
			return {top: 0, left: 0};
		}
	};
	
	/*var setHash = function(pHash){
		console.log(pHash);
	};*/
	
	// ============================================
    // public space
    return {
		// ============================================
        // public properties, e.g. strings to translate
		
		init: function(){
			this.recalc();
			this.draw();
		},
		
		
		recalc: function(){
			for(var i=0; i<_sections.length; i++){
				var name = _sections[i];
				var section = $("#" + name);
				_positions[name] = section.offset();
			};
			
		},
		
 		// ============================================
        // public methods
		draw: function(){
			
			var windowPos = $(window).scrollTop();
			var closest = "";
			var closestPos = 9999999;
			
			for(var i=0; i<_sections.length; i++){
				var name = _sections[i];
				var section = $("#" + name);
				var pos = getPosition(name);
				
				//console.log(i + " - " + windowPos + " - " + pos.top);
				for(var j=1; j<=4; j++){
					//$("#" + name + "fg" + j).css({backgroundPositionY: ((windowPos-pos.top)/-(1+(j*2)))});
					$("#" + name + "fg" + j).css({top: ((windowPos-pos.top)/-(1+(j*2)))});
				}
				
				$("#" + name).css({backgroundPositionY: ((windowPos-pos.top)/-4)-200});
				
				/*var dif = Math.abs(windowPos - pos.top);
				if(dif < closestPos){
					closest = name;
					closestPos = dif;
				}*/
				
			}
			
			//setHash(closest);
			
		},
		
		scrollTo: function(pName){
			var pos = getPosition(pName);
	     	$("html, body").animate({scrollTop: pos.top}, "slow", function(){
				window.location.href = "#" + pName; 
			});
		}
		
	};
}(); // end of app

// =======================================
// generic jQuery wire ups
$(function(){
	var SUPPORTPOSITIONFIXED = testFixedSupport();
	
	// initiate our parallax script
	cubic.parallax.init();
	
	var menuOpen = 422;
	
	var menu = $("#jumpNav");
	
	
	// redraw when we scroll
	$(window).scroll(function(){
		cubic.parallax.draw();
		
		
		var diff = menuOpen - $(window).scrollTop();
		if(diff < 10){
			if(SUPPORTPOSITIONFIXED){
				menu.css({
					position: "fixed",
					top: 0
				});
			}else{
				menu.css({
					top: $(window).scrollTop() // + 10
				});
			}
			menu.addClass("jumpNavBG");
		}else{
			menu.css({
				position: "absolute",
				top: menuOpen
			});
			menu.removeClass("jumpNavBG");
		}
	});
	
	// recalculate positions and redraw when the browser resizes
	$(window).resize(function(){
		cubic.parallax.recalc();
		cubic.parallax.draw();
	});
	
	// jump nav wireup
	$("#jumpNav a").click(function(e){
		var target = this.href.split("#");
		var hash = target[1];
		cubic.parallax.scrollTo(hash);
		e.preventDefault();
	});
	
});


// =================================
// test for position:fixed support
testFixedSupport = function () {
	var el = $('<div style="position:fixed; top:10px" />').appendTo(document.body);
	var result = el[0].offsetTop === 10;
	el.remove();
	return result;
}


