Ext.ux.PortalDropZone = function(el,cfg){
   
    this.colums=[];
    if(cfg.colums)
    {
    	for(var i=0,len=cfg.colums.length;i<len;i++)
    		this.colums[i]=Ext.get(cfg.colums[i]);
    	delete cfg.colums;
    }
    this.itemSelector='.block';
    Ext.ux.PortalDropZone.superclass.constructor.call(this,el, cfg);
    
    this.collectItems();
    Ext.dd.ScrollManager.register(this.el);
    this.el.ddScrollConfig = this.ddScrollConfig;
    
};

Ext.extend(Ext.ux.PortalDropZone, Ext.dd.DropTarget, {
    ddScrollConfig : {
        vthresh: 50,
        hthresh: -1,
        animate: true,
        increment: 200
    },
    createEvent : function(dd, e, data, col, c, pos){
        return {
            portal: this.portal,
            panel: data.panel,
            columnIndex: col,
            column: c,
            position: pos,
            data: data,
            source: dd,
            rawEvent: e,
            status: this.dropAllowed
        };
    },
    notifyOver : function(dd, e, data){
        var xy = e.getXY(), el = this.el, px = dd.proxy;

        // case column widths
        if(!this.grid){
            this.grid = this.getGrid();
        }

        // handle case scroll where scrollbars appear during drag
        var cw = el.dom.clientWidth;
        if(!this.lastCW){
            this.lastCW = cw;
        }else if(this.lastCW != cw){
            this.lastCW = cw;
            //portal.doLayout();
            this.grid = this.getGrid();
        }

        // determine column
        var col = 0, xs = this.grid.columnX, cmatch = false;
        for(var len = xs.length; col < len; col++){
            if(xy[0] < (xs[col].x + xs[col].w)){
                cmatch = true;
                break;
            }
        }
        // no match, fix last index
        if(!cmatch){
            col--;
        }

        // find insert position
        var p, match = false, pos = 0,
            c = this.colums[col],
            items = c.items.elements;

        for(var len = items.length; pos < len; pos++){
            p = items[pos];
            var h = p.getHeight();
            if(h !== 0 && (p.getY()+(h/2)) > xy[1]){
                match = true;
                break;
            }
        }

        var overEvent = this.createEvent(dd, e, data, col, c,
                match && p ? pos : c.items.getCount());

      

            // make sure proxy width is fluid
            px.getProxy().setWidth('auto');

            if(p){
                px.moveProxy(p.dom.parentNode, match ? p.dom : null);
            }else{
                px.moveProxy(c.dom, null);
            }

            this.lastPos = {c: c, col: col, p: match && p ? pos : false};
            this.scrollPos = this.el.getScroll();
            YAHOO.log(col+','+pos);
            
            //portal.fireEvent('dragover', overEvent)
            return overEvent.status;
    },

    notifyOut : function(){
        delete this.grid;
    },

    notifyDrop : function(dd, e, data){
        delete this.grid;
        if(!this.lastPos){
            return;
        }
        var c = this.lastPos.c, col = this.lastPos.col, pos = this.lastPos.p;
       //  alert(c.items.elements[pos]);
        c.dom.insertBefore(dd.panel.el.dom,c.items.elements[pos]?c.items.elements[pos].dom:null);
     
        this.collectItems();
        if(this.callback)
        {
        	this.callback(this.colums);
        }
        /*
        var dropEvent = this.createEvent(dd, e, data, col, c,
                pos !== false ? pos : c.items.getCount());

     

            dd.proxy.getProxy().remove();
            dd.panel.el.dom.parentNode.removeChild(dd.panel.el.dom);
            if(pos !== false){
                c.insert(pos, dd.panel);
            }else{
                c.add(dd.panel);
            }
            
            //c.doLayout();

           // this.portal.fireEvent('drop', dropEvent);

            // scroll position is lost on drop, fix it
            var st = this.scrollPos.top;
            if(st){
                var d = this.portal.body.dom;
                setTimeout(function(){
                    d.scrollTop = st;
                }, 10);
            }
        delete this.lastPos;*/
    },

    // internal cache of body and column coords
    getGrid : function(){
        var box = this.el.getBox();
        box.columnX = [];
        var colums=this.colums;
        for(var i=0,len=colums.length;i<len;i++)
        {
        	 box.columnX.push({x: colums[i].getX(), w: colums[i].getWidth()});
        }
        return box;
    },
    collectItems:function()
    {
    	for(var i=0,len=this.colums.length;i<len;i++)
    	{
    		//this.colums[i]=Ext.get(cfg.colums[i]);
    		this.colums[i].items=this.colums[i].select(this.itemSelector,true);
    		YAHOO.log(this.colums[i].items.getCount());
    	}
    }
});
