$(document).ready(function () {

    //Tooltips
    $("a.tip_trigger").live('mouseenter', function(){
        tip = $(this).find('.tip');
        tip.show(); //Show tooltip
    });

    $("a.tip_trigger").live('mouseleave', function() {
        tip.hide(); //Hide tooltip
    });


    $("a.tip_trigger").live('mousemove', function(e) {
        var mousex = e.pageX + 20; //Get X coodrinates
        var mousey = e.pageY + 20; //Get Y coordinates
        var tipWidth = tip.width(); //Find width of tooltip
        var tipHeight = tip.height(); //Find height of tooltip

        //Distance of element from the right edge of viewport
        var tipVisX = $(window).width() - (mousex + tipWidth);
        //Distance of element from the bottom of viewport
        var tipVisY = $(window).height() - (mousey + tipHeight);

        if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
            mousex = e.pageX - tipWidth - 20;
        } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
            mousey = e.pageY - tipHeight - 20;
        }
        //Absolute position the tooltip according to mouse position
        tip.css({  top: mousey, left: mousex });
    });


    /* CALENDAR HIDE/SHOW EFFECT */

    $("a.calendar-toggle").live('click', function(){

        $string = $(this).attr('name');
        $string = $string.split(' ');

        $("table#calendar").fadeTo('slow', 0.3);
        $("div.loading").show();

        $.ajax({
          url: '/calendar/ahah/'+$string[1],
          type: 'GET',
          dataType: 'HTML',
          success: function(data){
              $("table#calendar").replaceWith(data);
              $("table#calendar").fadeTo('fast', 1);
              $("div.loading").hide();
          }

        });


    });


    /* HIDE SHOW FILTERS */
    $("a.filter-toggle").live('click', function(){

        $string = $(this).attr('id');
        $string = $string.split('-');

        $(this).toggleClass('brown');

        $("li.filter-"+$string[1]).toggle();

    });


});
