﻿
//##########################################################################################################
function Typical29_Views_InitializeDragandDrop(){

    var record = null;
    var code = null;
    
    //Drag & Drop
    Ext.override(Ext.dd.DragSource, {
        onBeforeDrag : function(data, e) {
        
            var dragEl = Ext.get(this.getDragEl());
            dragEl.applyStyles({border:'','z-index':2000});
            var el = Ext.get(this.getEl());
        
            record = data.selections[0]
            code = record.get("Code")
            
            var imagePath = record.get("Image")
            dragEl.update("<img id='DragImage' src='" + imagePath + "' alt=''/>");
        },
        
        onDrag: function(e){
            isIconDrag = true;
            
            var element = Ext.get("renderArea").getXY()
            var widthFromOrigin = element[0]
            var heightFromOrigin = element[1]

            var mousepx = e.getXY(); //with respect to page
            var X1px = mousepx[0] - widthFromOrigin;
            var Y1px = mousepx[1] - heightFromOrigin;
            
            //Outside the rendered image
            DisplayPNG(document.getElementById('layerImg'), "Graphics/x.gif");
            if(X1px >= 0 && X1px <= 800 && Y1px >= 0 && Y1px <= 500)
            {
                if(code == "Steel")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Global.png");
                }
                else if(code == "Fabric")
                {   
                    //Check if the drag element is a finish or component
                    if(AllZonesRadio.getValue() == true)
                    {
                        DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Zone3.png");
                    }
                    else if(SelectedZoneRadio.getValue()  == true)
                    {
                        if(isTypical29Object(currentView, "Zone3", X1px, Y1px))
                        {
                            DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Zone3.png");
                        }
                    }
                }
                else if(code == "Laminate")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Worksurface.png");
                }
                else if(code == "Trim")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Global.png");
                }
                else if(code == "Accessories")
                {
                }
                else if(code == "Chair")
                {
                }
                else if(code == "Modesty_Panel")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Modesty_Panels.png");
                }
                else if(code == "Overhead_Storage")
                {
                    var code1 = record.get("Code1")
                    if(code1 == "1")
                    {
                        DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Overhead_Storage.png");
                    }
                    else
                    {
                        if(isT29Overhead == false || AllSectionsRadio.getValue() == true)
                        {
                            DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Overhead_Storage.png");
                        }
                    }
                }
                else if(code == "Fabric_Tack_Panel")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Zone3.png");
                }
                else if(code == "Task_Lights")
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/Global.png");
                }
                else if(code == "Below_Surface_Storage")
                {
                    var code1 = record.get("Code1")
                    if(AllSectionsRadio.getValue()  == true)
                    {
                        if(isTypical29Object(currentView, "LS1", X1px, Y1px))
                        {
                            //Storage 1
                            if(code1 == "D" || code1 == "E")
                            {
                                DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS1.png");
                            }
                        }
                        else if(isTypical29Object(currentView, "LS2", X1px, Y1px))
                        {
                            //Storage 2
                            if(code1 == "A" || code1 == "B" || code1 == "C" || code1 == "D" || code1 == "E")
                            {
                                DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS2.png");
                            }
                        }
                        else if(isTypical29Object(currentView, "LS3", X1px, Y1px))
                        {
                            //Storage 3
                            if(code1 == "D" || code1 == "E")
                            {
                                DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS3.png");
                            }
                        }
                        else if(isTypical29Object(currentView, "LS4", X1px, Y1px))
                        {
                            //Storage 4
                            if(code1 == "D" || code1 == "E")
                            {
                                DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS4.png");
                            }
                        }
                    }
                }
            }
        },
        onMouseUp: function(e){
            isIconDrag = false;
        
            var element = Ext.get("renderArea").getXY()
            var widthFromOrigin = element[0]
            var heightFromOrigin = element[1]

            var mousepx = e.getXY(); //with respect to page
            var X1px = mousepx[0] - widthFromOrigin;
            var Y1px = mousepx[1] - heightFromOrigin;
            
            //Within the rendered image
            if(X1px >= 0 && X1px <= 800 && Y1px >= 0 && Y1px <= 500)
            {
                if(code == "Steel")
                {
                    var rgb = record.get("RGB");
                    paintColor = rgb;
                    
                    RenderTypical29Image();
                }
                else if(code == "Fabric")
                {
                    //dataIndex: Image, FinishName, DisplayName, Family, FabricCategory, UpCharge, Code
                    var finishName = record.get("FinishName")
                    fName = finishName;
                    var FabricUpCharge = record.get("UpCharge");
                    
                    //Check if the drag element is a finish or component
                    if(AllZonesRadio.getValue() == true)
                    {
                        if(isT29Overhead == true)
                        {
                            zone3Mat = fName;
                            zone3T29MatUpCharge = FabricUpCharge;
                            RenderTypical29Image();
                        }
                        else
                            alert("Add Overhead Storage")
                    }
                    else if(SelectedZoneRadio.getValue()  == true)
                    {
                        if(isTypical29Object(currentView, "Zone3", X1px, Y1px))
                        {
                            if(isT29Overhead == true)
                            {
                                //Zone3
                                zone3Mat = fName;
                                zone3T29MatUpCharge = FabricUpCharge;
                                RenderTypical29Image();
                            }
                            else
                                alert("Add Overhead Storage")
                        }
                    }
                }
                else if(code == "Laminate")
                {
                    var finishName = record.get("FinishName")
                    worksurfaceFinish = finishName;
                    
                    RenderTypical29Image();
                }
                else if(code == "Trim")
                {
                    var rgb = record.get("RGB");
                    trimColor = rgb;
                    
                    RenderTypical29Image();
                }
                else if(code == "Accessories")
                {
                    alert('Not yet implemented');
                }
                else if(code == "Chair")
                {
                    alert('Not yet implemented');
                }
                else if(code == "Modesty_Panel")
                {
                    var code1 = record.get("Code1")
                    if(code1 == "1")
                    {
                       if(isT29ModestyPanel == true)
                        {
                            T29S1Storage1 = parseInt(T29S1Storage1) + 1;
                            T29S1Storage2 = parseInt(T29S1Storage2) + 1;
                            T29S1Storage3 = parseInt(T29S1Storage3) + 1;
                            T29S1Storage4 = parseInt(T29S1Storage4) + 1;
                            
                            isT29ModestyPanel = false;
                            RebuildTypical29BelowSurfaceStorage();
                        }
                    }
                    else if(code1 = "2")
                    {
                        if(isT29ModestyPanel == false)
                        {
                            T29S1Storage1 = parseInt(T29S1Storage1) - 1;
                            T29S1Storage2 = parseInt(T29S1Storage2) - 1;
                            T29S1Storage3 = parseInt(T29S1Storage3) - 1;
                            T29S1Storage4 = parseInt(T29S1Storage4) - 1;
                            
                            isT29ModestyPanel = true;
                            RebuildTypical29BelowSurfaceStorage();
                        }
                    }
                }
                else if(code == "Overhead_Storage")
                {
                    //dataIndex: Image, DisplayName, Category, Code, Code1
                    var code1 = record.get("Code1")
                    
                    if(code1 == "1")
                    {
                        S1_C = "000";
                        S1_D = "000";
                        
                        isT29Overhead = false;
                        isT29FabricTackPanel = false;
                        isT29TaskLights = false;
                        RenderTypical29Image();
                    }
                    else
                    {
                        var newConfig = -1;
                        
                        if(code1 == "2")
                        {
                            if(isT29FabricTackPanel == true)
                                 newConfig = "001"
                            else if(isT29FabricTackPanel == false)
                                 newConfig = "004"
                        }
                        else if(code1 == "3")
                        {
                            if(isT29FabricTackPanel == true)
                                newConfig = "002"
                            else if(isT29FabricTackPanel == false)
                                 newConfig = "005"
                        }
                        else if(code1 == "4")
                        {
                            if(isT29FabricTackPanel == true)
                                newConfig = "003"
                            else if(isT29FabricTackPanel == false)
                                 newConfig = "006"
                        }
                        
                        //Change overhead based on option selected
                        if(isT29Overhead == false || AllSectionsRadio.getValue() == true)
                        {
                            S1_C = newConfig;

                            isT29Overhead = true;
                            RenderTypical29Image();
                        }
                    }
                }
                else if(code == "Fabric_Tack_Panel")
                {
                    if(isT29Overhead == true)
                    {
                        //dataIndex: Image, DisplayName, Category, Code, Code1
                        var code1 = record.get("Code1")
                        if(code1 == "1") 
                        {
                            if(isT29FabricTackPanel == true)//No tack panels only if tack panel is added
                            {
                                var S1_C_Config = parseInt(S1_C) + 3;
                                var new_S1_C = "00" + S1_C_Config;
                                
                                S1_C = new_S1_C;
                                
                                isT29FabricTackPanel = false;
                                RenderTypical29Image();
                            }
                        }
                        else if(code1 == "2") 
                        {
                            if(isT29FabricTackPanel == false)// Add tack panels only if tack panel is not added
                            {
                                var S1_C_Config = parseInt(S1_C) - 3;
                                var new_S1_C = "00" + S1_C_Config;
                                
                                S1_C = new_S1_C;
                                
                                isT29FabricTackPanel = true;
                                RenderTypical29Image();
                            }
                        }
                    }
                    else
                        alert("Add Overhead Storage")
                }
                else if(code == "Task_Lights")
                {
                    if(isT29Overhead == true)
                    {
                        var code1 = record.get("Code1")
                        if(code1 == "1") //No Task Lights
                        {
                            S1_D = "000";
                            
                            isT29TaskLights = false;
                            RenderTypical29Image();
                        }
                        else if(code1 == "2")    //Add Task Lights
                        {
                            S1_D = "001";
                            
                            isT29TaskLights = true;
                            RenderTypical29Image();
                        }
                    }
                    else
                        alert("Add Overhead Storage")
                }
                else if(code == "Below_Surface_Storage")
                {
                    var code1 = record.get("Code1");
                    
                    if(AllSectionsRadio.getValue()  == true)
                    {
                        if(isTypical29Object(currentView, "LS1", X1px, Y1px))
                        {
                            //Storage 1
                            if(code1 == "D" || code1 == "E")
                            {
                                var temp = -1;
                                if(isT29ModestyPanel == true)   //Lower Storage
                                {
                                    if(code1 == "D")
                                        temp = "3";
                                    else if(code1 == "E")
                                        temp = "5";
                                }
                                else if(isT29ModestyPanel == false)
                                {
                                    if(code1 == "D")
                                        temp = "4";
                                    else if(code1 == "E")
                                        temp = "6";
                                }
                                
                                T29S1Storage1 = temp;
                                RebuildTypical29BelowSurfaceStorage();
                            }
                        }
                        else if(isTypical29Object(currentView, "LS2", X1px, Y1px))
                        {
                            //Storage 2
                            if(code1 == "A" || code1 == "B" || code1 == "C" || code1 == "D" || code1 == "E")
                            {
                                var temp = -1;
                                if(isT29ModestyPanel == true)   //Lower Storage
                                {
                                    if(code1 == "A")
                                        temp = "53";
                                    else if(code1 == "B")
                                        temp = "55";
                                    else if(code1 == "C")
                                        temp = "57";
                                    else if(code1 == "D")
                                        temp = "59";
                                    else if(code1 == "E")
                                        temp = "61";
                                }
                                else if(isT29ModestyPanel == false)
                                {
                                    if(code1 == "A")
                                        temp = "54";
                                    else if(code1 == "B")
                                        temp = "56";
                                    else if(code1 == "C")
                                        temp = "58";
                                    else if(code1 == "D")
                                        temp = "60";
                                    else if(code1 == "E")
                                        temp = "62";
                                }
                                
                                T29S1Storage2 = temp;
                                RebuildTypical29BelowSurfaceStorage();
                            }
                        }
                        else if(isTypical29Object(currentView, "LS3", X1px, Y1px))
                        {
                            //Storage 3
                            if(code1 == "D" || code1 == "E")
                            {
                                var temp = -1;
                                if(isT29ModestyPanel == true)   //Lower Storage
                                {
                                    if(code1 == "D")
                                        temp = "3";
                                    else if(code1 == "E")
                                        temp = "5";
                                }
                                else if(isT29ModestyPanel == false)
                                {
                                    if(code1 == "D")
                                        temp = "4";
                                    else if(code1 == "E")
                                        temp = "6";
                                }
                                
                                T29S1Storage3 = temp;
                                RebuildTypical29BelowSurfaceStorage();
                            }
                        }
                        else if(isTypical29Object(currentView, "LS4", X1px, Y1px))
                        {
                            //Storage 3
                            if(code1 == "D" || code1 == "E")
                            {
                                var temp = -1;
                                if(isT29ModestyPanel == true)   //Lower Storage
                                {
                                    if(code1 == "D")
                                        temp = "3";
                                    else if(code1 == "E")
                                        temp = "5";
                                }
                                else if(isT29ModestyPanel == false)
                                {
                                    if(code1 == "D")
                                        temp = "4";
                                    else if(code1 == "E")
                                        temp = "6";
                                }
                                
                                T29S1Storage4 = temp;
                                RebuildTypical29BelowSurfaceStorage();
                            }
                        }
                    }
                }
            }
            DisplayPNG(document.getElementById('layerImg'), "Graphics/x.gif");
        }
    });
}
//##########################################################################################################

//##########################################################################################################
function Typical29_RemoveComponentsMOver(X1px, Y1px)
{
    if(isComponentsPanelExpanded == true && isIconDrag == false)
    {
        DisplayPNG(document.getElementById('layerImg'), "Graphics/x.gif");
        if(X1px >= 0 && X1px <= 800 && Y1px >= 0 && Y1px <= 500)
        {
            if(AllSectionsRadio.getValue() == true)
            {
                if(isTypical29Object(currentView, "LS1", X1px, Y1px))//Lower Storage
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS1_R.png");
                }
                else if(isTypical29Object(currentView, "LS2", X1px, Y1px))
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS2_R.png");
                }
                else if(isTypical29Object(currentView, "LS3", X1px, Y1px))
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS3_R.png");
                }
                else if(isTypical29Object(currentView, "LS4", X1px, Y1px))
                {
                    DisplayPNG(document.getElementById('layerImg'), "Typicals/Typical" + currentTypical + "/View" + currentView + "/Masks/LS4_R.png");
                }
            }
        }
    }
}
function Typical29_RemoveComponentsClick(X1px, Y1px)
{
    if(isComponentsPanelExpanded == true && isIconDrag == false)
    {
        DisplayPNG(document.getElementById('layerImg'), "Graphics/x.gif");
        if(X1px >= 0 && X1px <= 800 && Y1px >= 0 && Y1px <= 500)
        {
            if(AllSectionsRadio.getValue()  == true)
            {
                if(isTypical29Object(currentView, "LS1", X1px, Y1px))//Lower Storage
                {
                    //Storage 1
                    var temp = -1;
                    if(isT29ModestyPanel == true)
                        temp = "1";
                    else if(isT29ModestyPanel == false)
                        temp = "2";

                    T29S1Storage1 = temp;
                    RebuildTypical29BelowSurfaceStorage();
                }
                else if(isTypical29Object(currentView, "LS2", X1px, Y1px))
                {
                    //Storage 2
                    var temp = -1;
                    if(isT29ModestyPanel == true)
                        temp = "51";
                    else if(isT29ModestyPanel == false)
                        temp = "52";

                    T29S1Storage2 = temp;
                    RebuildTypical29BelowSurfaceStorage();
                }
                else if(isTypical29Object(currentView, "LS3", X1px, Y1px))
                {
                    //Storage 3
                    var temp = -1;
                    if(isT29ModestyPanel == true)
                        temp = "1";
                    else if(isT29ModestyPanel == false)
                        temp = "2";

                    T29S1Storage3 = temp;
                    RebuildTypical29BelowSurfaceStorage();
                }
                else if(isTypical29Object(currentView, "LS4", X1px, Y1px))
                {
                    //Storage 4
                    var temp = -1;
                    if(isT29ModestyPanel == true)
                        temp = "1";
                    else if(isT29ModestyPanel == false)
                        temp = "2";

                    T29S1Storage4 = temp;
                    RebuildTypical29BelowSurfaceStorage();
                }
            }
        }
    }
}
//##########################################################################################################
