﻿// This JavaScript file defines the object "listItem"
ListItem = function(slRootObject, sControlId, Parent, sControlXAML, oDataObject, aXY)
{
    // Make object root scope accessible to inner objects
    var that = this;
    
    // Make arguments and derived varibles public
    this._sControlId = sControlId + "_";
    this._slRootObject = slRootObject;
    this._parent = Parent;
    this._oDataObject = oDataObject;
    this._host = this._parent.getHost();
    this._aXY = aXY;
    this._sControlXAML = sControlXAML;
    
    // Set default options
    this._margin = 3;

    // The first step is to retrive the XAML content for the "listItem"
    this._buildControl();
    
    //Initiate animation 
    this.tHighlight = new Tweener(this._host, this._parent);
}


ListItem.prototype =
{

    // Find name using original XAML name
    _findNameByXamlID : function(nameInXamlFile)
    {
        return this._parent.findName(this._getIdFor(nameInXamlFile));
    },


    // Get name in original XAML name using instances name
    _getIdFor : function(nameInXamlFile)
    {
        return this._sControlId + nameInXamlFile;
    },
    

    // Create control element from XAML
    _buildControl : function()
    {
        // Cusomise XAML to make names unique
        var originalXaml = this._sControlXAML.replace(/Name="/g, "Name=\"" + this._sControlId);

        // Create element from XAML
        var newElement = this._host.content.createFromXaml(originalXaml);

        // The XML object is added to the root Canvas
        this._parent.children.add(newElement);

        // Set control references
        this._setControlReferences();
        
        // Position and resize objects
        this._autoSize();
    },
    
    
    // Make references to elements
    _setControlReferences : function()
    {
        // Set positions and sizes
        this._Title = this._findNameByXamlID("listItemTitle");
        //this._Category = this._findNameByXamlID("listItemCategory");
        this._CommunityGroup = this._findNameByXamlID("listItemCommunity");
       // this._Description = this._findNameByXamlID("listItemDescription");
        this._state = this._findNameByXamlID("listItemState");
        this._listItem = this._findNameByXamlID("listItem");
        this._viewButton = this._findNameByXamlID("listItemViewButton");
        this._buttonBase = this._findNameByXamlID("baseButton");
        this._highlightBar = this._findNameByXamlID("highLightBar");
        this._flashEffect = this._findNameByXamlID("flashEffect");
        this._Date = this._findNameByXamlID("listItemDate");
        this._Topic = this._findNameByXamlID("listItemTopic");
      
        
        
        this._Title.text = "Name: " + this._formatData(this._oDataObject.Name, 25);
        this._CommunityGroup.text = "Community: " + this._formatData(this._oDataObject.CommunityGroup,20);
        this._Date.text = "Submitted: " + this._oDataObject.Date;
        this._state.text = "State: " + this._oDataObject.State;
        this._Topic.text = "Demo Topic: " + this._formatData(this._oDataObject.DemoTopic,20);
        
        
         //this._Title.text = this._formatData(this._oDataObject.Title,28);
       //  this._Category.text = "Community Group: " + this._oDataObject.Product + " with " + this._oDataObject.Name + " from " + this._oDataObject.State;
      //  this._Description.text = this._oDataObject.Description;
       // this._Description.text = this._formatData(this._oDataObject.Description, 25);
        
        // Hookup events
        this._viewButton.AddEventListener("MouseLeftButtonUp",
            Silverlight.createDelegate(this, this._viewClicked)
        );
        
       this._viewButton.addEventListener("MouseEnter",
            Silverlight.createDelegate(this, this._baseEnter));
            
       this._viewButton.addEventListener("MouseLeave",
            Silverlight.createDelegate(this, this._baseLeave));
            
       
    },
  
 
    _formatData: function(sData, cut) {
        var content = new String();
        var cutAmount = new Number(cut);
        if(sData.length >= cutAmount) {
            content = sData.substr(0,cutAmount);
            return  content + "...";
        }else {
            content = sData;
            return content;
        }
        
    },
    
    
    
    //BASE ENTER EVENT CALL
    _baseEnter: function(sender, eventArgs) {
    
     this.tHighlight.fade(this._highlightBar,
                     this._highlightBar.Opacity,
                     0.5,
                     "0:0:0",
                     "0:0:0.2"
                     );
  
  
   },
   
    _baseLeave: function(sender, eventArgs) {
      this.tHighlight.fade(this._highlightBar,
                     this._highlightBar.Opacity,
                     0,
                     "0:0:0",
                     "0:0:1"
                     );
  
   },
    
    // Resize and position elements
    _autoSize : function()
    {
        // Resize elements to fit parent
        
        this._listItem["Canvas.Top"] += this._margin + this._aXY[1];
        this._listItem["Canvas.Left"] += this._margin;
        
        /*
        this._listItem.Width = this._parent.Width - (2*this._margin) - 20;
        
        this._Title["Canvas.Top"] = this._margin;
        this._Title["Canvas.Left"] = this._margin;
        this._Title.Width = this._listItem.Width - (2*this._margin);
        
        this._Category["Canvas.Left"] = this._margin;
        this._Category.Width = this._listItem.Width - (2*this._margin);
        
        
        this._Description["Canvas.Top"] = this._Category["Canvas.Top"] + this._Category.actualHeight + this._margin;
        this._Description["Canvas.Left"] = this._margin;
        this._Description.Width = this._listItem.Width - (2*this._margin);
        
        this._viewButton["Canvas.Left"] = this._listItem.Width - this._margin - this._viewButton.Width;
        this._viewButton["Canvas.Top"] = this._Description["Canvas.Top"] + this._Description.actualHeight + this._margin;
        this._listItem.Height = this._viewButton["Canvas.Top"] + this._viewButton.Height + this._margin;
        */
        
        this.absoluteBottom = this._listItem.Height + this._listItem["Canvas.Top"];
    },
    
    
    // View button clicked event handler
    _viewClicked : function(sender, eventArgs)
    {
        this._slRootObject.videoPlayer.runVideo(this._oDataObject.Location)
        this._slRootObject.displayVideoDetails(this._oDataObject);
    },
    
    
    // Remove element from XAML tree
    _remove : function() {
        this._listItem.GetParent().Children.Remove(this._listItem);
    }
    
}