/*
 * Flexible Slider - jQuery plugin
 * Last update 2010-11-07
 * @requires jQuery >= v1.2.3
 *
 * Copyright (c) 2010 Jan Goldacker
 * http://jan-goldacker.de
 *
 * Built for jQuery library
 * http://jquery.com
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

(function($) {

        $.fn.FlexibleSlider = function(fsoptions)
        {

                // default configuration properties
                var fsdefaults = {
                        prevId:      'fsPrevBtn', // ID for prev button
                        prevText:    'prev',      // Text for prev button
                        nextId:      'fsNextBtn', // ID for next button
                        nextText:    'next',      // Text for next button
                        orientation: '',          // 'vertical' = vertical slid
                        speed:       800,         // milliseconds for change
                        number:      1,           // count of visible elements
                        nextnumber:  1,           // count of sliding elements (often 1 or the same like number)
                        fadeoutbtn:  1,           // 1 = all buttons faded out when last element is visible
                        wait:        1000         // 0 = no automatically next()-event, milliseconds = time for show content by interval / duration
                };

                var fsoptions = $.extend(fsdefaults, fsoptions);

                return this.each(function()
                {
                        var fsobj = $(this);              // current element-set
                        var fss = $("li", fsobj).length;  // count of elements
                        var fsw = fsobj.width();          // width for one element
                        var fsh = fsobj.height();         // height for one element
                        var fst = 0;                      // current element number
                        var fsp = 0;                      // background position / visible elements
                        var fsvertical = (fsoptions.orientation == 'vertical');

                        // generate html and css
                        $("ul", fsobj).css('width',fss * fsw);
                        if(!fsvertical)
                        {
                                 $("li", fsobj).css('float','left');
                        }/**
                        $(fsobj).after(
                                 '<div id="'+ fsoptions.prevId +'"><a href=\"javascript:void(0);\">' + fsoptions.prevText + '</a></div>' +
                                 '<div id="'+ fsoptions.nextId +'"><a href=\"javascript:void(0);\">' + fsoptions.nextText + '</a></div>'
                        );**/

                        //automatically slid
                        var fsaktiv;
                        if(fsoptions.wait != 0)
                        {
                                 fsaktiv = window.setInterval(function()
                                 {
                                         fsnext(true);
                                 }, fsoptions.wait);
                        }
                        $(this).mouseover(function()
                        {
                                 window.clearInterval(fsaktiv);
                        });

                        //prev and next
                        //$("a","#" + fsoptions.prevId).hide();
                        //$("a","#" + fsoptions.nextId).hide();
                        $("a","#" + fsoptions.nextId).click(function(event)
                        {
                                 event.preventDefault();
                                 fsnext(false);
                        });
                        $("a","#" + fsoptions.prevId).click(function(event)
                        {
                                 event.preventDefault();
                                 fsprev(false);
                        });
                        if(fss > 1)
                        {
                                 $("a","#" + fsoptions.nextId).fadeIn();
                        }

                        function fsnext(auto)
                        {
                                 if(auto == false)
                                 {
                                         window.clearInterval(fsaktiv);
                                 }
                                 if (fst + fsoptions.number < fss)
                                 {
                                         fsanimate("next");
                                 }
                                 if (fst + fsoptions.number >= fss && fsoptions.fadeoutbtn == 1)
                                 {
                                         $("a","#" + fsoptions.nextId).fadeOut();
                                 }
                                 $("a","#" + fsoptions.prevId).fadeIn();
                        }

                        function fsprev()
                        {
                                window.clearInterval(fsaktiv);
                                if (fst > 0)
                                {
                                  fsanimate("prev");
                                }
                                if (fst <= 0 && fsoptions.fadeoutbtn == 1)
                                {
                                  $("a","#" + fsoptions.prevId).fadeOut();
                                }
                                $("a","#" + fsoptions.nextId).fadeIn();
                        }

                        //slid effekt
                        function fsanimate(dir){
                                if(dir == "next"){
                                        fst = (fst >= fss-1) ? fss-1 : fst + fsoptions.nextnumber;
                                } else {
                                        fst = (fst <= 0) ? 0 : fst - fsoptions.nextnumber;
                                }
                                if(!fsvertical) {
                                        fsp = fst * fsw / fsoptions.number * -1;
                                        $("ul",fsobj).animate({
                                                marginLeft: fsp
                                        },fsoptions.speed);
                                } else {
                                        fsp = fst * fsh / fsoptions.number * -1;
                                        $("ul",fsobj).animate({
                                                marginTop: fsp
                                        },fsoptions.speed);
                                }
                        };
                });
        };
})(jQuery);
