
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['#Login'] = {onclick: this.openPopup.bind(this)};
    Behaviour.register('Login',rules);
    
    this.setPopupSize();
		
		// HACK If already in a popup, we can't allow add (doesn't save existing relation correctly)
		if(window != top) $$('#Login').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;
      popupLink = link.href+"&ajax=1";
			
			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();
  },
  
  setPopupSize: function() {
		try {
			this.popupHeight = parseInt(305);
			this.popupWidth = parseInt(336);
		} catch (ex) {
			this.popupHeight = this.defaultPopupHeight;
			this.popupWidth = this.defaultPopupWidth;
		}
	}
  
}

ComplexTableField.applyTo('#Login');
