var plugin;
var MainCanvas;
var DebugBox;
var timerSB;
var pauseTimer = null;
var imageLibrary = null;
var downloadComplete = false;

var  _root_xmouse = 0;
var  _root_ymouse = 0;

var menu = null;
var livingRoomMenu = null;
var gameRoomMenu = null;
var s = null;

var mainWindow = null;
var dialog = null;
var dialog2 = null;
var dialogLivingRoom = null;
var dialogHomeOffice = null;
var dialogGameRoom = null;
var visibleDialog = null;

var navBar = null;
var backgroundMovie = null;
var slider = null;
var videoWindow = null;
var showVideo = true;
var animateSkinkles = false;

var house = null;

var _lastTime = -1;
var _currTime = -1;
var runningAnimations = new Array();
var useJSTimer = false;

var windowTopOffset = 0;

var useZip = true;
var eventManager = null;

function MainCanvasLoaded(s)
{    
    //alert("MainCanvasLoaded");
    eventManager = new EventManager();
    
    s = s;
	MainCanvas = s.findName("MainCanvas");
	plugin = s.getHost();
    plugin.content.onResize = OnResized;
    DebugBox = s.findname("DebugBox");
    
    var downloader = plugin.createObject("downloader");
    downloader.addEventListener("DownloadProgressChanged", OnDownloadProgressChanged);
    downloader.addEventListener("Completed", OnDownloadCompleted);
    downloader.addEventListener("DownloadFailed", OnDownloadFailed);
    
    videoWindow = new VideoWindow(s);
    videoWindow.Show(null, showVideo);
    
    if (useZip)
    {
        MainCanvas.findname("ProgressBar")["Opacity"] = 1;
        
        //alert("downloader = " + downloader);
        downloader.open("GET", generalSettings["deployFilename"]);//"360frames2.deploy");
        downloader.send();
    }
    
    
    if (!useZip)
        FinishInit(s);
}

function MailDeviceCoords()
{
    var text = "";
    text += house.GetDeviceCoords();
    //alert(text);

    var url = "http://www.cicada.net/Mail.aspx?" + text;
    
    document.location=url;
}

function OnDownloadProgressChanged(sender, e)
{
    var percent = Math.floor(sender.DownloadProgress * 100)
    //dd("progress = " + percent + " %");
    var frac = sender.DownloadProgress;

    var clipRectHeight = MainCanvas.findname("LoadingCanvasDark")["Height"] * frac; 
    var clipRectDef = "0," + (40 - clipRectHeight) + ",720," + clipRectHeight;
    MainCanvas.findname("LoadingCanvasClipRect")["Rect"] = clipRectDef;
    

    MainCanvas.findname("PercentTextBlock")["Text"] = "" + percent + "%";
    var hbc = MainCanvas.findname("HouseBarCanvas");
    var oldbar = MainCanvas.findname("ProgressInnerBar")
    
    hbc["Opacity"] = 1;
    var startWidth = 200;
    var endWidth = 721;
    
    var www = startWidth + (endWidth - startWidth) * frac;
    hbc["Canvas.Left"] = 721 / 2 - www/2;
    hbc["Width"] = www;
    //hbc["Opacity"] = Math.cos(Math.sqrt(sender.DownloadProgress * 0.8) * Math.PI/2);    
    
    
    
    if (percent >= 100)
    {
        dd("");
    }
}

var assetsContents = null;
function OnDownloadCompleted(sender, e)
{
    //alert("dl complete");
    MainCanvas.findname("LoadingCanvas")["Visibility"] = "Collapsed";
    
    MainCanvas.findname("VideoWindow")["Visibility"] = "Visible";
    MainCanvas.findname("VideoWindow")["Opacity"] = 1;
    
    //alert("Setting source...");
    MainCanvas.findname("IntroMovie").setSource(sender, "demo.wmv");
    //dd("Set source to: " + MainCanvas.findname("IntroMovie")["Source"] );
    
    MainCanvas.findname("ProgressBar")["Opacity"] = 0;

    downloadComplete = true;
    //alert("OnDownloadCompleted");
    assetsContents = sender;

    //alert("dl complete");
    // Create an ImageSequence for the 360 rotation.
    //alert("try 1");
    //MainCanvas.findname("MainWindowImage").setSource(sender, "360_0001.jpg");
    //alert("try 1_end");
    
    var framesSuffix = generalSettings["deploy360Extension"];//".jpg";
    
    imageLibrary = new ImageSequence(721, 294, framesSuffix);
    imageLibrary.CreateImages(sender, "360_00", framesSuffix, 32);
    imageLibrary.CreateImages(sender, "BR_00", framesSuffix, 15);
    imageLibrary.CreateImages(sender, "LR_00", framesSuffix, 15);
    imageLibrary.CreateImages(sender, "Office_00", framesSuffix, 15);
    
    MainCanvas.findname("MainWindowImage").setSource(sender, "360_0001" + framesSuffix);
    MainCanvas.findname("HomeOfficeImage").setSource(sender, "Office_0015" + framesSuffix);
    MainCanvas.findname("GameRoomImage").setSource(sender, "BR_0015" + framesSuffix);
    MainCanvas.findname("LivingRoomImage").setSource(sender, "LR_0015" + framesSuffix);
    
    MainCanvas.findname("HomeOfficeBBImageRays").setSource(sender, "rays.png");
    MainCanvas.findname("TopStripBrush").setSource(sender, "topstrip.png");

    MainCanvas.findname("HomeOfficeBBImageRays2").setSource(sender, "rays2.png");

    //MainCanvas.findname("TopStrip2Brush").setSource(sender, "topstrip.png");
    MainCanvas.findname("FrostedImage").setSource(sender, "frosted_layer.png");
    
    MainCanvas.findname("LivingRoomBBImage").setSource(sender, "LivingRoom_bb.png");
    MainCanvas.findname("GameRoomBBImage").setSource(sender, "GameRoom_bb.png");
    MainCanvas.findname("HomeOfficeBBImage").setSource(sender, "HomeOffice_bb.png");
    
//    MainCanvas.findname("Dialog1Image").setSource(sender, "menu_body_bg.png");
//    MainCanvas.findname("LivingRoomDialogImage").setSource(sender, "menu_body_bg.png");
//    MainCanvas.findname("GameRoomDialogImage").setSource(sender, "menu_body_bg.png");
//    MainCanvas.findname("HomeOfficeDialogImage").setSource(sender, "menu_body_bg.png");
    
            //dd("loaded: " + imageLibrary.imageObjects.length + "   mainWindow.imageLibrary.zipContents = " + mainWindow.imageLibrary.zipContents);

    FinishInit(sender);
    
    if (videoWindow.videoComplete)
    {
        mainWindow.OnBeginMain();
        //alert("download complete and begun.");
    }
    else
    {
        //alert("Download complete, but video still playing");
    }
    
    var p = new Animation("pause", null, null, 0, 1, 0.2);
    p.OnStopScript = function() {
         if (showVideo) { videoWindow.Play();}
        // else { mainWindow.OnBeginMain(); livingRoomMenu.ShowHide(300,10,1);  }  
      }
    p.Play();
    //videoWindow.Play();
    
}

function OnDownloadFailed(sender, e)
{
    alert("download failed:" + sender);
}

function IsDefined(variable)
{
    if (variable == null)
        return false;
        
    if (variable == "undefined")
        return false;
        
    return true;
}

function GetSetting(obj, name, defaultVal)
{
    if (IsDefined(obj) && IsDefined(obj[name]) )
    {
        return obj[name];
    }
    
    return defaultVal;
}

function FinishInit(s)
{
    //alert("FinishInit");
    
    if (s == null)
        alert("NOT LOADING");
        
    // Create the main window.
    mainWindow = new MainWindow(s);
    mainWindow.OnSize();
    mainWindow.Show(null, true);
    
    // Create the Dialog.
    var revealOrientation = IsDefined(mainDialogSettings) ? ((mainDialogSettings["WipeOrientation"] == "vertical") ? 1 : 0) : 0;
    dialog = new Dialog(s, "Dialog1", revealOrientation);
    dialog.titleText = "Open up your digital life!";
    dialog.bodyText = "<Run>Select a room to see how Certified for</Run><LineBreak/><Run>Windows Vista products give you a</Run><LineBreak/><Run>superior experience.</Run>";
    dialog.BuildText();
    dialog.defaultX = GetSetting(mainDialogSettings, "x", 10);
    dialog.defaultY = GetSetting(mainDialogSettings, "y", 20);    
    
    revealOrientation = IsDefined(livingRoomDialogSettings) ? ((livingRoomDialogSettings["WipeOrientation"] == "vertical") ? 1 : 0) : 0;
    dialog2 = new Dialog(s, "LivingRoomDialog", revealOrientation);
    dialog2.titleText = "Living Room";
    dialog2.bodyText = "<Run>See how Certified for Windows Vista</Run><LineBreak/><Run>products connect your PC and your</Run><LineBreak/><Run>TV with a whole new world of</Run><LineBreak/><Run>entertainment.</Run>";
    dialog2.BuildText();
    dialogLivingRoom = dialog2;
    dialogLivingRoom.defaultX = livingRoomDialogSettings["x"];
    dialogLivingRoom.defaultY = livingRoomDialogSettings["y"];
    
    revealOrientation = IsDefined(homeOfficeDialogSettings) ? ((homeOfficeDialogSettings["WipeOrientation"] == "vertical") ? 1 : 0) : 0;
    dialogHomeOffice = new Dialog(s, "HomeOfficeDialog", revealOrientation);
    dialogHomeOffice.titleText = "Home Office";
    dialogHomeOffice.bodyText = "<Run>One of the busiest rooms in the house</Run><LineBreak/><Run>can also be one of the most fun.</Run><LineBreak/><Run>See how Certified for Windows Vista </Run><LineBreak/><Run>products make it happen.</Run>";
    dialogHomeOffice.BuildText();
    dialogHomeOffice.defaultX = homeOfficeDialogSettings["x"];
    dialogHomeOffice.defaultY = homeOfficeDialogSettings["y"];
    
    revealOrientation = IsDefined(gameRoomDialogSettings) ? ((gameRoomDialogSettings["WipeOrientation"] == "vertical") ? 1 : 0) : 0;
    dialogGameRoom = new Dialog(s, "GameRoomDialog", revealOrientation);
    dialogGameRoom.titleText = "Teen's Room";
    dialogGameRoom.bodyText = "<Run>Whether your game is staying ahead </Run><LineBreak/><Run>or staying in touch, this is where the</Run><LineBreak/><Run>action is.</Run>";
    dialogGameRoom.defaultX = gameRoomDialogSettings["x"];
    dialogGameRoom.defaultY = gameRoomDialogSettings["y"];
    dialogGameRoom.BuildText();
            
    // Create the NavBar
    navBar = new NavBar(s);
    navBar.xamlObject["Opacity"] = "1";
    //navBar.OnSize();    
    
    menu = new DropDownMenu(s, "HomeOffice");
    
    var homeOfficeTitleBody = "<Run>One of the busiest rooms in the house</Run><LineBreak/><Run>can also be one of the most fun.</Run><LineBreak/><Run>See how Certified for Windows Vista </Run><LineBreak/><Run>products make it happen.</Run>";
    menu.AddTitleStrip("Home Office", homeOfficeTitleBody);
    
    var sharePhotosBody = "<Run>Enjoy lightning-fast file access,</Run><LineBreak/><Run>stunning color fidelity, high-definition</Run><LineBreak/><Run>video support, and easy connections</Run><LineBreak/><Run>to features that help you make sharing</Run><LineBreak/><Run>your memories a natural part of</Run><LineBreak/><Run>your life.</Run>";
    menu.AddHeader("Share photos", sharePhotosBody);    
    var getMoreDoneBody = "<Run>Since Certified for Windows Vista</Run><LineBreak/><Run>productivity hardware and software </Run><LineBreak/><Run>are easier to use, better performing, </Run><LineBreak/><Run>and more secure, you get more done </Run><LineBreak/><Run>and you do it better.</Run>";
    menu.AddHeader("Get more done", getMoreDoneBody);  
    menu.AddHeader("Smarter software", "<Run>Get more security, reliability,</Run><LineBreak/><Run>and compatibility when you choose </Run><LineBreak/><Run>Certified for Windows Vista software.</Run>");      
    
    //menu.Build();
    
    
    livingRoomMenu = new DropDownMenu(s, "LivingRoom");
    var livingRoomTitleBody = "<Run>See how Certified for Windows Vista</Run><LineBreak/><Run>products connect your PC and your</Run><LineBreak/><Run>TV with a whole new world of</Run><LineBreak/><Run>entertainment</Run>";
    livingRoomMenu.AddTitleStrip("Living Room", livingRoomTitleBody);
    livingRoomMenu.AddHeader("Prime-time PC", "<Run>Accurate colors, seamless integration</Run><LineBreak/><Run>with Windows Vista and Windows</Run><LineBreak/><Run>Media Center, and advanced net-</Run><LineBreak/><Run>working bridge the gap between </Run><LineBreak/><Run>your PC and your TV.</Run>");  
    //livingRoomMenu.Build();

    gameRoomMenu = new DropDownMenu(s, "GameRoom");
    var gameRoomTitleBody = "<Run>Whether your game is staying ahead </Run><LineBreak/><Run>or staying in touch, this is where the</Run><LineBreak/><Run>action is.</Run>";
    gameRoomMenu.AddTitleStrip("Teen's Room", gameRoomTitleBody);
    gameRoomMenu.AddHeader("Get your game on", "<Run>Rich three-dimensional graphics,</Run><LineBreak/><Run>smooth video, and precision controls</Run><LineBreak/><Run>are just part of how Certified for</Run><LineBreak/><Run>Windows Vista gaming devices help</Run><LineBreak/><Run>you take your game to the next level.</Run>");    
    gameRoomMenu.AddHeader("Keep in touch!", "<Run>Get smooth video, easy setups, and </Run><LineBreak/><Run>precise control with Certified for </Run><LineBreak/><Run>Windows Vista devices designed to </Run><LineBreak/><Run>help you connect with the people </Run><LineBreak/><Run>who matter in your life.</Run>");  
    //gameRoomMenu.Build();
    
    
    var homeOfficeBB = new BillboardIcon(s, "HomeOfficeBB");
    
    var livingRoomBB = new BillboardIcon(s, "LivingRoomBB");
    
    var gameRoomBB = new BillboardIcon(s, "GameRoomBB");
    
    /*
	// Create a menu and build
	menu = new Menu(s);
	menu.BuildMenu();
	menu.ShowMenu(false);
	*/
	
	backgroundMovie = new BackgroundMovie(s);
	//backgroundMovie.CreateAssets();
	
	slider = new Slider(s);
	
	timerSB = s.findname("TimerSB");
	timerSB.addEventListener("Completed", OnTimerCompleted);
    
    if (!useJSTimer)
    {
        timerSB.Begin();
    }
    else
    {
        timerID = setInterval(TimedOut, 10);
    }
    
    _currTime = (new Date()).getTime();
    _lastTime = _currTime;
    	
    	
    pauseTimer = s.findname("PauseTimerSB");
    pauseTimer.addEventListener("Completed", OnPauseTimerCompleted);
    	
    // Construct house object.
    BuildHouse(s);
    
    StartStoryboardTicker();
    //dd("runningAnimations.length = " + runningAnimations.length);
    
    OnResized(null, null);
    
    mainWindow.imageLibrary = imageLibrary;
}

var storyboardTickerID = -1;
function StartStoryboardTicker()
{
    storyboardTickerID = setInterval(OnStoryboardTicker, 10);
}

function OnStoryboardTicker()
{
    timerSB.Begin();
}


var timerID = null;

function TimedOut()
{
    //alert("to");
    //clearTimeout(timerID);
    OnTimerCompleted(null, null);
}

function BuildHouse(s)
{
    house = new House();
    
    var livingRoom = new Room("LivingRoom");
    livingRoom.landingFrame = 0;
    livingRoom.dialog = dialogLivingRoom;
    livingRoom.menuX = 490;
    livingRoom.menuY = 20;  
    house.AddRoom("LivingRoom", livingRoom);
    livingRoom.AddNewDevice(s, "tv", "devices/tv.png", 263, 103, 108, 68, 50, true);
    livingRoom.AddNewDevice(s, "product", "devices/product.png", 172, 123, 67, 57, 33, true);
    livingRoom.AddNewDevice(s, "remote", "devices/remote.png", 350, 143, 35, 56, 34, true);
    livingRoom.AddNewDevice(s, "lr_router", "devices/lr_router.png", 406, 229, 47, 33, 30, true);
    livingRoom.AddNewDevice(s, "lr_router_rays", "devices/rays.png", 412, 215, 113, 29, 30, false);
    livingRoom.SetMenu(livingRoomMenu);
    livingRoom.Show(false);
    
    var gameRoom = new Room("GameRoom");
    gameRoom.landingFrame = 13;
    gameRoom.dialog = dialogGameRoom;
    gameRoom.menuX = 30;
    gameRoom.menuY = 20;  
    house.AddRoom("GameRoom", gameRoom);
    //gameRoom.AddNewDevice(s, "monitor19", "devices/monitor19.png", 259, 131, 80, 68, 30);
    gameRoom.AddNewDevice(s, "videocard", "devices/videocard.png", 260, 169, 47, 38, 30, true);
    gameRoom.AddNewDevice(s, "lifecam", "devices/lifecam.png", 533, 146, 20, 9, 30, true);
    gameRoom.AddNewDevice(s, "keyboard", "devices/keyboard.png", 487, 198, 79, 18, 30, true);
    gameRoom.AddNewDevice(s, "mouse", "devices/mouse.png", 569, 206, 20, 13, 30, true);
    gameRoom.AddNewDevice(s, "monitor19_2", "devices/monitor19_2.png", 506, 148, 74, 60, 30, true);
    gameRoom.AddNewDevice(s, "router", "devices/router.png", 302, 169, 48, 28, 30, true);
    gameRoom.AddNewDevice(s, "gr_router_rays", "devices/rays.png", 327, 165, 113, 29, 30, false);
    gameRoom.SetMenu(gameRoomMenu);
    gameRoom.Show(false);
      
    var homeOffice = new Room("HomeOffice");
    homeOffice.landingFrame = 0;
    homeOffice.dialog = dialogHomeOffice;
    homeOffice.AddNewDevice(s, "camera", "devices/camera.png", 483, 133, 28, 25, 30, true);//camera);
    homeOffice.AddNewDevice(s, "pictureframe", "devices/pictureframe.png", 475, 31, 42, 96, 30, true);//pictureFrame);
    homeOffice.AddNewDevice(s, "computer", "devices/computer.png", 334, 62, 93, 96, 50, true);
    homeOffice.AddNewDevice(s, "books", "devices/books.png", 132, 44, 59, 43, 30, true);//books);
    homeOffice.AddNewDevice(s, "notepad", "devices/notepad.png", 276, 124, 27, 20, 30, true);
    homeOffice.AddNewDevice(s, "videocamera", "devices/videocamera.png", 166, 111, 55, 28, 30, true);
    homeOffice.AddNewDevice(s, "printer", "devices/printer.png", 144, 185, 73, 49, 30, true);
    homeOffice.AddNewDevice(s, "router", "devices/homeoffice_router.png", 98, 135, 44, 41, 30, true);  
    homeOffice.AddNewDevice(s, "router_rays", "devices/rays.png", 120, 141, 113, 29, 30, false);  
    homeOffice.AddNewDevice(s, "flashdrive", "devices/flashdrive.png", 428, 146, 27, 16, 30, true);  
    homeOffice.SetMenu(menu);
    house.AddRoom("HomeOffice", homeOffice);
    homeOffice.Show(false);
    
    navBar.SelectItem(0, true);
}



function GoToRoom(_name)
{
    if (!videoWindow.videoComplete)
        return;
    
    if (navBar.InTransition && val)
    {
        //dd("!!!   !");
        return;
    }
            
    var name = _name;
    
    if (name.toLowerCase() == "Game Room") name = "GameRoom";
    else if (name.toLowerCase() == "Living Room") name = "LivingRoom";
    else if (name.toLowerCase() == "Home Office") name = "HomeOffice";
  
    house.GetRoom(name).defaultMenuIndex = 1;
    
    navBar.GoToRoom(name);
}

function GoToRoomAndOpenMenu(_roomName, menuIndex)
{
    if (navBar.InTransition)
    {
        //dd("!!!   !");
        return;
    }
    
    var roomName = _roomName;
    
    if (roomName == "Game Room" || roomName.toLowerCase() == "Teen's Room" || roomName.toLowerCase == "Teen'sRoom") roomName = "GameRoom";
    else if (roomName == "Living Room") roomName = "LivingRoom";
    else if (roomName == "Home Office") roomName = "HomeOffice";

    if (!videoWindow.videoComplete)
        return;
        
    if (house.IsRoomSelected(roomName) && house.GetSelectedRoom().menu.selectedHeaderIndex == menuIndex)
    {
        //dd("1");
        return;
    }
    else if (house.IsRoomSelected(roomName) && house.GetSelectedRoom().menu.selectedHeaderIndex != menuIndex)
    {
        //dd("2");
        house.GetSelectedRoom().menu.SetSelectedHeader(menuIndex);
    }
    else if (!house.IsRoomSelected(roomName))
    {
        //dd("3 roomName = " + roomName);

        // Set the default menu item for this room.
        house.GetRoom(roomName).defaultMenuIndex = menuIndex;
        
        navBar.GoToRoom(roomName);
        
        /*
        // pause
        var pauseAnim = new Animation("gtraomPause", null, null, 0, 1, 2.5);
        pauseAnim.OnStopScript = function() 
            {
                if (house.GetSelectedRoom() != null && house.GetSelectedRoom().menu.selectedHeaderIndex != menuIndex) house.GetSelectedRoom().menu.SetSelectedHeader(menuIndex);
            };
            
        pauseAnim.Play();
        */
    }
}

function SharePhotos()
{
    // Are we in the living room???
    var inLR = house.IsRoomSelected("HomeOffice");

    if (!inLR)
    {
        // Have to get to the Living Room.
        var anim = navBar.GoToRoom("HomeOffice");

        // If AN ALERT IS HERE, THE ONSTOP WILL WORK.
        
        var anim2 = menu.GetSelectedHeaderAnim(0);
        anim.OnStopAnimation = anim2;
        //anim.Play();
    }
}

function AddRunningAnimation(anim)
{
    var i = runningAnimations.push(anim);
    return i;
}

function RemoveRunningAnimation(anim)
{
    runningAnimations.pop(anim.AnimationIndex);
    anim.AnimationIndex = -1;
}

function OnTimerCompleted(s,e)
{
    _currTime = (new Date()).getTime();
    
    var _dT = (_currTime - _lastTime);
    
    //dd("dt = " + _dT);
    
 //   if (_dT > 10)
 //   {
        if (!useJSTimer)
        {
            //alert("restart");
            //timerSB.begin();
        }
        else
        {
            dd(_dT + "  !!! runningAnimations.length = " + runningAnimations.length);
            for (var i = 0; i < runningAnimations.length; i++)
            {
                runningAnimations[i].Tick();            
            }
        }
        
        _lastTime = _currTime;
 //   }
 //   else
 //   {
 //       pauseTimer.Begin();
 //   }
}

function OnPauseTimerCompleted(sender, e)
{
    OnTimerCompleted(sender, e);
}

var resizeCount = 0;

function OnResized(s,e)
{
    if (navBar == null)
        return;

    resizeCount++;
    if (resizeCount > 2) return;
    //dd("resizeCount = " + resizeCount);
    
    if (downloadComplete)
    {
        //alert("BEFORE: vw.top = " + videoWindow.xamlObject["Canvas.Top"] + "    height = " + videoWindow.xamlObject["Height"]);
        if (this.plugin.content.actualHeight != 294) return;//windowHeight == this.lastWindowHeight) return;
        
        navBar.OnSize();
        
        mainWindow.OnSize();
        videoWindow.OnSize();
        
        if (showVideo)
        {
	        var navTop = navBar.xamlObject["Canvas.Top"];
	        var navHeight = navBar.xamlObject["Height"];
	        navBar.xamlObject["Canvas.Top"] += navHeight;
	    }
        //alert("AFTER: vw.top = " + videoWindow.xamlObject["Canvas.Top"] + "    height = " + videoWindow.xamlObject["Height"]);
	}
}

function dd(txt)
{
    Debug(txt);
}

function Debug(txt)
{
    DebugBox.Text = txt;
}

function EvaluateUDI(fraction)
{
    alert("f = " + fraction);
    var startValue = 0;
    var endValue = 1;
    var dampingFactor = 0.8;
    
	//return startValue + (endValue - startValue) * fraction;
	var xi = dampingFactor;
	var omega = 2 * Math.PI;
	
	var beta = 1;
	var gamma = omega * Math.sqrt(1 - xi * xi);

	var extent = 6;
	
	var t = fraction * extent;

	var p1 = Math.exp(-1 * beta * t);
	var p2 = 	(
			  	beta/gamma * Math.sin(gamma * t) + 
				Math.cos(gamma * t)
				);
	var pp = p1 * p2;
	
	var ppp = 1 - pp;
	
	ppp = ppp;// / (Math.exp(-1 * beta * extent) * beta/gamma * Math.sin(gamma * extent) + Math.cos(gamma * extent)  );
	
	return startValue + (endValue - startValue) * ppp;
}

function ShowRoomDiv(index)
{
    for (var i = 0; i < house.rooms.length; i++)
    {
        var divName = house.rooms[i].name + "Div";
        //alert("i = " + i + "   divName = " + divName);
        setLayerVis(divName, false);
    }
    
    //alert("index = " + index + "   house.rooms[index] = " + house.rooms[index].name);
    if (index < 0 || index > house.rooms.length - 1 || house.rooms[index] == null)
        return;
     
    divName = house.rooms[index].name + "Div";
    setLayerVis(divName, true);
}

