var Selection = function()
{
    var hilighted;

    var _pub = {
        //returns an array of textnodes from a dom range passed to it
        getTextNodes: function(selection)
        {
            var ret = new Array();

            var start = Wrapper.originalTextNode(selection.startContainer);
            var end = Wrapper.originalTextNode(selection.endContainer);

            /*
            var start_block = TextNode.getBlockNode(start);
            if(tmp = TextNode.prevLeaf(start,start_block,'img'))
            {
                if(tmp.style.cssFloat == 'left' || tmp.style.cssFloat ==
                    'right')
                    ret.push(tmp);
            }
            */

            Wrapper.free();
            ret.push(start);
            if(start != end)
            {
                var start_top = start.parentNode.up('div.editable');

                if(!start_top)
                    return new Array();

                var walker = start;
                var found_end = 0;
                while(walker = TextNode.nextLeaf(walker,start_top,
                    'text,img'))
                {
                    ret.push(walker);
                    if(walker == end)
                    {
                        found_end = 1
                        break;
                    }
                }
                if(!found_end)
                    ret.push(null);
            }

            return ret;
        },

        activate: function(nodes,add)
        {
            var parent = null;
            Wrapper.free('hilighted');

            if(!nodes)
                nodes = hilighted;

            if(!Object.isArray(nodes))
                nodes = new Array(nodes);
            else if(!nodes.length)
                return;

            var current_div = null;

            nodes.each(function(node){
                if(node.parentNode)
                {
                    if(!current_div)
                    {
                        if(node.nodeType == 3)
                            current_div = node.parentNode.up('div.editable');
                        else
                            current_div = node.up('div.editable');
                    }
                    if(node.nodeType == 3)
                        parent = Wrapper.ancestor(node);

                    if((node.nodeType == 3 &&
                        !node.parentNode.match('p.add_content')) ||
                        (node.nodeType != 3 && node.match('img')))
                    {
                        Wrapper.wrap(node,'selected',1,1,add);
                    }
                    else if(node.nodeType != 3 && !node.match('p.add_content'))
                    {
                        var textnodes = TextNode.collectTextNodes(node);
                        textnodes.each(function(tn){
                            Wrapper.wrap(tn,'selected',1,1,add);
                        });
                    }
                }
            });

            if(parent)
                parent.blur();

            Pallette.refresh(current_div);
        },

        hilight: function(e,where)
        {
            if(Object.isElement(e))
            {
                hilighted = e;
                Wrapper.wrap(e,'hilighted',0,1);
                if(e.match('img') && !Util.match(e,'.floatLeft,.floatRight') &&
                    !e.up('.alignLeft,.alignCenter,.alignRight,.floatLeft,.floatRight'))
                    Editor.addBlinkers(e);
            }
            else if(where.inside)
            {
                var current =
                TextNode.findTextNode(e.pointerX(),e.pointerY(),e.element(),where.inside).node;
                if(current)
                {
                    if(!(Dropper.dropping() || RRMark2.selectInProgress()) && current != hilighted)
                    {
                        hilighted = current;
                        Wrapper.wrap(current,'hilighted',0,1);
                        if(!where.inside.match('p.add_content'))
                            Editor.addBlinkers(current);
                    }
                }
                else
                {
                    hilighted = null;
                    Wrapper.free('hilighted');
                    Editor.hideBlinkers();
                }
            }
            else
            {
                hilighted = null;
                Wrapper.free('hilighted');
                Editor.hideBlinkers();
            }
        },

        getHilighted: function()
        {
            return hilighted;
        },

        deHilight: function()
        {
            hilighted = null;
        }
    };

    return _pub;
}();
