
//The first version of the Sizing Library

function getWindowHeight() {
        // I think this function really needs improvement
        info=browserInfo();
        if (info.browserShortName=="IE")
			if (info.browserVersion=="7.0") {
				if (document.documentElement.clientHeight!=0) // this is a really bad solution. Have to dive into this
					return document.documentElement.clientHeight; // if change to clientWidth, the whole page will extend its height producing a scrollbar on the left.. 
				else
					return document.body.clientHeight;
			}
        IE=browserSnipper();
        if(!IE) // We're not sure whether this is right, but it works for us now
                return window.innerHeight;

        if (document.body && typeof(document.body.clientHeight)=="number") { //dexterT: changed a bit, sometimes document.body.clientHeight returns zero for ie6
			if(document.body.clientHeight != 0) {
				return document.body.clientHeight;
			} else {
				return document.documentElement.clientHeight;
			}
        } else {
                if (typeof(window.innerHeight)=="number") {
                        //Non-IE
                        return window.innerHeight;
                  }
                else {
                        if (document.documentElement && document.documentElement.clientHeight) {
                                //IE 6+ in 'standards compliant mode'
                                return document.documentElement.clientHeight;
                         }
                        else {
                                if (document.body && document.body.clientHeight) {
                                        //IE 4 compatible
                                        return document.body.clientHeight;
                                }
                                else {
                                        writeStatus("Browser not supported");
                                        return 800;
                                }
                         }
                  }
        }
 }
 
 
 function getWindowWidth() {

        if (document.body && typeof(document.body.clientWidth)=="number")
                return document.body.clientWidth;
        else {
                if (typeof(window.innerWidth)=="number") {
                        //Non-IE
                        return window.innerWidth;
                  }
                else {
                        if (document.documentElement && document.documentElement.clientWidth) {
                                //IE 6+ in 'standards compliant mode'
                                return document.documentElement.clientWidth;
                         }
                        else {
                                if (document.body && document.body.clientWidth) {
                                        //IE 4 compatible
                                        return document.body.clientWidth;
                                }
                                else {
                                        writeStatus("Browser not supported");
                                        return 800;
                                }
                        }
                  }
        }
 }
 
 
 function scaleToHeight(obj, height) {
        // Maybe better check the style width and height, hopefully to be added later
        if (objectReady(obj)) {
                // First we check if we have an orgWidth and orgHeight already
                if ((typeof(obj.orgWidth)!="number")||(typeof(obj.orgHeight)!="number")) {
                        // We have to get orgWidth and orgHeight
                        if ((typeof(obj.width)=="number")&&(typeof(obj.height)=="number")) {
                                // This seems to be the easiest way, just check width and height
                                obj.orgWidth=obj.width;
                                obj.orgHeight=obj.height;
                        }
                        else { // No width and height, check something else
                                if ((typeof(obj.offsetWidth)=="number")&&(typeof(obj.offsetHeight)=="number")) {
                                        obj.orgWidth=obj.offsetWidth;
                                        obj.orgHeight=obj.offsetHeight;
                                }
                                else {
                                        return false;
                                }
                        }
                }
                // We are supposed to have an orgWidth and orgHeigth
                obj.style.height=Math.round(height)+"px";
                obj.style.width=Math.round(height/obj.orgHeight*obj.orgWidth)+"px";
                return true;
        }
        else
                return false;
}
 

function scaleToWidth(obj, width) {
	if (!(typeof(obj.myFlash)=="boolean")) {
		if (typeof(obj)!="object") {
			window.alert("scaleToWidth not called with object. Type of obj parameter: "+typeof(obj));
			return true;
		}
	}
	if (obj.src=="Standard.jpg")
		debugger;
	// First check for IE and PNG
	if (browserInfo().browserShortName=="IE") {
		if (typeof(obj.myPNG)=="boolean") {
			if (typeof(obj.orgWidth)=="number") {
		        obj.style.width=Math.round(width)+"px";
				obj.style.height=Math.round(width/obj.orgWidth*obj.orgHeight)+"px";
				return true;
			}
			else
				return false;
		}
	}

	// Now see if we already figured something out
	// Parenthesis start here
	if ((typeof(obj.orgWidth)!="number")||(typeof(obj.orgHeight)!="number")) { // We didn't
            // First check if we have a offsetWidth and offsetHeight
         	if ((typeof(obj.offsetWidth)=="number")&&(typeof(obj.offsetHeight)=="number")) {
                 	if ((obj.offsetWidth!=0)&&(obj.offsetHeight!=0)) { //check if the offsetWidth and offsetHeight not equal to zero
                         	if (obj.offsetHeight>obj.offsetWidth) {// usually our picture the width is greater than height
                                 	if (!objectReady(obj)) {  //if the height is greater i check if the object is fully loaded to  get the accurate height and width
                                         	return false;
                                         }
                                         else {
                                         	obj.orgWidth=obj.offsetWidth;
                                                obj.orgHeight=obj.offsetHeight;
                                         }
                                 }
                                 else {
                                	obj.orgWidth=obj.offsetWidth;
                                        obj.orgHeight=obj.offsetHeight;
                                  }
                         }
                         else {
                         	if ((obj.width!=0)&&(obj.height!=0)) {   //check if we have a width and height
                                 	obj.orgWidth=obj.width;
                                         obj.orgHeight=obj.height;
                                 
                        	}
                                 else
                                 	return false;
                }
         }
         else 
		if ((typeof(obj.width)=="number")&&(typeof(obj.height)=="number")) {
                        // Otherwise maybe we have a width and a height
                        // They might be 0
       	       		if ((obj.width!=0)&&(obj.height!=0)) {
                    		obj.orgWidth=obj.width;
                                obj.orgHeight=obj.height;
	        	}
                	else {
	                     //modified robert try to figure out the scaling problem
        	             //check if the object has a style.width and height check the px
                	     if(typeof(obj.style.width)=="string") {
                        	       var str, i, tempWidth="", tempHeight;
	                               str=obj.style.width;
        	                       //we have to take out the px
                	              //take out the px for the width
                        	      //for the width
	                               for(i=0;i<str.length-2;i++) {
                                       tempWidth+=str.charAt(i);
                               }
                              //for the height
                              //take out the for the height
                              str=obj.style.height;
                              for(i=0;i<str.length-2;i++) {
                                         tempHeight+=str.charAt(i);
                              }
                              window.status="we arrive here";
                              obj.orgWidth=tempWidth;
                              obj.orgHeight=tempHeight;
                       }
                 else {
                         // We could do some more things to find height and width;
				window.alert("We should never arrive here");
                                return false;
                      }
               }
        }
        else {
             if (!objectReady(obj)) { //wait until the object is ready
                            return false;
             }
        }
         }
	obj.style.width=Math.round(width)+"px";
    obj.style.height=Math.round(width/obj.orgWidth*obj.orgHeight)+"px";
    return true;
}


function scaleObject(maxW, maxH, obj) {

        if (scaleToWidth(obj, maxW)) {
                if (obj.offsetHeight>maxH) {
                        // we have to scale to the height instead of the width
                        obj.style.height=Math.round(maxH)+"px";
                        obj.style.width=Math.round(maxH/obj.orgHeight*obj.orgWidth)+"px";
                }
                return true;
        }
        else
                return false;
}


function resizeFunc() {

        var resized;

        resized=resizeBasic();
	
        resized=resized&&(resizeNonBasic());

        if (!resized) {
                window.setTimeout(resizeFunc, 500);
                writeStatus("Loading...");
        }
        else
			writeStatus("Done");
}


function clearMargins(obj) {
	obj.style.margin="0";
	obj.style.padding="0";
	obj.style.border="none";
}


function resizeWindow(win, x, y, w, h) {

        // Only supports current window (or frame) with empty string.
        // only supports x/y and w/h pairs
        window.resizeBy(x,y);
        if (w < 100)
                w=100;
        if (h < 100)
                h=100;
        window.resizeTo(w,h);
        return win;
}


function resetSize(obj) {

        obj.style.width="auto";
        obj.style.height="auto";
        obj.orgWidth="a";
        obj.orgHeight="a";
}