/***************************************************************
* Function: ANIMLoadSketchBook()
***************************************************************/
void ANIMLoadSketchBook(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd,
			int &numPages, ANIMPageEntry ***pageEntryArray)
{
    *xformScaleFwd = new PositionAttitudeTransform;
    *xformScaleBwd = new PositionAttitudeTransform;

    MatrixTransform *sketchbookTrans = new MatrixTransform;
    Matrixf transMat, scaleMat;
    transMat.makeTranslate(Vec3(0, 0, ANIM_VIRTUAL_SPHERE_RADIUS));
    scaleMat.makeScale(Vec3(ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR,
			    ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR,
			    ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR));
    sketchbookTrans->setMatrix(transMat * scaleMat);

    (*xformScaleFwd)->addChild(sketchbookTrans);
    (*xformScaleBwd)->addChild(sketchbookTrans);

    // load sketch book node from VRML file, create page geodes 
    Node* sketchbookNode = osgDB::readNodeFile(ANIMDataDir() + "VRMLFiles/SketchBook.WRL");
    sketchbookTrans->addChild(sketchbookNode);


    // Load floorplan filenames from config file
    bool isFile = true;
    int j = 0, numTex;
    std::string file = "", dir, path;
    std::vector<std::string> filenames;
    
    dir = cvr::ConfigManager::getEntry("dir", "Plugin.CaveCADBeta.Floorplans", "/home/cehughes");
    dir = dir + "/";
    path = "Plugin.CaveCADBeta.Floorplans.0";
    file = cvr::ConfigManager::getEntry(path, "", &isFile);

    while (isFile)
    {
        filenames.push_back(dir + file);
        j++;
        char buf[50];
        sprintf(buf, "Plugin.CaveCADBeta.Floorplans.%d", j);
        std::string path = std::string(buf);
        file = cvr::ConfigManager::getEntry(path, "", &isFile);
    }
    
    numPages = j;

    //numPages = 3;
    
    // create tree structured page entry array 
    *pageEntryArray = new ANIMPageEntry*[numPages];

    for (int i = 0; i < numPages; i++)
    {
/*        char idxStr[16];
        if (i < 10) 
        {
            sprintf(idxStr, "0%d", i);
        }
        else if (i < 100) 
        {
            sprintf(idxStr, "%d", i);
        }*/
        string filename = filenames[i];//ANIMDataDir() + "Textures/Floorplans/Floorplan" + string(idxStr) + string(".JPG");

        (*pageEntryArray)[i] = new ANIMPageEntry;
        Switch *singlePageSwitch = new Switch;
        PositionAttitudeTransform *flipUpTrans = new PositionAttitudeTransform;
        PositionAttitudeTransform *flipDownTrans = new PositionAttitudeTransform;

        sketchbookTrans->addChild(singlePageSwitch);
        singlePageSwitch->addChild(flipUpTrans);
        singlePageSwitch->addChild(flipDownTrans);
        singlePageSwitch->setAllChildrenOff();

        // set up flip up / flip down animation paths for each page 
        Geode *flipUpGeode, *flipDownGeode;
        AnimationPathCallback *flipUpCallback, *flipDownCallback;
        ANIMCreateSinglePageGeodeAnimation(filename, &flipUpGeode, &flipDownGeode, &flipUpCallback, &flipDownCallback);

        flipUpTrans->addChild(flipUpGeode);
        flipUpTrans->setUpdateCallback(flipUpCallback);
        flipDownTrans->addChild(flipDownGeode);
        flipDownTrans->setUpdateCallback(flipDownCallback);

        // write into page entry array record 
        (*pageEntryArray)[i]->mSwitch = singlePageSwitch;
        (*pageEntryArray)[i]->mFlipUpAnim = flipUpCallback;
        (*pageEntryArray)[i]->mFlipDownAnim = flipDownCallback;
        (*pageEntryArray)[i]->mPageGeode = flipDownGeode;
        (*pageEntryArray)[i]->mTexFilename = filename;
    }

    (*pageEntryArray)[0]->mSwitch->setSingleChildOn(1);
    (*pageEntryArray)[1]->mSwitch->setSingleChildOn(0);

    // size of floorplan 
    



    // FIX THIS - put sizes in the config file or something
    float alt = -2.9f;
    (*pageEntryArray)[0]->mLength = 32;		(*pageEntryArray)[0]->mWidth = 16;	(*pageEntryArray)[0]->mAlti = alt;
    (*pageEntryArray)[1]->mLength = 128;	(*pageEntryArray)[1]->mWidth = 128;	(*pageEntryArray)[1]->mAlti = alt;
    (*pageEntryArray)[2]->mLength = 32;		(*pageEntryArray)[2]->mWidth = 16;	(*pageEntryArray)[2]->mAlti = alt;
    (*pageEntryArray)[3]->mLength = 32;		(*pageEntryArray)[3]->mWidth = 32;	(*pageEntryArray)[3]->mAlti = alt;




    // set up the forward / backward scale animation paths for sketch book root switch 
    AnimationPath* animationPathScaleFwd = new AnimationPath;
    AnimationPath* animationPathScaleBwd = new AnimationPath;
    animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING);
    animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING);
   
    Vec3 scaleFwd, scaleBwd;
    float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS;
    for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++)
    {
        float val = i * step;
        scaleFwd = Vec3(val, val, val);
        scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val);
        animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleFwd));
        animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleBwd));
    }

    AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME);
    AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); 
    (*xformScaleFwd)->setUpdateCallback(animCallbackFwd);
    (*xformScaleBwd)->setUpdateCallback(animCallbackBwd);
}
/***************************************************************
* Function: ANIMLoadSketchBook()
***************************************************************/
void ANIMLoadSketchBook(PositionAttitudeTransform** xformScaleFwd, PositionAttitudeTransform** xformScaleBwd,
			int &numPages, ANIMPageEntry ***pageEntryArray)
{
    *xformScaleFwd = new PositionAttitudeTransform;
    *xformScaleBwd = new PositionAttitudeTransform;

    MatrixTransform *sketchbookTrans = new MatrixTransform;
    Matrixf transMat, scaleMat;
    transMat.makeTranslate(Vec3(0, 0, ANIM_VIRTUAL_SPHERE_RADIUS));
    scaleMat.makeScale(Vec3(ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR,
			    ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR,
			    ANIM_PARA_PAINT_FRAME_ZOOM_FACTOR));
    sketchbookTrans->setMatrix(transMat * scaleMat);

    (*xformScaleFwd)->addChild(sketchbookTrans);
    (*xformScaleBwd)->addChild(sketchbookTrans);

    /* load sketch book node from VRML file, create page geodes */
    Node* sketchbookNode = osgDB::readNodeFile(ANIMDataDir() + "VRMLFiles/SketchBook.WRL");
    sketchbookTrans->addChild(sketchbookNode);

    /* create tree structured page entry array */
    numPages = 3;
    *pageEntryArray = new ANIMPageEntry*[numPages];
    for (int i = 0; i < numPages; i++)
    {
	char idxStr[16];
	if (i < 10) sprintf(idxStr, "0%d", i);
	else if (i < 100) sprintf(idxStr, "%d", i);
	string filename = ANIMDataDir() + "Textures/Floorplans/Floorplan" + string(idxStr) + string(".JPG");

	(*pageEntryArray)[i] = new ANIMPageEntry;
	Switch *singlePageSwitch = new Switch;
	PositionAttitudeTransform *flipUpTrans = new PositionAttitudeTransform;
	PositionAttitudeTransform *flipDownTrans = new PositionAttitudeTransform;

	sketchbookTrans->addChild(singlePageSwitch);
	singlePageSwitch->addChild(flipUpTrans);
	singlePageSwitch->addChild(flipDownTrans);
	singlePageSwitch->setAllChildrenOff();

	/* set up flip up / flip down animation paths for each page */
	Geode *flipUpGeode, *flipDownGeode;
	AnimationPathCallback *flipUpCallback, *flipDownCallback;
	ANIMCreateSinglePageGeodeAnimation(filename, &flipUpGeode, &flipDownGeode, &flipUpCallback, &flipDownCallback);

	flipUpTrans->addChild(flipUpGeode);
	flipUpTrans->setUpdateCallback(flipUpCallback);
	flipDownTrans->addChild(flipDownGeode);
	flipDownTrans->setUpdateCallback(flipDownCallback);

	/* write into page entry array record */
	(*pageEntryArray)[i]->mSwitch = singlePageSwitch;
	(*pageEntryArray)[i]->mFlipUpAnim = flipUpCallback;
	(*pageEntryArray)[i]->mFlipDownAnim = flipDownCallback;
	(*pageEntryArray)[i]->mPageGeode = flipDownGeode;
	(*pageEntryArray)[i]->mTexFilename = filename;
    }
    (*pageEntryArray)[0]->mSwitch->setSingleChildOn(1);
    (*pageEntryArray)[1]->mSwitch->setSingleChildOn(0);

    /* size of floorplan */
    (*pageEntryArray)[0]->mLength = 32;		(*pageEntryArray)[0]->mWidth = 16;	(*pageEntryArray)[0]->mAlti = -1.5f;
    (*pageEntryArray)[1]->mLength = 128;	(*pageEntryArray)[1]->mWidth = 128;	(*pageEntryArray)[1]->mAlti = -1.5f;
    (*pageEntryArray)[2]->mLength = 32;		(*pageEntryArray)[2]->mWidth = 16;	(*pageEntryArray)[2]->mAlti = -1.5f;

    /* set up the forward / backward scale animation paths for sketch book root switch */
    AnimationPath* animationPathScaleFwd = new AnimationPath;
    AnimationPath* animationPathScaleBwd = new AnimationPath;
    animationPathScaleFwd->setLoopMode(AnimationPath::NO_LOOPING);
    animationPathScaleBwd->setLoopMode(AnimationPath::NO_LOOPING);
   
    Vec3 scaleFwd, scaleBwd;
    float step = 1.f / ANIM_VIRTUAL_SPHERE_NUM_SAMPS;
    for (int i = 0; i < ANIM_VIRTUAL_SPHERE_NUM_SAMPS + 1; i++)
    {
	float val = i * step;
	scaleFwd = Vec3(val, val, val);
	scaleBwd = Vec3(1.f-val, 1.f-val, 1.f-val);
	animationPathScaleFwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleFwd));
	animationPathScaleBwd->insert(val, AnimationPath::ControlPoint(Vec3(),Quat(), scaleBwd));
    }

    AnimationPathCallback *animCallbackFwd = new AnimationPathCallback(animationPathScaleFwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME);
    AnimationPathCallback *animCallbackBwd = new AnimationPathCallback(animationPathScaleBwd, 
						0.0, 1.f / ANIM_VIRTUAL_SPHERE_LAPSE_TIME); 
    (*xformScaleFwd)->setUpdateCallback(animCallbackFwd);
    (*xformScaleBwd)->setUpdateCallback(animCallbackBwd);
}