

(function($){
	var opts;

  //-----------------------------------------------
	$.fn.makescrollable2 = function(options)
    {
  		// build main options
  		opts = $.extend({}, $.fn.makescrollable2.defaults, options);

  		// initalize
  		$.fn.makescrollable2.initialize();
  		
  		return this.each(function()
        {
        $(this).data('opts', opts);
                
             
        $.fn.makescrollable2.makeoutertags(this);     
        $.fn.makescrollable2.show_scrollboxbuttons(this, 0);
        $.fn.makescrollable2.initialize_events(this);
        
        $(this).find('.sb').scrollTo(0);
    		});
  	};
	//-----------------------------------------------
	$.fn.makescrollable2.initialize = function()
    {
  	$.scrollTo.defaults.axis = 'xy';
  	};
  //-----------------------------------------------
	$.fn.makescrollable2.makeoutertags = function(mainobj)
    {
  	var makeoutertags=$(mainobj).data('opts').makeoutertags;
  	if(!makeoutertags) return;
  	
  	var buffhtml=$(mainobj).html();
  	
  	var scrollbox_name=$(mainobj).data('opts').scrollbox_name;
  	var scrollbutton1_name=$(mainobj).data('opts').scrollbutton1_name;
  	var scrollbutton2_name=$(mainobj).data('opts').scrollbutton2_name;
  	var txt_name=$(mainobj).data('opts').txt_name;

  	  	
  	if( $(mainobj).find('.'+txt_name).length==0 )
  	  {
      buffhtml='<div class="'+txt_name+'">'+buffhtml+'</div>';
      }
	  
	if( $(mainobj).find('.'+scrollbox_name).length==0 )
      {
       buffhtml='<div class="'+scrollbox_name+'">'+buffhtml+'</div>';
      }
      	  	      
    if( $(mainobj).find('.'+scrollbutton1_name).length==0 )
      {
      buffhtml='<a href="#" class="'+scrollbutton1_name+'">&nbsp;</a>'+buffhtml;
      }
      
    if( $(mainobj).find('.'+scrollbutton2_name).length==0 )
      {
      buffhtml=buffhtml+'<a href="#" class="'+scrollbutton2_name+'">&nbsp;</a>';
      }
      		  	
  	$(mainobj).html(buffhtml);
	
	var txtwidth=0;
    $(mainobj).find('.'+txt_name+' a').each(function()
		{
		txtwidth+=$(this).outerWidth(true);
		
		});
		
		if(isIE6)
			txtwidth+=2;
			
	$(mainobj).find('.'+txt_name).css('width', txtwidth+'px');
  	
  	};
  //-----------------------------------------------	
  $.fn.makescrollable2.initialize_events = function(mainobj)
    {
    //***
    var scrollbox_name=$(mainobj).data('opts').scrollbox_name;
  	var scrollbutton1_name=$(mainobj).data('opts').scrollbutton1_name;
  	var scrollbutton2_name=$(mainobj).data('opts').scrollbutton2_name;
    var beforescroll=$(mainobj).data('opts').beforescroll;
	var itemperpage=$(mainobj).data('opts').itemperpage;
	
	
	$(mainobj).data('current', 0);
    
  	$(mainobj).find('.'+scrollbutton1_name).bind("click", 
    function(event) 
  		{
      event.preventDefault();
	  	  	        
      if(beforescroll) beforescroll();
      
      var scrollbox=$(mainobj).find('.'+scrollbox_name);    
      var duration=500;
	  
	  var current=$(mainobj).data('current');
           
	  var newpos=current-1;   
	  
	  if(newpos<0) newpos=0;
	  
	  //debug( newpos );
	  
	  $(mainobj).data('current', newpos);
      //$(mainobj).find('.txt a:eq('+newpos+')')
	  
	  var awidth=$(mainobj).find('.txt a').outerWidth(true);

                    
	  	$(mainobj).find('.'+scrollbox_name).stop().scrollTo(  (newpos*awidth)+'px' , duration, {easing:'linear', axis:'x'});   
	  });
  		
  	//***	
  	$(mainobj).find('.'+scrollbutton2_name).bind("click", 
    function(event) 
  		{
  		event.preventDefault();  		
      
      if(beforescroll) beforescroll();  		
  		  		    
      var scrollbox=$(mainobj).find('.'+scrollbox_name);          
      var duration=500;      
      var angle=$(mainobj).data('opts').angle;  
	  
	  var current=$(mainobj).data('current');
           
	  var newpos=current+1;   
	  var cnt=$(mainobj).find('.txt a').length;
	  
	  if(newpos>=cnt-itemperpage+1) newpos=cnt-itemperpage;
	  
	  $(mainobj).data('current', newpos);
	  
	  var awidth=$(mainobj).find('.txt a').outerWidth(true);
	  
	  //debug( newpos );
	  
     
      if(angle=='vertical')
        {
        var sh=$(scrollbox)[0].scrollHeight;
        var oh=$(scrollbox).outerHeight();
		
				      
  		$(mainobj).find('.'+scrollbox_name).stop().scrollTo(  (newpos*awidth)+'px' , duration, {easing:'linear', axis:'x'});
        }
      else
        {
        var sw=$(scrollbox)[0].scrollWidth;
        var ow=$(scrollbox).outerWidth();
		
      
  		$(mainobj).find('.'+scrollbox_name).stop().scrollTo(  (newpos*awidth)+'px' , duration, {easing:'linear', axis:'x'});
        }
  		});
  		
  	//***
  	/*$(mainobj).find('.'+scrollbutton1_name+', .'+scrollbutton2_name).bind("mouseleave", 
    function(event) 
  		{
      event.preventDefault();
  		$(mainobj).find('.'+scrollbox_name).stop();   
  		});	*/
  		
  	//***	
  	$(mainobj).find('.'+scrollbox_name).bind("scroll", 
    function(event) 
  		{
  		var buttonduration=$(mainobj).data('opts').buttonduration;
  		
  		$.fn.makescrollable2.show_scrollboxbuttons( mainobj, buttonduration );								   
  		});	
  		
  	};
	//-----------------------------------------------
	$.fn.makescrollable2.show_scrollboxbuttons = function(mainobj, duration)
    {
    var scrollbox_name=$(mainobj).data('opts').scrollbox_name;
  	var scrollbutton1_name=$(mainobj).data('opts').scrollbutton1_name;
  	var scrollbutton2_name=$(mainobj).data('opts').scrollbutton2_name;
    
    if(duration==null) duration=$(mainobj).data('opts').buttonduration;
    
    var scrollbox=$(mainobj).find('.'+scrollbox_name);
    var angle=$(mainobj).data('opts').angle;
      
    if( $(scrollbox).length==0 ) return; 
    
    if(angle=='vertical')
      {                 
      var st=$(scrollbox).scrollTop();
      var sh=$(scrollbox)[0].scrollHeight;
      var oh=$(scrollbox).outerHeight();               
                          
		/*
      if(st==0) $(mainobj).find('.'+scrollbutton1_name).animate({ opacity: 'hide' }, duration);
      else $(mainobj).find('.'+scrollbutton1_name).animate({ opacity: 'show' }, duration);*/
	 if(st==0) $(mainobj).find('.'+scrollbutton1_name).addClass('ni');
     else $(mainobj).find('.'+scrollbutton1_name).removeClass('ni');
            
      /*if(sh-st==oh) $(mainobj).find('.'+scrollbutton2_name).animate({ opacity: 'hide' }, duration);
      else $(mainobj).find('.'+scrollbutton2_name).animate({ opacity: 'show' }, duration);*/
		if(sh-st==oh) $(mainobj).find('.'+scrollbutton2_name).addClass('ni');
     	else $(mainobj).find('.'+scrollbutton2_name).removeClass('ni');
      }
    else
      {
      var sl=$(scrollbox).scrollLeft();
      var sw=$(scrollbox)[0].scrollWidth;
      var ow=$(scrollbox).outerWidth();		  
      
      /*if(sl==0) $(mainobj).find('.'+scrollbutton1_name).animate({ opacity: 'hide' }, duration);
      else $(mainobj).find('.'+scrollbutton1_name).animate({ opacity: 'show' }, duration);*/
	 if(sl==0) $(mainobj).find('.'+scrollbutton1_name).addClass('ni');
     else $(mainobj).find('.'+scrollbutton1_name).removeClass('ni');
      
      
      /*if(sw-sl==ow) $(mainobj).find('.'+scrollbutton2_name).animate({ opacity: 'hide' }, duration);
      else $(mainobj).find('.'+scrollbutton2_name).animate({ opacity: 'show' }, duration);*/
	  if(sw-sl==ow) $(mainobj).find('.'+scrollbutton2_name).addClass('ni');
     else $(mainobj).find('.'+scrollbutton2_name).removeClass('ni');
      }
    
  	//alert( $(scrollbox).html() );
  	};
	
	
	//-----------------------------------------------
  	$.fn.makescrollable2.defaults = 
      {
      speed           : 100, //pixel / sec
      baseduration    : 4000,
      buttonduration  : 300,
	  itemperpage	  : 2,
      angle           : 'vertical',
      makeoutertags   : false,
      scrollbox_name  : 'scrollbox',
      scrollbutton1_name  : 'scrollbutton1',
      scrollbutton2_name  : 'scrollbutton2',
      txt_name        : 'txt',
      beforescroll        : false
      };
	
	
})(jQuery);