(function($) {
    $.fn.carousel = function(options) {
        var current = 0;
        var element = this;
        var images = [];
        var interval;
        
        return this.each(function() {
            for (var i in options.images) {
                var image = new Image();
                $(image).load(imageLoaded);
                
                var src = options.images[i];
                if ($.browser.msie) src += '?' + (new Date()).getDate();
                image.src = src;
                
                images.push(image);
                
                if (i == 0) $(element).find('img:first').replaceWith(image);
                
                $(element).find('.nav .last').before('<li><a href="#' + i + '">' + (parseInt(i) + 1) + '</a></li>');
                $(element).find('.nav li:not(.last) a').click(navigate);
            }
            
            interval = setInterval(next, 8000);
            
            $(document).unload = $.noop;
        });
        
        function imageLoaded() {
            $(this).data('loaded', 1);
            var i = $.inArray(this, images);
            var classes = (i == 0) ? 'current' : '';
            $(element).find('.nav li:eq(' + i + ') a').addClass(classes);
        }
        
        function next() {
            var next = current + 1;
            if (next > (images.length - 1)) next = 0;
            if ($(images[next]).data('loaded') === 1 || $(images[next]).complete === true) {
                $(images[current]).css('z-index', 0).fadeOut('slow', function() {
                    $(this).detach();
                });
                show(next);
            }
        }
        
        function navigate(event) {
            event.preventDefault();
            
            var i = $(this).attr('href').match(/#(\d)/)[1];
            if (i == current) return;
            
            if (images[i].complete == false || $(images[i]).data('loaded') !== 1) return;
            
            clearInterval(interval);
            
            if ($(images[current]).queue() && $(images[current]).queue().length > 0) return; // Already animating
            $(images[current]).css('z-index', 0).fadeOut('slow', function() {
                $(this).detach();
            });
            
            show(i);
        }
        
        function show(i) {
            var image = images[i];
            $(element).append(image);
            $(image).css('z-index', 1).hide().fadeIn('slow');
            $(element).find('p').html(options.posts[i]);
            $(element).find('a.current').removeClass('current');
            $(element).find('li:eq(' + i + ') a').addClass('current');
            current = i;
        }
    };
})(jQuery);

function initClients() {
    if ($('#client-logos').length == 0) return;
    
    tictacClick = function (event, triggered) {
        event.preventDefault();
        
        var animate = !(triggered === true);
        var element = $(this).parent().children('div:first');
        var targetHeight = $(element).hasClass('closed') ? $(this).data('openHeight') : $(this).data('closedHeight');
        
        if (animate) {
            $(element).stop().animate({height: targetHeight}, 'slow');
        } else {
            $(element).stop().css('height', targetHeight);
        }
        
        var text = $(this).text();
        var text = $(element).hasClass('closed') ? text.replace('more', 'less') : text.replace('less', 'more');
        $(this).text(text);
        
        $(element).toggleClass('closed');
    }
    
    var margin = parseInt($('#client-logos p:first').css('margin-bottom'));
    $('#client-logos').css('margin-bottom', parseInt($('#client-logos').css('margin-bottom')) + margin);
    var openHeight = $('#client-logos .collection').height() - margin;
    if ($.browser.msie && $.browser.version == '7.0') openHeight += margin;
    var closedHeight = ($('#client-logos p:first').outerHeight() * 2) + margin;
    
    $('#client-logos').append('<a class="tictac" href="#client-logos">Show more clients</a>');
        
    $('#client-logos .tictac')
        .data('openHeight', openHeight)
        .data('closedHeight', closedHeight)
        .click(tictacClick)
        .trigger('click', true);
        
    var openHeight = $('#client-testimonials div').height();
    var closedHeight = $('#client-testimonials ul:first').outerHeight();
    
    $('#client-testimonials').append('<a class="tictac" href="#client-testimonials">Show more testimonials</a>');
    
    $('#client-testimonials .tictac')
        .data('openHeight', openHeight)
        .data('closedHeight', closedHeight)
        .click(tictacClick)
        .trigger('click', true);
}

function initFollow()
{
    $('#nav .follow').hover(
        function()
        {
            $(this).addClass('hover');
        },
        function()
        {
            $(this).removeClass('hover');
        }
    );
}

function initNewsletterSubscription()
{
    $('.newsletter-subscription-widget .privacy-policy div').hide();
    $('.newsletter-subscription-widget .privacy-policy h4')
        .append(' <span>+</span>')
        .wrapInner('<a href="#"></a>')
        .find('a').click(function()
        {
            var tictac = ($(this).children('span').text() == '+') ? '–' : '+';
            $(this).children('span').text(tictac);
            
            $(this).parent('h4').siblings('div').slideToggle('slow');
            
            return false;
        });
        
    // Setup input placeholder
        
    $('.newsletter-subscription-widget .field').each(function()
    {
        if (this.value !== '') return;
        if ('placeholder' in this) {
            $(this).attr('placeholder', $('label[for=' + this.id + ']').text());
        } else {
            var placeholder = $('label[for=' + this.id + ']').text();
            $(this)
                .attr('value', placeholder)
                .data('placeholder', placeholder)
                .addClass('blurred')
                .focus(function()
                {
                    if (this.value == $(this).data('placeholder')) {
                        $(this).attr('value', '').removeClass('blurred');
                    }
                })
                .blur(function()
                {
                    if (this.value == '') {
                        $(this).attr('value', $(this).data('placeholder')).addClass('blurred');
                    }
                });
        }
    });
    
    function clear() {
        $('.newsletter-subscription-widget .field').each(function()
        {
            if (this.value == $(this).data('placeholder')) $(this).attr('value', '');
        });
    }
    $('.newsletter-subscription-widget form').submit(clear);
    $(window).unload(clear);
    window.onbeforeunload = clear;
}

$(document).ready(function() {
    initClients();
    initFollow();
    initNewsletterSubscription();
});