/**
  OF / A PHP Framework Javascript utils with the Mootools help.
  @release 1.0
  @date 2008-02-22
*/

/**
 @param params {'event_name':'click', 'isbold':true} 
*/
function ofrequest (linkid, div, url, params) {
	var event_name = 'click';
	var isbold    = false;   // true|false si el link se marca como negrita cuando se clicka.
	var isloading = true;    // true|false si muestra el icono de loading.
	var log;
	var isconfirm = false; // solicita confirmación para ejecutar la acción del link.
	var confirmmsg; // mensaje de confirmación.
	
	params = typeof(params) != 'undefined' ? params : 0;
	if (params!=0) {
     if ($chk(params.event_name)) event_name = params.event_name;
     if ($chk(params.isbold)) isbold = params.isbold;
     if ($chk(params.isloading)) isloading = params.isloading;
     if ($chk(params.isconfirm)) isconfirm = params.isconfirm;
     if ($chk(params.confirmmsg)) confirmmsg = params.confirmmsg;
    };
	
	if (isloading==true) {
	 log = new Element('div', {
          'id':    'loading',
		  'class': 'ajax-loading',
          'html' : '&nbsp;',
          'styles': {
             
             }
        });
	};
	
	var log;
	var req = new Request.HTML({
		url:url,
		evalScripts: true,
		method:'get',
		update: $(div),
		encoding: 'utf-8',
		
		onComplete: function () { 
		  
		},
		
		onSuccess: function(html) {
			// if (isloading==true) { log.removeClass('ajax-loading'); };
			if (isloading==true) { log.destroy(); };
         	$(div).setStyle('opacity','0'); $(div).fade ('in');
		},
		
		onFailure: function() {
			// if (isloading==true) { log.removeClass('ajax-loading'); };
			if (isloading==true) { log.destroy(); };
			// $('result').set('text', 'The request failed.');
		}
		
	});
	
	if (linkid!='') {
	   
	   $(linkid).addEvent(event_name, function(e) {
		  e.stop();
		  if (isconfirm==true) {
		    if (!confirm(confirmmsg)) { return; } 
		  }
		  // if (isloading==true) { log = $(div).empty().addClass('ajax-loading'); };
		  if (isloading==true) { log.inject ($(div),'before'); };
		  req.send();
		  if (isbold) { this.setStyle('font-weight','bold'); };
	    });	
	
	   if (isbold==true) {
	     $(linkid).addEvent ('mouseout', function(e) { 
	      e.stop(); 
	      this.setStyle('font-weight','normal');
	     });
	   };
	
	} else {
	
	  if (isloading==true) { log.inject ($(div),'before'); };
	  req.send();
	
	};
	
}





/** 
 Same as ofrequest 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 ofpost (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();
		}
	});
  });
};



function ofwindow (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);
}


/**  
  For show/display and hide a layer. 
  @param  oid the identificator layer.
*/
function ofbox (oid) {
   var myslide = new Fx.Slide(oid);
   if ($(oid).getStyle('display') == 'none') {
     $(oid).setStyle ('display','block');
	 myslide.slideIn();
	 return;
   };
   myslide.toggle();
};


/** 
 This function close a layer.
 @param oid the identificator layer.
 */
function ofclose (oid) {
  var myslide = new Fx.Slide (oid);
  myslide.hide();
};


