
GB_OpenerObj = {};
GB_RefreshLink = "";

ComplexTableField = Class.create();
ComplexTableField.prototype = {
	
	// These are defaults used if setPopupSize encounters errors
	defaultPopupWidth: 560,
	defaultPopupHeight: 390,
	
	initialize: function() {
    var rules = {};
    rules['#cpd'] = {onclick: this.openPopup.bind(this)};
    Behaviour.register('cpd',rules);
    
    this.setPopupSize();
		
		// HACK If already in a popup, we can't allow add (doesn't save existing relation correctly)
		if(window != top) $$('#cpd').each(function(el) {Element.hide(el);});
  },
  
  openPopup: function(e, _popupLink, _table) {
    if(window != top) return true;
    
    this.setPopupSize();
    
    var el,type;
		var popupLink = "";
		if(_popupLink) {
			popupLink = _popupLink;
			table = _table;
		}else {
      var el = Event.element(e);
      link = el;
      //link.href
      popupLink = baseHref()+"home?Login=1&ajax=1";
			//alert(popupLink);
			if(el.length == 0){
        Event.stop(e);
				return false;
      }
    }
    
    var title = '<img src="themes/default/images/login-overlay.jpg" alt="Login To Your Account"/>';
    
    
    
    setInterval("window.scrollBy(0,0)",20);
    
    if(GB_ONLY_ONE) GB_ONLY_ONE.callback_fn = [];

    GB_show(
			title, 
			popupLink, 
			this.popupHeight, 
			this.popupWidth,
			this.refresh.bind(this)
		);
   
    if(e) {
			Event.stop(e);
		}
		
    return false;
  },
  
  refresh : function () {
    //window.location.reload();
    location.replace(baseHref() + 'cpd');
  },
  
  setPopupSize: function() {
		try {
			this.popupHeight = parseInt(305);
			this.popupWidth = parseInt(336);
		} catch (ex) {
			this.popupHeight = this.defaultPopupHeight;
			this.popupWidth = this.defaultPopupWidth;
		}
	}
  
}

ComplexTableField.applyTo('#cpd');

function baseHref(){
  var baseTags=document.getElementsByTagName('base');
  if(baseTags)
    return baseTags[0].href;
  else 
    return"";
}