var MV = this.MV || {};

MV.Util = (function($) {

    /*  Use our server-side url shortener to reduce a full url and a referring
     *  "method type" string to a short url. The callback argument should be
     *  a function that accepts one parameter- an object with 'short_url' and 'ref'
     *  attributes.
     */
    function shorten_url(url, method, callback) {
        var shortener_url = '/ref/get_link/?url=' + encodeURIComponent(url) + '&method=' + method;
        $.get(shortener_url, function(response) {
            if (response && callback) {
                var ref_code = response.match(/\/([\w\d]+)\/$/)[1];
                callback({ 'short_url': response,
                           'ref': ref_code });
            }
        });
    }

    return {
        shorten_url: shorten_url
    }
}(jQuery));


$(document).ready(function() {
    // TextfieldPlaceholders() requires jquery.placeholder.js
    var i = document.createElement('input');
    if (!('placeholder' in i)) {
        $('#dropDownInputs').TextfieldPlaceholders();
        $('#nav_search').TextfieldPlaceholders();
    }

    $('#footer_buttons img').hover(function() { // Mouse Over Function
        $('#social_label').show().html(this.alt);
    }, function() { // Mouse Out Function
        $('#social_label').hide().html('');
    });

    // Dynamic login box in header:
    $('#logInDropDown a#logIn').click(function(e) {
        $("#dropDownInputs").slideToggle();
        if ($("#dropDownInputs").is(":visible")) {
            $("#logInDropDown #username").focus();
        }
        e.preventDefault();
    });
    $('#logInDropDown #cancelLink').click(function() {
        $("#dropDownInputs").slideUp('fast');
    });

    // Highlight the active nav link, if any:
    (function() {
        var re_match = location.pathname.match(/^\/([\w-]+)\//);
        if(re_match && re_match.length > 1) {
            $('#nav a.' + re_match[1]).addClass('active');
        }
    })();

    // Dismiss messages with a click:
    $('#messages').click(function(e) {
        $(this).fadeOut('fast');
    });

    // Favourite room buttons:
    var fave_buttons = $('span.fave_buttons input');
    fave_buttons.click(function(e) {
        var button = this;
        $(button).addClass('loading');
        var url = $(button).attr('data-url');
        var action = $(button).attr('data-action');
        $.post(url, {'action': action}, function(response) {
            $(button).removeClass('loading').hide();
            $(button).siblings('input').show();
        });
    });
    fave_buttons.hover(function(e) {
        $(this).siblings('.tooltip').html($(this).attr('data-tooltip')).show();
    }, function(e) {
        $(this).siblings('.tooltip').html('').hide();
    })

    // Run facebook login js when fb_login buttons are clicked:
    $('a.fb_login').click(function(e) {
        e.preventDefault();
        if(window.FB && window.fb_login) {
            var options = { 'next': $(this).attr('data-next') };
            fb_login(options);
        }
        else {
            alert('Sorry, we could not log you in. Please refresh the page and try again.');
        }
    });
});

