function getBGParallax(tileheight, speedratio, scrollposition) 
{
	if(scrollposition > 2000)
		scrollposition = 2000;
    return ((tileheight) - (Math.floor(scrollposition / speedratio) % (tileheight+1)) - 517);
  
}

function getStyle(el, cssprop)
{
 if (el.currentStyle) //IE
  return el.currentStyle[cssprop]
 else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
  return document.defaultView.getComputedStyle(el, "")[cssprop]
 else //try and get inline style
  return el.style[cssprop]
}

window.onload = function() 
{
  document.getElementById('bud').setAttribute("style", "background-position: 0 301px");
  window.onscroll = function() 
  {
	  
	var scnWid,scnHei;
	if (self.innerHeight) // all except Explorer
	{
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}

    var posX = (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : window.pageXOffset;
    var posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;

   	var bud = document.getElementById('bud');
	
	if(posY + scnHei >= document.body.scrollHeight - 170)
	{
		//console.log(document.body.scrollHeight - (posY + scnHei));
		var mydiv=document.getElementById("bud")

		//var budMargin = getStyle(mydiv, 'background-position');
		//budMargin = budMargin.substring(4, budMargin.length - 2);
		
		var budScroll = document.body.scrollHeight - (posY + scnHei);
		//budScroll = (budMargin - budScroll) / 1;
		if(budScroll < 0)
		{
			budScroll = 0;
		}
		if(budScroll > 301)
		{
			budScroll = 301;
		}
		
		document.getElementById('bud').setAttribute("style", "background-position: 0 " + budScroll.toString() + "px");
		
		
		//console.log('MARGIN: ' + budMargin);
		
		//this.x -= (this.x - xPos) / paddleSpeed;	//easing
			//this.x = xpos; //no easing
	}
	
	var bg = document.getElementById('pageBG');
    var bgparallax = getBGParallax(517, 4, posY);
	
	
    document.getElementById('pageBG').setAttribute("style", "background-position: center " + bgparallax.toString() + "px");
  }
}
