//
// WinPop
// Classe de gestion du popup et du formulaire de contact
// v1.0 - 03/2008
// F.K Creations
//

//
// Constructeur
//
var net = new Object();
net.READY_STATE_UNINITIALIZED=0;
net.READY_STATE_LOADING=1;
net.READY_STATE_LOADED=2;
net.READY_STATE_INTERACTIVE=3;
net.READY_STATE_COMPLETE=4;
net.FRAMERATE=20;
net.FAST_FRAMERATE=1;
net.FACTOR=15;
net.DELAY=300;
var idTimer_1;
var idTimer_2;
var rapport = 1.0/4.0;  // On divise par 20
var mini = 1*rapport;

net.WinPop = function (id_, postParams, winWidth_, winHeight_) {
	this.id=id_;
	var overlayTargetName='overlay';
	this.req=null;
	this.params = postParams;
	this.opacity = 0;
	this.overlayOpacity = 80;
	this.width = 16;
	this.height = 0;
	this.winWidth = winWidth_;
	this.winHeight = winHeight_;
	this.loadingImage = 'themes/images/spinner.gif';
	this.onload=this.displayContent;
	this.onerror= this.defaultError;
	this.create();
}

//
// Procedures
//
net.WinPop.prototype={
	create:function() {
		var currentObj=this;
		var objBody = document.getElementsByTagName("body").item(0);
		var arrayPageSize = getPageSize(); 
		var arrayPageScroll = getPageScroll();

		/* Creation and properties of the overlay layer */
		var objOverlay = document.createElement("div");
		objOverlay.setAttribute('id','overlay');
		objOverlay.onclick = function () {net.WinPop.prototype.hideOverlay(currentObj); return false;}
		objOverlay.style.display = 'block';
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '90';
		objOverlay.style.width = '100%';
		objBody.appendChild(objOverlay);
		objOverlay.style.height = arrayPageSize[1] + 'px';
		objOverlay.style.display = 'block';		
		
		// Show overlay
		net.WinPop.prototype.showOverlay(currentObj);	
		
		// Creation de la boite de chargement
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = this.loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.display = 'none';
		objLoadingImage.style.zIndex = '100';
		objLoadingImage.style.top = (arrayPageScroll.top + ((arrayPageSize[3] - 8 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] + 5 - objLoadingImage.width) / 2) + 'px');
		objBody.appendChild(objLoadingImage);		
	},
	displayWindow:function() {
		var objBody = document.getElementsByTagName("body").item(0);
		var objContent = document.createElement("div");
		objContent.setAttribute('id','overlayWindow');
		objContent.style.display = 'none';
		objContent.style.position = 'absolute';
		objContent.style.top = '0';
		objContent.style.width = '0px';
		objContent.style.height = '0px';
		objContent.style.left = '0';
		objContent.style.zIndex = '95';
		objContent.style.width = this.winWidth+'px';
		objContent.style.height = this.winHeight+'px';
		objBody.appendChild(objContent);
		net.WinPop.prototype.openOverlayWindow(this);
	},
	onReadyState:function() {
		var req = this.req;
		var ready = req.readyState;
		if (ready==net.READY_STATE_COMPLETE) {
			var httpStatus=req.status;
			if (httpStatus == 200 || httpStatus==0) {
				this.onload.call(this);
			} else {
				this.onerror.call(this);
			}
		} else {
			this.defaultLoading();
		}
	},
	defaultError:function() {
		alert ("error fetching data!"
			   +"\n\nreadyState: "+this.req.readyState
			   +"\nstatus: "+this.req.status
			   +"\nheaders: "+this.req.getAllResponseHeaders());
	},
	defaultLoading:function() {
	}
}


net.WinPop.prototype.displayContent = function(obj) {
	target = document.getElementById('overlayWindow');
	new Ajax.Request ("includes/ctrDisplay_winpopup.php",
	{
		method:'post',
		parameters:'section=results&'+obj.params,
		onComplete:function (requester) {
			Element.update(target,requester.responseText);
			net.WinPop.prototype.displayCloseButton(obj);
		}
	});
}

//
// Procedure : showOverlay
//
net.WinPop.prototype.showOverlay = function(obj) {
	var targetOpacity=80;
	target = document.getElementById('overlay');
	if (obj.opacity < targetOpacity) {
		obj.opacity += net.FACTOR;
		if (obj.opacity > targetOpacity) {
			obj.opacity = targetOpacity;
		}
	}
	if (target.filters) {
		try {
			target.filters.item("DXImageTransform.Microsoft.Alpha").opacity = obj.opacity;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			target.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + obj.opacity + ")";
		}
	} else {
		target.style.opacity = obj.opacity / 100;
	}
	if (obj.opacity < targetOpacity) {
		setTimeout(function() { net.WinPop.prototype.showOverlay(obj); }, net.FRAMERATE);
	} else {
		target.style.display = 'block';
		obj.displayWindow();
	}	
}

//
// Procedure : hideOverlay
//
net.WinPop.prototype.hideOverlay = function(obj) {
	var targetOpacity=0;
	
	target = document.getElementById('overlay');
	targetContent = document.getElementById('overlayWindow');
	if (obj.opacity > targetOpacity) {
		obj.opacity -= net.FACTOR;
		if (obj.opacity < targetOpacity) {
			obj.opacity = targetOpacity;
		}
	}
	if (target.filters) {
		try {
			target.filters.item("DXImageTransform.Microsoft.Alpha").opacity = obj.opacity;
			targetContent.filters.item("DXImageTransform.Microsoft.Alpha").opacity = obj.opacity;
		} catch (e) {
			// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
			target.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + obj.opacity + ")";
			targetContent.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + obj.opacity + ")";
		}
	} else {
		target.style.opacity = obj.opacity / 100;
		targetContent.style.opacity = obj.opacity / 100;
	}
	if (obj.opacity > targetOpacity) {
		setTimeout(function() { net.WinPop.prototype.hideOverlay(obj); }, net.FRAMERATE);
	} else {
		Element.remove(target);
		Element.remove(targetContent);
		Element.remove('loadingImage');
		clearInterval(idTimer_1);
		clearInterval(idTimer_2);
	}	
}

//
// Procedure : openOverlayContent
//
net.WinPop.prototype.openOverlayWindow=function(obj) {
	var targetOpacity=obj.overlayOpacity;
	
	target = document.getElementById('overlayWindow');
	if (obj.height < obj.winHeight) {
		obj.height += net.FACTOR;
		if (obj.height > obj.winHeight) {
			obj.height = obj.winHeight;
		}
	}
	else if (obj.width < obj.winWidth) {
		obj.width += net.FACTOR;
		if (obj.width > obj.winWidth) {
			obj.width = obj.winWidth;
		}
	}
	
	if (target)
	{
		var arrayPageSize = getPageSize(); 
		var arrayPageScroll = getPageScroll();
		target.style.left=((arrayPageSize[0]-obj.width)/2)+'px';
		target.style.top=((arrayPageScroll.top+arrayPageSize[1]-obj.height)/2)+'px';
		target.style.width = obj.width+'px';
		target.style.height = obj.height+'px';
		target.style.display = 'block';
	}
	if (obj.width < obj.winWidth || obj.height < obj.winHeight) {
		setTimeout(function() { net.WinPop.prototype.openOverlayWindow(obj); }, net.FAST_FRAMERATE);
	} else {
		
		if (document.getElementById('loadingImage'))
			document.getElementById('loadingImage').style.display='none';
		net.WinPop.prototype.displayContent(obj);
		net.WinPop.prototype.listenScrolling(obj);
	}
}

//
// Procedure : displayCloseButton
//
net.WinPop.prototype.displayCloseButton=function(obj) {
	if (document.getElementById('overlayWindow'))
	{
		var target = document.getElementById('overlayWindow');
		var objCloseButtonTxt = document.createTextNode("fermer");
		var objCloseButton = document.createElement("a");
		objCloseButton.className='winPopupCloseButton';
		objCloseButton.id='closeButton';
		objCloseButton.style.display='block';
		objCloseButton.appendChild(objCloseButtonTxt);
		objCloseButton.onclick=function() {
			net.WinPop.prototype.hideOverlay(obj); return false;
		}
		target.appendChild(objCloseButton);
	}
}

//
// Procedure : listenScrolling
// Initialise l'ecouteur de scrolling, qui positionnera la fenetre toujours en mileu de page
//
net.WinPop.prototype.listenScrolling = function(obj) {
	Obj_divToScroll  = new net.DivScroll('overlayWindow', obj.winHeight, obj.winWidth);
	if(Obj_divToScroll.obj) {
    	idTimer_2 = setInterval('Obj_divToScroll.checkScroll()',10);
	}
}

net.DivScroll = function(id_, winHeight, winWidth){
	var obj = document.getElementById(id_);
	this.obj = obj;
	if(obj){
		obj.style.position = "absolute"; // IMPERATIF
		var arrayPageScroll  = getPageScroll();
		this.winHeight = winHeight;
		this.winWidth = winWidth;
		if (self.innerHeight) {	// all except Explorer		
			this.posX = arrayPageScroll.left + ((arrayPageSize[2]-winWidth)/2);
			this.posY = arrayPageScroll.top + ((arrayPageSize[3]-winHeight)/2);
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			this.posX = arrayPageScroll.left + ((arrayPageSize[2]-winWidth)/2);
			this.posY = arrayPageScroll.top + ((arrayPageSize[3]-winHeight-arrayPageScroll.top)/2);
		}
		this.newX = 0;
		this.newY = 0;
	}
}

net.DivScroll.prototype.checkScroll = function(){
	var arrayPageScroll  = getPageScroll();
	var arrayPageSize = getPageSize();
	
	if (self.innerHeight) {	// all except Explorer		
		var topPos  = ((arrayPageSize[3]-Obj_divToScroll.winHeight)/2);
		var leftPos  = ((arrayPageSize[2]-Obj_divToScroll.winWidth)/2);
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		var topPos  = ((arrayPageSize[3]-Obj_divToScroll.winHeight-arrayPageScroll.top)/2);
		var leftPos  = ((arrayPageSize[2]-Obj_divToScroll.winWidth)/2);
	}
	
	//-- New position  du menu
	Obj_divToScroll.newX = arrayPageScroll.left+leftPos;
	Obj_divToScroll.newY = arrayPageScroll.top+topPos;
	
	//-- Si pas la bonne Position
	if((Obj_divToScroll.posY != Obj_divToScroll.newY)||( Obj_divToScroll.posX != Obj_divToScroll.newX)){
		clearInterval(idTimer_1);
		idTimer_1 = setInterval("net.DivScroll.prototype.divReplace(" + Obj_divToScroll.newX +"," + Obj_divToScroll.newY +")", 1);
	}
	return( true);
}

net.DivScroll.prototype.divDeplace = function(x_, y_){
	if(arguments[0] != null){
		this.posX = x_;
		if (document.getElementById('overlayWindow'))
			document.getElementById('overlayWindow').style.left=parseInt(x_)+'px';
		else
			clearInterval(idTimer_1);
	}
	if(arguments[1] != null){
		this.posY = y_;
		if (document.getElementById('overlayWindow'))
			document.getElementById('overlayWindow').style.top=parseInt(y_)+'px';
		else
			clearInterval(idTimer_1);
	}
}
//---------------------------
net.DivScroll.prototype.divReplace = function(x_,y_){
  //-- Calcul Delta deplacement
	var delta_X = (x_- Obj_divToScroll.posX) *rapport;
	var delta_Y = (y_ - Obj_divToScroll.posY) *rapport;
	//-- Test si fin deplacement
	if((( delta_Y < mini)&&( delta_Y > -mini))&&(( delta_X < mini)&&( delta_X > -mini))){
		clearInterval(idTimer_1);
		Obj_divToScroll.divDeplace(x_, y_);
	}
	else{
		Obj_divToScroll.divDeplace(Obj_divToScroll.posX+delta_X, Obj_divToScroll.posY+delta_Y);
	}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
//
function getPageSize(){
	
	var pageScroll = getPageScroll();
	var xScroll_ie = pageScroll.left;
	var yScroll_ie = pageScroll.top;
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer		
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;		
		windowHeight = document.documentElement.clientHeight+yScroll_ie;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var left;
	var top;
	var docRef;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	if (self.pageXOffset) {
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollLeft){	 // Explorer 6 Strict
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		xScroll = document.body.scrollLeft;
	}
	return({top:yScroll, left:xScroll});
}
