/**
 * jQuery multi Coda Slider - Easy element scrolling using jQuery.
 * Copyright (c) 2008 Paco Toledo - pacotole(at)gmail(dot)com | http://www.milrayas.com
 * Licensed under GPL.
 * Date: 15/9/2008
 * @author Paco Toledo
 * @version 1.0
 *
 * based on Coda Slider Effect on http://jqueryfordesigners.com/coda-slider-effect/
 */

jQuery.fn.codaSlider = function(settings) {

    settings = jQuery.extend({
        horizontal:true // if true, we'll float all the panels left and fix the width of the container
    }, settings);

    return this.each(function(){
        var coda = $(this);

        var $panels = coda.find('.scrollContainer > div');
        var $container = coda.find('.scrollContainer');

        if (settings.horizontal) {
            $panels.css({
                'float' : 'left',
                'position' : 'relative', // IE fix to ensure overflow is hidden
                'padding' : '20px'
            });

            // calculate a new width for the container (so it holds all panels)
            $container.css('width', $panels[0].offsetWidth * $panels.length);
        }

        // collect the scroll object, at the same time apply the hidden overflow
        // to remove the default scrollbars that will appear
        var $scroll = coda.find('.scroll').css('overflow', 'hidden');

        // apply our left + right buttons
        coda.find('.navigation > .scrollButtons').css('cursor', 'pointer');

        // handle nav selection
        function selectNav() {
            $(this)
                .parents('ul:first')
                    .find('a')
                        .removeClass('selected')
                    .end()
                .end()
                .addClass('selected');
        }

        coda.find('.navigation a').click(selectNav);

        // go find the navigation link that has this target and select the nav
        function trigger(data) {
            var el = coda.find('.navigation a[href$="' + data.id + '"]').get(0);
            selectNav.call(el);
        }

        if (window.location.hash) {
            trigger({ id : window.location.hash.substr(1) });
        } else {
            coda.find('.navigation a:first').click();
        }

        // offset is used to move to *exactly* the right place, since I'm using
        // padding on my example, I need to subtract the amount of padding to
        // the offset.  Try removing this to get a good idea of the effect
        var offset = parseInt((settings.horizontal ?
            $container.css('paddingTop') :
            $container.css('paddingLeft'))
            || 0) * -1;

        var scrollOptions = {
            target: $scroll, // the element that has the overflow
            items: $panels, // can be a selector which will be relative to the target
            navigation: '.navigation a',
            prev: 'img.left', // selectors are NOT relative to document, i.e. make sure they're unique
            next: 'img.right',
            axis: 'xy', // allow the scroll effect to run both directions
            onAfter: trigger, // our final callback
            offset: offset,
            duration: 500, // duration of the sliding effect

            // easing - can be used with the easing plugin:
            // http://gsgd.co.uk/sandbox/jquery/easing/
            easing: 'swing'
        };

        // apply serialScroll to the slider - we chose this plugin because it
        // supports// the indexed next and previous scroll along with hooking
        // in to our navigation.
        coda.serialScroll(scrollOptions);
    });
}