// references to resizers.
var oR1, oR2, oR3, oR4

// window dimensions ... 
// kept updated on resize by grabWindowDimensions()
var iWinW, iWinH;
window.onresize = grabWindowDimensions;


// references to frames and resizers.
var oFrMenu, oFrLivraison, oFrImmobilier, oFrFinancier, oFrDocumentation;
var oR1, oR2, oR3, oR4;


// animation constants
var TARGET_CPU_RESOLUTION = 8;
var ANIMATION_LENGTH = 400;
var iAnimationStartTime;
var iAnimationId;

// the order the frame will slide open or closed in
var ANIMATION_FRAME_ORDER = [2,1,4,3];

// we can define a layout as the position of each of 
// the resize handles from their origins. this probabaly
// isn't the best way to do it, but oh well...

// these are the layouts that will be used for a 
// section if the user has never customized the page
// he is on.
var DEFAULT_LAYOUTS = {
	infos			:[184,245,29 ,4  ],// 
	presentation	:[29 ,4  ,29 ,4  ],// articles
	plans			:[140,275,140,4  ],// plans
	photos			:[29 ,4  ,29 ,210],// 
	mentions		:[29 ,4  ,29 ,275],// 
	contact			:[29 ,4  ,100,275],// 
	program			:[160,219,29 ,4  ],// 
	news			:[29 ,310,29 ,4  ],// lois fiscales
	gestion			:[29 ,250,29 ,4  ]// gestionnaires
	
// 1 = livraison (hauteur)
// 2 = menu (largeur)
// 3 = tarifs (hauteur)
// 4 = Documentation (largeur)

}

// a little bit of IE emulation for Moz to make my life easier
// thanks to webfx for the tip -- http://webx.eae.net
//
// grrrr! this only works in moz .9.2 7/26/2001 build or later.
// some versions of ns 6.1 shipped with .9.1 even! - so this won't work on those.
// will probably have to make it a prototype of Object object at some point for compatibility (menu)
if (typeof CSSStyleDeclaration != 'undefined' && !CSSStyleDeclaration.prototype.pixelLeft)
{
	CSSStyleDeclaration.prototype.__defineSetter__("pixelTop", function(iPx) { this.top = iPx + "px"; });
	CSSStyleDeclaration.prototype.__defineSetter__("pixelLeft", function(iPx) { this.left = iPx + "px"; });
	CSSStyleDeclaration.prototype.__defineSetter__("pixelRight", function(iPx) { this.right = iPx + "px"; });
	CSSStyleDeclaration.prototype.__defineSetter__("pixelBottom", function(iPx) { this.bottom = iPx + "px"; });
	CSSStyleDeclaration.prototype.__defineSetter__("pixelWidth", function(iPx) { this.width = iPx + "px"; });
	CSSStyleDeclaration.prototype.__defineSetter__("pixelHeight", function(iPx) { this.height = iPx + "px"; });

	CSSStyleDeclaration.prototype.__defineGetter__("pixelTop", function() { return parseInt(this.top); });
	CSSStyleDeclaration.prototype.__defineGetter__("pixelLeft", function() { return parseInt(this.left); });
	CSSStyleDeclaration.prototype.__defineGetter__("pixelRight", function() { return parseInt(this.right); });
	CSSStyleDeclaration.prototype.__defineGetter__("pixelBottom", function() { return parseInt(this.bottom); });
	CSSStyleDeclaration.prototype.__defineGetter__("pixelWidth", function() { return parseInt(this.width); });
	CSSStyleDeclaration.prototype.__defineGetter__("pixelHeight", function() { return parseInt(this.height); });

	HTMLElement.prototype.__defineGetter__("currentStyle", function () {
		var cs = {};
		var el = this;
		for (var i = 0; i < properties.length; i++) {
			cs.__defineGetter__(properties[i], encapsulateObjects(el, properties[i]));
		}
		return cs;
	});

	function encapsulateObjects(el, sProperty) {
		return function () {
			return document.defaultView.getComputedStyle(el, null).getPropertyValue(sProperty);
		}
	}
}


// starts it up, yo.
function initFrames()
{
	grabWindowDimensions();
	
	oR1 = document.getElementById("resizer1");
	oR2 = document.getElementById("resizer2");
	oR3 = document.getElementById("resizer3");
	oR4 = document.getElementById("resizer4");

	oFrDocumentation = document.getElementById("documentation");
	oFrMenu = document.getElementById("menu");
	oFrLivraison	 = document.getElementById("livraison");
	oFrImmobilier		 = document.getElementById("immobilier");
	oFrFinancier		 = document.getElementById("financier");

	// setup resizers and establish static limits
	Dov.init(oR1, null, 11, 11, 29, null, false, true);
	Dov.init(oR2, null, 4, null, 159, 159, false, false);
	Dov.init(oR3, null, null, null, 29, null, false, true);
	Dov.init(oR4, null, 4, null, 159, 159, true, false);

	// highlight
	for (var i = 1; i <= 4; i++)
	{
		var r = window["oR"+i];
		r.onmouseover	= highlightOn;
		r.onmouseout	= highlightOff;
		r.onDovStart	= window["setR"+i+"Limits"];
		r.onDov		= window["moveR"+i];
		r.onDovEnd		= endResizerDov;
	}

	// hook up display checking
	// note that .onresize is not a browser-provided 
	// event for most of these elements
	var a = [oFrDocumentation, oFrMenu, oFrLivraison, 
			 oFrImmobilier, oFrFinancier, oR4, oR3, oR2, oR1];

	for (var i = 0; i < a.length; i++)
	{
		a[i].onresize = checkDisplay;
	}

	// if this is undefined, but the browser is moz, then the version is too low.
	// alert(typeof oFrImmobilier.style.pixelLeft);
}; 
//
// see that little semi-colon after the closing brace??
// it proves that i'm cool.
// so there.


function highlightOn()
{
	this.over = true;
	this.style.backgroundColor = "#D07345";
};

function highlightOff()
{
	this.over = false;
	if (Dov.obj != this) this.style.backgroundColor = "#CCCCCC";
};

function endResizerDov()
{
	if (!this.over) this.onmouseout();
	persistLayout();
};


// turns an element off if it's width or height gets below 0,
// and back on when it both are atleast 1.

// unfortunately 'width' is determined by a number of factors, and sometimes even
// unknown (but non-zero). so this process is a bit convoluted
function checkDisplay()
{
	var hOk = true;
	var vOk = true;

	// if current width or height are know, it's easy.
	if (this.style.width != "" && this.style.pixelWidth <= 0) hOk = false;
	if (this.style.height != "" && this.style.pixelHeight <= 0) vOk = false;

	// otherwise, if left and right are known (or top and bottom) do subtraction
	if (this.style.left != "" && this.style.right != "")
	{
		if (iWinW - this.style.pixelLeft - this.style.pixelRight - (this.tagName == "IMG" ? 0 : 2) <= 0)
			hOk = false;
	}

	if (this.style.top != "" && this.style.bottom != "")
	{
		if (iWinH - this.style.pixelTop - this.style.pixelBottom - (this.tagName == "IMG" ? 0 : 2) <= 0)
			hOk = false;
	}
	// otherwise we cannot determine, so we will leave it on.

	this.style.display = hOk && vOk ? "block" : "none" ;
};





function setR1Limits(x, y)
{
	oR1.maxY = iWinH - 177;	// modif de 120 - nav102+18=120 : 159+18=177 (ou 196)
};

function setR2Limits(x, y)
{
	oR2.maxX = iWinW - oFrDocumentation.style.pixelWidth - 22;
};

function setR3Limits(x, y)
{
	oR3.minX = x;
	oR3.maxX = x;
	oR3.maxY = iWinH - 177;	// modif de 120 - nav102+18=120 : 159+18=177 (ou 196)
};

function setR4Limits(x, y)
{
	oR4.maxX = iWinW - oFrMenu.style.pixelWidth - 22;
};






function moveR1(x, y)
{
	oFrLivraison.style.pixelHeight = y - 38;
	oFrMenu.style.pixelBottom = y - 29; // modif de y + 6;
	oFrMenu.onresize();
	oFrLivraison.onresize();
};

function moveR2(x, y)
{
	// ugly, but optimized like crazy.
	oR1.style.pixelWidth = x - 13 > 0 ? x - 13 : 0 ;
	oFrLivraison.style.pixelWidth = oR1.style.pixelWidth;
	oFrMenu.style.pixelWidth = oR1.style.pixelWidth;
		
	// column2
	oFrFinancier.style.pixelLeft = x + 6;
	oFrImmobilier.style.pixelLeft = oFrFinancier.style.pixelLeft;

	oR3.style.pixelLeft = x + 7;

	// check displays
	oR1.onresize();
	oR3.onresize();
	oFrLivraison.onresize();
	oFrMenu.onresize();
	oFrLivraison.onresize();
	oFrImmobilier.onresize();

};

function moveR3(x, y)
{
	oFrImmobilier.style.pixelBottom = y - 29; // modif de y + 6;
	oFrFinancier.style.pixelHeight = y - 38;
	oFrImmobilier.onresize();
	oFrFinancier.onresize();
};

function moveR4(x, y)
{
	// column2
	oR3.style.pixelRight = x + 7;
	oFrFinancier.style.pixelRight = x + 6;
	oFrImmobilier.style.pixelRight = oFrFinancier.style.pixelRight;

	// column3
	oFrDocumentation.style.pixelWidth = x - 13 > 0 ? x - 13 : 0;

	oFrDocumentation.onresize();
	oFrImmobilier.onresize();
	oFrFinancier.onresize();
	oR3.onresize();
};

function grabWindowDimensions()
{
	iWinW = window.innerWidth  ? window.innerWidth  : document.body.clientWidth;
	iWinH = window.innerHeight ? window.innerHeight : document.body.clientHeight;
};




// for storing the layout to a cookie.
// basically just stores the position of each resizer in an
// slot of an array, and joins that array to a string.
// there are (currently) six sections, so there can be six cookies.
function persistLayout()
{
	var sRecord = [
		oR1.style.pixelBottom, oR2.style.pixelLeft,
		oR3.style.pixelBottom, oR4.style.pixelRight
	];

	sRecord = sRecord.join(",");
	createCookie(getSectionName(), sRecord, 365);
};

// for going back to how it was before.
function restoreLayout()
{
	if (iAnimationId) window.clearInterval(iAnimationId);

	var sSectName = getSectionName();
	var cookie = readCookie(sSectName);
	var aRecord = cookie == null ? DEFAULT_LAYOUTS[sSectName] : cookie.split(",");

	oR1.endPixelBottom = aRecord[0];
	oR2.endPixelLeft   = aRecord[1];
	oR3.endPixelBottom = aRecord[2];
	oR4.endPixelRight  = aRecord[3];

	oR1.A = (oR1.endPixelBottom - oR1.style.pixelBottom) / ANIMATION_LENGTH / ANIMATION_LENGTH;
	oR2.A = (oR2.endPixelLeft   - oR2.style.pixelLeft  ) / ANIMATION_LENGTH / ANIMATION_LENGTH;
	oR3.A = (oR3.endPixelBottom - oR3.style.pixelBottom) / ANIMATION_LENGTH / ANIMATION_LENGTH;
	oR4.A = (oR4.endPixelRight  - oR4.style.pixelRight ) / ANIMATION_LENGTH / ANIMATION_LENGTH;

	iAnimationStartTime = (new Date()).getTime();
	iAnimationId = window.setInterval("animateLayout(0)", TARGET_CPU_RESOLUTION);
};

// one frame of animation
function animateLayout(iNum)
{
	var elapsed = (new Date()).getTime() - iAnimationStartTime;

	while (window["oR"+ANIMATION_FRAME_ORDER[iNum]].A == 0 && iNum < 3) iNum++
	if (iNum == 4) return;

	if (elapsed >= ANIMATION_LENGTH) 
	{
		window.clearInterval(iAnimationId);
		elapsed = ANIMATION_LENGTH;
		iAnimationId = null;
	}

	var t = (ANIMATION_LENGTH - elapsed)*(ANIMATION_LENGTH - elapsed);
	
	switch (ANIMATION_FRAME_ORDER[iNum]) {
		case 1:
			oR1.style.pixelBottom = Math.round(oR1.endPixelBottom - t * oR1.A);
			moveR1(null, oR1.style.pixelBottom);
			break;
		case 2:
			oR2.style.pixelLeft   = Math.round(oR2.endPixelLeft - t * oR2.A);
			moveR2(oR2.style.pixelLeft, null);
			break;
		case 3:
			oR3.style.pixelBottom = Math.round(oR3.endPixelBottom - t * oR3.A);
			moveR3(null, oR3.style.pixelBottom);
			break;
		case 4:
			oR4.style.pixelRight  = Math.round(oR4.endPixelRight - t * oR4.A);
			moveR4(oR4.style.pixelRight, null);		
			break;
	}


	if (elapsed >= ANIMATION_LENGTH && iNum < 3) 
	{
		iAnimationStartTime = (new Date()).getTime();
		iAnimationId = window.setInterval("animateLayout("+(++iNum)+")", TARGET_CPU_RESOLUTION);
	}
};


function getSectionName()
{
	var sUrl = window.frames["buffer"].location.search;
	var aChoices = ["infos","presentation","plans","photos","mentions","contact","program","news","gestion"];
	var sSectionName = "program";
	var i = 0;

	for ( ; i < aChoices.length; i++) {
		if (sUrl.indexOf("?fram="+aChoices[i]) == 0) {

			sSectionName = aChoices[i];
			break;
		}
	}

	return sSectionName;
};


// an unfortuante bug in gecko causes little pieces of the dragged items
// to be left all over the screen. resizing fixes it.
function paintGecko()
{
	if (navigator.userAgent.toLowerCase().indexOf("gecko") != -1)
	{
		//window.resizeBy(0,1);
		//window.resizeBy(0,-1);
	}
};

// restoreLayout();
