 /* Coded with love by Lúcuma labs <http://lucumalabs.com> */

$.fn.shake = function () {
    this.each(function () {
        var jqNode = $(this);
        var x;
        jqNode.css({
            position: 'relative'
        });
        for (x = 1; x <= 2; x += 1) {
            jqNode
            .animate({left: -10}, 0)
            .animate({left: 0}, 30).animate({left: 10}, 30)
            .animate({left: 0}, 30)
            ;
        }
    });
    return this;
};


(function setUpSedeSelector () {
    var $ssub = $('ul.selectable-subpages');
    if ($ssub.length == 0){
        return $.noop;
    }
    var selItems = [];
    var dselItems = {};
    var $sub = $ssub.find('ul > li').hide();
    var $first, text;

    for (var i = 0, item; item = $sub[i]; i++){
        text = $.trim($(item).text());
        if (text in dselItems){
            continue;
        }
        dselItems[text] = 1;
        selItems.push('<option value="' + text + '">' + text + '</option>');
        if (i == 0){
            $first = $(item);
        }
    }
    if (!selItems.length){
        return $.noop;
    }
    var $select = $('<select><option value="">-</option></select>')
        .append($(selItems.join('')))
        .change(function(e){
            var val = $select.val();
            if (!val){
                $sub.show();
                $sub.parents('li').show();
                return $.noop;
            }
            $sub.parents('li').hide();
            var $item, $par;
            for (var i = 0, item; item = $sub[i]; i++){
                $item = $(item);
                $par = $item.parents('li');
                if ($.trim($item.text()) == val){
                    $item.show();
                    $par.show();
                } else {
                    $item.hide();
                }
            }
        });
    $('<li class="selector"><label>Filtrar por sede:</label></li>')
        .append($select).insertBefore($first.parents('li'));
}());


(function setUpContactForm () {
    var $contact_form = $('#contact_form').find('form');

    if ($contact_form.length == 0) {
        return $.noop;
    }
    var ERROR_CLASS = 'error';
    var ANIM_SPEED = 400;
    var $fullname = $('#form_fullname');
    var $spam = $('#form_web');
    var $email = $('#form_email');
    var $comment = $('#form_comment');
    var $btn = $contact_form.find('.submit button');
    var fullname, email, comment;
    var $thanks = false;
    
    $contact_form.bind('submit', function(e){
        if ($thanks){
            $thanks.remove();
        }
        fullname = $.trim($fullname.val());
        email = $.trim($email.val());
        comment = $.trim($comment.val());
        
        if (!fullname.length){
            $fullname.addClass(ERROR_CLASS).shake();
        }
        if (!email.length){
            $email.addClass(ERROR_CLASS).shake();
        }
        if (!comment.length){
            $comment.addClass(ERROR_CLASS).shake();
        }

        if (! $spam.val().length){
            $.post(this.action, {
                'fullname': fullname,
                'email': email,
                'comment': comment,
                'ajax': 1
            },
            function(){
                $fullname.removeClass(ERROR_CLASS);
                $email.removeClass(ERROR_CLASS);
                $comment.removeClass(ERROR_CLASS);
            });
        }
        $fullname.val('');
        $email.val('');
        $comment.val('');
        
        $thanks = $('<div class="message">Gracias. Lo estaremos revisando a la brevedad</div>').hide()
            .insertBefore($contact_form).slideDown(ANIM_SPEED);
        
        e.preventDefault();
        e.stopPropagation();
    });
    
    $btn.mousedown(function(e){
        fullname = $.trim($fullname.val());
        email = $.trim($email.val());
        comment = $.trim($comment.val());
        
        if (!fullname.length){
            $fullname.addClass(ERROR_CLASS).shake();
            e.preventDefault();
            e.stopPropagation();
        }
        if (!email.length){
            $email.addClass(ERROR_CLASS).shake();
            e.preventDefault();
            e.stopPropagation();
        }
        if (!comment.length){
            $comment.addClass(ERROR_CLASS).shake();
            e.preventDefault();
            e.stopPropagation();
        }
    });
}());


(function setUpEventsCalendar () {
    var $events_calendar = $('#events_calendar');

    if ($events_calendar.length == 0) {
        return $.noop;
    }
    $events_calendar.fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        editable: false,
        events: EVENTS,

        eventRender: function(event, element) {
            element.find('.fc-event-title')
                .append('<br><a class="button" href="' + event.url
                    + '">Ver</a>');
        },

        buttonText: {
            prev: '&nbsp;&#9668;&nbsp;',
            next: '&nbsp;&#9658;&nbsp;',
            prevYear: '&nbsp;&lt;&lt;&nbsp;',
            nextYear: '&nbsp;&gt;&gt;&nbsp;',
            today: 'hoy',
            month: 'mes',
            week: 'semana',
            day: 'día'
        },

        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'],
        firstDay: 1,
        isRTL: false
    });
}());


(function setUpNewsTabs () {
    var $news = $('.news');
    if ($news.length == 0) {
        return $.noop;
    }
    $news.find('h2 span').mousedown(function(e){
        e.preventDefault();
        e.stopPropagation();
        $news[0].className = 'news ' + this.parentNode.className;
    });
}());


(function setupGrayscale () {
    var $images = $('.initiatives a');
    if (typeof(grayscale) == 'undefined' || $images.length == 0){
        return;
    }

    $images.hover(
        function(){
            grayscale(this);
        },
        function(){
            grayscale.reset(this);  
        }
    );
}());


