 /************************************************************************************************************
	(C) www.dhtmlgoodies.com, September 2005

	Update log:

	January 18th, 2006: Added feature that makes the window cover selectboxes(ref. problem with windowed and window less elements).
	January 29th, 2006: Fixed problem showing the windows in Opera
	Feb, 8th 2006:  Added support for creating new windows dynamically
	March, 11th, 2006: Added support for getting content from external files by use of Ajax

	This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.

	Terms of use:
	You are free to use this script as long as the copyright message is kept intact. However, you may not
	redistribute, sell or repost it without our permission.

	Thank you!

	www.dhtmlgoodies.com
	Alf Magne Kalleland

	************************************************************************************************************/

	var readSizeFromCookie = false; // Determines if size and position of windows should be set/retreved by use of cookie
	var windowMinSize = [80,30]; // Mininum width and height of windows.

	var moveCounter = -1;
	var startEventPos = new Array();
	var startPosWindow = new Array();
	var startWindowSize = new Array();
	var initResizeCounter = -1;
	var activeWindow = false;
	var activeWindowContent = false;
	var windowSizeArray = new Array();
	var windowPositionArray = new Array();
	var currentZIndex = 10000;
	var windowStateArray = new Array(); // Minimized or maximized
	var activeWindowIframe = false;
	var divCounter = 0;
	var zIndexSet = false;
	var MSIEWIN = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Win')>=0 && navigator.userAgent.toLowerCase().indexOf('opera')<0)?true:false;
	var opera = navigator.userAgent.toLowerCase().indexOf('opera')>=0?true:false;
	var ajaxObjects = new Array();
	// variable ajoutées opar 123webconsulting
	var mouseSet=false;
	var mousex = 0; // position x de la souris, gérée dynamiquement, utilisée pour le positionnement des fenetres
	var mousey = 0; // position y de la souris, gérée dynamiquement, utilisée pour le positionnement des fenetres
	var imgpath='/floating_window/images/';//123webconsulting

	/*
	These cookie functions are downloaded from
	http://www.mach5.com/support/analyzer/manual/html/General/CookiesJavaScript.htm
	*/
	function Get_Cookie(name) {
		 var start = document.cookie.indexOf(name+"=");
		 var len = start+name.length+1;
		 if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
		 if (start == -1) return null;
		 var end = document.cookie.indexOf(";",len);
		 if (end == -1) end = document.cookie.length;
		 return unescape(document.cookie.substring(len,end));
	}
	// This function has been slightly modified
	function Set_Cookie(name,value,expires,path,domain,secure) {
		expires = expires * 60*60*24*1000;
		var today = new Date();
		var expires_date = new Date( today.getTime() + (expires) );
			var cookieString = name + "=" +escape(value) +
				 ( (expires) ? ";expires=" + expires_date.toGMTString() : "") +
				 ( (path) ? ";path=" + path : "") +
				 ( (domain) ? ";domain=" + domain : "") +
				 ( (secure) ? ";secure" : "");
			document.cookie = cookieString;
	}

	function cancelEvent()
	{
		return (moveCounter==-1 && initResizeCounter==-1)?true:false;
	}
	function initMove(e)
	{
		if(document.all)e = event;
		moveCounter = 0;
		switchElement(false,this);
		startEventPos = [e.clientX,e.clientY];
		startPosWindow = [activeWindow.offsetLeft,activeWindow.offsetTop];
		startMove();
		if(!MSIEWIN)return false; else return true;

	}

	function startMove()
	{
		if(moveCounter>=0 && moveCounter<=10){
			moveCounter++;
			setTimeout('startMove()',5);
		}
	}

	function stopMove(e)
	{
		if(document.all)e = event;
		moveCounter=-1;
		initResizeCounter=-1;
		if(!activeWindow || !activeWindowContent)return;
		var state = '0';
		if(windowStateArray[activeWindow.id.replace(/[^0-9]/g,'')])state = '1';

		Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
	}

	function moveWindow(e)
	{
		if(document.all)e = event;
		mousex = (navigator.appName.substring(0,3) == "Net") ? e.pageX : event.x+document.body.scrollLeft;
		mousey = (navigator.appName.substring(0,3) == "Net") ? e.pageY : event.y+document.body.scrollTop;
		if(moveCounter>=10){
			activeWindow.style.left = startPosWindow[0] + e.clientX - startEventPos[0]  + 'px';
			activeWindow.style.top = startPosWindow[1] + e.clientY - startEventPos[1]  + 'px';

		}

		if(initResizeCounter>=10){
			var newWidth = Math.max(windowMinSize[0],startWindowSize[0] + e.clientX - startEventPos[0]);
			var newHeight = Math.max(windowMinSize[1],startWindowSize[1] + e.clientY - startEventPos[1]);
			activeWindow.style.width =  newWidth + 'px';
			activeWindowContent.style.height = newHeight  + 'px';

			if(MSIEWIN && activeWindowIframe){
				activeWindowIframe.style.width = (newWidth) + 'px';
				activeWindowIframe.style.height = (newHeight+20) + 'px';
			}


		}

		if(!document.all)return false; else return true;
	}


	function initResizeWindow(e)
	{
		if(document.all)e = event;
		initResizeCounter = 0;
		switchElement(false,document.getElementById('dhtml_goodies_id' + this.id.replace(/[^\d]/g,'')));

		startWindowSize = [activeWindowContent.offsetWidth,activeWindowContent.offsetHeight];
		startEventPos = [e.clientX,e.clientY];

		if(MSIEWIN)activeWindowIframe = activeWindow.getElementsByTagName('IFRAME')[0];
		startResizeWindow();
		return false;

	}

	function startResizeWindow()
	{
		if(initResizeCounter>=0 && initResizeCounter<=10){
			initResizeCounter++;
			setTimeout('startResizeWindow()',5);
		}
	}

	function switchElement(e,inputElement)
	{
		if(!inputElement)inputElement = this;
		var numericId = inputElement.id.replace(/[^0-9]/g,'');
		var state = '0';
		if(windowStateArray[numericId])state = '1';

		if(activeWindow && activeWindowContent){
			Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);

		}
		currentZIndex = currentZIndex/1 + 1;
		activeWindow = document.getElementById('dhtml_goodies_id' + numericId);
		activeWindow.style.zIndex = currentZIndex;
		activeWindowContent = document.getElementById('windowContent' + numericId);

		Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);
	}

	function hideWindow()
	{
		switchElement(false,document.getElementById('dhtml_goodies_id' + this.id.replace(/[^\d]/g,'')));
		activeWindow.style.display='none';
	}

	function minimizeWindow(e,inputObj)
	{
		if(!inputObj)inputObj = this;
		var numericID = inputObj.id.replace(/[^0-9]/g,'');
		switchElement(false,document.getElementById('dhtml_goodies_id' + numericID));
		var state;
		if(inputObj.src.indexOf('minimize')>=0){
			activeWindowContent.style.display='none';
			document.getElementById('resizeImage'+numericID).style.display='none';
			inputObj.src = inputObj.src.replace('minimize','maximize');
			windowStateArray[numericID] = false;
			state = '0';
		}else{
			activeWindowContent.style.display='block';
			document.getElementById('resizeImage'+numericID).style.display='';
			inputObj.src = inputObj.src.replace('maximize','minimize');
			windowStateArray[numericID] = true;
			state = '1';
		}

		Set_Cookie(activeWindow.id + '_attr',activeWindow.style.left.replace('px','') + ',' + activeWindow.style.top.replace('px','') + ',' + activeWindow.style.width.replace('px','') + ',' + activeWindowContent.style.height.replace('px','') + ',' + activeWindow.style.zIndex + ',' + state,50);

	}
	function initWindows(e,divObj)
	{


		var divs = document.getElementsByTagName('DIV');

		if(divObj){
			var tmpDivs = divObj.getElementsByTagName('DIV');
			var divs = new Array();
			divs[divs.length] = divObj;

			for(var no=0;no<tmpDivs.length;no++){
				divs[divs.length] = tmpDivs[no];
			}
		}

		for(var no=0;no<divs.length;no++){

			if(divs[no].className=='dhtmlgoodies_window'){

				if(MSIEWIN){
					var iframe = document.createElement('IFRAME');
					iframe.style.border='0px';
					iframe.frameborder=0;
					iframe.style.position = 'absolute';
					iframe.style.backgroundColor = '#FFFFFF';
					iframe.style.top = '0px';
					iframe.style.left = '0px';
					iframe.style.zIndex = 100;


					var subDiv = divs[no].getElementsByTagName('DIV')[0];
					divs[no].insertBefore(iframe,subDiv);

				}
				if(divObj){
					divs[no].style.zIndex = currentZIndex;
					currentZIndex = currentZIndex /1 + 1;
				}
				divCounter = divCounter + 1;
				if(divCounter==1)activeWindow = divs[no];
				divs[no].id = 'dhtml_goodies_id' + divCounter;
				divs[no].onmousedown = switchElement;
				if(readSizeFromCookie)var cookiePos = Get_Cookie(divs[no].id + '_attr') + ''; else cookiePos = '';
				if(divObj)cookiePos='';
				var cookieValues = new Array();


				if(cookiePos.indexOf(',')>0){
					cookieValues = cookiePos.split(',');
					if(!windowPositionArray[divCounter])windowPositionArray[divCounter] = new Array();
					windowPositionArray[divCounter][0] = Math.max(0,cookieValues[0]);
					windowPositionArray[divCounter][1] = Math.max(0,cookieValues[1]);
				}

				if(cookieValues.length==5 && !zIndexSet){
					divs[no].style.zIndex = cookieValues[4];
					if(cookieValues[4]/1 > currentZIndex)currentZIndex = cookieValues[4]/1;
				}
				if(windowPositionArray[divCounter]){
					divs[no].style.left = windowPositionArray[divCounter][0] + 'px';
					divs[no].style.top = windowPositionArray[divCounter][1] + 'px';
				}

				var subImages = divs[no].getElementsByTagName('IMG');
				for(var no2=0;no2<subImages.length;no2++){
					if(subImages[no2].className=='resizeImage'){
						subImages[no2].style.cursor = 'nw-resize';
						subImages[no2].onmousedown = initResizeWindow;
						subImages[no2].id = 'resizeImage' + divCounter;
						subImages[no2].alt = ">>";
						break;
					}
					if(subImages[no2].className=='closeButton'){
						subImages[no2].id = 'closeImage' + divCounter;
						subImages[no2].onclick = hideWindow;
						subImages[no2].alt = "[X]";
					}
					if(subImages[no2].className=='minimizeButton'){
						subImages[no2].id = 'minimizeImage' + divCounter;
						subImages[no2].onclick = minimizeWindow;
						subImages[no2].alt = "[-]";
						if(cookieValues.length==6 && cookieValues[5]=='0'){
							setTimeout('minimizeWindow(false,document.getElementById("minimizeImage' + divCounter + '"))',10);
						}
						if(cookieValues.length==6 && cookieValues[5]=='1'){
							windowStateArray[divCounter] = 1;
						}


					}
				}
			}
			if(divs[no].className=='dhtmlgoodies_windowMiddle' || divs[no].className=='dhtmlgoodies_window_bottom'){
				divs[no].style.zIndex = 1000;

			}
			if(divs[no].className=='dhtmlgoodies_window_top'){
				divs[no].onmousedown = initMove;
				divs[no].id = 'top_bar'+divCounter;
				divs[no].style.zIndex = 1000;

			}

			if(divs[no].className=='dhtmlgoodies_windowContent'){
				divs[no].id = 'windowContent'+divCounter;
				divs[no].style.zIndex = 1000;
				if(cookieValues && cookieValues.length>3){
					if(!windowSizeArray[divCounter])windowSizeArray[divCounter] = new Array();
					windowSizeArray[divCounter][0] = cookieValues[2];
					windowSizeArray[divCounter][1] = cookieValues[3];
				}
				if(cookieValues && cookieValues.length==5){
					activeWindowContent = document.getElementById('windowContent' + divCounter);
				}
				if(windowSizeArray[divCounter]){
					divs[no].style.height = windowSizeArray[divCounter][1] + 'px';
					divs[no].parentNode.parentNode.style.width = windowSizeArray[divCounter][0] + 'px';

					if(MSIEWIN){
						iframe.style.width = (windowSizeArray[divCounter][0]) + 'px';
						iframe.style.height = (windowSizeArray[divCounter][1]+20) + 'px';
					}
				}
			}


		}

		if(!divObj){
			document.body.onmouseup = stopMove;
			document.body.onmousemove = moveWindow;
			document.body.ondragstart = cancelEvent;
			document.body.onselectstart = cancelEvent;
		}

		return divCounter;
	}

function createNewWindow(width,height,leftpos,toppos)
	{
		var div = document.createElement('DIV');
		div.className='dhtmlgoodies_window';
		document.body.appendChild(div);

		var topDiv = document.createElement('DIV');
		topDiv.className='dhtmlgoodies_window_top';
		div.appendChild(topDiv);

		var img = document.createElement('IMG');
		img.src = imgpath+'/top_left.gif';
		img.align='left';
		topDiv.appendChild(img);

		var img = document.createElement('IMG');
		img.src = imgpath+'/top_center.gif';
		img.className='topCenterImage';
		topDiv.appendChild(img);

		var buttonDiv = document.createElement('DIV');
		buttonDiv.className='top_buttons';
		topDiv.appendChild(buttonDiv);

		var img = document.createElement('IMG');
		img.src = imgpath+'/minimize.gif';
		img.className='minimizeButton';
		img.alt='[-]';
		buttonDiv.appendChild(img);

		var img = document.createElement('IMG');
		img.src = imgpath+'/close.gif';
		img.className='closeButton';
		img.alt='[X]';
		buttonDiv.appendChild(img);

		var img = document.createElement('IMG');
		img.src = imgpath+'/top_right.gif';
		buttonDiv.appendChild(img);

		var middleDiv = document.createElement('DIV');
		middleDiv.className='dhtmlgoodies_windowMiddle';
		div.appendChild(middleDiv);

		var contentDiv = document.createElement('DIV');
		contentDiv.className='dhtmlgoodies_windowContent';
		middleDiv.appendChild(contentDiv);

		var bottomDiv = document.createElement('DIV');
		bottomDiv.className='dhtmlgoodies_window_bottom';
		div.appendChild(bottomDiv);

		var img = document.createElement('IMG');
		img.src = imgpath+'/bottom_right.gif';
		img.className='resizeImage';
		img.alt='>>';
		bottomDiv.appendChild(img);

		var lg = windowSizeArray.length;
		if(lg == 0){
			lg=1;
		}
		windowSizeArray[lg] = [width,height];

		windowPositionArray[lg] = [leftpos,toppos];

		return initWindows(false,div);
	}

	function showAjaxContent(ajaxIndex,windowId)
	{
		try {
			document.getElementById('windowContent' + windowId).innerHTML = ajaxObjects[ajaxIndex].response;
		} catch (e) {}
	}

	function addContentFromUrl(url,windowId)
	{
		var ajaxIndex = ajaxObjects.length;
		ajaxObjects[ajaxIndex] = new sack();
		ajaxObjects[ajaxIndex].requestFile = url; // Specifying which file to get
		ajaxObjects[ajaxIndex].onCompletion = function (){ showAjaxContent(ajaxIndex,windowId); }; // Specify function that will be executed after file has been found
		ajaxObjects[ajaxIndex].runAJAX();  // Execute AJAX function
	}

	/* This function illustrates how you can create a new custom window dynamically */
/*
	function customFunctionCreateWindow(urlToExternalFile,width,height,left,toppos) {
		var divId = createNewWindow(width,height,left,toppos);
		document.getElementById('windowContent' + divId).innerHTML = "Partner's, chargement en cours...";
		if(urlToExternalFile)addContentFromUrl(urlToExternalFile,divId);	// Add content from external file
	}

	// fonction créées par 123webconsulting pour pouvoir gérer les fenetres librement
	// creer une fenetre par anticipation afin de pouvoir la gérer ultérieurement
	function myCreateWindow(urlToExternalFile,width,height,leftpos,toppos,theId,isVisible) {
*/
	function customFunctionCreateWindow(urlToExternalFile,width,height,leftpos,toppos,theId,isVisible,isCentered,useMouse) {
//alert("* "+urlToExternalFile+" - "+width+" - "+height+" - "+leftpos+" - "+toppos+" - "+theId+" - "+isVisible+" *")
		if (typeof isVisible == 'undefined') isVisible=true;
		if (typeof useMouse == 'undefined') useMouse=true;
		if (typeof isCentered == 'undefined') isCentered=false;
		if (typeof width =='undefined'
			|| typeof height =='undefined'
			|| (typeof leftpos =='undefined' && !isCentered && !useMouse)
			|| (typeof toppos =='undefined' && !isCentered && !useMouse)
		) return;
		if (typeof leftpos == 'undefined') leftpos=0;
		if (typeof toppos == 'undefined') toppos=0;
		var id='';
		if (typeof theId != 'undefined' && theId != '' && theId != 0  ) {
			try {
				// on verifie la présence d'un objet ayant l'id donné pour ne pas le creer 2 fois
				var obj=document.getElementById('dhtml_goodies_id' + theId);
				id=obj.id;
			} catch (e) {
			}
		}
		if (id == '') {
			// si aucun id n'a été donné ou s'il n'existe pas, on le cree
			var divId = createNewWindow(width,height,leftpos,toppos);
//alert("* "+width+" - "+height+" - "+leftpos+" - "+toppos+" *")
			document.getElementById('windowContent' + divId).innerHTML = "Partner's, chargement en cours...";
			if(urlToExternalFile)  addContentFromUrl(urlToExternalFile,divId);
			// on masque la fenetre si précisé à la création
			if (typeof isVisible !='undefined' && !isVisible)	myHideWindow(theId);
			// on repositionne l'ancetre à l'emplacement de la souris
			var larg = (top.document.body.clientWidth);
			var haut = (top.document.body.clientHeight);
			if (isCentered && useMouse ) myMoveWindow(parseInt((larg-width)/2),mousey,divId);
			else if (isCentered) myMoveWindow(parseInt((larg-width)/2),parseInt((haut-height)/2),divId);
			else if (useMouse) myMoveWindow(mousex,mousey,divId);
		}
	}

	// affiche une fenetre creée précédement
	function myShowWindow(theId)	{
		if (typeof theId =='undefined') return
		try {
			if (theId.indexOf("html_goodies_id")>0)
				var obj=document.getElementById(theId);
			else
				 var obj=document.getElementById('dhtml_goodies_id' + theId);
			obj.style.display='block';
		} catch (e) {
		}
	}
	// masque une fenetre creée précédement
	function myHideWindow(theId) {
		if (typeof theId =='undefined') return
		try {
			if (theId.indexOf("html_goodies_id")>0)
				var obj=document.getElementById(theId);
			else
				 var obj=document.getElementById('dhtml_goodies_id' + theId);
			obj.style.display='none';
		} catch (e) {
		}
		/*
		var divs = document.getElementsByTagName('DIV');
		for(var no=0;no<divs.length;no++){
		var id=(divs[no].id);
			if(divs[no].className=='dhtmlgoodies_window'){
				//alert (id + '==dhtml_goodies_id'+theId);
				if (id=='dhtml_goodies_id'+theId) {
					//alert('on ferm');
					divs[no].style.display='none';
					break;
				}
			}
		}
		*/
	}
	// déplace une fenetre creée précédement
	function myMoveWindow(leftpos,toppos,theId)	{
		if (typeof theId =='undefined'
			|| typeof leftpos =='undefined'
			|| typeof toppos =='undefined'
		) return
		try {
			var obj=document.getElementById('dhtml_goodies_id' + theId);
			obj.style.top=toppos+'px';
			obj.style.left=leftpos+'px';
		} catch (e) {
		}
	}
	// modifie le contenue d'une fenetre creée précédement
	function myChangeContent(content,theId)	{
		if (typeof theId =='undefined') return
		try {
			var obj=document.getElementById('windowContent' + theId);
			obj.innerHTML=content
		} catch (e) {
		}
	}
	// ferme une fenetre creée précédement identifiée a partir du nom d'une image qu'elle contient
	function myCloseWindowsByImage(imgname) {
		var theId=mySearchIdByImageName(imgname);
		//alert('recherche de '+imgname);
		if (theId != '') {
		//alert('trouvée dans ' + theId);
			try {
				var obj=document.getElementById(theId);
				obj.style.display='none';
			} catch (e) {
			}
		}
	}
	// retrouve l'id d'une fenetre creée précédement a partir du nom d'une image qu'elle contient
	function mySearchIdByImageName(imgname) {
		// liste des div du document
		var divs = document.getElementsByTagName('DIV');
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_window'){
				//alert('div '+divs[no].id);
				// liste des images de chaque
				var subContent = divs[no].getElementsByTagName('IMG');
				for(var no2=0;no2<subContent.length;no2++){
					//alert('image:'+subContent[no2].name);
					if(subContent[no2].name==imgname){
						// ok on a trouvé l'image cherchée
						var theId=divs[no].id;
						//alert('trouvée dans ' + theId);
						return theId
					}
				}
			}
		}
		return '';// si ici c'est que pas trouvée
	}
	// retrouve l'id d'une fenetre creée précédement a partir du l'id d'un div qu'elle contient
	function mySearchIdByDivId(id) {
		// liste des div du document
		var divs = document.getElementsByTagName('DIV');
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_window'){
				// liste des images de chaque
				var subContent = divs[no].getElementsByTagName('DIV');
				for(var no2=0;no2<subContent.length;no2++){
					if(subContent[no2].id==id){
						// ok on a trouvé l'id cherché
						var theId=divs[no].id;
						return theId
					}
				}
			}
		}
		return -1;// si ici c'est que pas trouvée
	}
if (typeof document.body != 'undefined') {// code nécessaire pour permettre le déplacement des fenetres
	try {
		window.onload = initWindows;
		document.body.onmouseup = stopMove;
		document.body.onmousemove = moveWindow;
		document.body.ondragstart = cancelEvent;
		document.body.onselectstart = cancelEvent;
	} catch (e) {}
}