function next(){
    var next = current + 1;
    if(next >= data.length)
        next = 0;
    return show(next);
}

var running = false;
function show(i){
    if(current == i || running) return false;
        
    running = true;
    current = i;//update current
    
    $('#feature-img').prepend('<img src="/'+data[i].image+'" alt="'+data[i].title+'" class="animation" />').css({width: '684px', left: '-342px'}).animate({left: '0'}, 600, function(){
        $('#feature-img img:last').remove();
        $('#feature-img').css({width: '342px', left: '0'});
        $('#feature-img img').removeClass('animation');
    });

    $('#left .content').fadeOut('fast', function(){//update content
        $('#left .content h1').html(data[i].text1);
        $('#left .content h2').html(data[i].text2);
        var link = '';
        if(data[i].link)
            link = data[i].link;
        $('#left .content .secondary-link-container').html(link);
        $('#left .content').fadeIn('fast', function(){ running = false; });
    });
    return false;//do not follow link
}

function modal(type){
    $('#modal-container div.contact, #modal-container div.testimonials').css('display', 'none');
    $('#modal-container div.'+type).css('display', 'block');
    $('#modal-container').attr('class', type);
    $('#modal-bg').css('opacity', 0);
    $('#modal-bg').css('display', 'block');
    $('#modal-bg').animate({opacity: 0.5}, 600);
    $('#modal-container').fadeIn('fast');
    $('#modal-bg, #modal-close').bind('click', function(){ return close(); });
    if(type == 'contact')
        $('#modal-bg').unbind('click');
    return false;
}

function close(){
    $('#modal-bg, #modal-container').fadeOut('fast', function(){$('#modal-container div.contact, #modal-container div.testimonials').css('display', 'none');});
    return false;
}

function testimonial(i){
    current_testimonial = i;//update current
    $('#modal-content .testimonials p').fadeOut('fast', function(){
        $('#modal-content p.testimonials').html(data_testimonial[i].text);
        $('#modal-content p.client').html(data_testimonial[i].client);
        $('#modal-content p.organisation').html(data_testimonial[i].organisation);
        $('#modal-content .testimonials p').fadeIn('fast');
    });
    return false;
}

function next_testimonial(){
    var next = current_testimonial + 1;
    if(next >= data_testimonial.length)
        next = 0;
    return testimonial(next);
}

function send_message(){
    /* reset */
    $('#contact-message, #contact-email, #contact-name').removeClass('error');
    var error = false;
    /* check */
    if($('#contact-message').val() == ''){
        $('#contact-message').addClass('error');
        error = true;
        document.getElementById('contact-message').focus();
    }
    if(!$('#contact-email').val().match(/\w@\w+.\w/)){
        $('#contact-email').addClass('error');
        error = true;
        document.getElementById('contact-email').focus();
    }
    if($('#contact-name').val() == ''){
        $('#contact-name').addClass('error');
        error = true;
        document.getElementById('contact-name').focus();
    }
    if(error){
        /* display error message */
        $('.contact h4').css('display', 'none');
        $('.contact h1, .contact h3').css('display', 'block');
    }else{
        /* send form */
        $.post('/contact.php',
            {
                name: $('#contact-name').val(),
                email: $('#contact-email').val(),
                message: $('#contact-message').val()
            },
            function(){
                /* success */
                $('#modal-content .contact').fadeOut('fast', function(){
                    $('#modal-content .contact').html('<div id="contact-success"><h4>Thanks.</h4><h2>We&rsquo;ll be in touch shortly.</h2></div>');
                    $('#modal-content .contact').fadeIn('fast');
                });
                /* reset form on modal close */
                $('#modal-bg, #modal-close').unbind('click');
                $('#modal-bg, #modal-close').bind('click', function(){
                    $('#modal-bg, #modal-container').fadeOut('fast', function(){
                        $('#modal-content .contact').html(form);
                    });
                });
            });
        /* sending */
        $('#modal-content .contact').html('<h4>Sending . . .</h4>');
        
    }
    return false;
}

jQuery.preloadImages = function(){for(var i = 0; i<arguments.length; i++){jQuery("<img>").attr("src", arguments[i]);}}

$(document).ready(function(){
    if(navigator.vendor == 'Camino' || window.opera == 'Opera')
        $('*').css('font-family', 'Georgia, Palatino, Times, serif');
    if(navigator.vendor == 'Camino')
        $('#contact-submit').valt('Send');
});
