﻿function onLoad()
	{
		initNagrodyAnimation();
		
	};
	
	var nagrodyPagesCount = 3;	
	var nagrodyWidth = 990;
	var interval = 8000; 
	var stopNagrodyAnimation = false;
	
	function initNagrodyAnimation()
	{
		setInterval("animateNagrody()", interval);
		
		// pages animation
		animation = new myAnimation();
	}
	
	function animateNagrody()
	{
		if (stopNagrodyAnimation) return;
		var currentMargin = document.getElementById("logoContainer").style.marginLeft;
		var margin = currentMargin.substring(0, currentMargin.length - 2) - nagrodyWidth;
		if (margin <= -(nagrodyWidth * nagrodyPagesCount)) margin = 0; 
//		document.getElementById("pagesContainer").style.marginLeft = margin + "px";
		animationStart(margin);
	}
	
	function prevProducts()
	{
		var currentMargin = document.getElementById("logoContainer").style.marginLeft;
		var margin = Number(currentMargin.substring(0, currentMargin.length - 2)) + nagrodyWidth;
		if (margin > 0) return; 
//		document.getElementById("pagesContainer").style.marginLeft = margin + "px";
		animationStart(margin);
	}
	
	
	function nextProducts()
	{
		currentMargin = document.getElementById("logoContainer").style.marginLeft;
		margin = Number(currentMargin.substring(0, currentMargin.length - 2)) - nagrodyWidth;
		if (margin <= -(nagrodyWidth * (nagrodyPagesCount))) return; 
//		document.getElementById("pagesContainer").style.marginLeft = margin + "px";
		animationStart(margin);
	}

	
	
// pages amination	
	
function setElementProperty(value)
{
	document.getElementById("logoContainer").style.marginLeft = value + "px";		
}	


function getElementProperty()
{
	var property = document.getElementById("logoContainer").style.marginLeft;
	return Number(property.substring(0, (property.length - 2)));
}	
	

// animation ////////////////////////////////////////////////////////////////////////////////////////////////////////////////


var animation;

function myAnimation()
{
	this.from = 0;
	this.to = 0;
	this.time = 0;
	this.play = false;
	this.interval = 15; // ms
	this.duration = 300; // ms
} 


function animationStart(to)
{
	if (animation.play) return;
	animation.from = getElementProperty();
	animation.to = to;
	animation.time = 0;
	if (animation.from != animation.to)	
	{
		animation.play = true;
		animate();
	}
}

	
function animationStop()
{
	animation.play = false;
}


function animate()
{
	animation.time += animation.interval;
	if (!animation.play || (animation.time > animation.duration)) 
	{
		animationStop();
		return;
	}
	setElementProperty(animationF(animation.time));
	setTimeout("animate()", animation.interval);
}


function animationF(time)
{
	var b = (4 * animation.to - 3 * animation.from) / 2 / animation.duration;
	var a = (animation.to - animation.from - b * animation.duration) / animation.duration / animation.duration;
	var c = animation.from;
	return Math.round(a * time * time + b * time + c);
}




