// --------------------------------------------------
//  AJAX COMMON JSON-OBJECT PACKAGE
// --------------------------------------------------

JcmsJsonRequest = Class.create();
JcmsJsonRequest.prototype = {
  initialize: function(elm) {
    this.elm      = elm;
    this.effect   = null;
    this.callback = null;
    this.rpc      = null;
    
    this.isOk     = false;
    this.isDone   = false;
    this.isUpdate = false;
    this.isEffect = false;
  },
  
  asyncJsonCall: function(){
     // Set browser to waiting mode
     Ajax.setWaitState(true,this.elm);
     
     try{
       // Call Effect
       if (this.effect)
         this.effect();
       else
         this.isEffect = true;
     
       // Call Json
       if (this.rpc)
         this.rpc();
     }
     catch(ex){
		   alert(I18N.glp('warn.json.sessiontimeout'));
		   Ajax.setWaitState(false,elm);
		}
  },
  
  asyncJsonCallBack: function(returnValue){
    if (returnValue){ 
      this.returnValue = returnValue;
    }
    else{
      this.isOk = true;
    }
    
    // Response is done
    this.isDone = true;
    
    // Call response callback
    this.asyncResponseCallBack();
  },
  
  asyncEffectCallBack: function(effect){
    // Effect is finished
    this.isEffect      = true;
    this.workingEffect = effect;
    
    // Call response callback
    this.asyncResponseCallBack();
  },
  
  asyncResponseCallBack: function(){
    if (!this.isDone)
      return;
      
    if (this.isUpdate)
      return;
      
    if (!this.isEffect)
      return;
      
    this.isUpdate = true;
    
    // Call callback
    if (this.callback)
      this.callback(this.returnValue, this.workingEffect);
    
    // Remove browser from waiting mode
    Ajax.setWaitState(false,this.elm);
  }
}


// --------------------------------------------------
//  AJAX COMMON PACKAGE
// --------------------------------------------------

if (!window.Ajax) {
  var Ajax = new Object();
}

Object.extend(Ajax,{
    version: '$Revision: 1.9 $',
    
    // ----------------------------
    //  Common usefull AJAX Method
    // ----------------------------
    
    setWaitState: function(wait, elm){
    
      var body = document.getElementsByTagName('body')[0];
      
      if (!Ajax.waitDiv){
        Ajax.waitDiv = document.createElement('DIV');
        Ajax.waitDiv.innerHTML     = I18N.glp('info.msg.loading');
        Ajax.waitDiv.className     = 'ajaxwait';
        Ajax.waitDiv.style.display = 'none';
				body.appendChild(Ajax.waitDiv);
      }
      
      
      // Set wait cursor
      if (wait){
        if (elm) elm.style.cursor  = 'wait';
        Ajax.waitDiv.style.display = 'block';
        body.style.cursor = 'wait';
        window.status = I18N.glp('info.msg.loading');
      }
      else{ // Reset cursor
        if (elm) elm.style.cursor  =  '';
        Ajax.waitDiv.style.display = 'none';
        body.style.cursor = '';
	      window.status='';
      }
    }
});


// ----------------------------------------------------
//  UTILITY OBJECT
// ----------------------------------------------------

// Util 'static Class'
if (!window.Util) {
  var Util = new Object();
}

Object.extend(Util,{

  // convert the given object in to a boolean value, or use
  // default value if object cannot be converted
  toBoolean: function(object, defaultValue) {
    if (typeof object == 'boolean') { return object; }
    if (object == 'false' || object == 'no' ) { return false; }
    if (object == 'true'  || object == 'yes') { return true;  }
    return defaultValue;
  },
  
  // Retrieve "position" and "dimension" of the given window's
  // viewport (or the current window if no window argument is given).
  // 
  // returns an object containing the following value :
  //   obj.x      = viewport X position in the screen
  //   obj.y      = viewport Y position in the screen
  //   obj.width  = viewport width
  //   obj.height = viewport height
  //
  // Warning: the position returned in internet explorer is the position
  // of the window not the viewport (i.e. the viewport position being the
  // position of the window + title and toolbars offset)
  //
  // cf. http://www.quirksmode.org/viewport/compatibility.html
  getViewportBounds: function(win) {
	  var vpWidth = 0; 
	  var vpHeight = 0;
	  var vpXposInScreen = 0;
	  var vpYposInScreen = 0;
	  
	  if (!win) {
	    win = self;
    }
    var doc = win.document;
    
    // 1. Viewport Position

    // all but mozilla
    if (win.screenTop){
      vpXposInScreen = win.screenLeft;
      vpYposInScreen = win.screenTop;
    }
    // mozilla
    else if (win.screenX){
      vpXposInScreen = win.screenX;
      vpYposInScreen = win.screenY;
    }
    
    // 2. Viewport Dimension
    
	  //   all except Explorer
	  if (win.innerHeight) {
	    vpWidth = win.innerWidth;
	    vpHeight = win.innerHeight;
	  }
	  //   Explorer 6 Strict Mode
	  else if (doc.documentElement && win.document.documentElement.clientHeight) {
	    vpWidth = doc.documentElement.clientWidth;
	    vpHeight = doc.documentElement.clientHeight;
	  }
	  //   other Explorers
	  else if (document.body) {
	    vpWidth = doc.body.clientWidth;
	    vpHeight = doc.body.clientHeight;
	  }
    
	  return { x: vpXposInScreen,
             y: vpYposInScreen,
             width: vpWidth,
             height: vpHeight };
	}
});


// ----------------------------------------------------
//  POPUP OBJECT
// ----------------------------------------------------

var Popup = {
  popupWindow: function(url, title, w, h, status, resizable, scrollbars, reuse, winOpener){

	  if (!status) status="no";
	  if (!w) w=320;
	  if (!h) h=260;
	 
	  resizable = "resizable=" + (Util.toBoolean(resizable, true) ? "yes" : "no");
    scrollbars = "scrollbars=" + (Util.toBoolean(scrollbars, true) ? "yes" : "no");
	 
	  if (reuse == undefined) { 
	    reuse = true; 
	  }
	 
	  if (!navigator.jalios) {
	    navigator.jalios = new Object();
	  }
	 
	  if (winOpener == undefined) {
	    winOpener = window;
	  }
	 
	  if (reuse && navigator.jalios.popupWindow && navigator.jalios.popupWindow.close) {
	    navigator.jalios.popupWindow.close();
	  }
	  
	  if (!winOpener.opener) {
	    title = '_blank';
	  }
	  
	  try {
	    navigator.jalios.popupWindow = winOpener.open(url, title, 'status=' + status + ',width=' + w+ ',height=' + h + ',menubar=no,'+ resizable + ',' + scrollbars);
	    if (!navigator.jalios.popupWindow){
	      alert(I18N.glp('warn.popup.blocker'));
	    }
	  }
	  catch(ex){
	     alert(I18N.glp('warn.popup.blocker'));
	  }
	  
	  // Set the focus if opener have the focus
	  if (winOpener.focus && navigator.jalios.popupWindow){
	    navigator.jalios.popupWindow.focus();
	  }
  },
  
  
  // resize the current window to the size of the given div.
  //
  // @param divID the div of which to retrieve dimension and to
  //        use as a reference for the new window size
  // @param offsetHeight an integer value that will be added to the
  //        new window height, use this value when you want to add some
  //        margin to the div height (default is 55 if not given)
  autoResize: function(divID, offsetHeight) {

    if (!offsetHeight) {
      offsetHeight = 55;
    }
 
    //var elementDim = $(document.body).getDimensions();
    var elementDim = $(divID).getDimensions();
    var vpBounds = Util.getViewportBounds(); // { x, y, width, height }
    
    // Compute the new height
    var newWinHeight = elementDim.height + offsetHeight;
    newWinHeight = Math.min(newWinHeight, self.screen.availHeight);
    
    // Make sure the window is not too high
    if (vpBounds.y && (newWinHeight + vpBounds.y > self.screen.availHeight) ) {
      newWinHeight = self.screen.availHeight - vpBounds.y;
    }
    
    // Resize the window
    window.resizeTo(vpBounds.width, newWinHeight);
  }
  
}


// ----------------------------------------------------
//  CONVENIENT
// ----------------------------------------------------

document.getElementsBySelector = function(selector) {
  return $$(selector);
}

