/*
Script for choosing the culture of he website
*/

/******************** Event registration ********************/
if(window.attachEvent) //IE
{
	window.attachEvent("onload", _initPageFunction);
	window.attachEvent("onresize", resizeWindow);
}
else if(window.addEventListener) //DOM
{
	window.addEventListener("load", _initPageFunction, false);
	window.addEventListener("resize", resizeWindow, false);
}

function _initPageFunction()
{

}

function showPopUp()
{
	var oDivPopUp = document.getElementById('popupComingFrom');
	var oDivShaddow = document.getElementById('divShaddow');

	var sDomain = getDomainName();

	if(oDivPopUp)
	{
		if(oDivShaddow)
		{
			var oDivPopUpWidth = oDivPopUp.style.width;
			var oDivPopUpHeight = oDivPopUp.style.height;

			if(Get_Cookie('popUpCountry') != null)
			{
				oDivPopUp.className = 'close';	
				oDivShaddow.className = 'close';

				truebody().style.overflow = 'auto';
			}
			else
			{
				var iHCenter = parseInt(((getScreenWidth() / 2) + getScrollingDistanceHorizontal()) - (oDivPopUpWidth.substr(0,oDivPopUpWidth.length - 2) / 2));
				var iVCenter = parseInt(((getScreenHeight() / 2) + getScrollingDistanceVertical()) - (oDivPopUpHeight.substr(0,oDivPopUpHeight.length - 2) / 2));
				
				if(navigator.appName == "Opera" || navigator.appName == "Netscape")
				{
					iVCenter = iVCenter / 2;
				}


				oDivPopUp.style.left = String(iHCenter) + "px";
				oDivPopUp.style.top = String(iVCenter) + "px";


				oDivPopUp.setAttribute("xPosition", iHCenter);
				oDivPopUp.setAttribute("yPosition", iVCenter);
				
				
				oDivShaddow.className = 'show';
				oDivPopUp.className = 'show';

				truebody().style.overflow = 'hidden';

				enableDrag();

				// Create cookie
				Set_Cookie('popUpCountry', 'true', '', '/', sDomain, '') ;

			}
		}	
	}
}

function closePopUp()
{
	var oDivPopUp = document.getElementById('popupComingFrom');
	var oDivShaddow = document.getElementById('divShaddow');
	var sDomain = getDomainName();


	if(oDivPopUp)
	{
		if(oDivShaddow)
		{	
			oDivPopUp.className = 'close';
			oDivShaddow.className = 'close';

			truebody().style.overflow = 'auto';

			// Create cookie
			Set_Cookie('popUpCountry', 'true', '', '/', sDomain, '') ;
		}
	}

	return false;
}

function getScrollingDistanceVertical()
{
	if (document.documentElement && !document.documentElement.scrollTop)
	{
		// IE6 +4.01 but no scrolling going on
		return (parseInt(document.documentElement.scrollTop));
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		// IE6 +4.01 and user has scrolled
		return (parseInt(document.documentElement.scrollTop));
	}
	else if (document.body && document.body.scrollTop)
	{
		// IE5 or DTD 3.2
	return (parseInt(document.body.scrollTop));
}
}

function getScrollingDistanceHorizontal()
{
	return (parseInt(document.body.scrollLeft));
}

function getScreenHeight()
{
	return (parseInt(truebody().offsetHeight));
}

function getScreenWidth()
{
	return (parseInt(truebody().offsetWidth));
}

function getDomainName()
{
	var sDomain = document.domain;

	var i = 0;
	var iNbrPoint = 0;
	
	if (sDomain)
	{
		while (i > -1)
		{
			i = sDomain.indexOf(".", i + 1);
			if (i > -1)
			{
				iNbrPoint += 1;
			}
		}
		
	}

	if (iNbrPoint >  1)
	{
		sDomain = sDomain.substr(sDomain.indexOf(".") + 1);
	}
	
	return sDomain;
}

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));
}

 

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  /*
  if the expires variable is set, make the correct 
  expires time, the current script below will set 
  it for x number of days, to make it for hours, 
  delete * 24, for minutes, delete * 60 * 24
  */

  if ( expires )
  {
		expires = expires * 1000 * 60 * 60 * 24;
  }
  
  var expires_date = new Date( today.getTime() + (expires) );
  
  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString()  : "" ) + 
  ( ( path ) ? ";path=" + path : "" ) + 
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}

function truebody()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

/************************** Drag *************************/

function enableDrag()
{
	var oDivPopup = document.getElementById('popupComingFrom');
	var oDivTitleBarHandle = document.getElementById('oTitleBarHandle');

	var oDocumentSize = getDocumentSize();
	var oTablePopUpPosition = getPosition(oDivPopup);

	var iMaxWidth = parseInt(oDocumentSize.width) - parseInt(oTablePopUpPosition.width) - 5;
	var iMaxHeight = parseInt(oDocumentSize.height) - parseInt(oTablePopUpPosition.height) - 5;
	
	Drag.init(oDivTitleBarHandle ,oDivPopup, 5, iMaxWidth, 5,iMaxHeight);

	oDivPopup.onDragEnd = dragEnd;
	
}

function disableDrag()
{
	Drag.Disable(document.getElementById('oTitleBarHandle'));
}

function dragEnd (x, y)
{
	var oDivPopup = document.getElementById('popupComingFrom');
	
	oDivPopup.setAttribute("xPosition", x);
	oDivPopup.setAttribute("yPosition", y);
}

/*********************** Utiltities **********************/

// Return an object with properties: x, y, width, height
function getPosition(el)
{
	var oDimension = new Object();
	if(el.getBoundingClientRect)
	{
		oDimension.x = el.getBoundingClientRect().left + Math.max(document.body.scrollLeft, document.documentElement.scrollLeft);
		oDimension.y = el.getBoundingClientRect().top + Math.max(document.body.scrollTop, document.documentElement.scrollTop);
		oDimension.width = el.getBoundingClientRect().right - el.getBoundingClientRect().left;
		oDimension.height =  el.getBoundingClientRect().bottom - el.getBoundingClientRect().top;
	}
	else if(document.getBoxObjectFor)
	{
    	  oDimension.x = document.getBoxObjectFor(el).x;
	  oDimension.y =  document.getBoxObjectFor(el).y;
	  oDimension.width = document.getBoxObjectFor(el).width;
	  oDimension.height = document.getBoxObjectFor(el).height;
	}

	return oDimension;
}

// Return an object with properties : width, height
function getDocumentSize()
{
	var oSize = new Object();
	
	if (document.body.scrollHeight)
	{
		oSize.height = document.body.scrollHeight;
	}
	else
	{
		oSize.height = document.documentElement.offsetHeight;
	}
	
	if (document.body.scrollWidth)
	{
		oSize.width = document.body.scrollWidth;
	}
	else
	{
		oSize.width = document.documentElement.offsetWidth;
	}

	return oSize;
}

function resizeWindow()
{
	var oDivPopup = document.getElementById('popupComingFrom');

	if (oDivPopup)
	{	

		if (oDivPopup.className == "show")
		{	
			setDivPosition(oDivPopup.getAttribute("xPosition"), oDivPopup.getAttribute("yPosition"));

			enableDrag();
		}	
	}
	
}

function setDivPosition(x,y)
{
	var oDivPopup = document.getElementById('popupComingFrom');
	var oPage = getPosition(document.getElementById('contentPageContainer'))
	var oDocument = getDocumentSize();
		
	oDivPopup.style.left = parseInt(x);
	oDivPopup.style.top = parseInt(y);

}
