var  hometeaserScroll = Class.create({

	initialize: function(divid,scrollPanelDivId){
		this.durationTime = 0.9;
		
		this.divid = divid;
		this.functionIsCalled = new Array();
		var parentElement = $(this.divid).up('.hometaserdetail');
		var parentId = parentElement.identify();
		var NumberOfLargeTeasers = $$('div#'+this.divid+' div.hometeaserdetailcontent-big').length;
		var NumberOfSmallTeasers = $$('div#'+this.divid+' div.hometeaserdetailcontent').length;
		$(parentId).setStyle({
			'visibility': 'hidden',
			'display': 'block'
		});
		this.totalHeight =$(divid).getHeight();
		$(parentId).setStyle({
			'visibility': 'visible',
			'display': 'none'
		});
		this.scrollHeight = 280;
			
		if(this.totalHeight - this.scrollHeight <= this.scrollHeight){
			$(scrollPanelDivId).hide();
		}
		else{	
			this.calculateEndPos();
		}
		
		//manipulate the srcoll heights and duration
		this.scrollHeightsUp = Array();
		this.durationTimesUp = Array();
		
		this.scrollHeightsDown = Array();
		this.durationTimesDown = Array();
		
		this.scrollPos = 0;
		
		if(NumberOfLargeTeasers==0){
			for (i=0;i < NumberOfSmallTeasers;i++){
				this.scrollHeightsUp[i] = this.scrollHeight/2;
				this.durationTimesUp[i] = this.durationTime/2;
				
				this.scrollHeightsDown[i] = this.scrollHeight/2;
				this.durationTimesDown[i] = this.durationTime/2;
			}
		}
		else{
			this.scrollHeights = Array();
			for (i=0;i < NumberOfLargeTeasers-1;i++){
				this.scrollHeightsUp[i] = this.scrollHeight;
				this.durationTimesUp[i] = this.durationTime;
			}
			for (i=NumberOfLargeTeasers-1;i < NumberOfSmallTeasers+NumberOfLargeTeasers-1;i++){
				if(i == NumberOfSmallTeasers-2+NumberOfLargeTeasers-1){
					//last-1
					this.scrollHeightsUp[i] = this.scrollHeight;
					this.durationTimesUp[i] = this.durationTime;
					break;
				}
				else{
					this.scrollHeightsUp[i] = this.scrollHeight/2;
					this.durationTimesUp[i] = this.durationTime/2;
				}
			}
			
			this.scrollHeightsDown[0] = this.scrollHeight;
			this.durationTimesDown[0] = this.durationTime;
			if(NumberOfSmallTeasers){
				for (i=1;i <= NumberOfLargeTeasers-1;i++){
					this.scrollHeightsDown[i] = this.scrollHeight;
					this.durationTimesDown[i] = this.durationTime;
				}
			}
			else{
				for (i=1;i < NumberOfLargeTeasers-1;i++){
					this.scrollHeightsDown[i] = this.scrollHeight;
					this.durationTimesDown[i] = this.durationTime;
				}
			}
			for (i=NumberOfLargeTeasers;i < NumberOfSmallTeasers-1+NumberOfLargeTeasers-1;i++){
				if(i == NumberOfLargeTeasers+NumberOfSmallTeasers-2){
					//last-1
					this.scrollHeightsDown[i] = this.scrollHeight;
					this.durationTimesDown[i] = this.durationTime;
					break;
				}
				else{
					this.scrollHeightsDown[i] = this.scrollHeight/2;
					this.durationTimesDown[i] = this.durationTime/2;
				}
			}
			//alert(this.scrollHeightsUp);
			//alert(this.scrollHeightsDown);
		}
	},
	calculateEndPos: function(){
		this.endPos = (this.totalHeight - this.scrollHeight) * (-1);
	},
	getCurrentPos: function(){
		var currentPos = $(this.divid).getStyle('top');
		//for calculate, exclude last 2 letters (px)
		return parseInt(currentPos.substr(0,(currentPos.length-2)));
	},
	registerFunction: function(functionName){
		this.functionIsCalled.push(functionName);
	},
	unregisterFunction: function(){
		this.functionIsCalled.shift();
		if(this.functionIsCalled.length > 0){
			if(this.functionIsCalled[0] == 'scrollUp'){
				this.scrollUp(true);
			}
			else{
				this.scrollDown(true);
			}
		}
	},
	checkIfFunctionCalled: function(){
		if(this.functionIsCalled.length > 1){
			return true;
		}
		return false;
	},
	scrollUp: function(withoutRegister){
		if(withoutRegister==undefined){
			this.registerFunction('scrollUp');
		}
		if(!this.checkIfFunctionCalled() || withoutRegister!=undefined){
			this.currentPos = this.getCurrentPos();
			var myClass = this;
			if(this.currentPos == this.endPos || this.currentPos == 0){
				$(this.divid).setStyle({
					'top': '0px'
				});
				this.scrollPos = 0;
				//alert("PASS1: "+this.scrollPos);
				//now scroll
				new Effect.Morph($(this.divid), {
					style: 'top:'+(0-this.scrollHeightsUp[this.scrollPos])+'px;',
					duration: this.durationTimesUp[this.scrollPos],
					afterFinish: function(){
						myClass.scrollPos++;
						myClass.unregisterFunction();
					}
				});
			}
			else{
				//alert("PASS2: "+this.scrollPos);
				//now scroll
				new Effect.Morph($(this.divid), {
					style: 'top:'+(this.currentPos-this.scrollHeightsUp[this.scrollPos])+'px;',
					duration: this.durationTimesUp[this.scrollPos],
					afterFinish: function(){
						myClass.scrollPos++;
						myClass.unregisterFunction();
					}
				});
			}
		}
	},
	scrollDown: function(withoutRegister){
		if(withoutRegister==undefined){
			this.registerFunction('scrollDown');
		}
		if(!this.checkIfFunctionCalled() || withoutRegister!=undefined){
			this.currentPos = this.getCurrentPos();
			var myClass = this;
			//alert(this.currentPos);
			if(this.currentPos == 0 || this.currentPos == this.endPos){
				$(this.divid).setStyle({
					'top': this.endPos+'px'
				});
				
				this.scrollPos=0;
				//alert("PASS3: "+this.scrollPos);
				//now scroll
				new Effect.Morph($(this.divid), {
					style: 'top:'+(this.endPos+this.scrollHeightsDown[this.scrollPos])+'px;',
					duration: this.durationTimesDown[this.scrollPos],
					afterFinish: function(){
						myClass.scrollPos = myClass.scrollHeightsDown.length-1;
						myClass.unregisterFunction();
					}
				});
			}
			else{
				//alert("PASS4: "+this.scrollPos);
				//now scroll
				new Effect.Morph($(this.divid), {
					style: 'top:'+(this.currentPos+this.scrollHeightsDown[this.scrollPos])+'px;',
					duration: this.durationTimesDown[this.scrollPos],
					afterFinish: function(){
						myClass.scrollPos--;
						myClass.unregisterFunction();
					}
				});
			}
		}
	}
});