var mp_sliderDisable = false;

jQuery.fn.panelSlider = function(opts) {
  var totalWidth = opts.panelWidth * $('table.slider_panel td.slider_cell', this).length;
  this.width(opts.panelWidth);
  this.height(opts.panelHeight);
  this.css('position', 'relative');
  this.css('float', 'left');
  this.css('overflow', 'hidden');
  
  $('table.slider_panel', this).css('position', 'relative');
  $('table.slider_panel', this).css('left', '0px');
  $('table.slider_panel', this).width(totalWidth);
  $('table.slider_panel', this).height(opts.panelHeight);
  $('table.slider_panel td.slider_cell', this).width(opts.panelWidth);
  $('table.slider_panel td.slider_cell', this).height(opts.panelHeight);
  //$('table td', this).css('border-right-width')
  //$('table td', this)[ $('table td', this).length - 1].style.borderRight = "none";
  
  $(opts.prevBtn).css('float', 'left');
  $(opts.nextBtn).css('float', 'left');
  
  $(opts.prevBtn).css('cursor', 'pointer');
  $(opts.nextBtn).css('cursor', 'pointer');
  
  //alert(this.height());
  //alert($(opts.prevBtn).height() + "-" + $(opts.nextBtn).height());
  
  var prevH = (this.height() - $(opts.prevBtn).height()) / 2;
  var nextH = (this.height() - $(opts.nextBtn).height()) / 2;
  
  //alert(prevH + "-" + nextH);
  
  $(opts.prevBtn).css('margin-top', prevH);
  $(opts.nextBtn).css('margin-top', nextH);
  
  $(opts.prevBtn).css('margin-right', 5);
  $(opts.nextBtn).css('margin-left', 5);
  
  var tbl = $('table.slider_panel', this);
  tbl.css('margin-left', 0);
  $(opts.prevBtn).css('visibility', 'hidden');
  if($('table.slider_panel td.slider_cell', this).length < 2)
  	$(opts.nextBtn).css('visibility', 'hidden');
  	
  $(opts.nextBtn).click(
  	function()
	{
		//alert(parseInt(tbl.css('left')));
		//alert(tbl.css('left'));
		if(parseInt(tbl.css('left')) > (0 - (totalWidth - opts.panelWidth)) && !window.mp_sliderDisable)
		{
			window.mp_sliderDisable = true;
			var ml = parseInt(tbl.css('left')) - opts.panelWidth;
			
			$(opts.prevBtn).css('visibility', 'visible');
			
			if(!(parseInt(tbl.css('left')) > (0 - (totalWidth - 2*opts.panelWidth))))
				$(opts.nextBtn).css('visibility', 'hidden');
				
			//alert($(opts.nextBtn).css('visibility'));
			tbl.animate(
				{left: ml + "px"},
				{ duration: opts.duration, queue: false, complete: function(){window.mp_sliderDisable=false;} }
			);
			//alert("1_");
		}
		//alert("end");
	}
  );
  
  $(opts.prevBtn).click(
  	function()
	{
		//alert(parseInt(tbl.css('left')));
		//alert("start");
		if((parseInt(tbl.css('left')) < 0)  && !window.mp_sliderDisable)
		{
			
			window.mp_sliderDisable = true;
			var ml = parseInt(tbl.css('left')) + opts.panelWidth;
			
			$(opts.nextBtn).css('visibility', 'visible');
			
			if(!(parseInt(tbl.css('left')) < (0 - opts.panelWidth)))
				$(opts.prevBtn).css('visibility', 'hidden');
				
			//alert("2");
			tbl.animate(
				{left: ml + "px"},
				{ duration: opts.duration, queue: false, complete: function(){window.mp_sliderDisable=false;} }
			);
			//alert("_2");
		}
		//alert("end");
	}
  );
  
};
