DropDownMenu.prototype.RootName = "DropDownMenu";

DropDownMenu.prototype.headers = null;
DropDownMenu.prototype.headerCanvases = null;
DropDownMenu.prototype.NumHeaders = 0;

DropDownMenu.prototype.HeaderHeight = 23;
DropDownMenu.prototype.HeaderWidth = 198;
DropDownMenu.prototype.HeaderLeftMargin = 10;
DropDownMenu.prototype.HeaderSpacer = 8;
DropDownMenu.prototype.BodyHeight = 85;//115;
DropDownMenu.prototype.TitleHeight = 27;
DropDownMenu.prototype.TitleWidth = 198;

DropDownMenu.prototype.body = null;
DropDownMenu.prototype.enabled = false;
DropDownMenu.prototype.room = null;

function DropDownMenu(s, roomName)
{
    this.s = s;
    this.RootName = roomName + "DropDownMenu";
    
    this.plugin = s.getHost();
	this.MainCanvas = this.s.findname("MainCanvas");
	this.xamlObject = this.MainCanvas.findname(this.RootName);

    //alert("this.RootName = " + this.RootName + "      xaml = " + this.xamlObject);

    this.headers = new Object();
    this.headerCanvases = new Array();

    this.xamlObject["Visibility"] = "Collapsed";
	//this.Build();
	
	//this.DefineTabs();
}

DropDownMenu.prototype.tabIndices = new Object();
DropDownMenu.prototype.tabNames = new Object();

DropDownMenu.prototype.DefineTabs = function()
{
	this.tabIndices["tab1"] = -1;
	this.tabIndices["tab2"] = 0;
	this.tabIndices["tab4"] = 1;
	this.tabIndices["tab8"] = 2;
	
	this.tabNames[-1] = "tab1";
	this.tabNames[0] = "tab2";
	this.tabNames[1] = "tab4";
	this.tabNames[2] = "tab8";
	
}

DropDownMenu.prototype.Build = function()
{
    this.AddTitleStrip("Home Office");
    
    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>";
    this.AddHeader("Share photos", sharePhotosBody);   
    
    this.AddHeader("Get more done", "For real!");  
   
    var smarterSoftwareBody = "<Run>Since Certified for Windows Vista</Run><LineBreak/><Run>productivity hardware and software are</Run><LineBreak/><Run>easier to use, better performing, and</Run><LineBreak/><Run>more secure, you get more done and</Run><LineBreak/><Run>you do it better.</Run>";
    this.AddHeader("Smarter software", smarterSoftwareBody);  
}

DropDownMenu.prototype.titleStrip = null;
DropDownMenu.prototype.titleHeader = null;

DropDownMenu.prototype.AddTitleStrip = function(text, titleBody)
{
    var t = '';
    
    // Create the container.
    t += '<Canvas Canvas.Left="0" Canvas.Top="' + this.TitleHeight + '" Width="' + this.TitleWidth + '" Height="' + this.HeaderHeight + '" Cursor="Hand">';
        t += '<Canvas.Background>';
            t += '<ImageBrush ImageSource="img/menu_title_bg.png">';
            t += '</ImageBrush>';
        t += '</Canvas.Background>';
    t += '</Canvas>';
    var container = this.plugin.content.createFromXaml(t);
    this.titleStrip = container;
    
    // Create label.   
    t = '<TextBlock Foreground="#0d6d9a" Canvas.Left="' + this.HeaderLeftMargin + '" Canvas.Top="3" FontFamily="Verdana" FontSize="12" FontWeight="Bold">' + text + '</TextBlock>';
    var textBlock = this.plugin.content.createFromXaml(t);
    container.children.add(textBlock);
      
    this.titleHeader = new MenuHeader( this, text, container, null, textBlock, null, titleBody, null, null);
    this.titleHeader.changeTextColorWhenSelected = false;
    
    //var menuHeader = new MenuHeader(this, text, container, null, textBlock, null, bodyText);
    
    this.headers[0] = this.titleHeader;//this.headers[this.NumHeaders] = this.titleHeader;
    this.headerCanvases.push(container);
    this.NumHeaders++;
    
    this.xamlObject.children.add(container);
    
    container.addEventListener("MouseLeftButtonUp", 
        Silverlight.createDelegate(this, this.OnHeaderMouseUp)
            );
    container.addEventListener("MouseLeftButtonDown", 
        Silverlight.createDelegate(this, this.OnHeaderMouseDown)
            );
    return null;//return menuHeader;
}

DropDownMenu.prototype.AddHeader = function(text, bodyText)
{
    var t = '';
    
    // Create the container.
    var headerTop = 22 + this.NumHeaders * this.HeaderHeight;
        
    t += '<Canvas Canvas.Left="0" Canvas.Top="' + headerTop + '" Width="' + this.HeaderWidth + '" Height="' + this.HeaderHeight + '" Cursor="Hand">';
        t += '<Canvas.Background>';
            t += '<ImageBrush ImageSource="img/menu_top_bg.png">';
            t += '</ImageBrush>';
        t += '</Canvas.Background>';
    t += '</Canvas>';
    var container = this.plugin.content.createFromXaml(t);
    
    // Create label.   
    t = '<TextBlock Foreground="#ff000000" Canvas.Left="' + this.HeaderLeftMargin + '" Canvas.Top="3" FontFamily="Verdana"  FontSize="9.5" FontWeight="Normal">' + text + '</TextBlock>';
    var textBlock = this.plugin.content.createFromXaml(t);
    container.children.add(textBlock);
    
    
    
    
    // Create arrow.
    t = '<Canvas Canvas.Left="' + (textBlock["ActualWidth"] + this.HeaderLeftMargin + this.HeaderSpacer) + '" Canvas.Top="7">';
    t += '</Canvas>';
    var arrowContainer = this.plugin.content.createFromXaml(t);
      
   // t = '<Path Width="10" Height="7" Stretch="Fill" StrokeThickness="0" Fill="#FF000000" Data="F1 M 152.889,422.222L 289.778,469.333L 151.111,510.222L 171.556,467.556L 152.889,422.222 Z ">';
   // t += '</Path>';
   // var arrowPath = this.plugin.content.createFromXaml(t);
    
    t = '<Path Width="10" Height="7" Stretch="Fill" StrokeThickness="0" Fill="#FF000000" Data="F1 M 152.889,422.222L 289.778,469.333L 151.111,510.222L 171.556,467.556L 152.889,422.222 Z ">';
	t += '<Path.RenderTransform>';
    t += '<RotateTransform CenterX="0" CenterY="0" Angle="0"/>';
    t += '</Path.RenderTransform>';
    t += '</Path>';
    var arrowPath = this.plugin.content.createFromXaml(t);
    
    //arrowPath["RenderTransform"] = arrowRenderTransform;
        
    arrowContainer.children.add(arrowPath);
        
    container.children.add(arrowContainer);
    
    var arrow1 = arrowContainer;
    
     // Create ROTATED arrow.
    t = '<Canvas Canvas.Left="' + (textBlock["ActualWidth"] + this.HeaderLeftMargin + this.HeaderSpacer + 7) + '" Canvas.Top="7" Visibility="Collapsed">';
    t += '</Canvas>';
    var arrowContainer2 = this.plugin.content.createFromXaml(t);
      
   // t = '<Path Width="10" Height="7" Stretch="Fill" StrokeThickness="0" Fill="#FFFFFFFF" Data="F1 M 152.889,422.222L 289.778,469.333L 151.111,510.222L 171.556,467.556L 152.889,422.222 Z ">';
   // t += '</Path>';
   // var arrowPath2 = this.plugin.content.createFromXaml(t);
    
    t = '<Path Width="10" Height="7" Stretch="Fill" StrokeThickness="0" Fill="#FFFFFFFF" Data="F1 M 152.889,422.222L 289.778,469.333L 151.111,510.222L 171.556,467.556L 152.889,422.222 Z ">';
	t += '<Path.RenderTransform>';
    t += '<RotateTransform CenterX="0" CenterY="0" Angle="90"/>';
    t += '</Path.RenderTransform>';
    t += '</Path>';
    var arrowPath2 = this.plugin.content.createFromXaml(t);
    
   // arrowPath2["RenderTransform"] = arrowRenderTransform2;
        
    arrowContainer2.children.add(arrowPath2);
        
    container.children.add(arrowContainer2);   
    
    var arrow2 = arrowContainer2;
    
       
    //dd(this.RootName +" adding event");
    // Setup events.
    container.addEventListener("MouseLeftButtonUp", 
        Silverlight.createDelegate(this, this.OnHeaderMouseUp)
            );
    container.addEventListener("MouseLeftButtonDown", 
        Silverlight.createDelegate(this, this.OnHeaderMouseDown)
            );
    
    var menuHeader = new MenuHeader(this, text, container, null, textBlock, arrowPath, bodyText, arrow1, arrow2);
    
    this.SetHeader(this.NumHeaders, menuHeader);//this.headers[this.NumHeaders] = menuHeader;
    this.headerCanvases.push(container);
    this.NumHeaders++;
    this.xamlObject.children.add(container);
    
    return menuHeader;
}


DropDownMenu.prototype.OnHeaderMouseDown = function(sender, e)
{
    if (this.animating) return;
    
    if (!this.enabled) return;
    
    var index = -1;


        
    for (var i = 0; i < this.headerCanvases.length; i++)
    {        
        if (sender["Canvas.Top"] == this.headerCanvases[i]["Canvas.Top"])
            index = i;
    }

    var menuHeader = this.GetHeader(index);//this.headers[index];
    
    this.MouseDownHeaderIndex = index;
    //menuHeader.SetState(1);
}

DropDownMenu.prototype.OnHeaderMouseUp = function(sender, e)
{
    if (!this.enabled) return;
    
    if (this.animating || this.locked)
    {
        //dd("clicked while animating");
        return;
    }
    
    if (navBar.InTransition)
    {
        //alert("navBar.InTransition = " + true);
        return;
    }
    
    else
    {
        //dd("");
    }    
    
    var index = -1;
    var menuHeader = null;

    //this.animating = true;


    if (menuHeader == null)
    {        
        for (var i = 0; i < this.headerCanvases.length; i++)
        {
            if (sender["Canvas.Top"] == this.headerCanvases[i]["Canvas.Top"])
                index = i;
        }

        var menuHeader = this.GetHeader(index);//this.headers[index];
        //dd("Clicked: " + menuHeader.text);
    }
    
    this.MouseUpHeaderIndex = index;
    
    if (this.MouseDownHeaderIndex != this.MouseUpHeaderIndex)
    {
        return;
    }
    
    menuHeader.SetState(1);
        
    
    this.locked = true;
    var disableAnim = new Animation("disable", null, null, 0, 1, 0.6);
    disableAnim.OnStopScript2 = function() { house.GetSelectedRoom().menu.locked = false; }
    disableAnim.Play();
   
    this.SetSelectedHeader(index);
}

DropDownMenu.prototype.MouseDownHeaderIndex = -1;
DropDownMenu.prototype.MouseUpHeaderIndex = -1;

DropDownMenu.prototype.selectedHeaderIndex = -1;
DropDownMenu.prototype.animating = false;
DropDownMenu.prototype.locked = false;

DropDownMenu.prototype.SetSelectedHeader = function(index)
{  
    var anim = this.GetSelectedHeaderAnim(index);
    
    anim.baseAnimation.OnPostFractionScript = function(frac) { if (frac < 1) { house.GetSelectedRoom().menu.animating = true; } else { house.GetSelectedRoom().menu.animating = false; } }

    /*
    anim.baseAnimation.OnPostFractionScript =
            function(frac)
            {
                if (frac < 1) 
                { 
                   if (house.GetSelectedRoom() != null && house.GetSelectedRoom().menu != null)
                        house.GetSelectedRoom().menu.animating = true;
                }
                else 
                {
                    //if (house.GetSelectedRoom() != null && house.GetSelectedRoom().menu != null)
                        //house.GetSelectedRoom().menu.animating = false; 
                }
            }
    */
          
    this.animating = true;        
    anim.Play();
}

DropDownMenu.prototype.GetHeader = function(index)
{
    if (index == -2) return this.titleHeader;
    else
        return this.headers[index];
}

DropDownMenu.prototype.SetHeader = function(index, header)
{
    if (index == -2)
        this.titleHeader = header;
    else
        this.headers[index] = header;
}

DropDownMenu.prototype.GetSelectedHeaderAnim = function(index)
{
    var rc = null;
    
    var originalSelectedHeaderIndex = this.selectedHeaderIndex;
    
    //alert("index = " + index + "            sel = " + this.selectedHeaderIndex);
    var selChanged = (index != this.selectedHeaderIndex);
    var needToRecover = (this.selectedHeaderIndex != -1 ) || (this.selectedHeaderIndex != -1 && this.selectedHeaderIndex != index);
    
    var recoverAnim = new CompoundAnimation();

    recoverAnim.baseAnimation.duration = 0.3;
    rc = recoverAnim;
    
    if (this.selectedHeaderIndex != -1)
    {
        if (this.selectedHeaderIndex != index)
            selChanged = true;
            
        var oldSelectedHeader = this.GetHeader(this.selectedHeaderIndex);//this.headers[this.selectedHeaderIndex];
        oldSelectedHeader.Select(false);
        
        for (var i = this.selectedHeaderIndex + 1; i < (this.headerCanvases.length); i++)
        {
            var menuHeaderCanvas = this.headerCanvases[i];
            var top = menuHeaderCanvas["Canvas.Top"];
            var newTop = top - this.BodyHeight;
            
            var startVal = top;
            var endVal = newTop;
            
            //alert("start, end = " + startVal + ", " + endVal);
            var anim = new Animation("drop" + i, menuHeaderCanvas, "Canvas.Top", startVal, endVal, 0.3);
            anim.OnPostFractionScript = function(frac) { if (frac < 1) { if (house.GetSelectedRoom() != null) { house.GetSelectedRoom().menu.animating = true; }} else {} }
            recoverAnim.Add(anim);
        }
        
        if (this.selectedHeaderIndex && this.selectedHeaderIndex != -1)
        {
            var bodyTop = oldSelectedHeader.body.xamlObject["Canvas.Top"];
            var bodyDropAnim = new Animation("bodyDrop" + i, oldSelectedHeader.body.xamlObject, "Canvas.Top", bodyTop, bodyTop - this.BodyHeight, 0.3);
            bodyDropAnim.OnPostFractionScript = function(frac) { if (frac < 1) { house.GetSelectedRoom().menu.animating = true; } else {  } }

            var bodyDropHAnim = new Animation("bodyDropH" + i, oldSelectedHeader.body.xamlObject, "Height", this.BodyHeight, 0, 0.3);
            bodyDropHAnim.OnPostFractionScript = function(frac) { if (frac < 1) { house.GetSelectedRoom().menu.animating = true; } else {} }
      
            var bodyClipDropAnim = new Animation("bodyClipDropH" + i, oldSelectedHeader.body, "clipHeight", this.BodyHeight, 0, 0.3);
            bodyClipDropAnim.OnPostFractionScript = function(frac) { if (frac < 1) { house.GetSelectedRoom().menu.animating = true; } else {} }
            
            
            bodyClipDropAnim.OnFractionScript = Silverlight.createDelegate(oldSelectedHeader.body, oldSelectedHeader.body.Update);
                
            recoverAnim.Add(bodyClipDropAnim);
            

            //recoverAnim.Add(bodyDropAnim);
            recoverAnim.Add(bodyDropHAnim);
        }
    }
    
    if (index != -1 )
    {
    
        var newSelectedHeader = this.GetHeader(index);//this.headers[index];
        
        if (!selChanged)
        {
            newSelectedHeader.Select(false);
            this.selectedHeaderIndex = -1;
            //alert("unset");
        }
        else
        {
        
            newSelectedHeader.Select(true);
        }
        
        if (index != this.selectedHeaderIndex && this.selectedHeaderIndex != -1)
            selChanged = true;
        
        if (selChanged)
        {
            this.selectedHeaderIndex = index;
        }
    }
    else if (index == -1 && originalSelectedHeaderIndex != -1)
    {
        needToRecover = true;
        selChanged = false;
    }
    else if (originalSelectedHeaderIndex == -1 && index != -1)
    {
        needToRecover = false;
        selChanged = true;
    }
    
     
//    if (this.selectedHeaderIndex == -1)
//        alert(" " + originalSelectedHeaderIndex + ", " + index + " :::: selChanged = " + selChanged + "          needToRecover = " + needToRecover);
      
    if (!selChanged && needToRecover)
    {
        //recoverAnim.Play();
        rc = recoverAnim;
        
        this.selectedHeaderIndex = -1;

    }
    else if (selChanged && needToRecover)//this.selectedHeaderIndex != -1)
    {
        //recoverAnim.Play();
        rc = recoverAnim;
        recoverAnim.baseAnimation.OnStopScript = Silverlight.createDelegate(this, this.DropHeader);
        this.selectedHeaderIndex = index;
    }
    else
    {
        this.DropHeader();
        this.selectedHeaderIndex = index;
    }

    if (this.selectedHeaderIndex == -1)
    {
        if (house.selectedRoom != -1)
        {
            house.rooms[house.selectedRoom].DeselectDevices();
            house.rooms[house.selectedRoom].SetDevicesColorState(true);
        }
    }
    
    //-----------------
    if (this.OnSelectionChangeScript != null)
        this.OnSelectionChangeScript(this.selectedHeaderIndex, originalSelectedHeaderIndex);
 
     
    return rc;  
}

DropDownMenu.prototype.OnSelectionChangeScript = function(index, originalSelectedHeaderIndex)
{
    var room = house.GetSelectedRoom();
    
    if (room.dialog != null && room.dialog.IsVisible())
    {
        //room.dialog.xamlObject["Opacity"] = 0;
        room.dialog.ShowHide(-1, -1, 0.04);
    }
    
    if (index == -1)
    {
        //toggleLayer(this.tabNames[originalSelectedHeaderIndex]);
    }
    
    
    if (room.name == "HomeOffice")
    {
        if (index == 0)
        {
            OnSilverlightEvent("MenuChanged", "Home Office");
        }
        else if (index == 1)
        {
            //toggleLayer(this.tabNames[index]);
            room.DeselectDevices();
            // Share photos. Light up camera, computer, frame.
            room.GetDevice("camera").SetSelected(true);
            room.GetDevice("computer").SetSelected(true);
            room.GetDevice("pictureframe").SetSelected(true);
            room.GetDevice("printer").SetSelected(true);
            room.GetDevice("videocamera").SetSelected(true);
            
            OnSilverlightEvent("MenuChanged", "Share photos");
        }
        else if (index == 2)
        {
            //toggleLayer(this.tabNames[index]);
            room.DeselectDevices();
            // Get more done. Light up camera, computer, frame.
            room.GetDevice("notepad").SetSelected(true);
            room.GetDevice("computer").SetSelected(true);
            room.GetDevice("books").SetSelected(true);
            room.GetDevice("flashdrive").SetSelected(true);
            room.GetDevice("router").SetSelected(true);
            room.GetDevice("router_rays").SetSelected(true);
            
            OnSilverlightEvent("MenuChanged", "Get more done");
       }
        else if (index == 3)
        {
            //toggleLayer(this.tabNames[index]);
            room.DeselectDevices();
            // Smarter software.
            room.GetDevice("notepad").SetSelected(true);
            room.GetDevice("books").SetSelected(true);
            OnSilverlightEvent("MenuChanged", "Smarter software");
       }
    }
    else if (house.GetSelectedRoom().name == "GameRoom")
    {
        if (index == 0)
        {
            OnSilverlightEvent("MenuChanged", "Game Room");
        }
        else if (index == 1)
        {
            room.DeselectDevices();
            room.GetDevice("monitor19_2").SetSelected(true);
            room.GetDevice("videocard").SetSelected(true);
            room.GetDevice("mouse").SetSelected(true);
            room.GetDevice("keyboard").SetSelected(true);
            OnSilverlightEvent("MenuChanged", "Get your game on");
        }
        else if (index == 2)
        {
            room.DeselectDevices();
            room.GetDevice("monitor19_2").SetSelected(true);
            room.GetDevice("mouse").SetSelected(true);
            room.GetDevice("keyboard").SetSelected(true);
            room.GetDevice("lifecam").SetSelected(true);
            room.GetDevice("router").SetSelected(true);
            room.GetDevice("gr_router_rays").SetSelected(true);
            OnSilverlightEvent("MenuChanged", "Keep in touch");
        }
    }
    else if (house.GetSelectedRoom().name == "LivingRoom")
    {
        if (index == 0)
        {
            OnSilverlightEvent("MenuChanged", "Living Room");
        }
        else if (index == 1)
        {
            room.DeselectDevices();
            room.GetDevice("tv").SetSelected(true);
            room.GetDevice("product").SetSelected(true);
            room.GetDevice("remote").SetSelected(true);
            room.GetDevice("lr_router").SetSelected(true);
            room.GetDevice("lr_router_rays").SetSelected(true);
            OnSilverlightEvent("MenuChanged", "Prime-time PC");
        }
    }
}

DropDownMenu.prototype.DropHeader = function()
{
    //dd("dh !!!");
        
    var index = this.selectedHeaderIndex;
   
    if (index == -1)
        return;

   //alert("now: " + index + "        changed = " + true + "     total headers = " + this.headerCanvases.length);// + " animating (" + this.headers.length - (index + 1) + ")  headers downward");
    // Animate all headers "below" this one down by the body height.
    var dropAnim = new CompoundAnimation();
    dropAnim.baseAnimation.duration = 0.3;
    
    
    for (var i = index + 1; i < (this.headerCanvases.length); i++)
    {
        var menuHeaderCanvas = this.headerCanvases[i];
        
        var top = menuHeaderCanvas["Canvas.Top"];
        var newTop = top + this.BodyHeight;
        //alert("i = " + i + "       top = " + top + " to " +newTop);       
        var anim = new Animation("drop" + i, menuHeaderCanvas, "Canvas.Top", top, newTop, 0.3);
        
       
        dropAnim.Add(anim);
    }    
        
    var selectedHeader = this.GetHeader(this.selectedHeaderIndex);//this.headers[this.selectedHeaderIndex];
    //alert("this.header = " + selectedHeader.body.text);
    var headerBottom = 
        0//selectedHeader.xamlObject["Canvas.Top"] 
        + selectedHeader.xamlObject["Height"];
    
    var bodyDropAnim = new Animation("bodyDrop" + i, selectedHeader.body.xamlObject, "Canvas.Top", 0, headerBottom, 0.3);
    var bodyDropHAnim = new Animation("bodyDropH" + i, selectedHeader.body.xamlObject, "Height", 0, this.BodyHeight, 0.3);
    var bodyClipDropAnim = new Animation("bodyClipDropH" + i, selectedHeader.body, "clipHeight", 0, this.BodyHeight, 0.3);
    bodyClipDropAnim.OnFractionScript = Silverlight.createDelegate(selectedHeader.body, selectedHeader.body.Update);
    dropAnim.Add(bodyClipDropAnim);
    
        
    //dropAnim.Add(bodyDropAnim);
    dropAnim.Add(bodyDropHAnim);  
        
    dropAnim.baseAnimation.OnPostFractionScript = 
            function(frac) { 
                if (frac < 1) 
                { 
                    if (house.GetSelectedRoom() != null && house.GetSelectedRoom().menu != null)
                        house.GetSelectedRoom().menu.animating = true;
                }
                else if (frac >= 1)
                {
                    //dd("d2");
                    if (house.GetSelectedRoom() != null && house.GetSelectedRoom().menu != null)
                    {
                        house.GetSelectedRoom().menu.animating = false;
                    }
                } 
            }
 //   dropAnim.baseAnimation.OnStopScript = function() {
 //                    //dd("d2");
 //                    house.GetSelectedRoom().menu.animating = false;
 //                     }
    

    dropAnim.Play();
    
    return dropAnim;
}

DropDownMenu.prototype.IsVisible = function()
{
    if (this.xamlObject["Opacity"] > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

DropDownMenu.prototype.ShowHide = function(posX, posY)
{
    this.xamlObject["Opacity"] = (this.xamlObject["Opacity"] == 0) ? 1 : 0;
    this.xamlObject["Canvas.Left"] = posX;
    this.xamlObject["Canvas.Top"] = posY;
    
    if (this.xamlObject["Opacity"] > 0)
    {
        this.enabled = true;
        this.xamlObject["Visibility"] = "Visible";
        
        var startIndex = 0;
        //alert("this.room = " + this.room.name);  
        if (this.room != null && this.room.defaultMenuIndex > -1)
        {
            startIndex = this.room.defaultMenuIndex;
        }
         
        this.SetSelectedHeader(startIndex);
    }
    else
    {
        this.enabled = false;
        this.xamlObject["Visibility"] = "Collapsed";
    }
}


//-------------------------------

MenuHeader.prototype.canvas = null;
MenuHeader.prototype.transform = null;
MenuHeader.prototype.text = null;
MenuHeader.prototype.textBlock = null;
MenuHeader.prototype.arrowPath = null;
MenuHeader.prototype.body = null;
MenuHeader.prototype.xamlObject = null;
MenuHeader.prototype.menu = null;
MenuHeader.prototype.arrow1 = null;
MenuHeader.prototype.arrow2 = null;
MenuHeader.prototype.changeTextColorWhenSelected = true;

function MenuHeader(menu, text, canvas, transform, textBlock, arrowPath, bodyText, arrow1, arrow2)
{
    this.menu = menu;
    this.text = text;
    this.canvas = canvas;
    this.transform = transform;
    this.textBlock = textBlock;    
    this.arrowPath = arrowPath;
    this.body = new MenuBody(bodyText, this);
    this.body.header = this;
    this.xamlObject = this.canvas;
    this.arrow1 = arrow1;
    this.arrow2 = arrow2;
}

MenuHeader.prototype.selected = false;

MenuHeader.prototype.Select = function(val)
{
    if (!this.selected && val)
    {
        this.SetArrowState(1);
        if (this.changeTextColorWhenSelected) this.textBlock.Foreground="#ffffffff";
//        this.arrowPath.Fill="#ffffffff";
        this.selected = true;
    }
    else if (this.selected && !val)
    {
        this.SetArrowState(0);
        this.selected = false;
        if (this.changeTextColorWhenSelected) this.textBlock.Foreground="#ff000000";
//        this.arrowPath.Fill="#ff000000";
    }
}

MenuHeader.prototype.SetArrowState = function(val)
{
    if (this.arrow1 == null || this.arrow2 == null) return;
    
//    if (val == 0)
//        this.transform.Angle = 0;
//    else if (val == 1)
//        this.transform.Angle = 90;
    //alert("val = " + val + "    arrow1's vis = " + this.menu.arrow1["Visibility"] + "    arrow2's vis = " + this.menu.arrow2["Visibility"]);
    
    if (val == 0)
    {
        this.arrow1["Visibility"] = "Visible";
        this.arrow2["Visibility"] = "Collapsed";
    }
    else
    {
        this.arrow1["Visibility"] = "Collapsed";
        this.arrow2["Visibility"] = "Visible";
    }

}

MenuHeader.prototype.SetState = function(val)
{
    if (val == 1)
    {
        if (this.changeTextColorWhenSelected) this.textBlock.Foreground = "#ffeeeeff";
//        this.arrowPath.Fill = "#ffeeeeff";
   }
    else
    {
        if (this.changeTextColorWhenSelected) this.textBlock.Foreground = "#ff000000";
//        this.arrowPath.Fill = "#ffeeeeff";
    }
}

//-------------------------------

MenuBody.prototype.header = null;
MenuBody.prototype.text = null;
MenuBody.prototype.bodyHeight = 100;
MenuBody.prototype.xamlObject = null;
MenuBody.prototype.text = null;
MenuBody.prototype.clip = null;
function MenuBody(text, header)
{
    this.header = header;
    this.text = text;
    
    this.Build();
}

MenuBody.prototype.Build = function()
{
    var t = '';
    t += '<Canvas Width="198" Canvas.Top="' + this.header.menu.HeaderHeight + '" Height="0">';
        t += '<Canvas.Background>';
            t += '<ImageBrush ImageSource="img/menu_body_bg.png">';
            t += '</ImageBrush>';
        t += '</Canvas.Background>';
    t += '</Canvas>';
    
    var container = plugin.content.createFromXaml(t);
    
    if (this.text != null)
    {
        t = '<Canvas Canvas.Left="10" Canvas.Top="9">';
        t += '<TextBlock Foreground="#ffffffff" FontFamily="Verdana"  FontSize="9.5" FontWeight="Normal">' + this.text + '</TextBlock>';
		t += '<Canvas.Clip><RectangleGeometry Rect="0,0,198,0"></RectangleGeometry></Canvas.Clip>';
        t += '</Canvas>';
       
        var textContainer = plugin.content.createFromXaml(t);
        		
		this.clip = textContainer.clip;
        
        container.children.add(textContainer);   
    }
    
    this.xamlObject = container;
    
    this.header.canvas.children.insert(0, this.xamlObject);
    
    //alert("created, boddy.xamlObject = " + this.xamlObject);
}

MenuBody.prototype.clipHeight = 0;

MenuBody.prototype.Update = function()
{
    //dd("" + this.clipHeight);
    this.clip["Rect"] = "0,0,198," + this.clipHeight;
}

