﻿       //NOTES TO SELF
       // Replace CSS PROPRTY SWITCH with strategy pattern
       // Replace ANCHOR TYPE SWITCH with strategy pattern
       //        to abstract methodoly for detemining the "MASTER" element
       //        also to remove anchorTarget property from the DivEdge object
        function GetCurrentCSSStyle(elementObj, cssProperty)
        {
            if (document.defaultView && document.defaultView.getComputedStyle)
            {
                return document.defaultView.getComputedStyle(elementObj, null).getPropertyValue(cssProperty);
            }
            else
            {
                // Replace this switch with something more elegant later, such
                // as regex  
                switch(cssProperty.toLowerCase())
                {
                    case "padding-top": cssProperty = "paddingTop"; break;
                    case "padding-bottom": cssProperty = "paddingBottom"; break;
                    case "border-top-width": cssProperty = "borderTopWidth"; break;
                    case "border-bottom-width": cssProperty = "borderBottomWidth"; break;
                    case "margin-top": cssProperty = "marginTop"; break;
                    case "margin-bottom": cssProperty = "marginBottom"; break;
                }
                
                return elementObj.currentStyle[cssProperty];
            }
        }
        function GetBottomEdgeYCoord(elementObj)
        {
            var bounds = Sys.UI.DomElement.getBounds(elementObj);
            return  bounds.y + bounds.height;
        }
        
        function PerformEdgeMatchingBottom(edgeSet)
        {
        
            var yCoordOfTallestEdge = 0;
            var TallestEdge = null;
            
            // Eventually evolve this to strategy pattern
            switch (edgeSet.anchorType)
            {
                case "GreatestEdge":
                    yCoordOfTallestEdge = GetBottomEdgeYCoord(edgeSet.Edges[0].elementObject);
                    tallestEdge = edgeSet.Edges[0].elementObject;
                    
                    for(var i = 1; i < edgeSet.Edges.length; i++)
                    {
                        var edgeHeight = GetBottomEdgeYCoord(edgeSet.Edges[i].elementObject)
                        
                        if (edgeHeight > yCoordOfTallestEdge)
                        {
                            yCoordOfTallestEdge = edgeHeight;
                            tallestEdge = edgeSet.Edges[i].elementObject;
                        }
                    } 
                    break;
                case "AnchorTarget":
                    for(var i = 1; i < edgeSet.Edges.length; i++)
                    {
                        if (edgeSet.Edges[i].anchorTarget == true)
                        {
                            yCoordOfTallestEdge = GetBottomEdgeYCoord(edgeSet.Edges[i].elementObject);
                            tallestEdge = edgeSet.Edges[i].elementObject;
                        }                        
                    }                 
                    break;               
            }    

            for(var i = 0; i < edgeSet.Edges.length; i++)
            {   
                var top = Sys.UI.DomElement.getLocation(edgeSet.Edges[i].elementObject).y;
                
                var topPadding = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "padding-top"));
                var bottomPadding = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "padding-bottom"));
                var topborder = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "border-top-width"));
                var bottomborder = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "border-bottom-width"));
                var topMargin = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "margin-top"));
                var bottomMargin = parseInt(GetCurrentCSSStyle(edgeSet.Edges[i].elementObject, "margin-bottom"));

                if (topPadding + "" == "NaN") topPadding = 0;
                if (bottomPadding + "" == "NaN") bottomPadding = 0;
                if (topborder + "" == "NaN") topborder = 0;
                if (bottomborder + "" == "NaN") bottomborder = 0;
                if (topMargin + "" == "NaN") topMargin = 0;
                if (bottomMargin + "" == "NaN") bottomMargin = 0;                
                
                edgeSet.Edges[i].elementObject.style.height = (yCoordOfTallestEdge - top - topPadding - bottomPadding - topMargin - topborder - bottomborder - bottomMargin) + "px";
            }
        }
        
        function ResolveEdgeElementInstances(edgeSet)
        {   
            var retValue = true;
            
            for(var divEdgeId = 0; divEdgeId < edgeSet.Edges.length; divEdgeId++)
            {
                if (edgeSet.Edges[divEdgeId] && edgeSet.Edges[divEdgeId].typeName == "DivEdge")
                {
                    var pageElem = document.getElementById(edgeSet.Edges[divEdgeId].elementId);
                    edgeSet.Edges[divEdgeId].elementObject = pageElem;
                    retValue = retValue && (pageElem);
                }
            }
            
            return retValue;
        }
        
        function ProcessEdgeSet(edgeSet)
        {
            if (edgeSet && edgeSet.typeName == "DivEdgeSet")
            {
            
                if (!ResolveEdgeElementInstances(edgeSet))
                {
                    throw new Error("Not all edge elements could be resolved, please check the specified element ID's in DivEdgeSet named '" + edgeSet.name + "'.");
                }
                                
                if (edgeSet.edgeType == "Bottom")
                {
                    PerformEdgeMatchingBottom(edgeSet);
                }
            }
        }
        
        function ProcessDivEdgeMatchingConfiguration(configurationToProcess)
        {
            for(var prop in configurationToProcess)
            { 
                if (configurationToProcess[prop].typeName && configurationToProcess[prop].typeName == "DivEdgeSet")
                {
                    ProcessEdgeSet(configurationToProcess[prop]);
                }
            }
        }