(function($){

    $.fn.leanimation = function(opts) {
        var options = $.extend({},$.fn.leanimation.defaults,opts);

        var container_selector = '#' + $(this).attr('id');
        $(container_selector).css('position','relative');
        $(container_selector).data('container_selector',container_selector);

        //Put all options to the container.
        $(container_selector).data('options',options);

        //setup current position
        $(container_selector).data('current_position',options.current);

        //Indicates whether the animation is being paused.
        $(container_selector).data('pause',false);

        //If there's no panel class specified, we assume that the childen of the container is the panels
        var panels = options.panel_class ? $(options.panel_class,$(this)) : $(this).children();

        //Some panel properties.
        $(container_selector).data('panels',panels);
        $(container_selector).data('panel_width',$(panels.get(0)).outerWidth());
        $(container_selector).data('panel_height',$(panels.get(0)).outerHeight());
		

        //If the navigation existed. Then create event for each navigat item.
        if (options.navigation) {
            $(container_selector).data('navigation_items',$(options.navigation));
            $(container_selector).data('navigation_items').each(function(){
                $(this).click(function(){
                    var next_position = $(container_selector).data('navigation_items').index(this) + 1;
                    goto(next_position);
                    return false;

                });
            });
        }

        //Navigation buttons
        if (options.next_button) {
            $(options.next_button).click(goNext);
        }
        if (options.prev_button) {
            $(options.prev_button).click(goBack);
        }
        if (options.first_button) {
            $(options.first_button).click(goFirst);
        }
        if (options.last_button) {
            $(options.last_button).click(goLast);
        }

        if (options.play_pause_button) {
            $(options.play_pause_button).click(function(){
                //Turn play on/off
                console.log('Pause clicked!');
                $(container_selector).data('pause',!$(container_selector).data('pause'));

                if (!$(container_selector).data('pause')) {
                    //Notify anyone interested in the auto_play_started event.
                    animation();
                    $(container_selector).trigger('auto_play_started',$(container_selector).data());
                } else {
                    //Notify anyone interested in the auto_play_paused event.
                    $(container_selector).trigger('auto_play_paused',$(container_selector).data());
                }
            });
        }

        init();

        /**
				 *	Move to a panel, with no animation.
				 */
        function init() {

            var data = $(container_selector).data();

            //If there's only 1 panel, then no animation.
            if (data.panels.length < 2) {
                return;
            }

            //Call init function of the effect plugin
            eval('$.fn.leanimation.effect.' + data.options.animation + '.init(data)');

            if (options.auto_play) {
                //start playing
                setTimeout(autoPlay,options.delay);
                //This is called when an animation is finished.
                $(container_selector).bind('finish_animation',function(){
                    setTimeout(autoPlay,options.delay);
                });
            }

        }

        function autoPlay() {
            //Is still auto-playing?
            if (!$(container_selector).data('pause')) {
                if ($(container_selector).data('current_position') == $(container_selector).data('panels').length) {
                    var position = 1;
                } else {
                    var position = $(container_selector).data('current_position') + 1;
                }
                $(container_selector).data('next_position',position);
                animation();
            }
        }

        function goNext(){
            if ($(container_selector).data('current_position') == $(container_selector).data('panels').length) {
                var position = 1;
            } else {
                var position = $(container_selector).data('current_position') + 1;
            }
            goto(position);
            return false;
        }

        function goBack(){
            if ($(container_selector).data('current_position') == 1) {
                var position = 1;
            } else {
                var position = $(container_selector).data('current_position') - 1;
            }
            goto(position);
            return false;
        }

        function goFirst(){
            goto(1);
            return false;
        }


        function goLast(){
            var position = $(container_selector).data('panels').length;
            goto(position);
            return false;
        }

        //The goto function is used for any event other than auto-playing event.
        function goto(position) {

            //Stop the auto animation if it's playing.
            $(container_selector).data('pause',true);

            //Notify anyone interested in the finish_animation event.
            $(container_selector).trigger('auto_play_paused',$(container_selector).data());

            $(container_selector).data('next_position',position);
            animation();

        }

        function animation() {
            var data = $(container_selector).data();
            //Notify anyone interested in the start_animation event.
            $(data.container_selector).trigger('start_animation',$(container_selector).data());
            
            	if (data.navigation_items) {
					$(data.navigation_items).removeClass('active');
					$(data.navigation_items.get(data.next_position-1)).addClass('active');
				}

            //Call the plugin for doing animation.
            eval('$.fn.leanimation.effect.' + data.options.animation + '(data)');
        }
    };

    $.fn.leanimation.defaults = {
        'direction': 'horizontal',
        'delay': 1000,
        'duration': 2000,
        'current': 1,
        'animation': 'sliding',
        'easing': 'easeOutBounce',
        'auto_play':false
    }


    /**
				 *	This is used for any plugin to call when the animation is finished.
				 * 	Any plugin has to use this one to make the animation working properly.
				 */
    $.fn.leanimation.finished_animation = function(data) {
        $(data.container_selector).data('current_position',data.next_position);
        //Notify anyone interested in the finish_animation event.
        $(data.container_selector).trigger('finish_animation',data);
    }

    $.fn.leanimation.effect = {};

})(jQuery);


