var isIE = (document.getElementById && document.all)?true:false;
var isNS4 = (document.layers)?true:false;
var isNS6 = (document.getElementById && !document.all)?true:false;
function myGetElementById(id) {
	if ( isIE ) {
		var str = "document.all('" + id + "')";
		var o = eval(str);
		return o;
	}
	return document.getElementById(id);
}

// these globals that can be used from other routines
var pageWidth;
var pageHeight;

function findDimensions() { // Resize background -> #background / #background_image
try{ 
    
	var frameWidth;
    var frameHeight;
    var obj;
	
    if (document.documentElement && document.documentElement.clientWidth && !(navigator.appVersion.indexOf('Safari') != -1) ) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	} else if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
    } else if (document.body) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
    } else
		return;

    // if there's valid info, update the globals so they can be used elsehere
    pageWidth = frameWidth;
    pageHeight = frameHeight;
	
    if ( (obj = myGetElementById('background')) ) {
		obj.style.width = frameWidth+'px';
		obj.style.height = frameHeight+'px';
    }

    if ( (obj = myGetElementById('background_image')) ) {
		Img = new Image();
		if(obj.src.indexOf('pix.gif') >= -1 && Home){
			//console.log("here");
			//Img.src = path_DOSSIER_SQUELETTE+'/images/pictures/'+g_ImageTable[0][0];
			Img.src = g_ImageTable[0][0];
			//console.log(g_ImageTable[0][0]);
		} else {
			Img.src = obj.src;
		}
		
		Img.onload = function(){
			fixImage(obj, Img, frameWidth, frameHeight);
		}
		
		fixImage(obj, Img, frameWidth, frameHeight);
    }
	setTimeout("findDimensions()",5000);
  
  } catch(err){ 
	    var text= document.createTextNode('<!--  Erreur js (resize.js)'+err+'  -->');
	    document.body.appendChild(text);
	    // alert('Erreur JS (resize.js): '+err); 
  }
}

function fixImage(obj, Img, frameWidth, frameHeight){
	//console.log(Img.src);
	//console.log(obj.src);
	//console.log(' ' + Img.width + ' ' + Img.height);
	
	//if(Img.width == 0 && Img.height == 0) findDimensions();
	
	var iWidth = Img.width;
	var iHeight = Img.height;
	var iRatio = iWidth / iHeight;
	var fRatio = frameWidth / frameHeight;
	
	// reset to default position on resize/load
	obj.style.top = obj.style.left = 0;
	
	/*if(!Home) {
		obj.style.width = frameWidth+'px';
		obj.style.height = (frameWidth / iRatio)+'px';
	} else {*/
	//console.log(' ' + iRatio + ' ' + (16/9) + ' ' + (4/3));
	if (Math.round(iRatio) >= Math.round('16/9')) {
		if ( iRatio < fRatio ) {
			obj.style.height = frameHeight+'px';
			obj.style.width = (frameHeight * iRatio)+'px';
		} else {
			obj.style.width = frameWidth+'px';
			obj.style.height = (frameWidth / iRatio)+'px';
		}
	} else {
		if ( iRatio < fRatio ) {
			obj.style.width = frameWidth+'px';
			obj.style.height = (frameWidth / iRatio)+'px';
		} else {
			obj.style.height = frameHeight+'px';
			obj.style.width = (frameHeight * iRatio)+'px';
		}
	}
}

addLoadEvent(findDimensions); // Resize background -> #background / #background_image
addResizeEvent(findDimensions);

