/**
* Site specific JS
*
*
* @author   Matthew Davies (md@langarth.com)
* @date     01 June 2011
*/

var msm = {
    init: function () {
        this.clients().contact().contactSend();
        return this;
    },
    clients: function () {
        $('a.cl').click(function (e) {
            var clicked = $(this);
            var section = clicked.attr('href');
            e.preventDefault();
            $('a.cl').removeClass('bold');
            clicked.addClass('bold');
            $('.clients #content div').not(':hidden').fadeOut(400, function () {
                $('#'+section).fadeIn(400);
            });
            return false;
        });
        $('a.serv').click(function (e) {
            var clicked = $(this);
            var section = clicked.attr('href');
            e.preventDefault();
            $('a.serv').removeClass('bold');
            clicked.addClass('bold');
            $('.services #content div').not(':hidden').fadeOut(400, function () {
                $('#'+section).fadeIn(400);
            });
            return false;
        });
        return this;
    },
    contact: function () {
        var val = {};
        var formElements = 'input, textarea';
        $(formElements).bind('focus', function() {
            var id = $(this).attr('id');
            val.id = $(this).val();
            $(this).val('');
            $(this).unbind('focus');
        });
        $(formElements).blur(function() {
            var id = $(this).attr('id');
            var new_val = $(this).val();
            if(new_val == '')
            {
                $(this).val(val.id);
            }
        });
        return this;
    },
    contactSend: function () {
        var options = { 
            target:       '#result',
            beforeSubmit: msm.contactValidate,
            success:      msm.contactSuccess
        }; 
        $('.contact form').ajaxForm(options);
        return this;
    },
    contactValidate: function () {
        var name = $('#name').val();
        if(name === 'Name') { name = null; }
        var email = $('#email').val();
        if(email === 'Email address') { email = null; }
        var phone = $('#phone').val();
        if(phone === 'Phone number') { phone = null; }
        if(!name || !email || !phone)
        {
            $('#error').text('Please make sure you enter your name, email address and phone number');
            return false;
        }
        else
        {
            return true;
        }
    },
    contactSuccess: function () {
        $('#error').text('');
        $('.contact form').fadeOut(500, function () {
            $('#result').fadeIn(500);
        });
        return true;
    }
};

$(document).ready(function () {
    msm.init();
    $('a.notReady').click(function (e) {
        e.preventDefault();
        alert("This section of the site is not yet ready");
        return false;
    });
});
