(function(document) {
	
	window.Vertical = window.Vertical || function() {
		
	};
	
	Vertical.prototype.move = function(ctx, el, timing, easing, callback) {
	    if ($(ctx)[0].nodeName == 'HTML' || $(ctx)[0].nodeName == 'BODY') {
	    	if (Math.abs($(el).offset().top - $(window).scrollTop()) > 0) {
	    		$(ctx).animate({ scrollTop: $(el).offset().top + 'px' }, { duration: timing, easing: easing, complete: callback, queue: false });
	    	}
	    }
	};
	
})(document);


(function(document) {
	
	window.Horizontal = window.Horizontal || function(context) {
		this.context = context;
    	this.scroll_region = $('> .scroll-region', context);
    	this.active_el = null;
		this.reposition();
	};
	
	Horizontal.prototype.move = function(el, timing, easing, callback) {
	    if (el) this.active_el = $(el);
	    this.context.animate({ height: this.active_el.outerHeight() + 'px' }, { queue: false });
	    this.scroll_region.animate({ left: (this.active_el.position().left * -1) + 'px' }, { duration: timing, easing: easing, complete: callback, queue: false });
	};
	
	Horizontal.prototype.reposition = function() {
	    var children, t, total_width;
	    children = $('section', this.context);
	    total_width = 0;
	    
	    $.each($(children), function() {
	        if ($(this).outerHeight > t) t = $(this).outerHeight;
	        var width = ($(window).width() > kbodyMinWidth ? $(window).width() : kbodyMinWidth);
	        $(this).width(width + 'px');
	        total_width += width;
	    });
	    
	    this.context.height($(children[0]).outerHeight() + 'px');
	    $('.scroll-region', this.context).width(total_width + 'px');
	    
	    if (this.active_el)
	    	$(this.scroll_region).css('left', (this.active_el.position().left * -1) + 'px');
	};
	
})(document);


(function(document) {
	
	window.Layering = window.Layering || function() {
		jQuery.each($('.layers *'), function(key, el) {
		    var config = el.getAttribute('data-layer-positions');
		    if (config !== null)
		    	config = jQuery.parseJSON(config);
		    
		    if (config === null || config.x === undefined || config.y === undefined)
		    	$(el).hide();
		    
		    $(el).css('background-position', config.x + ' ' + config.y);
		    $(el).show();
		});
	}
	
	Layering.prototype.reposition = function() {
	    $.each($('.layers *'), function(key, el) {
	    	var config = el.getAttribute('data-layer-positions');
	    	if (config !== null) config = jQuery.parseJSON(config);
	    	
	    	var y = (parseInt(config.y.end) - (Math.ceil((($(window).scrollTop() - $(el).offset().top) * config.rate)))) + 'px';
	    	var windowWidth = ($(window).width() <= kbodyMinWidth) ? kbodyMinWidth : $(window).width();
	    	var x = Math.ceil((windowWidth - ksectionWidth) / 2) + parseInt(config.x) + 'px';
	    	
	    	$(el).css('background-position', x + ' ' + y);
	    });
	};
	
})(document);
