// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

/*------------------------------------------------------------------------------------
 
	Preloader
	A very simple image preloader object
  
 	Usage:
 	
    Preloader.add(path);
    Preloader.onFinish(func);
    Preloader.load();
    
      path: 		A string or array of strings of image paths to preload
      func:     A function or array of functions to be called after images are loaded

      load():   Start the preloader
      
------------------------------------------------------------------------------------*/

var Preloader = {
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,
  
  add: function(image){
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },
  
  checkFinished: function(){
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}

/**
 * Pasa elementos de un select box multiple a otro.
 * @author Amed Rodriguez 29/Mayo/08
 */
function SendElement( sel1, sel2 ) 
{
	obj = document.getElementById( sel1 );
  	for ( i=0; i<obj.length; i++ )
	{
		opt = obj.options[i];
		if ( opt.selected ) 
		{
			valor = opt.value;
			txt = obj.options[i].text;
			obj.options[i] = null;
			obj2 = document.getElementById( sel2 );
			opc = new Option(txt, valor);
			eval(obj2.options[obj2.options.length] = opc);
		}
	}
}

/**
 * Selecciona todos los elementos de un select box multiple.
 * @author Amed Rodriguez 29/Mayo/08
 */
function SelectElements( select_id )
{
	obj = document.getElementById( select_id );
	for ( j=0; j<obj.length; j++ ) 
	{
		obj[ j ].selected = true;
	}
}

/**
 * Convierte un numero a formato moneda.
 */
function formatCurrency(num) {
	num = num.toString();
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function muestra(numero)
{
	identificador="popup_comment_"+numero;

	document.getElementById(identificador).style.display='block';
	mostrando=true;
}

function esconde(numero)
{
	identificador="popup_comment_"+numero;
	document.getElementById(identificador).style.display='none';
	mostrando=false;
}

//==========================================
// Check All boxes
//==========================================
function CheckAll(fmobj)
{
	for (var i=0;i<fmobj.elements.length;i++)
	{
		var e = fmobj.elements[i];
		if ((e.name != 'allbox') && (e.type=='checkbox') && (!e.disabled))
		{
			e.checked = fmobj.allbox.checked;
		}
	}
}


//==========================================
// Check All boxes
//==========================================
(function($){
	$(function(){
		$('#no_enviados').bind("click", function(){
			$('.no-enviado').each(function(){
				$(this).attr("checked", "true");
			});
		});
		return false;
	});
})(jQuery);

