/**
  parche mootols v1.2 para peticiones HTML 
 */
Request.HTML.implement({
   processHTML: function(text){
         var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
             text = (match) ? match[1] : text;
            
             var container = new Element('div');
            
             return $try(function(){
                var root = '<root>' + text + '</root>', doc;
                 if (Browser.Engine.trident){
                     doc = new ActiveXObject('Microsoft.XMLDOM');
                     doc.async = false;
                     doc.loadXML(root);
                 } else {
                     doc = new DOMParser().parseFromString(root, 'text/html');
                 }
                 root = doc.getElementsByTagName('root')[0];
                 for (var i = 0, k = root.childNodes.length; i < k; i++){
                     var child = Element.clone(root.childNodes[i], true, true);
                     if (child) container.grab(child);
                 }
                 return container;
             }) || container.set('html', text);
         }
    
     });
  

function js_slide (linkid, div, mode) {
	
   var mySlide = new Fx.Slide($(div), {mode: mode});
   mySlide.hide();
   $(linkid).addEvent('click', function(e){
     e.stop();
     mySlide.toggle();
   })
   
}



function js_popup (linkid, div) {
  
  $(div).setStyle ('opacity','0');
  $(div).setStyle ('position','absolute');
    
  $(linkid).addEvent('mouseover', function(e){
    e.stop();
	var x = e.client.x + 10;
    var y = e.client.y + 15;
	$(div).setStyle ('position','absolute');
	$(div).setStyle ('left',x);
	$(div).setStyle ('top',y);
	$(div).fade ('in');
  });
  
  $(linkid).addEvent('mouseout', function(e){ 
    $(div).setStyle ('opacity',0);
  });
  
}


/**
 @param params {'event_name':'click', 'isbold':true} 
*/
function js_request (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;
	
	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 (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 (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();
	
	};
	
}




function js_form_send (formid, div) {
	var log;
	
	var req = new Request.HTML ({
	    url:$(formid).action,
		evalScripts: true,
		method:'post',
		data: $(formid),
		update: $(div),
		
		onComplete: function () { 
		  
		},
		
		onSuccess: function(html) {
			log.destroy();
         	$(div).fade('in');
		},
		
		onFailure: function() {
			log.destroy();
		}
	
	});
			
	$(formid).addEvent('submit', function(e) {
		e.stop();
	    	
		$(div).setStyle('opacity','0.3');
		log = new Element('div', {
          'id':    'loading',
		  'class': 'ajax-loading',
          'html' : '&nbsp;',
          'styles': {
             'margin-bottom':'-10px'
             }
        });
		log.inject ($(div),'before');
		req.send();
		
	});
}


function js_close (mydiv) {
  // var tw = new Fx.Tween ($(mydiv),{'duration':500}).start ('opacity','0').chain( function(){ $(mydiv).empty(); } );
  // $(mydiv).tween ('opacity',0);
  var tw = new Fx.Tween ($(mydiv),{'duration':1000}).start ('opacity','0'); 
  tw.start ('height','0').chain ( function () { $(mydiv).empty(); } );
}


function js_sleep(seconds) {
    seconds *= 1000;
    var start = new Date().getTime();
    while (new Date().getTime() < start + seconds);
    
    return 0;
}

function js_bubble_menu (classlink, classbubble, relativedistance, orientation) {
	var myPages = $$('.'+classlink);
	var myBubbles = $$('.'+classbubble);
	
	// relativedistance  = '-100px' 
	// orientation = 'vertical|horizontal' 
	// Set bubbles opacity to zero so they're hidden initially and toggle visibility on for their container	
	myBubbles.setStyle('opacity', 0);
	//$('bubbleWrap').setStyle('visibility','visible')
	js_bubble_menu_close();
	
	// Add our events to the pages
	myPages.each(function(el, i) {
		el.set('morph', {link : 'cancel'});
		
		el.addEvents({
			'mouseenter': function() {
				js_bubble_menu_close (classbubble);
				if (orientation=='vertical') {
				   myBubbles[i].morph({
					'opacity' : 1,
					'margin-top' : relativedistance
				   });
				} else {
				   myBubbles[i].morph({
					'opacity' : 1,
					'margin-left' : relativedistance
				   });
				};
			}
		});
	});
	
}


function js_bubble_menu_close (classbubble) {
  var bb = $$('.'+classbubble);
  bb.each (function (el, i) {
     el.set ('morph', {link: 'cancel'});
     bb[i].morph({ 'opacity': 0, 'margin-top': 0 });
  });
};


function js_window_open (mypage, myname, w, h, s, r) {
  var winL = Math.round((screen.width - w) / 2);
  var winT = Math.round((screen.height - h) / 2);
  var winprops = 'height='+h+',width='+w+',scrollbars='+s+',resizable='+r;
  win = window.open(mypage, myname, winprops);
  win.moveTo(winL,winT);
}




