/**
 * Scrollpane.js
 * This file is lisenced under LGPL 
 * Copyright (C) 2006 nightlabs
 * author : khaled at nightlabs dot de
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; 
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * @param _parent The container, which must be a div.
 * @param _child The component that will be scrolled within the parent.
 *	This can be another div or an image or whatever can be visible.
 * @param _cssClass A string referencing the desired stylesheet class. If missing, this will default to "defaultScroll".
 */
function Scrollpane (_parent, _child, _cssClass)
{
	util = Util.getInstance();
	this.parent = _parent;
	this.child = _child;
	this.cssClass = _cssClass;
	if (this.cssClass == null) this.cssClass = "defaultScroll";
	
	this.createComposite = function (){
		this.composite = document.createElement("div");
		this.composite.style.position= "absolute";
		this.child.style.position= "absolute";
		this.composite.style.overflow = "hidden";
		this.composite.appendChild(this.child);
		this.parent.appendChild(this.composite);
	}
	this.createComposite();
	this.scrollbarHorizontal = new Scrollbar(this.parent, "horizontal", _cssClass);
	this.scrollbarHorizontal.addScrollListener(util.createMethodReference(this, "horizontalScrolled"));
	this.scrollbarVertical = new Scrollbar(this.parent, "vertical", _cssClass);
	this.scrollbarVertical.addScrollListener(util.createMethodReference(this, "verticalScrolled"));
	this.layout();
	//	sizeController.addElement(this.composite, onResize);
}
Scrollpane.prototype.layout = function () {
//this.adjustParentPosition();
var VVis,HVis;
// checking if scrollbars need to be displayed 
// first Part is for the case the Element only needs scrollbars after the ones from the other orientation are beeing displayed
 if  (this.child.offsetHeight >  this.parent.offsetHeight) VVis = true;
 	 else VVis = false ;
 	 //alert(this.child.offsetWidth+ "parent:"+  this.parent.offsetWidth);
 if  (this.child.offsetWidth >  this.parent.offsetWidth) HVis = true;
 	 else HVis = false;
 	 
 if  (HVis != VVis){ 
 	 if(HVis == true){
 	 	if( this.child.offsetHeight > this.parent.offsetHeight - this.scrollbarHorizontal.getHeight()) VVis = true;
 	 }		
	 if(VVis == true){
 	 	if( this.child.offsetWidth > this.parent.offsetWidth - this.scrollbarVertical.getWidth()) HVis = true;
 	 }		
 }
 if (HVis == true){
	 this.scrollbarHorizontal.setVisibility(true);
	 this.composite.style.height = this.parent.offsetHeight - this.scrollbarHorizontal.getHeight() + "px";
 }
 else {
 		this.scrollbarHorizontal.setVisibility(false);
		this.composite.style.height = this.parent.offsetHeight + "px";
	}	
 if (VVis == true){	 
	 this.scrollbarVertical.setVisibility(true);
	 this.composite.style.width = this.parent.offsetWidth - this.scrollbarVertical.getWidth() + "px";
 }
 else {
 		this.scrollbarVertical.setVisibility(false);
 		this.composite.style.width = this.parent.offsetWidth  +"px";
 	}
 	
    this.scrollbarHorizontal.setWidth(this.composite.offsetWidth);
	

	if(navigator.appName == "Netscape" && util.getStyleRule( "#" + this.parent.id ,"position") == "static"){
		this.scrollbarHorizontal.setTop(this.parent.offsetHeight - this.scrollbarHorizontal.getHeight()+ this.parent.offsetTop-1);  
	}
	else this.scrollbarHorizontal.setTop(this.parent.offsetHeight - this.scrollbarHorizontal.getHeight());  
	this.scrollbarHorizontal.layout("horizontal");
	this.scrollbarHorizontal.resizeSlider(this.child.offsetWidth);
	this.scrollbarHorizontal.checkHorizontalSliderPositions();
	this.scrollbarHorizontal.fireScrollEvent();
	
	this.scrollbarVertical.setHeight(this.composite.offsetHeight);
	
	if(navigator.appName == "Netscape" && util.getStyleRule( "#" + this.parent.id ,"position") == "static"){
		this.scrollbarVertical.setLeft(this.parent.offsetWidth - this.scrollbarVertical.getWidth()+ this.parent.offsetLeft -1);  
	}
	else this.scrollbarVertical.setLeft(this.parent.offsetWidth - this.scrollbarVertical.getWidth() );  
	this.scrollbarVertical.layout("vertical");
	this.scrollbarVertical.resizeSlider(this.child.offsetHeight);
	this.scrollbarVertical.checkVerticalSliderPositions();
	this.scrollbarVertical.fireScrollEvent();
}
Scrollpane.prototype.verticalScrolled = function (event){
	var sliderRange = this.scrollbarVertical.getSliderPxRange();
	var position  = this.scrollbarVertical.getSliderOffsetTop() - this.scrollbarVertical.getImageHeight();
	var factor = this.child.offsetHeight / sliderRange;
	this.child.style.top = -(factor * position) + "px"; 
	this.adjustParentPosition();
}
Scrollpane.prototype.horizontalScrolled = function (event){
	var sliderRange = this.scrollbarHorizontal.getSliderPxRange();
	var position  = this.scrollbarHorizontal.getSliderOffsetLeft() - this.scrollbarHorizontal.getImageWidth() ;
	var factor = this.child.offsetWidth / sliderRange;
	this.child.style.left = -(factor * position) + "px"; 
	this.adjustParentPosition();
}
Scrollpane.prototype.adjustParentPosition = function (){
    if (this.child.offsetHeight < this.parent.offsetHeight && this.child.offsetHeight <= this.composite.offsetHeight) {
 		if (this.scrollbarHorizontal.getVisibility()=="hidden") {
 			this.child.style.top = parseInt((this.parent.offsetHeight - this.child.offsetHeight)/2) +"px";
 		}
 		else this.child.style.top = parseInt((this.parent.offsetHeight - this.child.offsetHeight - this.scrollbarVertical.getWidth())/2) +"px";
 	}
 
	 if (this.child.offsetWidth < this.parent.offsetWidth && this.child.offsetWidth <= this.composite.offsetWidth){
	 	if (this.scrollbarVertical.getVisibility()=="hidden"){
	 		this.child.style.left= parseInt((this.parent.offsetWidth - this.child.offsetWidth)/2) +"px";
	 	}
	 	else this.child.style.left = parseInt((this.parent.offsetWidth - this.child.offsetWidth - this.scrollbarHorizontal.getHeight())/2) +"px";
	 }
}
function ScrollEvent(_source)
{
	this.source = _source;
	
	function getSource()
	{
		return this.source;
	}
}