// Used with box2d style velocity (m/s where m = 32 pixels), but box2d is not required
void CCParallaxScrollNode::updateWithVelocity(Point vel, float dt)
{
	vel = vel * PTM_RATIO;
  
  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);
		Point relVel = scrollOffset->getRelVelocity() * PTM_RATIO;
		Point totalVel = vel + relVel;
		Point offset = ccpCompMult((totalVel * dt), scrollOffset->getRatio());
    
    Node *child = scrollOffset->getTheChild();
		child->setPosition(child->getPosition() + offset);
		
		if ( (offset.x < 0 && child->getPosition().x + child->getContentSize().width < 0) ||
        (offset.x > 0 && child->getPosition().x > _range.width) ) {
			child->setPosition((child->getPosition() + Point(-SIGN(offset.x) * fabs(scrollOffset->getScrollOffset().x), 0)));
		}
		
		// Positive y indicates upward movement in cocos2d
		if ( (offset.y < 0 && child->getPosition().y + child->getContentSize().height < 0) ||
        (offset.y > 0 && child->getPosition().y > _range.height) ) {
			child->setPosition((child->getPosition() + Point(0, -SIGN(offset.y) * fabs(scrollOffset->getScrollOffset().y))));
		}
	}
}
// Used with box2d style velocity (m/s where m = 32 pixels), but box2d is not required
void CCParallaxScrollNode::updateWithVelocity(CCPoint vel, float dt)
{
	vel = ccpMult(vel, PTM_RATIO);
//	CCLog("count: %i", _scrollOffsets->count());
    CCObject* object;
    CCARRAY_FOREACH(_scrollOffsets, object)
    {
        CCParallaxScrollOffset* scrollOffset = dynamic_cast<CCParallaxScrollOffset*>(object); 
		CCPoint relVel = ccpMult(scrollOffset->getRelVelocity(), PTM_RATIO);
		CCPoint totalVel = ccpAdd(vel, relVel);
		CCPoint offset = ccpCompMult(ccpMult(totalVel, dt), scrollOffset->getRatio());
        
        CCNode *child = scrollOffset->getTheChild();
		child->setPosition(ccpAdd(child->getPosition(), offset));
		
		if ( (offset.x < 0 && child->getPosition().x + child->getContentSize().width < 0) ||
            (offset.x > 0 && child->getPosition().x > _range.width) ) {
			child->setPosition(ccpAdd(child->getPosition(), ccp(-SIGN(offset.x) * fabs(scrollOffset->getScrollOffset().x), 0)));
		}
		
		// Positive y indicates upward movement in cocos2d
		if ( (offset.y < 0 && child->getPosition().y + child->getContentSize().height < 0) ||
			(offset.y > 0 && child->getPosition().y > _range.height) ) {
			child->setPosition(ccpAdd(child->getPosition(), ccp(0, -SIGN(offset.y) * fabs(scrollOffset->getScrollOffset().y))));
		}
	}
void CCParallaxScrollNode::updateWithYPosition(float y, float dt)
{
  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);

		Node *child = scrollOffset->getTheChild();
		float offset = y * scrollOffset->getRatio().y;
		child->setPosition(Point(child->getPosition().x, scrollOffset->getOrigPosition().y + offset));
	}
}
void CCParallaxScrollNode::removeChild(Sprite *node, bool cleanup)
{
  cocos2d::Vector<CCParallaxScrollOffset *> tobeDeleted;

  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);

		if( scrollOffset->getTheChild() == node){
			tobeDeleted.pushBack(scrollOffset);
			break;
		}
	}

  for (int i = 0; i < tobeDeleted.size(); i ++)
  {
    _scrollOffsets.eraseObject(tobeDeleted.at(i));
  }
}