// JavaScript Document




Scrollio = {};

Scrollio.init = function(mask, options) {
		
		
		
	if (options === undefined) options = {};
	options.speed = (options.speed === undefined) ? 1 : options.speed * .1;
	options.scrollEase = (options.scrollEase === undefined) ? "easeInOutSine" : options.scrollEase;	
	options.maskEase = (options.maskEase === undefined) ? "easeInOutSine" : options.maskEase;
	options.startingLocation = (options.startingLocation === undefined) ? undefined : options.startingLocation;
	
	Scrollio.options = options;
	//alert(this.options.onComplete)
	this.mask = mask;
	$(this.mask).children().wrapAll("<div id='content'></div>");
	
	this.content = $("#content");
	
	this.pages = $(this.content).children();
	this.currentPage = (this.options.startingLocation === undefined) ? this.pages.first() : $(this.options.startingLocation);
	
	
	$(this.pages).fadeTo(0, 0);
	
	
	Scrollio.index = $(this.currentPage).index();
	 
	
	
$(this.mask).css({
		
		"display": "block",
		"float": "left",
		"width":"100%",
		"overflow-y": "hidden",
		
	});
	
	var y = $(Scrollio.content).offset().top + (-1 * $(Scrollio.currentPage).offset().top);
	y = Math.ceil(y);

	
	$(Scrollio.content).css({
		
			"position": "relative",
			"top": y,
		
	  })
	  
	
	$(this.mask).animate({
		
		"height": this.currentPage.outerHeight()		
		
	}, this.currentPage.outerHeight() / this.options.speed, this.options.maskEase, function(){
		
		$(Scrollio.currentPage).fadeTo(500, 1, "easeInSine", Scrollio.options.onLoadComplete);
		
		if (Scrollio.options.navigation != undefined) {
		
			var i = $(Scrollio.currentPage).index();
			Scrollio.currentButton = $(Scrollio.options.navigation + " ul li a").get(i)
			
			$(Scrollio.options.navigation + " ul li a").click(Scrollio.navigate);
			$(Scrollio.options.navigation + " ul li a").hover(Scrollio.mouseOver, Scrollio.mouseOut);
			
			var speed = (Scrollio.options.startingLocation === undefined) ? 500 : 0;
			$(Scrollio.currentButton).addClass("selected").animate({
					
				backgroundPosition:"(0 0px)"}, 
				speed, 
				"easeInSine")
		}
			
	});
	
	
	
	
	
	
}


//fixes a bug in Chrome for Jquery's offset()
window.onload = function() {
	
	var y = $(Scrollio.content).offset().top + (-1 * $(Scrollio.currentPage).offset().top);
	y = Math.ceil(y);

	
	$(Scrollio.content).css({
		 
			"top": y,
		
	  })
}


Scrollio.navigate = function(){
	
	var index = $(this).parent().index();
	
	if (Scrollio.index != index) {
		
	Scrollio.index = index;	
	
	var newPage = $(Scrollio.content).children().get(Scrollio.index);
	$(this).addClass("selected");
	
	$(Scrollio.currentButton).animate({
				
				backgroundPosition:"(0 60px)"}, 
				333, 
				"easeInSine",
				 
				function(){
					
					$(this).removeClass("hover");
					$(this).removeClass("selected");	
					
					
					Scrollio.calculate(newPage);
				}
				
			)
			
	Scrollio.currentButton = $(this);	
	
	}
	 
							 
}


Scrollio.scrollToPage = function(newPage) {
	
	
	var index = $(newPage).index();
	
	if (Scrollio.index != index) {
		
	Scrollio.index = index;	
		
	$(Scrollio.currentButton).animate({
				
		backgroundPosition:"(0 60px)"}, 
		333, 
		"easeInSine",
		function(){
			$(Scrollio.currentButton).removeClass("selected");
			$(Scrollio.currentButton).removeClass("hover");
			
			var button = $(Scrollio.options.navigation + " ul li a").get(index);
			Scrollio.currentButton = button;
			
			
				$(Scrollio.currentButton).animate({
				
					backgroundPosition:"(0 0px)"}, 
					333, 
					"easeInSine",
					
					function(){
					$(this).addClass("selected");
					Scrollio.calculate(newPage);
				}
			
			)
			
		}
		
	)
	
	}
}


Scrollio.mouseOver = function() {
	
	$(this).addClass("hover");
	
	$(this).stop().animate(
	
			{backgroundPosition:"(0 0)"}, 
			{duration:200, easing:"easeOutExpo"}
			
	)
	
		
}

Scrollio.mouseOut = function() {
	
	if ($(this).hasClass("selected") != true)	{
		$(this).stop().animate({
				
				backgroundPosition:"(0 60px)"}, 
				333, 
				"easeInSine",
				 
				function(){
					
					$(this).removeClass("hover")	
					
				}
				
			)
		
	}
}

Scrollio.hideOthers = function() {
	
	$(this.pages).fadeTo(0, 0);
	$(this.currentPage).fadeTo(0, 1);
	
}
	
Scrollio.adjust = function(params) {
		
	var maskSpeed = params.speed;
	
	  $(this.mask).stop().animate({
	
			height: $(this.currentPage).outerHeight(),
			
		}, maskSpeed, this.options.maskEase, params.onComplete)
			  
		
}

Scrollio.calculate = function(newPage) {
		
		
	var currentY = $(this.content).offset().top - $(newPage).offset().top;
	currentY = Math.abs(currentY);
	currentY = Math.floor(currentY);
	var newY = $(this.content).offset().top - $(this.currentPage).offset().top;
	newY = Math.floor(newY);
	newY = Math.abs(newY)
	var distance = (currentY > newY) ? currentY - newY : newY - currentY;
	var scrollSpeed = distance / this.options.speed;
	var maskSpeed = Math.abs($(this.currentPage).height() - $(newPage).height()) / (this.options.speed *.4);
	
	
	if (this.options.navigation != undefined) {
	
		$(this.options.navigation + " ul li a").unbind("click", this.navigate);
	}
	
	if (newY != currentY) {
	
	$(this.pages).stop().fadeTo(666, .5, "easeOutSine", function() {
		
		$(newPage).fadeTo(0, 1);
		
		
				
		 if ($(Scrollio.currentPage).height() > $(newPage).height()) {
			 
		 	//contract
			var maskDistance = $(Scrollio.currentPage).outerHeight() - $(newPage).outerHeight(); 
			maskSpeed = (maskDistance < 200) 
				? 200 / Scrollio.options.speed 
				: maskDistance / Scrollio.options.speed;
				
			Scrollio.contract(newPage, maskSpeed, scrollSpeed)
			
		 } else {
			//expand	
			var maskDistance = $(newPage).outerHeight()  - $(Scrollio.currentPage).outerHeight();
			maskSpeed = (maskDistance < 200) 
				? 200 / Scrollio.options.speed 
				: maskDistance / Scrollio.options.speed;
				
			Scrollio.expand(newPage, maskSpeed, scrollSpeed)
		 }
		 
		});
	
	}
	
}	

Scrollio.expand = function(newPage, maskSpeed, scrollSpeed) {
	
	
	var y = $(this.content).offset().top + -1 * $(newPage).offset().top;
	y = Math.ceil(y);
	
	
	
	$(Scrollio.content).stop().animate({
		 
			"top": y,
		
	  }, scrollSpeed, Scrollio.options.scrollEase, function() {
										  
		  $(Scrollio.mask).stop().animate({
		
				height: $(newPage).outerHeight(),
				
			}, maskSpeed, Scrollio.options.maskEase, function() {
					
				Scrollio.currentPage = newPage;
				
				if (Scrollio.options.navigation != undefined) {
				
					$(Scrollio.options.navigation + " ul li a").click(Scrollio.navigate);
				}
				
				Scrollio.options.onComplete;
				
			})
		  
	});
	
}

Scrollio.contract = function(newPage, maskSpeed, scrollSpeed) {
	
	
	var y = $(this.content).offset().top + -1 * $(newPage).offset().top;
	y = Math.ceil(y);
	
	
	
	$(Scrollio.mask).stop().animate({
		
		height: $(newPage).outerHeight(),
		
	}, maskSpeed, Scrollio.options.maskEase, function(){
		
		Scrollio.currentPage = newPage;
		
		
		$(Scrollio.content).stop().animate({
		 
			"top": y,
		
		 }, scrollSpeed, Scrollio.options.scrollEase, function() {
		
			if (Scrollio.options.navigation != undefined) {
			
				$(Scrollio.options.navigation + " ul li a").click(Scrollio.navigate);
			}	 
			
			Scrollio.options.onComplete;
			 
		});
		
		
	})
	
}



