var contact = {
    option: {
        classQrCode: 'qr-code',
        classSms: 'sms-vcard',
        classEmail: 'email-vcard',
        idEmail: 'email',
        idSms: 'sms',
        idContent: 'fancy_div',
        idDropDown: 'dropDown'
    },
    init: function(){
        contact.qrcode.init();
        contact.email.init();
        contact.sms.init();
        contact.dropDown.init();
    },
    startLoading: function(){
        $('#fancy_div').html('');
        $.fn.fancybox.showLoading();
    },
    endLoading: function(){
        $('#fancy_loading').hide();
    },
    qrcode: {
        init: function(){
            $('.' + contact.option.classQrCode).fancybox({'overlayShow': true, 'overlayOpacity': .5, titleShow: false});
        }
    },
    sms: {
        init: function(){
            $('.' + contact.option.classSms).fancybox({'overlayShow': true, 'overlayOpacity': .5, 'frameWidth': 500, 'frameHeight': 110, hideOnContentClick: false, autoScale: true, autoDimensions: true, 'callbackOnShow': contact.sms.initContent, callbackOnStart: contact.sms.onFancyBoxStart});
            $('#' + contact.option.idSms).hide();
            $('#' + contact.option.idSms + ' .error').hide();
            $('#' + contact.option.idSms).removeClass('nonjs');
        },
        onFancyBoxStart: function() {
            if($.browser.msie) {
                var btnElem = $('.fancy-link:first');
                var text = btnElem.text();
                btnElem.empty().html(text).css("color", "white");
            }
        },
        initContent: function() {
            $('#' + contact.option.idContent + ' form')
                .submit(contact.sms.submit)
                .find(".submit-btn")
                    .click(function(){
                        $(this).parents("form:first").submit();
                        return false;
                    });
            Cufon.refresh();
        },
        submit: function(){
            var valueToTest = contact.sms.prepare($(this).find('input[name=sms]').val());
            if (!contact.sms.isValidNumber(valueToTest)) {
                $(this).find('.error').show();
                return false;
            }
            $(this).find('input[name=sms]').val(valueToTest);
            var serlizedFormData = $(this).serialize();
            contact.startLoading();
            $.post('/contact-us', serlizedFormData, contact.sms.response, 'json');
            return false;
        },
        prepare: function(str) {
            return str.replace(/\+/, "").replace(/^0/, "44");
        },
        isValidNumber: function(str){
            return (str.match(/^[0-9]{11,}/));
        },
        response: function(response){
            contact.endLoading();
            if (response.Status == 'ok') {
                $('#fancy_div').html('<h2>Your Essence SMS has been sent</h2>');
            } else {
                $('#fancy_div').html('<h2>Your Essence SMS could not be sent, please check your phone number and try again</h2>');
            }
        }
    },
    email: {
        box: null,
        init: function(){
            contact.email.box = $('.' + contact.option.classEmail).fancybox({'overlayShow': true, 'overlayOpacity': 0, 'frameWidth': 400, 'frameHeight': 100, 'hideOnContentClick': false, 'callbackOnShow': contact.email.initContent});
            $('#' + contact.option.idEmail).hide();
            $('#' + contact.option.idEmail + ' .error').hide();
        },
        initContent: function(){
            $('#' + contact.option.idContent + ' form').submit(contact.email.submit);
        },
        submit: function(){
            if (!contact.email.isValidEmail($(this).find('input[name=email]').val())) {
                $(this).find('input[name=email]').css({borderColor:'#cc0000'});
                $(this).find('.error').show();
                return false;
            }
            contact.startLoading();
            $.post('/contact-us', $(this).serialize(), contact.email.response, 'json');
            return false;
        },
        isValidEmail: function(str){
            return (str.indexOf('.') > 2) && (str.indexOf('@') > 0);
        },
        response: function(response){
            contact.endLoading();
            if (response.Status == 'ok') {
                $('#fancy_div').html('<h2>Your Essence vCard has been emailed to you</h2>');
            } else {
                $('#fancy_div').html('<h2>Your Essence vCard could not be sent, please check your email address and try again</h2>');
            }
        }
    },
    dropDown: {
        init: function(){
            
            $('.' + contact.option.idDropDown).hide();
            $('.' + contact.option.idDropDown).parent().find('a:first').click(function(){
                if ($(this).parent().find('.' + contact.option.idDropDown + ':visible').length > 0) {
                    $(this).parent().find('.' + contact.option.idDropDown).slideUp();
                } else {
                    $(this).parent().find('.' + contact.option.idDropDown).slideDown();
                }
                return false;
            });
        }
    }
}
$(document).ready(function () {
    contact.init();
});
