﻿Item = function(dropElement)
{
    this.DropElement = dropElement;
    this.Width = ParseNumber(GetElementWidth(this.DropElement));
    this.Height = ParseNumber(GetElementHeight(this.DropElement));
    this.OwnerPrefix = dropElement.getAttribute('OwnerPrefix');
    
    // sets the 'snap to grid' separation
    this.HorizontalSeparationIncrement = 1;
    
    // tool bar item constants
    this.ToolBarItemVerticalPosition = -5;
    
    this.DataItemRequiresRefresh = false;
    this.IsWholeChordItem = false;
}

Item.prototype =
{
    GetPosCenterTop : function()
    {
        Log('Item->GetPosCenterTop');            
        this.PosCenterTop = ParseNumber(this.DropElement.style.top) + (this.Height / 2);
        Log('Item->GetPosCenterTop->' + this.PosCenterTop); 
        return this.PosCenterTop;
    },
    
    
    GetPosCenterLeft : function()
    {
        Log('Item->GetPosCenterLeft');
        this.PosCenterLeft = ParseNumber(this.DropElement.style.left) + (this.Width / 2);
        Log('Item->GetPosCenterLeft->' + this.PosCenterLeft);
        return this.PosCenterLeft;
    },   
    
    
    // adjusts the positioning (not always height/2 but in this case height as we position from the bottom of the image)
    GetActualTop : function(verticalPosition, stringSeparation)
    {
        Log('Item->GetActualTop');
        
        if (this.IsWholeChordItem)
        {
            return (verticalPosition * stringSeparation) - 5; // this.Height is the height of the whole chord div not the item so needs to be adjusted
        }
        else
        {
            return (verticalPosition * stringSeparation) - (this.Height / 2);
        }
    },
    
    
    // adjusts the positioning (not always width/2 but in this case width as we position from the edge of the image)
    GetActualLeft : function(horizontalPosition, fretHeight)
    {
        Log('Item->GetActualLeft')
        return (horizontalPosition * fretHeight) - (this.Width / 2); // adding an aesthetic adjustment
    },
    
    
    GetImage : function(droppedImageData, horizontalPosition, verticalPosition, newImageId, imageSourceName)
    {
        var newItemHTML = "<img id='" + newImageId + "'"
                            + "  name='" + newImageId + "'"
                            + "  alt='" + droppedImageData + "'"
                            + "  src='" + strImageThemeRootPath + "/" + imageSourceName + ".gif'"
                            + "  OwnerPrefix='" + this.OwnerPrefix + "'"
                            + "  ImgNumber='" + droppedImageData + "'"
                            + "  ImgData='" + droppedImageData + "'"
                            + "  ImageSrcName='" + imageSourceName + "'"
                            + "  HorizontalPosition='" + horizontalPosition + "'"
                            + "  VerticalPosition='" + verticalPosition + "'"
                            + "  ItemType='Tab'"
                            + "  drag='true'"
                            + "  style='position:absolute;top:0px;left:0px;visibility:hidden;z-index:100;cursor:default'/>";
                        
        return newItemHTML;
    },
    
    
    
    CurrentDataCanRemain : function(currentDataItem)
    {
        Log('CurrentDataCanRemain->CurrentDataItem->' + currentDataItem);
        
        if (ParseNumber(currentDataItem) != 0)
        {
            // its another number
            Log('CurrentDataCanRemain->False');
            return false;
        }
        
        Log('CurrentDataCanRemain->True');
        return true;
    },
    
    
    
    
    ResetItem : function()
    {
        // the element might be a tool box image dropped or it might be one of the images re-positioned
        if (this.DropElement.id.indexOf('newImg') != -1)
        {
            Log('Item->ResetItem->Reset new image');
            
            // nothing to do as we dont want to reset anything as this was a re-positioned item
            this.DropElement.style.visibility = 'hidden';
            return; 
        }
        else if (this.DropElement.id.indexOf('_tbi') != -1) 
        {        
            Log('Item->ResetItem->Reset tool box item');
            
            this.DropElement.setAttribute('VerticalPosition', this.ToolBarItemVerticalPosition);
            
            // use a new tabgrid container to do the positioning for us
            new TabGridContainer(this.DropElement, DEFAULT_SCALE).SetPosition(this.DropElement);
        }
        else if (this.DropElement.id.indexOf('divGeneratedDropChord') != -1) 
        {
            // its a dropped whole chord
            Log('Item->ResetItem->Reset whole chord');

            // we can position the dropElement back to where it was as it can be used again whole
            var dropChordPosition = new DropChordPosition();
            this.DropElement.style.top = dropChordPosition.Top + 'px';
            this.DropElement.style.left = dropChordPosition.Left + 'px';
        }
        else if (this.DropElement.id.indexOf('_img') != -1) 
        {
            // its a positioned image from hidden data being re-moved (ie, was there when the page loaded)
            this.DropElement.style.visibility = 'hidden';
        }
    }
}