/* InicializaciÃ³n en españl para la extensión 'UI date picker' para jQuery. */
jQuery(function($){
	$.datepicker.regional['es'] = {
		closeText: 'Cerrar',
		prevText: '&#x3c;',
		nextText: '&#x3e;',
		currentText: 'Hoy',
		monthNames: ['Enero','Febrero','Marzo','Abril','Mayo','Junio',
		'Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'],
		monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun',
		'Jul','Ago','Sep','Oct','Nov','Dic'],
		dayNames: ['Domingo','Lunes','Martes','Mi&eacute;rcoles','Jueves','Viernes','S&aacute;bado'],
		dayNamesShort: ['Dom','Lun','Mar','Mi&eacute;','Juv','Vie','S&aacute;b'],
		dayNamesMin: ['D','L','M','M','J','V','S'],
		weekHeader: 'Sm',
		dateFormat: 'dd/mm/yy',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['es']);
});


function trackGA()
{
    var aps = Array.prototype.slice;
    var args = aps.call( arguments );

    var action=['_trackEvent'].concat(args);

//    if(console.log)//Para debuguear
//    {
//        console.log(action);
//    }

    _gaq.push(action);
}

/**
 *
 * @param provinciaInput
 * @param centroInput
 */
function Agenda(provinciaInput,centroInput)
{
    this.centroInput=centroInput;
    this.provinciaInput=provinciaInput;

    this.opciones=new Object();

    this.init();
}

Agenda.prototype.init = function()
{
    var self=this;

    this.centroInput.children('optgroup').each(function(){

      var provincia =jQuery(this);
      var label=provincia.attr('label');

      self.opciones[label]=new Array();

      provincia.children().each(function(){
          var centro=jQuery(this);
          self.opcionesPut(label,centro.attr('value'),centro.text());
      }) ;

      provincia.remove();
    });

    self.refreshCentros();

    this.provinciaInput.change(function(){self.refreshCentros();}) ;

    this.provinciaInput.msDropDown();
    this.centroInput.msDropDown();
};

Agenda.prototype.opcionesPut=function(label,key,value)
{
    this.opciones[label][key]=value;
};

Agenda.prototype.refreshCentros = function()
{
    var self=this;

    var label=this.provinciaInput.find('option:selected').text();

    this.centroInput.children().each(function(){

      var centro=jQuery(this);

      if(centro.val()!=='')
      {
          centro.remove();
      }
    });

    var centros=this.opciones[label];

    for(var centro in centros )
    {
        self.centroInput.append('<option value="'+centro+'">'+centros[centro]+'</option>');
    }

    this.centroInput.msDropDown();//Actualiza el listado de centros
};
