// [[ 0. Portable event handler registration functions for IE DOM2. ]]
/*
This code is from the book JavaScript: The Definitive Guide, 5th Edition, by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN #0596101996)
Source : http://www.davidflanagan.com/javascript5/display.php?n=17-2&f=17/eventHandler.js
*/
var eventHandler = {};

if ( document.addEventListener )
	{
		eventHandler.add = function(element, eventType, handler) { element.addEventListener(eventType, handler, false); };
		eventHandler.del = function(element, eventType, handler) { element.removeEventListener(eventType, handler, false); };
	}
else if ( document.attachEvent )
	{
		eventHandler.add = function(element, eventType, handler)
			{
				if ( eventHandler._find(element, eventType, handler) != -1 )
					{ return; }

				var wrappedHandler = function(e)
					{
						if ( !e ) { e = window.event; }

						var event = {
							_event: e,
							type: e.type,
							target: e.srcElement,
							currentTarget: element,
							relatedTarget: ( e.fromElement ? e.fromElement:e.toElement ),
							eventPhase: ( e.srcElement == element ? 2:3 ),

							clientX: e.clientX, clientY: e.clientY,
							screenX: e.screenX, screenY: e.screenY,

							altKey: e.altKey, ctrlKey: e.ctrlKey,
							shiftKey: e.shiftKey, charCode: e.keyCode,

							stopPropagation: function() { this._event.cancelBubble = true; },
							preventDefault: function() { this._event.returnValue = false; }
						}

						if ( Function.prototype.call )
							{ handler.call(element, event); }
						else
							{
								element._currentHandler = handler;
								element._currentHandler(event);
								element._currentHandler = null;
							}
					};

					element.attachEvent('on'+ eventType, wrappedHandler);

					var h = {
						element: element,
						eventType: eventType,
						handler: handler,
						wrappedHandler: wrappedHandler
					};

				var d = ( element.document || element );
				var w = d.parentWindow;

				var id = eventHandler._uid();
				if ( !w._allHandlers ) { w._allHandlers = {}; }
				w._allHandlers[id] = h;

				if ( !element._handlers ) { element._handlers = []; }
				element._handlers.push(id);

				if ( !w._onunloadHandlerRegistered )
					{
						w._onunloadHandlerRegistered = true;
						w.attachEvent('onunload', eventHandler._removeAllHandlers);
					}
			};

		eventHandler.del = function(element, eventType, handler)
			{
				var i = eventHandler._find(element, eventType, handler);
				if (i == -1) { return; }

				var d = ( element.document || element );
				var w = d.parentWindow;

				var handlerId = element._handlers[i];
				var h = w._allHandlers[handlerId];
				element.detachEvent('on'+ eventType, h.wrappedHandler);
				element._handlers.splice(i, 1);
				delete w._allHandlers[handlerId];
			};

		eventHandler._find = function(element, eventType, handler)
			{
				var handlers = element._handlers;
				if (!handlers) { return -1; }

				var d = element.document || element;
				var w = d.parentWindow;

				for(var i = handlers.length - 1; i >= 0; i--)
					{
						var handlerId = handlers[i];
						var h = w._allHandlers[handlerId];
						if (h.eventType == eventType && h.handler == handler)
							{ return i; }
					}
				return -1;
			};

		eventHandler._removeAllHandlers = function()
			{
				var w = this;
				for ( id in w._allHandlers )
					{
						var h = w._allHandlers[id];
						h.element.detachEvent('on'+ h.eventType, h.wrappedHandler);
						delete w._allHandlers[id];
					}
			}

		eventHandler._counter = 0;
		eventHandler._uid = function() { return 'h'+ eventHandler._counter++; };
	}

//__ GLOBAL variables _____
var iRealPageHeight = ''; //__ set in 'fnRealPageHeight()' and used in 'fnPlaceFooter()'

//__ FUNCTIONS _____
function chgActivePI(oElem) /* 'product' script use - for 'keeping' active product image selected - ['oElem' == this] */
{
	var mainNode = document.getElementById('sideIMG'), sideIMGs = new Array(), iLargeView = document.getElementById('iMAIN');
	for( var i = 0; i < mainNode.childNodes.length; i++ ) { if( mainNode.childNodes[i].nodeType != 3 && mainNode.childNodes[i].nodeName == 'DD' ) { mainNode.childNodes[i].childNodes[0].className = ''; } }
	oElem.className = "aION";
}

function fnElemHeight(oElem) /* 'product' script use - for good height behavior when 'opening/closing' product description zones */
{
	var iElemCHeight = oElem.clientHeight, iParentCHeight = oElem.parentNode.clientHeight;
	var iSiblingCHeight = ( oElem.previousSibling.nodeType != 1 ? oElem.previousSibling.previousSibling.clientHeight : oElem.previousSibling.clientHeight );
	oElem.style.height = iParentCHeight - (iElemCHeight + iSiblingCHeight) + 'px';
}

function chgActivePD(oElem) /* 'product' script use - for 'opening/closing' product description zones */
{
	var oElemParent = oElem.parentNode.parentNode, aElemChildsDD = new Array();

	for( var i = 0; i < oElemParent.childNodes.length; i++ )
		{ if( oElemParent.childNodes[i].nodeType != 3 && oElemParent.childNodes[i].nodeName == 'DD' ) { aElemChildsDD.push(oElemParent.childNodes[i]); } }
	//__ we look for the next 'real' sibling of this Node (text elements excluded).
	var oElemNextSib = ( oElem.parentNode.nextSibling.nodeType != 1 ? oElem.parentNode.nextSibling.nextSibling : oElem.parentNode.nextSibling );

	for( var j = 0; j < aElemChildsDD.length; j++ )
		{
			//__ we look for the previous 'real' sibling of this Node (text elements excluded).
			var oElemPrevSib = ( aElemChildsDD[j].previousSibling.nodeType != 1 ? aElemChildsDD[j].previousSibling.previousSibling : aElemChildsDD[j].previousSibling );

			oElemPrevSib.className = '';
			aElemChildsDD[j].style.display = 'none';
		}
	oElem.parentNode.className = 'aP';
	oElemNextSib.style.display = 'block';
	oElem.blur();
}

function fnProdLinks()
{
	var oElemParent = document.getElementById('cPDet'), iElemParentCHeight = oElemParent.clientHeight;
	var oElemH1 = "", oElemDL = "", iElemH1CHeight = "", iElemDLCHeight = "";
	for( var i = 0; i < oElemParent.childNodes.length; i++ )
		{
			if( oElemParent.childNodes[i].nodeType == 1 )
				{
					if( oElemParent.childNodes[i].nodeName == 'H1' ) { oElemH1 = oElemParent.childNodes[i]; iElemH1CHeight = oElemH1.clientHeight; }
					if( oElemParent.childNodes[i].nodeName == 'DL' ) { oElemDL = oElemParent.childNodes[i]; iElemDLCHeight = oElemDL.clientHeight; }
				}
		}
	var iPosTop = iElemParentCHeight - (iElemH1CHeight + iElemDLCHeight + 10);
	oElemDL.style.position = 'relative';
	oElemDL.style.top = iPosTop +'px';
	//window.status = oElemH1.nodeName +': '+ iElemH1CHeight +'|'+ oElemDL.nodeName +': '+ iElemDLCHeight ;
}

function fnRealPageHeight()
{
	iRealPageHeight = document.getElementById('zPAGE').scrollHeight - 80;
}

function fnPlaceFooter()
{
	var bTestPhase	= false; //__ boolean parameter to [enable|disable] test elements throughout the functions _____
	var iBodyHeight = document.body.clientHeight,
			iBodyWidth	= document.body.clientWidth,
			iPageHeight = document.getElementById('zPAGE').scrollHeight,
			oZFootChild = document.getElementById('zFOOT');

	if( bTestPhase === true ) /*__ TEST element __*/
		{ window.status = (iBodyHeight +'|'+ iBodyWidth); }

	for( var x = 0; x < oZFootChild.childNodes.length; x++ )
		{ if( oZFootChild.childNodes[x].nodeType == 1 && oZFootChild.childNodes[x].nodeName == 'DIV' ) { oZFootChild = oZFootChild.childNodes[x]; } }
	var iCorrectHeight = (iPageHeight < iBodyHeight ? iBodyHeight-80 : iRealPageHeight);
	var iMoveBy = iCorrectHeight;
	document.getElementById('zPAGE').style.height = iCorrectHeight +'px';
	oZFootChild.style.top = iMoveBy +'px';

	if( bTestPhase === true ) /*__ TEST element __*/
		{ window.status += ' [ iPageHeight:'+ iPageHeight +'||iBodyHeight:'+ iBodyHeight +'||iRealPageHeight:'+ iRealPageHeight +' ]'; }

	//__ placement & display for PNG filtered backgrounds for IE _____
	var sUserAgent = navigator.userAgent,
			reNavIE = /msie\s(5\.[5-9]|[6]\.[0-9]|[7]\.[0-9]*).*(win)/i,
			reNavIE7 = /msie\s([7]\.[0-9]*).*(win)/i,
			iBgImgH = 0,
			iBgImgW = 0;
	if( sUserAgent.match(reNavIE) )
		{
			if( document.getElementById('zBIMG').style.backgroundImage.match(/pCONT/g) != null )
				{ iBgImgH = 512; iBgImgW = 341; }
			else
				{ iBgImgH = 427; iBgImgW = 333; }
			var oBgImage0 = document.getElementById('fBGzBIMG');
			var oBgImage1 = document.getElementById('fBGzPAGE');
			var iPosTopImage0 = iBodyHeight - iBgImgH;
			var iPosTopImage1 = iBodyHeight - 265;

			//__ special declaration for IE7 _____
			if( sUserAgent.match(reNavIE7) )
				{
					var iCorrectDecal = 80;
					iPosTopImage0 = iCorrectHeight - iBgImgH + iCorrectDecal;
					iPosTopImage1 = iCorrectHeight - 205;
				}
			oBgImage0.style.top = iPosTopImage0 +'px';
			oBgImage0.style.left = (iBodyWidth - iBgImgW) +'px';
			oBgImage1.style.top = iPosTopImage1 +'px';
			oBgImage1.style.left = '0px';
			oBgImage0.style.display = 'block';
			oBgImage1.style.display = 'block';
		}
}

function fnPlaceFooter2()
{
	var sUserAgent = navigator.userAgent,
			reNavIE = /msie\s(5\.[5-9]|[6]\.[0-9]|[7]\.[0-9]*).*(win)/i;
	var oDocBody = document.body,
			iClientHeight = oDocBody.clientHeight,
			iScrollHeight = (sUserAgent.match(reNavIE) ? document.body:(document.documentElement ? document.documentElement:document.body)).scrollHeight,
			oZFootChild = document.getElementById('zFOOT');
}

function fnInfoBox1()
{
	var oBody = document.body,
			oBox = document.createElement('DIV'),
			iBodyHeight = oBody.clientHeight,
			iBodyWidth	= oBody.clientWidth;
	var sContent = document.createTextNode('[clientHeight : '+ iBodyHeight +'] [clientWidth : '+ iBodyWidth +']');
	oBox.appendChild(sContent);
	oBody.appendChild(oBox);
	oBox.className = 'box';
}

function fnInfoBox2()
{
	var sUserAgent = navigator.userAgent,
			reNavIE = /msie\s(5\.[5-9]|[6]\.[0-9]|[7]\.[0-9]*).*(win)/i,
			reNavIE7 = /msie\s([7]\.[0-9]*).*(win)/i;
	var oBody = (sUserAgent.match(reNavIE) ? document.body:(document.documentElement ? document.documentElement:document.body)),
			oBox = document.createElement('DIV'),
			iPageHeight = oBody.scrollHeight,
			iChildNode = (sUserAgent.match(reNavIE)?0:1),
			oZFootChild = document.getElementById('zFOOT').childNodes[iChildNode].offsetHeight;
	var sContent = document.createTextNode('[scrollHeight : '+ iPageHeight +'] [Footer Height : '+ oZFootChild +']');
	oBox.appendChild(sContent);
	document.body.appendChild(oBox);
	oBox.className = 'box';
	oBox.style.top = '30px';
}

function fnGoogleSearch()
{
	var sInputSearch = document.getElementById('inpSRCH').value, sHostSearch = 'www.teknoglam.com';
	if( sInputSearch != '' )
		{ window.open('http://www.google.fr/search?as_epq='+ sInputSearch +'&as_sitesearch='+ sHostSearch +'&hl=fr'); window.location = '/Accueil'; }
}

function fnOpenLinkBlank(e)
{
	var oLink = e.currentTarget;
	var sURL = oLink.getAttribute('href');
	window.open(sURL, '_blank');
	e.preventDefault();
	return false;
}

function fnHandleLinkClick()
{
	var oLink, list = document.getElementsByTagName('a'), i;
	for( i = 0; i < list.length; i++ )
		{
			oLink = list.item(i);
			if( oLink.getAttribute('href') && oLink.className )
				{
					if( (' '+ oLink.className +' ').indexOf(' fnOpenNW ') != -1 )
						{
							eventHandler.add(oLink, 'click', fnOpenLinkBlank);
						}
				}
		}
}
