jQuery(document).ready(function($) {
    $('a[rel=external]').attr('target', '_blank')
    
    // Add last class for IE
    if ( ! $.support.cssFloat) {
        $('#top nav li:last-child').addClass('last').css('marginRight', '0')
        $('#top nav li:not(:last-child)').append(' /')
        $('#office.bottom p:last-child').addClass('last').css('marginBottom', '1em')
    }
    
    if ($('body').hasClass('iphone')) {
        $('#portfolio #slider').swipe({
            threshold: {
                x: 200,
                y: 100
            },
            swipeLeft: function() { nextClient('left') },
            swipeRight: function() { nextClient('right') }
        })
        // for web app links
        $.each($('#top nav a'), function() {
            $(this).attr('href', 'javascript:window.location=\''+$(this).attr('href')+'\'')
        })   
    }
    
    // Fade in content block on load
    $('#content').fadeIn(1500)
    
    // Portfolio nav
    $('#portfolio nav a').attr('href', '#')
    $('#portfolio nav a').click(function() {
        var index = $('#portfolio nav a').index($(this))
        
        changeClient(index)
    })
    
    // Show the next message
    $('#next').removeAttr('href')
    var counter = 0;
    $('#subnav a').click(function() {
        if ($('#content').hasClass('portfolio'))
        {
            if ($(this).attr('id') == 'next')
                nextClient('left')
            else if ($(this).attr('id') == 'prev')
                nextClient('right')
        }
        else
        {
            // increase/decrease counter
            if ($(this).attr('id') == 'next')
                (counter < $('#messages p').size() - 1) ? counter++ : counter = 0
            else if ($(this).attr('id') == 'prev')
                (counter > 0) ? counter-- : counter = $('#messages p').size() - 1
                
            // fade out current message
            $('#messages p').stop(true, true).fadeOut('slow')

            // fade in next message
            $('#messages p').eq(counter).delay(500).fadeIn('slow')
        }
    })
    
    // Services accordian
    $('#services h2').wrapInner('<a class="accordian"></a>')
    $('#services h2 a').click(function() {
        // get current heading
        var index = $('#services h2 a').index(this)
        
        // hide all p tags again
        $('#services p').stop(true,true).slideUp('fast')
        
        // show associated p tag
        $('#services p').eq(index).slideDown('slow')
    })
});

nextClient = function( direction ) {
    if (direction == 'left') {
        var index = $('#portfolio nav a').index($('#portfolio nav a.active')) + 1
        if (index == $('#portfolio nav a').size()) index = 0
    }
    else {
        var index = $('#portfolio nav a').index($('#portfolio nav a.active')) - 1 
        if (index == -1) index = $('#portfolio nav a').size() - 1
    }
    
    changeClient(index)
}

changeClient = function( index ) {
    // clear active links
    $('#portfolio nav a').removeClass('active')
    
    // set nextClient as active and get index
    $('#portfolio nav a').eq(index).addClass('active')
    
    var delta = index * -950
    
    $('#portfolio #slider .wrapper').animate({
        'marginLeft' : delta
    }, 750, 'swing')
}
