//Originaly Written by Nathan Faubion: http://n-son.com
//Modified and customized by Josef Kalvas // JQuery required
         
function jsScroller (o, tscroll, bscroll,replacer) {
  var self = this;
  var replacer=$(replacer);
  var replacerimg=$(replacer).children('img:first-child');
  var tscroller=$(tscroll);
  var bscroller=$(bscroll);
  var links=o.find('a');
  links.each(function(index){
  var lnk=$(this);
  var img_koef=412/170;
  
    lnk.click(function(event) {
      event.preventDefault();
      replacerimg.attr("src",lnk.attr('href'));
      var limg = lnk.children(':first-child');
      var lnk_height = limg.height();
      //alert(lnk_height +" : " +(Math.ceil(lnk_height * img_koef)+"px"));
      replacerimg.css("height","auto");
      replacer.css("height",Math.ceil(lnk_height * img_koef)+"px");
      self.updateScroller();
        });
    }); 
  w=o.width();
  h=o.height(); 
  
  var o=$('.inner');           
   tscroller.live('mouseover',function() {
   self.startScroll(0, 10);
  }); 
  bscroller.mouseover(function() {
   self.startScroll(0, -10);
  });

  tscroller.mouseout(function() {
   self.stopScroll();
  });  
  bscroller.mouseout(function() {
   self.stopScroll();
  });
	
  //Private methods
	
	this.updateScroller=function(){
	
//	alert('upd');
	 var rh = $(replacer).height();
	 if (rh<10){rh=307;}
	 $('#slider').css("height",rh+"px");
	 self.viewableHeight=rh;
	
	 
   if (this._y < this.viewableHeight - this.totalHeight){ 
			this._y = this.viewableHeight - this.totalHeight;
			this._setPos(this._x,this._y);
	 }
	 
	 }
	this._setPos = function (x, y) {
	if (this.totalHeight < 200){
		  this.totalHeight = $('#slider .inner').height() - 5;
		}
		if (x < this.viewableWidth - this.totalWidth){ 
			x = this.viewableWidth - this.totalWidth;
			}
		if (x > 0) {x = 0;}
		if (y < this.viewableHeight - this.totalHeight){ 
			y = this.viewableHeight - this.totalHeight;
			}
		if (y > 0){y = 0;}
		this._x = x;
		this._y = y;
		//with (o.style) {
		o.css('left',this._x +"px");
		o.css('top',this._y +"px");
   // 	left = this._x +"px";
		//	top  = this._y +"px";
		//}
	};
	
	//Public Methods
	this.reset = function () {
		this.content = o;
		//this.totalHeight = o.height() -5;
		this.totalHeight = $('#slider .inner').height() - 5;
		this.totalWidth	 = o.width();//o.offsetWidth;
		this._x = 0;
		this._y = 0;
	//	with (o.style) {
		o.css('left',"0px");
		o.css('top',"0px");
		//}
	};
	this.scrollBy = function (x, y) {
		this._setPos(this._x + x, this._y + y);
	};
	this.scrollTo = function (x, y) {
		this._setPos(-x, -y);
	};
	this.stopScroll = function () {
		if (this.scrollTimer) {window.clearInterval(this.scrollTimer);}
    	};
	this.startScroll = function (x, y) {
		this.stopScroll();
		this.scrollTimer = window.setInterval(
			function(){ self.scrollBy(x, y); }, 40
		);
	};

	
	//variables
	this.content = o;
	this.viewableWidth  = w;
	this.viewableHeight = h;
	this.totalWidth	 = o.offsetWidth;
	this.totalHeight = o.offsetHeight-5;
	this.scrollTimer = null;
	this.reset();
	this.updateScroller();
}

