/**
* naScrollControl
*
* !! for prototype.js version 1.5.0
* initial coding is strictly for userprofile infobox scroll
* @author goran@nachtausgabe.de
*/


var naScrollControl = Class.create();
naScrollControl.prototype = {

  /**
  * constructor
  */
  initialize: function(options) {

	// option parameters
	this.options = Object.extend({
      speed:           		5,
      frequency:			10,
      scrollDivElementID:   "userProfileShowInfoboxPositionDivId",
      arrowUpElementID:     "userProfileShowInfoboxArrowUpId",
      arrowDownElementID:   "userProfileShowInfoboxArrowDownId"
	}, options || {});

	// state holders
	this.posY = 0;
	this.mouseIsDown = false;
	this.timer = null;
	
	// Event bindings
	this.startScrollingUp = this._startScrollingUp.bindAsEventListener(this);
	this.startScrollingDown = this._startScrollingDown.bindAsEventListener(this);
	this.doneScrolling = this._doneScrolling.bindAsEventListener(this);

	Event.observe(this.options.arrowUpElementID, "mousedown", this.startScrollingUp);
	Event.observe(this.options.arrowDownElementID, "mousedown", this.startScrollingDown);
	Event.observe(window.document, "mouseup", this.doneScrolling);

    
  },

  /**
  * destructor
  */
  destroy: function() {
    Event.stopObserving(this.options.arrowUpElementID, "mousedown", this.scrollUp);
  	Event.stopObserving(this.options.arrowDownElementID, "mousedown", this.scrollDown);
  	Event.stopObserving(window.document, "mouseup", this.doneScrolling);
  },

  /**
  * private function start scrolling up
  */
  _startScrollingUp: function(event) {
  	this.mouseIsDown = true;
  	this.timer = setInterval(this._scrollUp.bind(this), this.options.frequency);
  },  


  /**
  * private function start scrolling down
  */
  _startScrollingDown: function(event) {
  	this.mouseIsDown = true;
  	this.timer = setInterval(this._scrollDown.bind(this), this.options.frequency);
  },  

  /**
  * private function done scrolling
  */
  _doneScrolling: function(event) {
  	clearInterval(this.timer);
  	this.timer = null;
  	this.mouseIsDown = false;
  	return false;
  },  



  /**
  * private function scroll up
  */
  _scrollUp: function() {
  	if(this.mouseIsDown){
		this.posY = parseInt($(this.options.scrollDivElementID).getStyle("top"));
		this.posY = this.posY + this.options.speed;
		if(this.posY > 0) this.posY = 0;
		if(this.posY <=0 ){
			$(this.options.scrollDivElementID).setStyle({top: + this.posY + "px" });
			setTabsClasses(this.posY);
		}
	};
	return false;	
  },


  /**
  * private function scroll down
  */
  _scrollDown: function() {
  	if(this.mouseIsDown){
		this.posY = parseInt($(this.options.scrollDivElementID).getStyle("top"));
		this.posY = this.posY - this.options.speed;
		if(this.posY > -($(this.options.scrollDivElementID).getHeight()-120)){
			$(this.options.scrollDivElementID).setStyle({top: + this.posY + "px" });
			setTabsClasses(this.posY);
		}
	};
	return false;  	
  }
 
}


/**
* helper function for setting the class attribute of the
* infobox tabs while scrolling
*/
function setTabsClasses (posY) {
	posY = -(posY);
	
	var pos0 = 0;
	var pos1 = (Position.positionedOffset($("userProfileShowInfoboxLifeDivId")))[1];
	var pos2 = (Position.positionedOffset($("userProfileShowInfoboxStyleDivId")))[1];
	var pos3 = (Position.positionedOffset($("userProfileShowInfoboxContactDivId")))[1];
	
	//alert( (Position.positionedOffset($("userProfileShowInfoboxStyleDivId")))[1]  );
	//alert(posY + "," + pos0+ "," + pos1 + "," + pos2 + "," + pos3); 
	
	if((posY >= pos0) && (posY < pos1)){
		$("userProfileShowInfoboxTabAboutId").addClassName("selected");
		$("userProfileShowInfoboxTabLifeId").removeClassName("selected");
		$("userProfileShowInfoboxTabStyleId").removeClassName("selected");
		$("userProfileShowInfoboxTabContactId").removeClassName("selected");
	}

	else if((posY >= pos1) && (posY < pos2)){
		$("userProfileShowInfoboxTabAboutId").removeClassName("selected");
		$("userProfileShowInfoboxTabLifeId").addClassName("selected");
		$("userProfileShowInfoboxTabStyleId").removeClassName("selected");
		$("userProfileShowInfoboxTabContactId").removeClassName("selected");
	}

	else if((posY >= pos2) && (posY < pos3)){
		$("userProfileShowInfoboxTabAboutId").removeClassName("selected");
		$("userProfileShowInfoboxTabLifeId").removeClassName("selected");
		$("userProfileShowInfoboxTabStyleId").addClassName("selected");
		$("userProfileShowInfoboxTabContactId").removeClassName("selected");
	}

	else if((posY >= pos3)){
		$("userProfileShowInfoboxTabAboutId").removeClassName("selected");
		$("userProfileShowInfoboxTabLifeId").removeClassName("selected");
		$("userProfileShowInfoboxTabStyleId").removeClassName("selected");
		$("userProfileShowInfoboxTabContactId").addClassName("selected");
	}

}//setTabsClasses
