  /* SLIDER
 *   Fa scorrere un elemento
 */
$.fn.get_slider = function(configuration){
    var conf = configuration || {};
    conf.container = $(this);
    return new $.slider(conf);
};
   $.slider = function(configuration){
       var s = this;
    var _container = configuration.container;
    var _width = configuration.width || _container.parent().width();
    var _height = configuration.height || _container.parent().height();
    var _list,_timeout,_configure,_slide,_init,k,jdata,_offset,news_number;
    var _op =  configuration.op;
    
        _init = function(){
            var jdata,html,html_tmp = '';
            var type = configuration.type == 'orizontal' ? 'orizontal' : 'vertical';
            s.continuous = configuration.continuous || 0;
            $.ajax({
                async: false,
                method: 'post',
                url: 'responsedata.php?op=' + _op,
                success: function(data) {
                    jdata = $.parseJSON(data);
                }
            });
            _width = _width > 30 ? _width - 30 : 100;
            _height = _height > 30 ? _height - 30 : 100;
            if(type == 'orizontal'){
                html = '<table style="display:inline;" cellpadding="2"><tr>';
                html_tmp = '';
                $.each(jdata,function(){
					 html_tmp += '<td>' + this.elem + '</td>';
					});
                
                html += html_tmp;
                if(s.continuous){
                    html += html_tmp;
                }
                html += '</tr></table>';
            } else {
                html = '<ul style="list-style-type: none;margin: 0px;padding:5px;">';
                html_tmp = '';
                for(k in jdata.resp){
                    html_tmp += '<li>';
                    html_tmp += '<div class="news_date">' + s.date_format.replace( /%(.)\1*/g,function(s){return jdata.resp[k].complete_date[s];}) + '</div>';
                    html_tmp += '<div class="news_subject">' +
                            (jdata.resp[k].dest_url ?
                                '<a href="' + jdata.resp[k].dest_url + '" target="' + jdata.resp[k].dest_target + '">' + jdata.resp[k].subject + '</a></div>':
                                jdata.resp[k].subject + '</div>');
                    html_tmp += '<div class="news_body">' + jdata.resp[k].body + '</div>';
                    html_tmp += '</li>';
                }
                html += html_tmp;
                if(s.continuous){
                    html += html_tmp;
                }
                html += '</ul>';
            }

            _container
                .css({'overflow':'hidden','position':'relative','padding':'0px','width': _width+'px','height': _height+'px'});
               // .wrap('<div id="bannerCont" style="width:'+_width+'px;height:'+_height+'px;padding:15px;"/>');

            _container.append(html);

            _list = _container.children();
            _list.css({'position':'absolute'});
            _list.children().each(function(){
                $(this).css({'width':$(this).width() + 'px','height':$(this).height() + 'px'});
            });
            s.list_height = Math.round(_list.height());
            s.list_width = Math.round(_list.width());
            s.container_height = Math.round(_container.height());
            s.container_width = Math.round(_container.width());
            s.direction = configuration.direction;
            s.top = 0;
            s.left = 0;
            _list.css({top : s.top + 'px', left : s.left} );
            s.x = configuration.pixel_step_x || 1;
            s.y = configuration.pixel_step_y || 1;
            s.interval = configuration.interval || 10;
            

            s.stop_flag = configuration.stop_flag === 1 ? 1 : 0;
            if($.browser.msie){
                if(parseInt($.browser.version, 10) < 9){
                    if(_op == 'scroll'){
                        s.x = s.x / 70;
                        s.y = s.y / 70;
                    }else{
                        s.x = s.x / 20;
                        s.y = s.y / 20;
                    }
                }
            }
            s.slide = function(){
                clearTimeout(_timeout);
                if(!s.stop_flag){
                    _slide();
                    _timeout = setTimeout(s.slide,s.interval);
                }
            }

            s.stop = function(){
                s.stop_flag = 1;
                s.slide();
            }

            s.start = function(){
                s.stop_flag = 0;
                s.slide();
            }

            s.set_direction = function(direction){
                s.direction = direction;
                _configure();
            }
        };

        _configure = function(){
            switch(s.direction){
                case 'right':
                    _slide = function(){
                        clearTimeout(_timeout);
                        s.left = s.left + s.x;
                        _list.css('left',s.left + 'px');
                        if(s.continuous){
                            if(s.left > (s.list_width/2*(-1) + s.container_width)){
                                s.left = (s.left - s.list_width/2);
                            }
                        } else {
                            if(s.left > s.container_width){
                                s.left = s.list_width*(-1);
                            }
                        }
                        _timeout = setTimeout(s.slide,s.interval);
                    }
                    break;
                case 'down':
                    _slide = function(){
                        clearTimeout(_timeout);
                        s.top = s.top + s.y;
                        _list.css('top',s.top + 'px');
                        if(s.continuous){
                            if(s.top > (s.list_height/2*(-1) + s.container_height)){
                                s.top = (s.top - s.list_height/2);
                            }
                        } else {
                            if(s.top > s.container_height){
                                s.top = s.list_height*(-1);
                            }
                        }
                        _timeout = setTimeout(s.slide,s.interval);
                    }

                    break;
                case 'left':
                    _slide = function(){
                        clearTimeout(_timeout);
                        s.left = s.left - s.x;
                        _list.css('left',s.left + 'px');
                        if(s.continuous){
                            if(s.left < s.list_width*(-1)/2){
                                s.left = (s.left + s.list_width/2);
                            }
                        } else {
                           if(s.left < s.list_width*(-1)){
                               s.left = s.container_width;
                           }
                        }
                        
                        _timeout = setTimeout(s.slide,s.interval);
                    }

                    break;
                default:
                    s.direction = 'up';
                    _slide = function(){
                        clearTimeout(_timeout);
                        s.top = s.top - s.y;
                        _list.css('top',s.top + 'px');
                        if(s.continuous){
                            if(s.top < s.list_height*(-1)/2){
                                s.top = (s.top + s.list_height/2);
                            }
                        } else {
                           if(s.top < s.list_height*(-1)){
                               s.top = s.container_height;
                           }
                        }
                        _timeout = setTimeout(s.slide,s.interval);
                    }

            }
        }
        _configure();
    
    _init();
}
