/*
(c) 2006 Klaus Meusburger, http://www.gmg.biz

This script needs library prototype.js.

// To resize three divs on your site, when window will be resized make this
DivResizer.add("IDofDiv1",200,100); // id,width,height
DivResizer.add("IDofDiv2",0,120);
DivResizer.add("IDofDiv3",10,0);

// To resize your div on pageload, call following method once at end of document.
DivResizer.ResizeAll();

*/


// you can use this variables on your site for browser specific calculations
var ie = true;
var ie6 = false;
var moz = false;
var op = false;

if(navigator.userAgent.indexOf("MSIE 6.0") >= 0){
	ie = true;
	ie6 = true;
}
else if(navigator.userAgent.indexOf("Firefox") >= 0){
	ie = false;
	moz = true;
}
else if(navigator.userAgent.indexOf("Opera") >= 0)
{
	ie = false;
	op = true;
}

var DivResizer = {

  /* constructor */
  initialize : function() {
		this.arr = new Array();
		this.harr = new Array();
		this.warr = new Array();
		this.minwarr = new Array();
		this.minharr = new Array();
		this.windowHeight = 0;
		this.windowWidth = 0;
		this.calculateWindow();
		//Event.observe(window, 'resize', DivResizer.resizeAll, false);
   },
   
	/* add new div */	
   add : function(divid,width,height,minw,minh){
		this.arr.push(divid);
		this.warr.push(width);
		this.harr.push(height);
		
		if(minw == null)
		    minw = 0;
		    
		if(minh == null)
		    minh = 0;
		
		this.minwarr.push(minw);
		this.minharr.push(minh);
   },
   
	/* remove existing div */
	remove : function(divid){
		this.arr = this.arr.without(divid);
	},
	
	/* resize all added divs */
	resizeAll : function(){
		
		this.calculateWindow();
		
		for(var i=0;i<this.arr.length;i++)
		{
			this.setDimensions(this.arr[i],this.warr[i],this.harr[i], this.minwarr[i], this.minharr[i]);
		}
	},
	
	/* resize a specific div with special width and height */
	setDimensions : function(divid,width,height,minw,minh){
		
		var div = $(divid);
		
		if(div != null)
		{
			
			//div.style.overflow = "auto";
			
			// height	
			if(height > 0)
				if(this.windowHeight > 0 && this.windowHeight - height > minh)
				{
					div.style.height = (this.windowHeight - height) + "px";
					div.height = (this.windowHeight - height);
				}
			
			// width
			if(width > 0)
				if(this.windowWidth > 0 && this.windowWidth - width > minw){
					div.style.width = (this.windowWidth - width) + "px";
					div.width = (this.windowWidth - width);
				}
		}
	},
	
	/* true wenn div schon im divresizer existiert */
	exists : function(divid)
	{
		for(var i=0;i<this.arr.length;i++)
		    if(this.arr[i] == divid)
		        return true;
		        
		return false;
	},
	
	/* get dimensions of window */
	calculateWindow : function()
	{
		this.windowHeight = this.getWindowHeight();
		this.windowWidth = this.getWindowWidth();
	},
	
	/* get height of window */
	getWindowHeight : function(){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myHeight = window.innerHeight;
			ie = false;
		} else if( document.documentElement &&
			( document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myHeight = document.documentElement.clientHeight;
		  
		} else if( document.body &&  document.body.clientHeight ) {
			//IE 4 compatible
			myHeight = document.body.clientHeight;
		}
		  
		return myHeight;
	},
	
	/* get width of window */
	getWindowWidth : function(){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
		} else if( document.documentElement &&
			( document.documentElement.clientWidth ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
		} else if( document.body &&  document.body.clientWidth ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
		}
  
		 return myWidth;
	}
	
}


/* init */
DivResizer.initialize();

/* helper method */
function resize2()
{
	DivResizer.resizeAll();
}

/* register on windowresize */
if(Event)
    if(Event.observe)
        Event.observe(window, 'resize', resize2, false);
        
        
window.addEvent('resize', function() {
	DivResizer.resizeAll();
});
