/* Copyright 2011 © DutchBridge
 * http://www.dutchbridge.nl
 * Author: Kenny Korte <kenny@dutchbridge.nl>
 */

(function ($) {
    "use strict";
    var settings = {
        placeholder : '#placeholder',
        prefix      : 'bgslider-'
    }, methods = {
        init : function (options) {

            return this.each(function () {

                if (options) {
                    $.extend(settings, options);
                }

                var $this = $(this), data = $this.data('backgroundSlider'),  backgroundSlider = $(settings.placeholder),
                    elements = $this.children('div'), current = elements.filter('.slide.active:first');

                if (!data) {

                    $(this).data('backgroundSlider', {
                        target      : $this,
                        placeholder : backgroundSlider,
                        elements    : elements,
                        current     : current
                    });

                }

                $this.backgroundSlider('prepare');
            });
        },
        destroy : function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider'), window;

                $(window).unbind('.backgroundSlider');
                data.placeholder.remove();
                $this.removeData('backgroundSlider');
            });
        },
        start : function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider');

                if (!data.current.length) {
                    data.current = data.elements.first('.slide');
                }
                $this.backgroundSlider('show', data.current);
            });
        },
        show : function ($el) {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider');

                $(data.elements).removeClass('active');
                $(data.placeholder).find('img').removeClass('active');

                $($el).addClass('active');
                $(data.placeholder).find('#image_' + $el.attr('id')).addClass('active');
            });
        },
        next: function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider');

                if (data.current.next('.slide').length) {
                    data.current = data.current.next('.slide');
                } else {
                    data.current = data.elements.first('.slide');
                }
                $this.backgroundSlider('show', data.current);
            });
        },
        prev: function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider');

                if (data.current.prev('.slide').length) {
                    data.current = data.current.prev('.slide');
                } else {
                    data.current = data.elements.last('.slide');
                }
                $this.backgroundSlider('show', data.current);
            });
        },
        prepare : function () {
            return this.each(function () {
                var $this = $(this), data = $this.data('backgroundSlider');

                data.placeholder.html('');
                $.each(data.elements, function (index) {
                    var slide = $(this), image = $(slide).find('img'), id = $(slide).attr('id'), next = $(slide).find('.panel a'),
                        text = $this.find('.text'), button = $(slide).find('a.switch');

                        
                    if (id == '' || id === undefined) {
                        id = settings.prefix + index;
                        slide.attr('id', id);
                    }
                    image.attr('id', 'image_' + id);

                    data.placeholder.append(image);
                    
                    if (data.elements.length <= 1) {
                        $(next).hide();
                    }

                    $(next).bind('click.backgroundSlider', function (e) {
                        e.preventDefault();
                        $this.backgroundSlider('next');
                    });

                    $(button).bind('click.backgroundSlider', function (e) {
                        e.preventDefault();
                        text.toggleClass('active');
                        $this.toggleClass('text');
                    });
                });

                $this.backgroundSlider('start');
            });
        }
    };

    $.fn.backgroundSlider = function (method) {

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' +  method + ' does not exist on jQuery.backgroundSlider');
        }
        return false;

    };

})(jQuery);
