window.addEvent('domready', function(){
    // prepare menu
    menu = new DropMenu('menu-container');
    /*****************
     * Scroll to top
     *****************/
    var scroll = new Fx.Scroll(window, {
        duration: 1000,
        transition: Fx.Transitions.Quart.easeOut
    });

    if ($('toTop')) {
        $('toTop').addEvent('click', function(event) {
            event = new Event(event).stop();
            scroll.toTop();
        });
    }

    /***************
     * Submit form
     ***************/
    /* display the error box */
    var error_box = new Fx.Morph($('error_box'), {
        duration: '1500',
        link: 'cancel',
        transition: Fx.Transitions.Quad.easeOut
    });

    // assign event to submit button
    if ($('submitbutton')) {
        $('submitbutton').addEvent('click', function(event) {
            event = new Event(event).stop();
            var errors = validateForm('contactForm', '.input_field');
            if (errors.length != 0) {
                error_box.start({
                    'opacity' : [0,1],
                    'display' : ['none', 'block']
                });
            } else {
                error_box.start({
                    'display' : ['block', 'none']
                });
                submitForm('contactForm');
            }
        });
    }


    /*********************
     * Lender logos loader
     *********************/
    var logos = $('footer').getElements('.logos');
    var hash = new Hash({counter: 1, logo : logos});

    //we initiate our periodical and pass and bind the hash var
    // also the timer starts at start up
    // call $clear(simpleTimer); to prevent this if desirable
    var simpleTimer = animateLogos.periodical(2000, hash);


});


// form validation
var validateForm = function(form_id, element_selector) {
    var errors = new Array();
    var inputs = $(form_id).getElements(element_selector);

    inputs.each(function(item, index) {
        item.set('morph', {duration: 400, link: 'cancel', transition: Fx.Transitions.Quad.easeOut});

        if (item.value == '') {
            errors.push(item.id);
            item.morph('.error');
        } else {
            $(item.id).setStyles({'border': '1px solid #CCCC00', 'background-color': '#FFFFCC'});
            errors.erase(item.id);

            // check if the field is of email type and is valid
            if (item.id == 'email') {
                var email = item.value;
                apos = email.indexOf("@");
                dotpos = email.lastIndexOf(".");
                if (apos<1||dotpos-apos<2) {
                    $(item.id + '_error').set('html', '<span style="color: #FC0000">invalid format</span>');
                    item.morph('.error')
                    errors.push(item.id);
                } else {
                    $(item.id + '_error').set('text', '');
                    $(item.id).setStyles({'border': '1px solid #CCCC00', 'background-color': '#FFFFCC'});
                    errors.erase(item.id);
                }
            }
        }
    });
    return errors;
}


var submitForm = function (form_id) {
    $(form_id).submit();
    $(form_id + '_container').set('html', '<span style="color: #85B545"><strong>Thank your for your enquiry. We will get back to you shortly.</strong></span>');
}


var animateLogos = function(){
    //each time this function is called
    //the var currentTime will increment by one
    //also notice the use of "this.counter"
    //"this" is the hash
    //and "counter" is the key
    var block_logos = $('footer').getElements('.block_logos');
    var current_time = this.counter++;
    var length = this.logo.length;
    var image_to_show;
    if (current_time % length != 0) {
      image_to_show = current_time % length;
    } else
      image_to_show = 11;

    // morph effect
    var effect = new Fx.Morph($('img' + image_to_show), {
        duration: '1500',
        transition: Fx.Transitions.Quart.easeOut
    });

    if (image_to_show % 3 == 1) {
        $('block_logos' + image_to_show).set('morph', {duration: 1000, link: 'cancel', transition: Fx.Transitions.Quad.easeOut});
        // show all block_logos
        $('block_logos' + image_to_show).morph({'opacity' : [0, 1], 'display' : 'block'});
    }

    if ($('img' + image_to_show).getStyle('display') != 'block') {
        effect.start ({
            'opacity' : [0,1],
            'display' : ['none', 'block']
        });
    }

    // hide previous
    if (image_to_show %3 == 1 && image_to_show > 1) {
        var previous = image_to_show - 3;
        $('block_logos' + previous).morph({'display' : 'none'});
    } else if (image_to_show %3 == 1 && image_to_show == 1) {
        var previous = block_logos.getLast();
        $(previous.id).morph({'display' : 'none'});
    }
}


