// {{{ function slugify(text) 
function slugify(text) 
{
	text = text.toLowerCase().replace(/\s/gi, "-").replace(/[^a-z0-9\s]+/ig, '-');
	return text;
}
// }}}
// {{{ function gTrackEvent(category, action, optional_label, optional_value)
function gTrackEvent(category, action, optional_label, optional_value)
{
    if (typeof(_gaq) != "undefined" && typeof(action) == 'string' && typeof(category) == 'string')
    {
        var data = ['_trackEvent', category, action];
        if (typeof(optional_label) == 'string')
        {
            data.push(optional_label);
            if (typeof(optional_value) == 'number')
            {
                data.push(optional_value);
            }
        }
        _gaq.push(data);
    }
}
// }}}
// {{{ function make_cookie (name, value, days) 
function make_cookie (name, value, days) 
{
    var expires = '';
    if (days) 
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}
// }}}
// {{{ function read_cookie (name) 
function read_cookie (name) 
{
    var nameEQ = name + '=',
        ca = document.cookie.split(';'),
        cal = ca.length,
        i, c;
    for (i=0; i < cal; i++) 
    {
        c = ca[i];
        while (c.charAt(0) === ' ')
        {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(nameEQ) === 0)
        {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
// }}}
if (read_cookie('js_enabled') != '1')
{
    make_cookie('js_enabled', '1');
    if (location.pathname == '/work/')
    {
        location.reload();
    }
}
$(document).ready(function(){
    // {{{ newsletter signup
    var sign_up = $('#sign-up'),
        sign_up_text = 'Get our Newsletter';
    // {{{ text field
    $('input.newsletter').each(function(){
        var el = $(this);
        el
        .data('disabled', false)
        .val(sign_up_text)
        .focus(function(){
            if (el.val() === sign_up_text)
            {
                el.val('');
            }
        })
        .blur(function(){
            if (el.val() === '')
            {
                el.val(sign_up_text);
            }
        })
        .keypress(function(e){
            if (e.which == 13 && !el.data('disabled'))
            {
                el.attr('disabled', 'disabled').data('disabled', true);
                sign_up.data('disabled', true);
                $.post(
                    '/service/subscribe/',
                    { email: el.val() },
                    function(response) {
                        if (response.success)
                        {
                            el.val('Thanks! A confirmation has been sent.');
                        }
                        else
                        {
                            var message = 'Please use a valid e-mail';
                            switch (response.code)
                            {
                                case 214:
                                    message = "You're on the list already!";
                                break;
                            }
                            el.val(message).removeAttr('disabled').data('disabled', false);
                            sign_up.data('disabled', false);
                        }
                    },
                    'json'
                );
            }
        });
    });
    // }}}
    sign_up
        .data('disabled', false)
        .click(function(){
            if (!$(this).data('disabled'))
            {
                $('input.newsletter').trigger({
                    type: 'keypress',
                    which: 13
                });
            }
        });

    // }}}
});
