/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */
 
componentTextWithScroll = function(name)
{
	this.__componentText = __componentText;
	this.__componentText(name);	
	
	this.imagesCompleteInfo = new Array();
	this.sliderLowOffset = 0;
	this.sliderHighOffset = 0;	
	this.dragobject = null;
	this.buttonsActive = true;
	this.mouseIsDown = false;
	this.textHeight = true;
	this.trackHeight = 0;
	this.timer = null;
	
	this.headerHeight = 0;
	this.footerHeight = 0;
	this.buttonPadding = 3;
	this.textSpacing = 0;
	this.contentPadding = 0;
	this.checkboxesFieldH = 0;
};

componentTextWithScroll.prototype = new __componentText;
componentTextWithScroll.prototype.type = "componentTextWithScroll";

componentTextWithScroll.prototype.onBaseCreate = function(obj)
{
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
		this.resourcesStatus = lms.resourcesStatus();
		if (this.disableOnlineOfflineMix) this.resourcesURL = resourcesURL;
		else this.resourcesURL = lms.resourcesURL();
	};
	this.resourcesURL = unescape(this.resourcesURL); 
	
	this.imagesNameArr = [ this.arrowTopOver, this.arrowTopOut, this.arrowBottomOver, this.arrowBottomOut, this.sliderOver, this.sliderOut, this.sliderBckg ];
	this.getPreloadImagesArr();
	this.setCompleteArrayInfo(this.completeInfoStyleImages);

	this.captureMouseEvents();
};

componentTextWithScroll.prototype.componentTextWithScrollBaseShow = componentTextWithScroll.prototype.show;
componentTextWithScroll.prototype.show = function(obj)
{
	this.placeHTMLObiects();
	this.getTextHeight();
	this.setHTMLObiects();

	this.mouseIsDown = true;
	this.moveScroll(0);
	this.mouseIsDown = false;
	
	this.componentTextWithScrollBaseShow();
}

componentTextWithScroll.prototype.setHTMLObiects = function(obj)
{
	if (this.buttonsActive){
		var arrowTopImg= document.getElementById(this.id + "_arrowTopImg");
		var arrowBottomImg = document.getElementById(this.id + "_arrowBottomImg");
		if (arrowTopImg) this.setImageSrc(this.resourcesURL + this.arrowTopOut, arrowTopImg);
		if (arrowBottomImg) this.setImageSrc(this.resourcesURL + this.arrowBottomOut, arrowBottomImg);	
	}else{
		var arrowTopImg= document.getElementById(this.id + "_arrowTopImg");
		var arrowBottomImg = document.getElementById(this.id + "_arrowBottomImg");
		if (arrowTopImg) this.setImageSrc(this.resourcesURL + this.arrowTopOff, arrowTopImg);
		if (arrowBottomImg) this.setImageSrc(this.resourcesURL + this.arrowBottomOff, arrowBottomImg);	
		document.getElementById(this.id + "_trackDiv").style.visibility = "hidden";		
		document.getElementById(this.id + "_arrowTopDiv").style.cursor = "default";
		document.getElementById(this.id + "_arrowBottomDiv").style.cursor = "default";
	};

	var trackImg= document.getElementById(this.id + "_trackImg");
	var trackBckgImg = document.getElementById(this.id + "_trackBckgImg");
	
	if (trackImg) this.setImageSrc(this.resourcesURL + this.sliderOut, trackImg);
	if (trackBckgImg) this.setImageSrc(this.resourcesURL + this.sliderBckg, trackBckgImg);	
};

componentTextWithScroll.prototype.getTextHeight = function(obj)
{
	var tableDiv = document.getElementById(this.id + "_tableDiv");
	var Table = document.getElementById(this.id + "Table");

	tableDiv.style.top = 0;
	
	if (this.dir == "rtl") {
		tableDiv.style.left = this.getImageWidth('sliderBckg');
	} else {
		tableDiv.style.left = 0;
	}	
	
	
	tableDiv.style.width = this.w - 2*this.borderWidth - this.getImageWidth('sliderBckg'); 
	//########
	this.textHeight = Table.scrollHeight;
	this.buttonsActive = (this.h < this.textHeight) ? true : false;
	//########
	tableDiv.style.height = this.h - 2*this.borderWidth;
	tableDiv.style.clip = 'rect(0px '+eval(this.w - 2*this.borderWidth)+'px '+eval(this.h - 2*this.borderWidth)+'px 0px)';
};

componentTextWithScroll.prototype.placeHTMLObiects = function(obj)
{
	var arrowTopDiv = document.getElementById(this.id + "_arrowTopDiv");
	var arrowBottomDiv = document.getElementById(this.id + "_arrowBottomDiv");
	var trackBckgDiv = document.getElementById(this.id + "_trackBckgDiv");
	var trackDiv = document.getElementById(this.id + "_trackDiv");
	var tableDiv = document.getElementById(this.id + "_tableDiv");
	var Table = document.getElementById(this.id + "Table");
	
	arrowTopDiv.style.top = 0;
	if (this.dir == "rtl") {
		arrowTopDiv.style.left = this.contentPadding;
	} else {
		arrowTopDiv.style.left = this.w - this.getImageWidth('arrowTopOut')-2*this.borderWidth-this.contentPadding;
	}
	arrowTopDiv.style.width = this.getImageWidth('arrowTopOut');
	arrowTopDiv.style.height = this.getImageHeight('arrowTopOut');
	arrowTopDiv.style.clip = 'rect(0px '+this.getImageWidth('arrowTopOut')+'px '+this.getImageHeight('arrowTopOut')+'px 0px)';

	arrowBottomDiv.style.top = this.h - this.getImageHeight('arrowBottomOut')-2*this.borderWidth;
	if (this.dir == "rtl") {
		arrowBottomDiv.style.left = this.contentPadding;
	} else {
		arrowBottomDiv.style.left = this.w - this.getImageWidth('arrowBottomOut')-2*this.borderWidth-this.contentPadding;
	}	
	arrowBottomDiv.style.width = this.getImageWidth('arrowBottomOut');
	arrowBottomDiv.style.height = this.getImageHeight('arrowBottomOut');
	arrowBottomDiv.style.clip = 'rect(0px '+this.getImageWidth('arrowBottomOut')+'px '+this.getImageHeight('arrowBottomOut')+'px 0px)';
	
	trackBckgDiv.style.top = 0;
	if (this.dir == "rtl") {
		trackBckgDiv.style.left = this.contentPadding;
	} else {
		trackBckgDiv.style.left = this.w - this.getImageWidth('sliderBckg')-2*this.borderWidth-this.contentPadding;
	}
	trackBckgDiv.style.width = this.getImageWidth('sliderBckg');
	trackBckgDiv.style.height = this.h-2*this.borderWidth;
	trackBckgDiv.style.clip = 'rect(0px '+this.getImageWidth('sliderBckg')+'px '+this.h+'px 0px)';
	
	this.sliderLowOffset = this.getImageHeight('arrowTopOut');
	this.trackHeight = this.h - this.getImageHeight('arrowTopOut') - this.getImageHeight('arrowBottomOut') - 2*this.borderWidth;
	this.sliderHighOffset = this.trackHeight - this.getImageHeight('sliderOut');

	trackDiv.style.top = this.sliderLowOffset;
	if (this.dir == "rtl") {
		trackDiv.style.left = this.contentPadding;
	} else {
		trackDiv.style.left = this.w - this.getImageWidth('sliderOut')-2*this.borderWidth-this.contentPadding;
	}	
	trackDiv.style.width = this.getImageWidth('sliderOut');
	trackDiv.style.height = this.getImageHeight('sliderOut');
	trackDiv.style.clip = 'rect(0px '+this.getImageWidth('sliderOut')+'px '+this.getImageHeight('sliderOut')+'px 0px)';
	
	Table.width = this.w - this.getImageWidth('sliderBckg') - 2*this.borderWidth;
};

componentTextWithScroll.prototype.setCompleteArrayInfo = function(style)
{
	var imagesStr = style;
	
	var imgArr = imagesStr.split(';');
	for (var i = 0; i < imgArr.length; i++){
		var oneImg = imgArr[i].split(',');		
		if (!this.imagesCompleteInfo[oneImg[0]]){
			this.imagesCompleteInfo[oneImg[0]] = new Array();
			this.imagesCompleteInfo[oneImg[0]][0] = oneImg[1];
			this.imagesCompleteInfo[oneImg[0]][1] = oneImg[2];		
		};
	};
};

componentTextWithScroll.prototype.onPaint = function(obj, rep)
{
	return;
};

componentTextWithScroll.prototype.scrollWindow = function(obj)
{
	window.scrollTo(0, this.y);
};

componentTextWithScroll.prototype.moveScroll = function(scrollValue)
{
	if (!this.mouseIsDown) return;
	
	var	tableDiv = document.getElementById(this.id + "_tableDiv");
	var	trackDiv = document.getElementById(this.id + "_trackDiv");
	
	var scrollTopTemp = tableDiv.scrollTop;
	var scrollNewPos = tableDiv.scrollTop - scrollValue;
	tableDiv.scrollTop = scrollNewPos;
	
	var maxScrollHeight = tableDiv.scrollHeight - this.h + this.headerHeight + 2*this.contentPadding + 2*this.textSpacing + this.footerHeight + this.checkboxesFieldH;
	maxScrollHeight = (maxScrollHeight == 0)?1:maxScrollHeight;
	var percScrollPos = (tableDiv.scrollTop/maxScrollHeight);	
	if (scrollValue < 0){
		if (tableDiv.scrollTop == scrollNewPos){
			var scrollPos = this.sliderLowOffset + this.trackHeight*percScrollPos - this.getImageHeight('sliderOut')*percScrollPos;
			trackDiv.style.top = scrollPos;
		}else{
			trackDiv.style.top = this.sliderHighOffset + this.sliderLowOffset;
		};			
	}else{
		var scrollPos = this.sliderLowOffset + this.trackHeight*percScrollPos - this.getImageHeight('sliderOut')*percScrollPos;
		trackDiv.style.top = scrollPos;
	}
	
	this.timer = setTimeout("page.all['"+this.id+"'].moveScroll("+scrollValue+");", 100);
}

componentTextWithScroll.prototype.moveUp = function(obj)
{
	if (!this.buttonsActive) return;

	this.mouseIsDown = true;
	var scrollValue = 20;
	this.moveScroll(scrollValue);	
}

componentTextWithScroll.prototype.moveDown = function(obj)
{
	if (!this.buttonsActive) return;
	
	this.mouseIsDown = true;
	var scrollValue = -20;
	this.moveScroll(scrollValue);
	
};

componentTextWithScroll.prototype.moveSlider = function () 
{
	this.mouseIsDown = false;
	var tableDiv = document.getElementById(this.id + "_tableDiv");
	var Table = document.getElementById(this.id + "Table");
	
	if (this.dragobject) {
		if (window.event.clientX >= 0 && window.event.clientY >= 0) {
			var trackH = this.dragobjectParent.offsetHeight;
			var trackT = this.dragobjectParent.offsetTop;
			var mouseClickPos = window.event.clientY - this.y - this.sliderLowOffset + document.body.scrollTop;
			
			if (mouseClickPos < trackH/2){
				tableDiv.scrollTop = 0;
				this.dragobjectParent.style.top = this.sliderLowOffset;
			}else if (mouseClickPos > trackH/2 && mouseClickPos < eval(this.sliderHighOffset + trackH/2))
			{
				var divTop = eval(this.sliderLowOffset + mouseClickPos - trackH/2);
				this.dragobjectParent.style.top = divTop;				
				var realTrackPos = divTop - this.sliderLowOffset;
				var trackFromTopPrec = (realTrackPos/this.sliderHighOffset).toFixed(3);
				var maxScrollTop = Table.offsetHeight - tableDiv.offsetHeight;
				tableDiv.scrollTop = maxScrollTop*trackFromTopPrec;
			}else if (mouseClickPos > this.sliderHighOffset)
			{
				this.dragobjectParent.style.top = this.sliderHighOffset + this.sliderLowOffset;
				tableDiv.scrollTop = tableDiv.scrollHeight;
			};
		};
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	};
};

componentTextWithScroll.prototype.mouseUp = function()
{
	this.mouseIsDown = false;
};

componentTextWithScroll.prototype.onSliderFree = function()
{
	var sliderDIV = document.getElementById(this.id + "_trackDiv");
	
	if(this.dragobject) {
		this.eventMouseUp(event);
		this.dragobject = false;
	};
};

componentTextWithScroll.prototype.onSliderClick = function(obj, objParent)
{
	window.event.returnValue = false;
	window.event.cancelBubble = true;	
	
	this.dragobjectParent = objParent;
	this.dragobject = obj;
};

componentTextWithScroll.prototype.changeImage = function(obj, imageName)
{
	if (!this.buttonsActive) return;

	if (obj) this.setImageSrc(this.resourcesURL + imageName, obj);
	//obj.src = this.resourcesURL + imageName;
}

componentTextWithScroll.prototype.getImageHeight = function( imgName )
{
	return parseInt(this.imagesCompleteInfo[imgName][1]);
};

componentTextWithScroll.prototype.getImageWidth = function( imgName )
{
	return parseInt(this.imagesCompleteInfo[imgName][0]);
};

componentTextWithScroll.prototype.onFree = function()
{
	if (this.timer != null){
		window.clearTimeout(this.timer);
		this.timer = null;
	};
}

componentTextWithScroll.prototype.setImageSrc = function(value, obj)
{
	value = unescape(value);
	if (obj) {
		if (this.pngFix && is.ie55UP && !is.ie7UP) {
			var is_span = (obj.tagName.toUpperCase() == 'SPAN');
			if (is_span){
				var src = ".." + value.substr(value.lastIndexOf("/resources/"), value.length);
				obj.src = value;
				
				var psArr = this.getPosSize(obj);
				
				var png_img = new Image();
				png_img.src = value;
				var loaded = this.waitForImage(png_img, 50);
				
				var sizingMethod = "scale";
				if (loaded || (!isNaN(png_img.width) && !isNaN(png_img.height))) {
					if (parseInt(png_img.width) == parseInt(psArr[2])
							&& "" + parseInt(png_img.height) == parseInt(psArr[3])) {
						sizingMethod = "image";
					}
				}				
				
				obj.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader (src=\'' + src + '\',sizingMethod=\''+sizingMethod+'\'';
			}else{
				obj.src = value;			
			}
		}else{
			obj.src = value;			
		}
		return true;
	}
	return false;
};
