if (typeof Structura == "undefined") {
    var Structura = {};
}

if (typeof Structura.MapSelecter == "undefined") {
    Structura.MapSelecter = function(){
        var hiddenFieldElements = new Array();
        var areaStatuses = new Array();
        
        var coordinates = null;
        
        var jg = null;
        var jgSelected = null;
        
        Structura.MapSelecter.AddElement = function(hiddenFieldID, regionElement)
        {
            hiddenFieldElements[hiddenFieldElements.length] = document.getElementById(hiddenFieldID);
            
            var el = document.getElementById(regionElement);
            
            coordinates = el.coords.split(",");
            
            var Xpoints = new Array();
            var Ypoints = new Array();
        
            for(var i = 0; i < coordinates.length; i++){
                Xpoints[Xpoints.length] = parseInt(coordinates[i++].trim());
                Ypoints[Ypoints.length] = parseInt(coordinates[i].trim());
            }
            
            var areaIndex = GetAreaIndex(el);
            
            jgSelected.setColor("#ffff00");
            jgSelected.fillPolygon(Xpoints, Ypoints, 'onmouseover="Structura.MapSelecter.AreaOverImage(null,\'' + areaIndex + '\')"' + ' onclick="Structura.MapSelecter.AreaClickByIndex(\'' + areaIndex + '\')"', "noDisplay " + GetPolygonClassName(areaIndex));

            jgSelected.paint();

            SetPolygonVisibility(areaIndex, document.getElementById(hiddenFieldID).value);
        };
        
        Structura.MapSelecter.Validate = function()
        {
            var isRegionSelected = false;
            for(var i = 0 ; i < areaStatuses.length; i++)
            {
                if(areaStatuses[i] == "selected" || areaStatuses[i] == "selectedover")
                {
                    hiddenFieldElements[i].value = "selected";
                    isRegionSelected = true;
                }
                else
                {
                    hiddenFieldElements[i].value = "unselected";
                }
            }
            if(!isRegionSelected)
            {
                alert('Select at least 1 region on the map');
            }
            return isRegionSelected;
        }
        
        
        Structura.MapSelecter.AreaClickByIndex = function(areaIndex)
        {
            var areaStatus = areaStatuses[areaIndex -1];
            
            if(areaStatus == "selected" || areaStatus == "selectedover")
            {
                SetPolygonVisibility(areaIndex, "unselectedover");
            }
            else if(areaStatus == "unselected" || areaStatus =="unselectedover") {
                SetPolygonVisibility(areaIndex, "selectedover");
            }
        }
        
        GetAreaIndex = function(el)
        {
            return parseInt(el.id.substring(el.id.lastIndexOf("_") + 1));
        }
        
        SetPolygonVisibility = function(areaIndex, status)
        {
            areaStatuses[areaIndex -1] = status;
            var color = "#ffff00";
            var visibilityClassName = " divDraw001Transparent";
            
            if(status == "selected")
            {
                visibilityClassName = " divDrawTransparent";
                color = "#00ff33";
            }
            if(status == "selectedover")
            {
                visibilityClassName = " divDrawTransparent";
                color = "#ff0000";
            }
            if(status == "unselectedover")
            {
                visibilityClassName = " divDrawTransparent";
                color = "#ffff00";
            }
            if(status =="unselected")
            {
                visibilityClassName = " divDraw001Transparent";
            }
        
            var areaPolygons = YAHOO.util.Dom.getElementsByClassName(GetPolygonClassName(areaIndex), "DIV", document.getElementById('divMap'));
            
            for(var i = 0; i < areaPolygons.length; i++){
                areaPolygons[i].className = GetPolygonClassName(areaIndex) + visibilityClassName;
                
                if(color != null)
                {
                    areaPolygons[i].style.backgroundColor = color;
                }
            }
        }
        
        GetPolygonClassName = function(index)
        {
            return 'classArea_' + index;
        }
        
        Structura.MapSelecter.Initialize = function()
        {
            jg = new jsGraphics("divMap");
            jgSelected = new jsGraphics("divMap");
        }
        
        Structura.MapSelecter.AreaOverImage = function(evt, areaIndex)
        {
            for(var i = 0; i < areaStatuses.length; i++)
            {
                var areaStatus = areaStatuses[i];
                
                if(i == areaIndex -1)
                {
                    if(areaStatus == 'unselected')
                    {
                        SetPolygonVisibility(i + 1,"unselectedover");
                    }
                    else if(areaStatus == 'selected' || areaStatus == "selectedover")
                    {
                        SetPolygonVisibility(i + 1, "selectedover");
                    }
                }
                else
                {
                    if(areaStatus == "unselectedover")
                    {
                        SetPolygonVisibility(i + 1, "unselected");
                    }
                    else if(areaStatus == "selectedover")
                    {
                        SetPolygonVisibility(i + 1, "selected");
                    }
                }
            }
        };
    };
}