// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var tipo = 0;
var tooltips = [];

function creaCalendarioContacto()
{
    var cal = new CalendarPopup("cal");
    cal.setReturnFunction("recogerValoresCalContacto");
    configuraCalendario(cal);
    return cal;
}

function creaCalendarioAviso()
{
    var cal = new CalendarPopup("cal");
    cal.setReturnFunction("recogerValoresCalAviso");
    configuraCalendario(cal);
    return cal;
}

function creaCalendarioUsuario()
{
    var cal = new CalendarPopup("cal");
    cal.setReturnFunction("recogerValoresCalUsuario");
    configuraCalendario(cal);
    return cal;
}

function configuraCalendario(cal)
{
    cal.setMonthNames('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
    cal.setMonthAbbreviations("Ene","Feb","Mar","Abr","May","Jun","Jul","Ago","Sep","Oct","Nov","Dec");
    cal.setDayHeaders("D","L","M","X","J","V","S");
    cal.setWeekStartDay(1);
    cal.setTodayText("Hoy");
}

function mostrarCal(tipo) {
    var d = new Date(), mes = $F(tipo + '_mes'), dia = $F(tipo + '_dia');

    if (mes != '' && dia != '') { d.setMonth(mes - 1); d.setDate(dia); }
    popcal.currentDate = d;
    popcal.showCalendar('linkCal');

    return false;
}

function mostrarCalContacto(tipoCal) {
    tipo = tipoCal;
    var d = new Date(), mes = jQuery('#aviso' + tipo + '_mes').val(), dia = jQuery('#aviso' + tipo + '_dia').val();
    if (mes != '' && dia != '') { d.setMonth(mes - 1); d.setDate(dia); }
    popcal.currentDate = d;
    popcal.showCalendar('linkCal' + tipo);

    return false;
}

function recogerValoresCalAviso(y, m, d) {
    jQuery('#aviso_dia').val(d); jQuery('#aviso_mes').val(m);
}

function recogerValoresCalUsuario(y, m, d) {
    jQuery('#usuario_dia').val(d); jQuery('#usuario_mes').val(m);
}

function recogerValoresCalContacto(y, m, d) {
    jQuery('#aviso' + tipo + '_mes').val(m); jQuery('#aviso' + tipo + '_dia').val(d);
    jQuery('#textoAviso' + tipo).html(d + " de " + nombreMes(m));
}

function nombreMes(m) {
    var meses = ['', 'Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'];
    return meses[m];
}

function cargarTooltips()
{
    tooltips.each( function(t) {  new Effect.Tooltip(t[0], t[1], t[2]); });
}

/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
var Fabtabs = Class.create();

Fabtabs.prototype = {
	initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
	},
	setupTab : function(elm) {
		Event.observe(elm,'click',this.activate.bindAsEventListener(this),false)
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		Event.stop(ev);
		this.show(elm);
		this.menu.without(elm).each(this.hide.bind(this));
	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');

	},
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}