﻿
House.prototype.rooms = null;
House.prototype.nameToRoom = null;
House.prototype.name = "House";
House.prototype.selectedRoom = -1;

function House()
{
    this.rooms = new Array();
    this.nameToRoom = new Object();
}

House.prototype.GetDeviceCoords = function()
{
    var text = "";
    
    for (var i = 0; i < this.rooms.length; i++)
    {
        var room = this.rooms[i];
        text += room.GetDeviceCoords();
        text += "&";
    }
    
    return text;
}

House.prototype.AddRoom = function(roomName, room)
{
    room.house = this;
    
    this.rooms.push(room);
    this.nameToRoom[roomName] = room;
}

House.prototype.GetRoom = function(name)
{
    return this.nameToRoom[name];
}

House.prototype.StopRooms = function()
{
    for (var i = 0; i < this.rooms.length; i++)
    {
        var room = this.rooms[i];
        room.DeselectDevices();
        
        
        room.SetDevicesColorState(true);
        //room.Show(false);
    }
}

House.prototype.IsRoomSelected = function(name)
{
    if (this.selectedRoom == -1)
        return false;
        
    var rc = this.rooms[this.selectedRoom].name == name;
    
    if (!rc)
    {
        rc = this.rooms[this.selectedRoom].name.toLowerCase() == name.toLowerCase();
    }
    
    return rc;
}

House.prototype.GetSelectedRoom = function()
{
    if (this.selectedRoom == -1)
        return null;
        
    return this.rooms[this.selectedRoom];
}
//----------------------------------------------

Room.prototype.house = null;
Room.prototype.menu = null;
Room.prototype.devices = null;
Room.prototype.nameToDevice = null;
Room.prototype.xamlObject = null;
Room.prototype.name = null;
Room.prototype.landingFrame = 0;
Room.prototype.menuX = -1;
Room.prototype.menuY = -1;
Room.prototype.defaultMenuIndex = 0;

function Room(xamlName)
{
    if (xamlName == null) this.xamlObject = null;
    else
    {
        this.xamlObject = MainCanvas.findname(xamlName);
    }
    this.name = xamlName;
    this.devices = new Array();
    this.nameToDevice = new Object();
}

Room.prototype.SetMenu = function(menu)
{
    this.menu = menu;
    menu.room = this;
}

Room.prototype.GetDeviceCoords = function()
{
    var text = "&";
    
    for (var i = 0; i < this.devices.length; i++)
    {
        text += this.devices[i].GetDeviceCoords();
        text += "\r\n";
    }
    return text;    
}

Room.prototype.AddDevice = function(deviceName, device)
{
    device.name = deviceName;
    device.room = this;
    
    this.devices.push(device); 
    this.nameToDevice[deviceName] = device;
    device.Show(false);
    // Construct the actual XAML.

}

Room.prototype.AddNewDevice = function(s, deviceName, imageUrl, x, y, w, h, auraWidth, hasGlow)
{
    var device = new Device();
    device.room = this;
    device.SetImage(s, this.deviceImage, imageUrl);
    device.name = deviceName;
    device.SetPosition(x,y);
    device.width = w;
    device.height = h;
    device.auraWidth = auraWidth;
    device.hasGlow = hasGlow;
    device.BuildDevice(s);
    
    this.AddDevice(deviceName, device);
}

Room.prototype.GetDevice = function(name)
{
    return this.nameToDevice[name];
}

Room.prototype.SetDevicesColorState = function(val, exclude)
{
    for (var i = 0; i < this.devices.length; i++)
    {
        if (exclude != null && this.devices[i].name == exclude.name)
        {
            dd("not turning off: " + this.devices[i].name);
            continue;
        }    
        this.devices[i].deviceImage["Opacity"] = val ? 1 : 0;
        this.devices[i].deviceImageGrey["Opacity"] = val ? 0 : 1;
    }
}

Room.prototype.Show = function(val)
{
    var rc = null;
    
    this.menu.animating = false;
    
    if (this.xamlObject != null)
    {
        //this.xamlObject["Visibility"] = val ? "Visible" : "Collapsed";
        //this.xamlObject["Opacity"] = val ? 1 : 0;
    }
    
    //if (val)
    {
        //MainCanvas.findname(this.name)["IsHitTestVisible"] = val;
        
        for (var i = 0; i < this.devices.length; i++)
        {
            if (this.devices[i].IsVisible())
                continue;
                
            var animVis = this.devices[i].GetShowAnim(val, 0.4);
            rc = animVis;
            
            this.devices[i].SetColorState(true);
            
            this.devices[i].AddListeners(val);
            
            if (animVis != null)
            {
                var pauseAnim = new Animation("pause", null, null, 0, 0.2, 0.2 + 0.3 * Math.random());
                pauseAnim.OnStopAnimation = animVis;
                pauseAnim.Play();
            }
        }
    }
    
//    if (rc != null)
//    {
//        rc.OnStopScript2 = function() { dd("stopped"); navBar.InTransition = false; }
//    }
}

Room.prototype.DeselectDevices = function()
{
    for (var i = 0; i < this.devices.length; i++)
    {
        var device = this.devices[i];
        
        device.SetColorState(false);
        
        if (device.selected)
        {
            device.SetSelected(false);
            
        }
    }
}

//-----------------------------------------------

Device.prototype.room = null;
Device.prototype.xamlObject = null;
Device.prototype.brushObject = null;
Device.prototype.imagePath = null;
Device.prototype.imagePathGrey = null;
Device.prototype.name = null;
Device.prototype.hasGlow = true;

function Device()
{
}

Device.prototype.x = 0;
Device.prototype.y = 0;
Device.prototype.width = 10;
Device.prototype.height = 10;
Device.prototype.blurCanvas = null;
Device.prototype.blurRotateTransform = null;
Device.prototype.blurScaleTransform = null;

Device.prototype.blur2Canvas = null;
Device.prototype.blur2RotateTransform = null;
Device.prototype.blur2ScaleTransform = null;
Device.prototype.blurImage = null;
Device.prototype.blur2Image = null;

Device.prototype.draggable = false;
Device.prototype.dragging = false;
Device.prototype.moveAnchor = new Object();
Device.prototype.auraWidth = 30;

Device.prototype.SetPosition = function(x, y)
{
    this.x = x;
    this.y = y;
    if (this.xamlObject == null) return;
    this.xamlObject["Canvas.Left"] = x;
    this.xamlObject["Canvas.Top"] = y; 
    
}

Device.prototype.GetDeviceCoords = function()
{
    var text = "";
    text += this.name + ".x=" + this.x + "&" + this.name + ".y=" + this.y + "&";
    return text;
}

//Device.prototype.OnMouseLeftButtonDown = function(sender, e)
//{
//    this.SetSelected(!this.selected);
//}



Device.prototype.SetImage = function(s, imageObj, path)
{
    this.imagePath = path;
    
    if (imageObj != null)
    {
    
        var justFilename = path;
        var indexOfSlash = path.indexOf("/")
        if (indexOfSlash != -1) 
            justFilename = path.substring(indexOfSlash + 1, path.length);
        imageObj.setSource(s, justFilename);
   }
}

Device.prototype.SetImageGrey = function(s, imageObj, path)
{
    this.imagePathGrey = path;
    if (imageObj != null)
    {
    
        var justFilename = path;
        var indexOfSlash = path.indexOf("/")
        if (indexOfSlash != -1) 
            justFilename = path.substring(indexOfSlash + 1, path.length);
        imageObj.setSource(s, justFilename);
   }
}

Device.prototype.built = false;
Device.prototype.deviceImage = null;
Device.prototype.deviceImageGrey = null;


Device.prototype.BuildDevice = function(s)
{
    if (this.imagePath == null)
    {
        //alert("Image not set on device - " + this.name);
        return;
    }
     this.imagePathGrey = this.imagePath.replace(".png", "-grey.png");
       
    var t = '';
//    this.x -= this.width;
//    this.y -= this.height;

    var glowWidth = this.auraWidth;
    //dd("imagePath = " + this.imagePath + "       imagePathGrey = " + this.imagePathGrey);
    
    t += '<Canvas Width="' +  this.width + '" Height="' + this.height + '" Canvas.Left="' + this.x + '" Canvas.Top="' + this.y + '"   >';
        t += '<Canvas Width="' +  this.width + '" Height="' + this.height + '"  >';
            t += '<Image Opacity="1" Width="' + this.width + '" Height="' + this.height + '" Source="' + this.imagePath + '"></Image>';
        t += '</Canvas>';
        t += '<Canvas Width="' +  this.width + '" Height="' + this.height + '"  >';
            t += '<Image Opacity="0" Width="' + this.width + '" Height="' + this.height + '" Source="' + this.imagePathGrey + '"></Image>';
        t += '</Canvas>';
    t += '</Canvas>';
    var deviceRootCanvas = plugin.content.createFromXaml(t);

    this.deviceImage = deviceRootCanvas.children.getItem(0).children.getItem(0);
    this.deviceImageGrey = deviceRootCanvas.children.getItem(1).children.getItem(0);
    //alert("deviceImageGrey = " + this.deviceImageGrey);
    
    this.SetImage(s, this.deviceImage, this.imagePath);
    
    

    var justFilename = this.imagePath;
    if (justFilename.indexOf("/") != -1)
        justFilename = justFilename.substring(justFilename.indexOf("/") + 1, justFilename.length);
    
    this.SetImageGrey(s, this.deviceImageGrey, this.imagePathGrey);

    //alert("mainWindow.imageLibrary = " + mainWindow.imageLibrary);// + "      justFilename = " + justFilename);
    //this.deviceImage.setSource(mainWindow.imageLibrary.zipContents, justFilename);

    //----------------------------------------------------------
    // the blur.
    //glowWidth = 15;
    var glowLeft = this.width/2 - glowWidth/2;//this.width/4;
    var glowTop = this.height/2 - glowWidth/2;//this.width/4;
    var hasGlowOpac = "1";
    if (!this.hasGlow) hasGlowOpac = "0";
    
    t = '<Canvas Canvas.Left="' + glowLeft + '" Canvas.Top="' + glowTop + '" Width="' + glowWidth + '" Height="' + glowWidth + '" Opacity="0" >';
        t += '<Canvas Canvas.Left="0" Canvas.Top="0"  Width="' + glowWidth + '" Height="' + glowWidth + '" Opacity="' + hasGlowOpac + '" >';
        t += '<Canvas.Background>';
            t += '<ImageBrush>';// ImageSource="' + "devices/aura_mid.png" + '">';
            t += '</ImageBrush>';
        t += '</Canvas.Background>';
        t += '</Canvas>';
    t += '</Canvas>';
    //t += '<Canvas Canvas.Left="' + glowWidth/2 + '" Canvas.Top="' + glowWidth/2 + '" Width="5" Height="5" ><Rectangle Fill="#ffffff00" Width="5" Height="5"/></Canvas>';
    this.blurCanvas = plugin.content.createFromXaml(t);
    this.blurImage = this.blurCanvas.children.getItem(0)["Background"];
    
    this.SetImage(s, this.blurImage, "devices/aura_mid.png");
    
    
    //t = '<Canvas.RenderTransform>';
   // t += '</Canvas.RenderTransform>';
    //var rtx = plugin.content.createFromXaml(t);
    
    t = '<TransformGroup>';
    t += '</TransformGroup>';
    var txgroup = plugin.content.createFromXaml(t);
    //rtx.children.add(txgroup);
       
    t = '<RotateTransform CenterX="' + glowWidth/2 + '" CenterY="' + glowWidth/2 + '" Angle="90"/>';
    var rottx = plugin.content.createFromXaml(t);
    
    t = '<ScaleTransform CenterX="' + glowWidth/2 + '" CenterY="' + glowWidth/2 + '" ScaleX="1" ScaleY="1"/>';
    var scaletx = plugin.content.createFromXaml(t);
    
    txgroup.children.add(rottx);
    txgroup.children.add(scaletx);
    
    this.blurCanvas.RenderTransform = txgroup;
    
    this.blurRotateTransform = rottx; 
    this.blurScaleTransform = scaletx; 
    
    deviceRootCanvas.children.insert(0, this.blurCanvas);
    //---------------------------------------------
    

    //----------------------------------------------------------
    // the blur.
    t = '<Canvas Canvas.Left="' + glowLeft + '" Canvas.Top="' + glowTop + '" Width="' + glowWidth + '" Height="' + glowWidth + '" Opacity="0" >';
        t += '<Canvas Canvas.Left="0" Canvas.Top="0"  Width="' + glowWidth + '" Height="' + glowWidth + '" >';
        t += '<Canvas.Background>';
            t += '<ImageBrush>';// ImageSource="' + "devices/aura_mid.png" + '">';
            t += '</ImageBrush>';
        t += '</Canvas.Background>';
        t += '</Canvas>';
    t += '</Canvas>';
    
    this.blur2Canvas = plugin.content.createFromXaml(t);
    this.blur2Image = this.blur2Canvas.children.getItem(0)["Background"];
     
    this.SetImage(s, this.blur2Image, "devices/aura_mid.png");
   
    //t = '<Canvas.RenderTransform>';
    //t += '</Canvas.RenderTransform>';
    //var rtx2 = plugin.content.createFromXaml(t);
    
    t = '<TransformGroup>';
    t += '</TransformGroup>';
    var txgroup2 = plugin.content.createFromXaml(t);
    //rtx.children.add(txgroup);
       
    t = '<RotateTransform CenterX="' + glowWidth/2 + '" CenterY="' + glowWidth/2 + '" Angle="90"/>';
    var rottx2 = plugin.content.createFromXaml(t);
    
    t = '<ScaleTransform CenterX="' + glowWidth/2 + '" CenterY="' + glowWidth/2 + '" ScaleX="1" ScaleY="1"/>';
    var scaletx2 = plugin.content.createFromXaml(t);
    
    txgroup2.children.add(rottx2);
    txgroup2.children.add(scaletx2);
    
    this.blur2Canvas.RenderTransform = txgroup2;
    
    this.blur2RotateTransform = rottx2; 
    this.blur2ScaleTransform = scaletx2; 
    
    deviceRootCanvas.children.insert(0, this.blur2Canvas);
    //---------------------------------------------


    
    /*
    var token = deviceRootCanvas.addEventListener(
        "MouseLeftButtonDown", 
        Silverlight.createDelegate(this, this.OnMouseLeftButtonDown)
        );
      */  
    //alert("this.room.xamlObject = " +1);    
    this.room.xamlObject.children.add(deviceRootCanvas);
        
    this.deviceRootCanvas = deviceRootCanvas;
    this.xamlObject = deviceRootCanvas;
    this.built = true;
}

Device.prototype.deviceRootCanvas = null;


Device.prototype.OnMouseLeftButtonDown = function(sender, e)
{
    //dd("down on " + this.name);
    //this.SetSelected(!this.selected);
    
    this.dragging = true;

    this.moveAnchor["x"] = e.GetPosition(null).x - this.deviceRootCanvas["Canvas.Left"];
    this.moveAnchor["y"] = e.GetPosition(null).y - this.deviceRootCanvas["Canvas.Top"];
}

Device.prototype.OnMouseLeftButtonUp = function(sender, e)
{
    //dd("Up!");
    //this.SetSelected(!this.selected);
    
    this.moveAnchor["x"] = -1;
    this.moveAnchor["y"] = -1;
    
    this.dragging = false;
}

Device.prototype.OnMouseMove = function(sender, e)
{
    //dd("move " + e.GetPosition(null).x);
    if (this.dragging)
    {
        this.x = e.GetPosition(null).x;
        this.y = e.GetPosition(null).y;
        
        this.deviceRootCanvas["Canvas.Left"] = this.x - this.moveAnchor["x"];
        this.deviceRootCanvas["Canvas.Top"] = this.y - this.moveAnchor["y"];
    }
}


Device.prototype.FlashAnimation = null;

Device.prototype.TriggerBlurAnimation = function(dur, roundTrip)
{
    var wStart = 1;
    var wEnd = 3;
    var hStart = 1;
    var hEnd = 3;    
    var opacStart = 0;
    var opacEnd = 0.6;
    
    // band-aid.
    if (this.name == "computer") 
        hEnd = 3.8;
    else if (this.name == "pictureframe")
    {
        hEnd = 3.5;
        wEnd = 3.4;
        opacEnd = 0.4;
    }

    var anim1 = new Animation("growW", this.blurScaleTransform, "ScaleX", wStart, wEnd, dur);
    var anim2 = new Animation("growH", this.blurScaleTransform, "ScaleY", hStart, hEnd, dur);
    var anim3 = new Animation("blurOpac", this.blurCanvas, "Opacity", opacStart, opacEnd, dur);
    var anim4 = new Animation("blurSpin", this.blurRotateTransform, "Angle", 0, 360, dur);
     
    var comp = new CompoundAnimation();
    comp.Add(anim1);
    comp.Add(anim2);
    comp.Add(anim3);
    comp.duration = dur;
    //comp.Add(anim4);
        
    if (roundTrip)
        comp.baseAnimation.easeFunction = function(frac) { return Math.sin(Math.PI/2 * frac); };
    else
        comp.baseAnimation.easeFunction = function(frac) { return (1 - Math.cos(Math.PI/2 * frac))/2; };
    
    this.FlashAnimation = comp;
    comp.Play();
}


Device.prototype.IsVisible = function()
{
    if (this.xamlObject == null) return false;
    return (this.xamlObject["Visibility"] == "Visible") && (this.xamlObject["Opacity"] != 0);
}

Device.prototype.Show = function(val)
{
    if (!this.built)
        this.BuildDevice(null);

    if (this.xamlObject != null)
    {
        this.xamlObject["Visibility"] = val ? "Visible" : "Collapsed";
        this.xamlObject["Opacity"] = val ? 1 : 0;
    }
    

}

Device.prototype.AddListeners = function(val)
{
    if (val && this.draggable)
    {
        //alert( "this = " + this.name + "        w = " + this.deviceRootCanvas["Width"] + "    h = " + this.deviceRootCanvas["Height"] );
//      var token = deviceRootCanvas.addEventListener(
//        "MouseLeftButtonDown", 
//        Silverlight.createDelegate(this, this.OnMouseLeftButtonDown)
//        );
      
        this.MouseLeftButtonDownToken = this.deviceRootCanvas.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.OnMouseLeftButtonDown));
        this.MouseLeftButtonUpToken = this.deviceRootCanvas.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.OnMouseLeftButtonUp));
        this.MouseMoveToken = this.deviceRootCanvas.addEventListener("MouseMove", Silverlight.createDelegate(this, this.OnMouseMove));
    }
    else if (this.MouseLeftButtonDownToken != null)
    {
        this.deviceRootCanvas.removeEventListener(this.MouseLeftButtonDownToken);
        this.deviceRootCanvas.removeEventListener(this.MouseLeftButtonUpToken);
        this.deviceRootCanvas.removeEventListener(this.MouseMoveToken);
    } 
}

Device.prototype.MouseLeftButtonDownToken = null;
Device.prototype.MouseLeftButtonUpToken = null;
Device.prototype.MouseMoveToken = null;

Device.prototype.GetShowAnim = function(val, dur)
{
    if (!this.built)
        this.BuildDevice();

    if (this.xamlObject != null)
    {
        this.xamlObject["Visibility"] = val ? "Visible" : "Collapsed";

        var animVis = new Animation("animVis", this.xamlObject, "Opacity",
            val ? 0 : 1,
            val ? 1: 0,
            dur
            );
            
        return animVis;
    }
    return null;
}

Device.prototype.selected = false;
Device.prototype.SelectedAnimation = null;
Device.prototype.ToSelectedAnimation = null;

Device.prototype.SetSelected = function(val)
{
    var wasSelected = this.selected;
    var dur = 0.2;
    
    //this.deviceImage["Opacity"] = val ? 1 : 0;
    //this.deviceImageGrey["Opacity"] = val ? 0 : 1;
    
    
    if (val == true)
    {
        if (this.SelectedAnimation != null)
        {
            this.SelectedAnimation.baseAnimation.OnStopAnimation = null;
            this.SelectedAnimation.baseAnimation.Stop();
        }
        
        
        /*
        // Start the animation.
        var startAngle = this.blur2RotateTransform.Angle;
        this.SelectedAnimation = new CompoundAnimation();
        var sel1 = new Animation("blur2Spin", this.blur2RotateTransform, "Angle", startAngle, startAngle + 360, 1);
        var sel2 = new Animation("blur2Opac", this.blur2Canvas, "Opacity", 0.2, 0.4, 1);
        sel2["freq"] = 0.631;
        sel2.easeFunction = function(frac) { return (1 + Math.sin(frac * 2 * Math.PI * this["freq"]))/2; };
        sel2.OnStopScript = function() { this["freq"] = 0.5 + 4 * Math.random();  }
        
        sel1.ease = false;
        sel2.ease = false;
        this.SelectedAnimation.Add(sel1);
        this.SelectedAnimation.Add(sel2);
        this.SelectedAnimation.baseAnimation.ease = false;
        //this.SelectedAnimation.baseAnimation.OnStopAnimation = this.SelectedAnimation;
        
        var wStart = 1;
        var wEnd = 4;
        var hStart = 1;
        var hEnd = 4;    
        // Ramp up.
        var anim1 = new Animation("growW", this.blur2ScaleTransform, "ScaleX", wStart, wEnd, dur);
        var anim2 = new Animation("growH", this.blur2ScaleTransform, "ScaleY", hStart, hEnd, dur);
        var anim3 = new Animation("blurOpac", this.blur2Canvas, "Opacity", 0, 0.2, dur);
        var comp = new CompoundAnimation();
        comp.easeFunction = null;
        comp.duration = dur;
        comp.baseAnimation.duration = dur;
        comp.Add(anim1);
        comp.Add(anim2);
        comp.Add(anim3);
        comp.baseAnimation.OnStopAnimation = this.SelectedAnimation;
        this.ToSelectedAnimation = comp;
        comp.Play();
        */        
        
        //this.room.SetDevicesColorState(false, this);
        
        this.SetColorState(true);
        
        this.TriggerBlurAnimation(40, true);
    }
    else
    {
        //alert("fade " + this.name);
        var fade = new Animation("fade", this.blurCanvas, "Opacity", 1, 0, 0.3);
        fade.Play();
        
        if (this.SelectedAnimation != null)
        {
            this.ToSelectedAnimation.baseAnimation.duration = 0.0001;
            this.SelectedAnimation.duration = 0.00001;
            this.SelectedAnimation.baseAnimation.ease = false;
            this.ToSelectedAnimation.baseAnimation.easeFunction = function(frac) { return 1 - frac; }
            this.ToSelectedAnimation.baseAnimation.OnStopAnimation = null;
            this.SelectedAnimation.baseAnimation.OnStopAnimation = this.ToSelectedAnimation;
            //this.SelectedAnimation.Stop();
        }
    }
    this.selected = val;
}

Device.prototype.SetColorState = function(val)
{
    this.deviceImage["Opacity"] = val ? 1 : 0;
    this.deviceImageGrey["Opacity"] = val ? 0 : 1;
}