/**
  APF / A PHP Framework Javascript utils with the Mootools help.
  @release 1.0
  @date 2008-02-22
*/



/** 
  @param url url donde obtener el contenido / url when you want to call
  @param oid identificador de capa / the layer where you want to display the result.
  @param he  objeto con los parámetros de visualización / some params as width,height,visible 
  for example:
    APF_request ('http://mydomain.com/','mydivlayer',{visible:true, height:300});
*/
 function APF_request (url, oid, params) {
  if (!$(oid))  { return; };
  oid = $(oid);
  params = typeof(params) != 'undefined' ? params : 0;
  var height=0;
  var visible=true;
  var loading=undefined;

  if (params!=0) {
    height    = params.height;
    visible   = params.visible;
	loading   = params.loading;
  }
  if ($chk(loading)) { $(loading).setOpacity(1); };

  var fh   = new Fx.Style(oid, 'height',  {duration: 2000, transition: Fx.Transitions.Elastic.easeOut});
  var fade = new Fx.Style(oid, 'opacity', {duration: 1000});

  new Ajax(url, {
				method: "get",
				update: $(oid),
         		async: true,
				onRequest: function() {
					oid.setOpacity(0);
				},
				onComplete: function() {
					if (visible==false) { oid.setOpacity(0); } else { fade.start(0,1); };
                    if (parseInt(oid.getStyle('height'))<height) { fh.start(0, height); };
                    if ($(loading)) { $(loading).setOpacity(0); };
					this.evalScripts();
				}
			}).request();
 };




/** 
 Same as AJAX_request but in this case I make a post, not a get. 
 @param formid identificador del formulario / the form identificator, it is said <form id='myformid' ... 
 @param loadingid div con el botón de carga. / a div layer where I will display a loading gif image
 @param mylayer capa donde se realiza toda la gestión / a div layer where I wnat to display the result page.
 For example:
  AJAX_request_post ('myformid', 'myloadinglayerwithgifimage', 'mylayerwhereIwanttodisplaythecontent');
 **/
function APF_post (formid, loadingid, mylayer) {
  if (!$(formid)) { return; };
  $(formid).addEvent('submit', function(e) {
	new Event(e).stop();
	if (!$(loadingid)) { 
      var log = $(loadingid).empty().addClass('ajax-loading');
	} else { var log; };

    var uform = $(mylayer);
    evalScripts: true;

    this.send({
        update: mylayer,
        onRequest: function () {
     
		},
		onComplete: function() {
          log.removeClass('ajax-loading');
		  this.evalScripts();
		}
	});
  });
};




/**  
  For show/display and hide a layer. 
  @param  oid the identificator layer.
  @params params some aditional params. 

*/
function Box (oid, params) {
   params = typeof(params) != 'undefined' ? params : 0;
   if (params!=0) {
    height  = params.height;
    width   = params.width;
   }

   if ($(oid).getStyle('opacity') >=1 ) {
      new Fx.Style (oid, "opacity").start (1,0);
	  if (height>0) { $(oid).effect('height').start(height,0); }
  	  if (width>0)  { $(oid).effect('width').start(width,0); }
 	  return;
   };

    new Fx.Style (oid, "opacity").start (0,1);
    if (height>0) { $(oid).effect('height').start(0,height); }
    if (width>0)  { $(oid).effect('width').start(0,width); }
 
};

/** 
 This function close a layer.
 @param oid the identificator layer.
 */
function Close (oid) {
  new Fx.Style (oid, "height", {duration: 1200, transition: Fx.Transitions.Quart.easeInOut}).start (1,0);
  $(oid).setOpacity (0);
};




/** 
 Only for testing purposes
 */
function BoxFade (oid, wi) {

  if ($(oid).getStyle('opacity') >=1 ) {
      new Fx.Style (oid, "opacity").start (1,0);
      $(oid).effect('height').start(wi,0); 
	  return;
   };

   new Fx.Style (oid, "opacity").start (0,1);
   $(oid).effect('height').start(0,wi); 
};


/** 
 Only for testing purposes
 */
function Fade (oid) {
   if ($(oid).innerHTML=='') { return; };
   new Fx.Style (oid, 
	   "opacity",
	   {duration:1200,
	   transition: Fx.Transitions.Quart.easeInOut}
	   ).start (0,1);
};



/**
  Only a simple example also. Only for testing.
**/
function AJAX_post (url, formid, mylayer) {

  new Ajax(url, {
				method: "post",
				data: $(formid),
				update: $(mylayer),
         		async: true,
				evalScripts: true,
			    evalResponse: true
			}).request();
};


/** 
 Only a simple example also. Only for testing.
 */
function AJAX_get (url, oid, hide, height) {
  oid = $(oid);
  if (hide) { oid.setOpacity (0); };
  var log = $(oid).empty().addClass('ajax-loading');

  var fh   = new Fx.Style (oid, 'height',  {duration: 1000, transition: Fx.Transitions.Bounce.easeOut});
  var fade = new Fx.Style (oid, 'opacity', {duration: 1000});
  new Ajax(url, {
				method: "get",
				update: $(oid),
         		async: true,
				onRequest: function() {
					if (hide) { fade.start(0,1); };
					fh.start(0,height);
					
				},
				onComplete: function() {
					if (hide) { fade.start(1,0); };
					log.removeClass('ajax-loading');

					this.evalScripts();
				}
			}).request();
 };



function APF_window (mypage, myname, w, h){
  var winL = Math.round((screen.width - w) / 2);
  var winT = Math.round((screen.height - h) / 2);
  var winprops = 'height='+h+',width='+w+',scrollbars=no,resizable=no';
  win = window.open(mypage, myname, winprops);
  win.moveTo(winL,winT);
}


