
function autoscrollL(){
	var speed=getSpeed(0, 30);
	$('#imagebar').animate({left: 0}, speed, 'linear', function(){
			autoscrollR();
	});
}
function autoscrollR(){
	var speed=getSpeed(-ib_length, 30);
	$('#imagebar').animate({left: -ib_length}, speed, 'linear', function(){
			autoscrollL();
	});
}
function getSpeed(ib_pos, speed_factor){
	var left_pos=parseInt($('#imagebar').css("left"));
	var distance=Math.abs(ib_pos-left_pos);
	return speed_factor*distance;
}
function moveBar(ib_pos, speed_factor){
	var speed=getSpeed(ib_pos, speed_factor);
	$('#imagebar').animate(
		{
			left: ib_pos
  		}
  		, speed, 'linear'
  	);
}
function stopBar(){
	$('#imagebar').stop();
}

$.widget("ui.sliderbar", {
	// default options
	options: {
		option1: "defaultValue",
		hidden: true
	},
	
	_create: function() {
		$('#imagebar')
			.bind("mouseenter.button", function(){
				stopBar();
			})
			.bind("mouseleave.button", function(){
				$('#ib_header').slideUp(600);
				autoscrollR();
			});
		$('#controlsL')
			.addClass("sb-basic")		
			.bind( "mouseenter.button", function() {				
				$( '#controlsL' ).addClass( "sb-hover"); 
				stopBar();
				moveBar(-ib_length, 5);
			})
			.bind( "mouseleave.button", function() {
				$( '#controlsL' ).removeClass( "sb-hover");
				stopBar();
			});
		$('#controlsR')
			.addClass("sb-basic")		
			.bind( "mouseenter.button", function() {				
				$( '#controlsR' ).addClass( "sb-hover"); 
				stopBar();
				moveBar(0, 5);
			})
			.bind( "mouseleave.button", function() {
				$( '#controlsR' ).removeClass( "sb-hover");
				stopBar();
			});
		$(".wrapper")
			.bind("mouseenter.button", function(){
				var img_name=$(this).attr("img_name");
				var title=$(this).attr("title");
				var special=$(this).attr("special");
				$('#display').html(
					"<div id='imagebox'><img height=\"400px\" src=\"images/listings/"+img_name+"\"></div>"+title+special
				);
				$('#missing').stop(1,1);
				$('#missing').fadeOut(600);
				$('#display').stop(1,1);
				$('#display').fadeIn(600);
				$('#ib_header').slideDown(600);
			})
			.bind( "mouseleave.button", function() {
				$('#display').stop(1,1);
				$( '#display' ).fadeOut(300);
				$('#missing').stop(1,1);
				$( '#missing' ).fadeIn(300);
			})
			.click(function (){
				var listing_id=$(this).attr('listing_id');
				var url='view-featured-'+listing_id+'.html';
				var listnum=$(this).attr('listnum');
				var myform= document.forms['listnum'+listing_id];
				myform.submit();
				return false;
			});	
		autoscrollR();
	},
		

   value: function() {
     // calculate some value and return it
     return this._calculate();
   },
   length: function() {
     return this._someOtherValue();
   },
   destroy: function() {
       $.Widget.prototype.destroy.apply(this, arguments); // default destroy
        // now do other stuff particular to this widget
   }
 });

