// if (typeof(localStorage.portalWindowCount) == "undefined") {
//  localStorage.portalWindowCount = 0;
// }
var portalWindowCount = 0;
// ------------------------------------------------------------------------------------
// Open portal window
// ------------------------------------------------------------------------------------
function openPortalWindow(url, title, closeButton, width, height, callback) {
  height = adjustHeight(height);
  
  var cb = (typeof(callback) == "undefined") ? ' ' : callback;
  if (parent.Ext.getCmp('portfolio-tabpanel')) {
    parent.Ext.getCmp('portfolio-tabpanel').fireEvent('openPortalWindow', url, title, closeButton, width, height); 	
  } else if (parent.parent.Ext.getCmp('portfolio-tabpanel')) {
    parent.parent.Ext.getCmp('portfolio-tabpanel').fireEvent('openPortalWindow', url, title, closeButton, width, height); 	
  } else if (parent.parent.Ext.getCmp('gasa-frontpage')) {   // gasa portal should never be named gasa-frontpage :-(
    parent.parent.Ext.getCmp('gasa-frontpage').fireEvent('openPortalWindow', url, title, closeButton, width, height, cb); 	
    return;
  }
}

// ------------------------------------------------------------------------------------
// Adjust height
// ------------------------------------------------------------------------------------
function adjustHeight(height) {
  var h = new String(height);
  var p = (parent.parent) ? parent.parent : parent;
  if (h.indexOf('%') == -1 && p) {
    if( typeof( p.window.innerHeight ) == 'number' ) {  //Non-IE
      clientHeight = p.window.innerHeight;
    } else if(p.document.documentElement && p.document.documentElement.clientHeight) {  //IE 6+ in 'standards compliant mode'
      clientHeight = p.document.documentElement.clientHeight;
    } else if(p.document.body && p.document.body.clientHeight) { //IE 4 compatible
      clientHeight = p.document.body.clientHeight;  
    }
    height = (height > clientHeight) ? clientHeight - 30 : height;
  }
  return height;
}

// ------------------------------------------------------------------------------------
// Portal window
// ------------------------------------------------------------------------------------
function portalWindow(url, title, closeButton, width, height) {
  var t = (typeof(title) == "undefined") ? ' ' : title;
  var w = (typeof(width) == "undefined") ? 500 : width;
  var h = (typeof(height) == "undefined") ? 500 : height;

  var header = (t === ' ') ? false : true;

  // var id = 'portal-window-' + localStorage.portalWindowCount ++;
  var id = 'portal-window-' + portalWindowCount ++;
  if (!window) {
    var window = new Ext.Window({
      id: id
      ,header:header
      ,title:t
      ,width: w
      ,height: h
      ,minWidth: 10
      ,minHeight: 10
      ,layout: 'fit'
      ,plain:true
      ,modal: false
      ,autoScroll : true
      ,collapsible: true
      ,maximizable: true
      ,buttonAlign:'right'
      ,listeners: {
        closeWindow: function() {
          window.hide();
          window.destroy();
        },
        maximize : function() {
          if (Ext.isIE) { window.location.reload(); }
        }
      }		
    });
  }
  // insert portal window id into url
  if (url.indexOf("?") == -1) {
    url = url + '?portalWindowID='+id;
  } else {
    url = url + '&portalWindowID='+id;
  }  
  
  window.html = '<iframe scrolling="auto" frameborder="0" width="99%" height="99%" src="'+url+'"></iframe>';
  var close = (typeof(closeButton) == "undefined") ? false : closeButton;
  if (close) {
    window.addButton({
      text: menu.I18N['closeWindow']
      ,handler: function() {
        window.hide();
        window.destroy();
      }
    });
  }
  window.show();
}

/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.ux.Portal = Ext.extend(Ext.Panel, {
    layout: 'column'
    ,autoScroll:true
    ,cls:'x-portal'
    ,defaultType: 'portalcolumn'
    ,initComponent : function(){
      Ext.ux.Portal.superclass.initComponent.call(this);
      this.addEvents({
        validatedrop:true
        ,beforedragover:true
        ,dragover:true
        ,beforedrop:true
        ,drop:true
      });
    }
    ,initEvents : function(){
      Ext.ux.Portal.superclass.initEvents.call(this);
      this.dd = new Ext.ux.Portal.DropZone(this, this.dropConfig);
    }
});
Ext.reg('portal', Ext.ux.Portal);


Ext.ux.Portal.DropZone = function(portal, cfg){
    this.portal = portal;
    Ext.dd.ScrollManager.register(portal.body);
    Ext.ux.Portal.DropZone.superclass.constructor.call(this, portal.bwrap.dom, cfg);
    portal.body.ddScrollConfig = this.ddScrollConfig;
};

Ext.extend(Ext.ux.Portal.DropZone, 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(), portal = this.portal, px = dd.proxy;

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

        // handle case scroll where scrollbars appear during drag
        var cw = portal.body.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 = portal.items.itemAt(col),
            items = c.items.items;

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

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

        if(portal.fireEvent('validatedrop', overEvent) !== false &&
           portal.fireEvent('beforedragover', overEvent) !== false){

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

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

            this.lastPos = {c: c, col: col, p: match && p ? pos : false};
            this.scrollPos = portal.body.getScroll();

            portal.fireEvent('dragover', overEvent);

            return overEvent.status;;
        }else{
            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;

        var dropEvent = this.createEvent(dd, e, data, col, c,
                pos !== false ? pos : c.items.getCount());

        if(this.portal.fireEvent('validatedrop', dropEvent) !== false &&
           this.portal.fireEvent('beforedrop', dropEvent) !== false){

            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.portal.bwrap.getBox();
        box.columnX = [];
        this.portal.items.each(function(c){
             box.columnX.push({x: c.el.getX(), w: c.el.getWidth()});
        });
        return box;
    }
});