/*
 * 	WellenSlider - jQuery plugin
 *	written by Jiri Dedic
 *
 *	Copyright (c) 2011 Jiri Dedic (jirded@gmail.com)
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	example: $("#images").WellenSlider();
 *	
 * 	<div id="images">
 *          <ul>
 *              <li><img src="images/01.jpg" alt="" /></li>
 *		<li><img src="images/02.jpg" alt="" /></li>
 *		<li><img src="images/03.jpg" alt="" /></li>
 *		<li><img src="images/04.jpg" alt="" /></li>
 *		<li><img src="images/05.jpg" alt="" /></li>
 *          </ul>
 *	</div>
 *
 */

(function($) {

	$.WellenSlider = {
            
            // default configuration properties
            defaults: {
                speed:       800,
                s:           0,
                w:           0,
                h:           0,
                ts:          0,
                t:           0,
                items:       {}
            },
            
            buildSlider: function(options) {
                return this.each(function() {
                    this.options = {};
                    $.extend (this.options, $.WellenSlider.defaults, options);
                    var plugin=this;
                    
                    plugin.options = {
                        s: $('li', plugin).length,
                        w: $(plugin).width(),
                        h: $(plugin).height(),
                        ts: 0,
                        t: 0,
                        items: $(plugin).find('li')
                    };
                    
                    plugin.options.ts = plugin.options.s-1;
                    
                    $(plugin).find('ul').css({width: plugin.options.w*plugin.options.s});
                    
                    changeContent();

                    // ovladaci sipecky
                    var controls = document.createElement('div');
                    var arrow_left = document.createElement('a');
                    var arrow_right = document.createElement('a');
                    $(controls).addClass('controls');
                    $(controls).append(arrow_right).append(arrow_left);
                    $(plugin).find('ul').after(controls);

                    $(arrow_left).addClass('left').attr('href', '#');
                    $(arrow_right).addClass('right').attr('href', '#');

                    // jednotlive LI elementy
                    plugin.options.items.each(function(i) {
                        var $el = $(this).find(':first-child');
                        if ($el.get(0).tagName == 'VIDEO') {
                            var source = $el.attr('source');
                            var div = document.createElement('div');
                            var video_id = $(plugin).attr('id')+'-video-'+i;
                            $(div).attr('id', video_id);
                            $(this).append(div);
                            jwplayer(video_id).setup({
                                width: '100%',
                                height: '100%',
                                flashplayer: "./flash_player/player.swf",
                                file: source,
                                skin: "./flash_player/schoon.zip"
                            }).onPlay(function() { $('#stopvideo').html($(this).attr('id')); });
                        }
                    });

                    $(arrow_left).click(function() {
                        animate('prev');
                        changeContent();
                        return false;
                    });

                    $(arrow_right).click(function() {
                        animate('next');
                        changeContent();
                        return false;
                    });
                    
                    // interni funkce
                    function animate(dir) {

                        if (dir == 'next') {
                            plugin.options.t = (plugin.options.t >= plugin.options.ts) ? 0 : plugin.options.t + 1;
                        }
                        else {
                            plugin.options.t = (plugin.options.t <= 0) ? plugin.options.ts : plugin.options.t - 1;
                        };

                        var p = (plugin.options.t * plugin.options.w) * (-1);
                        
                        $('ul', plugin).animate(
                            {marginLeft: p},
                            plugin.options.speed
                        );
                            
                        jwplayer($('#stopvideo').html()).stop();
                        $('#stopvideo').html('');
                    };
                    
                    function changeContent() {
                        $(plugin).changeContent();
                    }
                  
                })
            },
            
            changeContent: function() {
                var plugin=$(this).get(0);
                var div = $(plugin).find('ul li:eq('+plugin.options.t+')').find('div.content_item');
                $(plugin).find('.content').html(div.html());                                    
            },
            
            goToPosition: function(pos) {
                var plugin=$(this).get(0);
                plugin.options.t = parseInt(pos);
                var p = (plugin.options.t * plugin.options.w) * (-1);
                $('ul', plugin).animate(
                    {marginLeft: p},
                    plugin.options.speed
                );
                $(plugin).changeContent();
            }
	  
	};
        
        $.fn.WellenSlider = $.WellenSlider.buildSlider;
        $.fn.goToPosition = $.WellenSlider.goToPosition;
        $.fn.changeContent = $.WellenSlider.changeContent;
        
})(jQuery);
