/*
***************************************************************************************
* CONTENT POSITIONING SCRIPT
*==============================*
*                     Author: Cornel Boudria
*               Release Date: May 25, 2004
*
***************************************************************************************
***************************************************************************************
*                       file: content.js
*            current Version: 1.01
*                       Date: October 27, 2004
***************************************************************************************
*       Bug (Update) History:
*               - 10/27/2004: Mac OS Absolute Positioning Bug Resulted in empty content.
*                             10/27/2004 This Update Optimized Element coordination to 
be compatible cross-browser 
*                             and cross-platform.
***************************************************************************************
*/

var g_LocaleReference;
var g_PlacedContainerReference;
var runOnce = false;

var g_LocaleList = new Array();
var g_ContentList = new Array();
var g_LocaleContentPair = new Array();


function Initialize(lrTagName,crTagName)
{
var tempLocaleList = document.getElementsByTagName(lrTagName);
var tempContentList = document.getElementsByTagName(crTagName);
var n = 0;
var hold = "";

//FILL GLOBAL LOCALELIST ARRAY
for(x=0; x < tempLocaleList.length; x++)
{
 hold = tempLocaleList[x].id;
 if(hold != "") {g_LocaleList[n] = hold; n++;}
}
//->

//CLEAR DUMMY VARIABLES
hold = null;
hold = "";
n = 0;
//->

//FILL GLOBAL CONTENTLIST ARRAY
for(x=0; x < tempContentList.length; x++)
{
 hold = tempContentList[x].id;
 if(hold != "") {g_ContentList[n] = hold; n++;}
}
//->

var diff = 0;
/*
if(g_LocaleList.length == g_ContentList.length) {}
if(g_LocaleList.length > g_ContentList.length) {cnt=g_ContentList.length; diff = g_LocaleList.length - g_ContentList.length;}
if(g_LocaleList.length < g_ContentList.length) {cnt=g_LocaleList.length; diff = g_LocaleList.length - g_ContentList.length;}
*/
diff = g_LocaleList.length - g_ContentList.length;
SyncContentWithLocale(g_LocaleList.length,diff);
}

function SyncContentWithLocale(count,fDiff)
{

var MatchArray = new Array();
var matches = false; var MatchesCnt = 0;
 for(x=0; x < count; x++)
 {
  for(y=0; y < count-fDiff; y++) //(y=count - 1; y >= 0; y--)
  {
   if(g_LocaleList[x].substring(g_LocaleList[x].indexOf("_Locale"),0)==g_ContentList[y].substring(g_ContentList[y].indexOf("_Content"), 0))
   {
 //   alert("'" + g_LocaleList[x] + "'" + " at " + x + " matches " + "'" + g_ContentList[y] + "'" + " at " + y);
    MatchArray[MatchesCnt] = x + "," + y;  //alert(MatchArray[MatchesCnt]); //g_LocaleList[x];
    MatchesCnt++;    
   }
  }//-> END INNER FOR LOOP
 }//-> End OUT FOR LOOP

g_LocaleContentPair = MatchArray;

initLocaleRefContRef(g_LocaleContentPair);
}

function initLocaleRefContRef(oArray)
{

var tempL = 0;
var tempC = 0;
var dummy = new Array();
 for(x=0; x < oArray.length; x++)
 { 
  dummy = oArray[x].split(",");
  tempL = parseInt(dummy[0]);
  tempC = parseInt(dummy[1]);
 // alert("g_LocaleList: " + g_LocaleList[tempL] + "\ng_ContentList: " + g_ContentList[tempC]);
  runOnce = false;
  positionElements(g_LocaleList[tempL],g_ContentList[tempC]);
 }
}

function positionElements(LocaleRef,ContAreaRef){

 var localeObj = document.getElementById(LocaleRef);      // 'LOCALE' Object  -> local scope
 var contentObj = document.getElementById(ContAreaRef);   // 'CONTENT' Object -> local scope
 var cNodes = new Array();                                //  Buffer Array    -> local scope


/*********************/
/* Fill cNodes Array */
/*********************/
 for(c = 0; c < contentObj.childNodes.length; c++)
  cNodes[c] = contentObj.childNodes[c];


/***************************************/
/* Append cNodes Elements to localeObj */
/***************************************/
 for(c = 0; c < cNodes.length; c++)
  localeObj.appendChild(cNodes[c]);


/***************************************************/
/* Cleanup cNodes to make room for next contentObj */
/***************************************************/
 cNodes = null;
}

//*********************
//* UTILITY FUNCTIONS *
//*********************
//Following Code Courtesy of Brainjar.com
//------------------------------------------------->
function getPageOffsetLeft(el) {

  var x;

  // Return the x coordinate of an element relative to the page.

  x = el.offsetLeft;
  if (el.offsetParent != null)
    x += getPageOffsetLeft(el.offsetParent);

  return x;
}

function getPageOffsetTop(el) {

  var y;

  // Return the y coordinate of an element relative to the page.

  y = el.offsetTop;
  
  if (el.offsetParent != null)
    y += getPageOffsetTop(el.offsetParent);

  return y;
}
//------------------------------------------------->

function getViewportHeight() {

    // supported in Mozilla, Opera, and Safari
     if(window.innerHeight)
         return window.innerHeight;
	
    // supported in standards mode of IE, but not in any other mode
     if(window.document.documentElement.clientHeight)
         return document.documentElement.clientHeight;
	
    // supported in quirks mode, older versions of IE, and mac IE (anything else).
    return window.document.body.clientHeight;
}

function getViewportWidth() {

    // supported in Mozilla, Opera, and Safari
     if(window.innerWidth)
         return window.innerWidth;
	
    // supported in standards mode of IE, but not in any other mode
     if(window.document.documentElement.clientWidth)
         return document.documentElement.clientWidth;
	
    // supported in quirks mode, older versions of IE, and mac IE (anything else).
    return window.document.body.clientWidth;
}
