// Namespace
var joeburton = {};


// If the module object hasn't be instantiated then create a new module object
if (joeburton.Module === undefined || typeof joeburton.Module !== "object") {
    joeburton.Module = {};
}


// Slides up the status bar
joeburton.Module.slideStatusBar = function () {
    
    // This function breaks ie
    function isiPhone() {
        if(!$.browser.msie) {
            window.addEventListener('load', function () {
                setTimeout(function () { window.scrollTo(0, 1); }, 1);
            }, false);
        }
    }
};


// Page scrolling 
joeburton.Module.smoothScroll = {

    //for links you can add the class 'smooth-scroll' and then add the target to the href
    //for example <a class="smooth-scroll" href="#target">Some link</a>
    init: function(){
        var that = this;

        //delay so all html has rendered correctly to pick up scroll
        setTimeout(function() {
            $('.smooth-scroll').each(function() {

                var target = $(this).attr('href');

                $(this).live('click', function(e) {
                    e.preventDefault();
                    that.scrollPage(target);
                });

            });
        }, 2000);

    },

    //target should be a string such as '#target'
    scrollPage: function(target, duration){
        var duration = duration || 'slow';
        $('html, body').animate({
            scrollTop: ($(target).offset().top - 20) }, duration);
    }

};


joeburton.Module.clearErrors = function () {
    
    formEle = $('form').find('.item');
    
    $.each(formEle, function(){
         
         $(this).live('focus', function() {
            
            var that = $(this);
         
            if (that.hasClass('highlight')) {
                that.removeClass('highlight');
            }
            
            if (that.hasClass('textarea-highlight')) {
                that.removeClass('textarea-highlight');
            }  
            
        });           

    });    

};


// Initialise modules
joeburton.init = function () {
	
    if (this.Module) {
        
        // console.log(this.Module);
        
        // Loop through each function/object
        $.each(joeburton.Module, function(name, funcname){

            // If any function is built as an object and has an init() method run its init function
            funcname = (typeof funcname === "object") ? this['init'] : funcname;

            // Call all standard functions
            if (typeof funcname === "function") {

                funcname.call(this);

            }

        });

    }

};


$(document).ready(function() {
    
    // Initialise all modular functions
    joeburton.init();

    // Submit the contact form
    $('#submit').click(function (e) {

        e.preventDefault();
        
        // To submit the form via AJAX set ajaxMe to true, false will use standard post method.
        var data = ($('#contact')),
            ajaxMe = true,
            which = "contactForm";
            
        joeburton.Module.validation(data, ajaxMe, which);

    })

});	
