/*
** (c) 2004-9 Southstar Computers Limited, All Rights Reserved
*/

function me() {
  me.hash = "";
}

function isMemberOfString( v_strItem, v_strSet, v_strSeparator ) {
  if ( typeof( v_strItem ) == "undefined" || typeof( v_strSet ) == "undefined" || typeof( v_strSeparator ) == "undefined" ) {
    return false;
  } else {
    var arrSet = v_strSet.split( v_strSeparator );
    if ( arrSet.length > 0 ) {
      for ( i = 0; i< arrSet.length; i++ ) {
        if ( v_strItem == arrSet[ i ] ) {
          return true;
        }
      }
    } else {
      return false;
    }
  }
}

function getDateAsYYYYMMDD() {
  var dtmNow      = new Date();
  var intMonth    = dtmNow.getMonth() + 1;
  var strDate;
  
  strDate = dtmNow.getFullYear().toString();
  if ( intMonth < 10 ) {
    strDate = strDate + "0" + intMonth.toString();
  } else {
    strDate = strDate + intMonth.toString();
  }
  if ( dtmNow.getDate() < 10 ) {
    strDate = strDate + "0" + dtmNow.getDate().toString();
  } else {
    strDate = strDate + dtmNow.getDate().toString();
  }
  return strDate;
}

function getDaysFrom( strYYYYMMDD ) {
  var dtmNow      = new Date();
  var strYYYY     = strYYYYMMDD.substring( 0, 4 );
  var strMM       = strYYYYMMDD.substring( 4, 6 );
  var strDD       = strYYYYMMDD.substring( 6, 8 );
  var dtmTest     = new Date( strYYYY, strMM - 1, strDD );
  var numDays     = (dtmTest.getTime() - dtmNow.getTime()) / (86400000);
  return numDays;
}

function getYYYYFrom( strYYYYMMDD ) {
  return strYYYYMMDD.substring( 0, 4 );
}

function getMMFrom( strYYYYMMDD ) {
  return strYYYYMMDD.substring( 4, 6 );
}

function getDDFrom( strYYYYMMDD ) {
  return strYYYYMMDD.substring( 6, 8 );
}

function getElementsByClass( v_objRoot, v_strClass ) {
  var arrResults = new Array();
  var intResultIndex = 0;
  var intNumberOfElements = v_objRoot.childNodes.length;
  var intIndex;
  
  for (intIndex = 0; intIndex < intNumberOfElements; intIndex++ ) {
    if ( isMemberOfString( v_strClass, v_objRoot.childNodes[ intIndex ].className, " " ) ) {
      arrResults[ intResultIndex++ ] = v_objRoot.childNodes[ intIndex ];
    }
  }
  return arrResults;
}

function getElementsByName( v_objRoot, v_strName ) {
  var arrResults = new Array();
  var intResultIndex = 0;
  var intNumberOfElements = v_objRoot.childNodes.length;
  var intIndex;
  
  for (intIndex = 0; intIndex < intNumberOfElements; intIndex++ ) {
    if ( v_objRoot.childNodes[ intIndex ].name == v_strName ) {
      arrResults[ intResultIndex++ ] = v_objRoot.childNodes[ intIndex ];
    }
  }
  return arrResults;
}

function getElementsByTagAndClass( v_strTagName, v_strClassName ) {
  var arrCandidates = document.getElementsByTagName( v_strTagName );
  var arrResults = new Array();
  var intResultIndex = 0;
  var intNumberOfElements = arrCandidates.length;
  var intIndex;
  
  for (intIndex = 0; intIndex < intNumberOfElements; intIndex++ ) {
    var objElement = arrCandidates[ intIndex ];
    var strClasses = objElement.className;
    if ( isMemberOfString( v_strClassName, strClasses, " " ) ) {
      arrResults[ intResultIndex++ ] = objElement;
    }
  }
  return arrResults;
}

function renameClass( r_objElement, v_strOldClass, v_strNewClass ) {
  var strClasses = r_objElement.className;
  if ( isMemberOfString( v_strOldClass, strClasses, " " ) ) {
    var arrClasses = strClasses.split( " " );
    for ( var i = 0; i < arrClasses.length; i++ ) {
      var strClass = arrClasses[ i ];
      if ( strClass == v_strOldClass ) {
        arrClasses[ i ] = v_strNewClass;
      }
    }
    r_objElement.className = arrClasses.join( " " );
  }
}

function showDetails(strCategory) {
  self.status = strCategory;
  self.open("index.php?Category=" + strCategory, "_top"); 
}

function setVisibility( v_objRoot, v_strClass, v_strCategory ) {
  var arrElements = getElementsByClass( v_objRoot, v_strClass );
  var intNumberOfElements = arrElements.length;
  var objElement = {};
  
  for ( intIndex = 0; intIndex < intNumberOfElements; intIndex++ ) {
    objElement = arrElements[ intIndex ];
    if ( isMemberOfString( v_strCategory, objElement.className, " " ) ) {
      objElement.style.visibility = "visible";
      objElement.style.display = "block";
    } else {
      objElement.style.visibility = "hidden";
      objElement.style.display = "none";
    }
  }  
}

function setElementVisibility( r_objElement, v_blnVisible ) {
  if ( r_objElement != null ) {
    if ( v_blnVisible == true ) {
      r_objElement.style.visibility = "visible";
      r_objElement.style.display = "block";
    } else {
      r_objElement.style.visibility = "hidden";
      r_objElement.style.display = "none";
    }
    setState( r_objElement.name, v_blnVisible.toString, 7 );
  }
}

function toggleVisibility( strItem ) {
  var objTarget = document.getElementById(strItem);
  var intNumberOfElements = objTarget.childNodes.length;
  var objChild;
  
  for (var intIndex = 0; intIndex < intNumberOfElements; intIndex++) {
    objChild = objTarget.childNodes[ intIndex ];
    if (objChild.nodeName != "#text" && objChild.nodeName != "#comment" ) {
      setElementVisibility(objChild, (objChild.style.visibility == "hidden"));
    }
  }
}

function hideSectionsExcept( v_strParent, v_strSectionName, v_strSectionClass ) {
  var objRoot = document.getElementById( v_strParent );
  var arrElements = getElementsByTagAndClass( "DIV", v_strSectionClass );
	if ( arrElements.length > 0 ) {
		var objElement;
	  for ( var intIndex = 0; intIndex < arrElements.length ; intIndex++ ) {
      objElement = arrElements[ intIndex ];
	    setElementVisibility( objElement, false );
	  }
	  objElement = document.getElementById( v_strSectionName );
		if ( objElement ) {
  	  setElementVisibility( objElement, true );
		}
    //setCookie( "section", v_strSectionName, 7 );
	}
  //window.scrollTo( 0, 0 );
} 

function treeRevise( objRootElement, strClass, strAttribute, strPrefix, strNewValue, strSuffix ) {
/*
** treeRevise starts with a root element and looks at children. If they have the given
** class, the specified attribute value is revised by applying a Prefix, new value or suffix
** according to the following rules.
**   If new value = false, then keep the old value, otherwise use the new value.
**   If prefix is not "", then prepend prefix.
**   If suffix is not "", then append suffix.
** With these rules we can provide a new value or prefix/suffix an old value.
*/
  var arrChildren;
  var objChildElement;
	var strOldValue;
  
  if ( objRootElement.nodeType ) {
    switch ( objRootElement.nodeType ) {
      case 1: /* Element */
			  /* Try for browsers supporting Node.childNodes[] */
			  if ( objRootElement.childNodes ) {
					arrChildren = objRootElement.childNodes;
				} else if ( objRootElement.children ) {
					arrChildren = objRootElement.children;
				}
		    for ( var i in arrChildren ) {
		      objChildElement = arrChildren[ i ];
		      if ( isMemberOfString( strClass, objChildElement.className, " " ) ) {
		        if ( strNewValue ) {
		          objChildElement.setAttribute( strAttribute, strPrefix + strNewValue + strSuffix );
		        } else {
							if ( objChildElement.hasAttribute( strAttribute) ) {
							  strOldValue = objChildElement.getAttribute( strAttribute );
						  } else {
								strOldValue = "";
							}
		          objChildElement.setAttribute( strAttribute, strPrefix + strOldValue + strSuffix );
		        }
		      }
		    }
			  break;
      
      case 2: /* Attribute */
      
      case 3: /* Text */
      
      case 9: /* document */
      
    }
  }  
}

function highlightSoonestEvent( v_strRoot, v_strClass, v_arrClassesToHide ) {
  var objRoot     = document.getElementById( v_strRoot );
  var arrElements = getElementsByClass( objRoot, v_strClass );
  var intElements = arrElements.length;
  var strYYYYMMDD = getDateAsYYYYMMDD();
  var intDifference;
  var intIndex;
  var intCounter = 0;
  var objElement;
 
  for (intIndex = 0; intIndex < intElements; intIndex++ ) {
    objElement = arrElements[ intIndex ];
    if ( objElement.id ) {
      /* Calculate days between date indicated by id (YYYYMMDD) and now */
      intDifference = getDaysFrom( objElement.id );
      if ( intDifference < -0.75 ) { /* Catch same-day events, until late in the day */
        /* Note, same day test is poor until we include time-of-day into the calculations */
        /* Historical events - lets reduce their significance by applying <style>past */
        renameClass( objElement, objElement.className, objElement.className + "past" );
        if ( v_arrClassesToHide.constructor == Array ) {
          for ( var i in v_arrClassesToHide ) {
            treeRevise( objElement, v_arrClassesToHide[ i ], 'style', '', 'visibility:hidden;display:none;', '' );
          }
        }
      } else if ( (intCounter == 0) && (intDifference <= 31) ) {  /* Look one month ahead */
        intCounter++;
        objElement.style.fontSize = "larger";
        objElement.style.fontWeight = "bold";
        objElement.style.color = "red"; 
        objElement.style.textDecoration = "blink";   /* Not supported by IE      */
        objElement.style.fontVariant = "small-caps"; /* Not supported by Mozilla */
        objElement.style.textTransform = "lowercase";
      }
    }
  }
}

function hideExpiredElements( v_strRoot, v_strClass ) {
  var objRoot     = document.getElementById( v_strRoot );
  var arrElements = getElementsByClass( objRoot, v_strClass );

  for ( var intIndex = 0; intIndex < arrElements.length; intIndex++ ) {
    var objElement = arrElements[ intIndex ];
    if ( objElement.id ) {
      var intDifference = getDaysFrom( objElement.id );
      if ( intDifference < 0 ) { /* Catch same-day events, until late in the day */
        setElementVisibility( objElement, false );
      } else {
        setElementVisibility( objElement, true );
      }
    }
  }  
}

function hideElementsOutsideRange( v_strRoot, v_strClass, v_intDaysBefore, v_intDaysAfter ) {
  var objRoot     = document.getElementById( v_strRoot );
  var arrElements = getElementsByClass( objRoot, v_strClass );

  for ( var intIndex = 0; intIndex < arrElements.length; intIndex++ ) {
    var objElement = arrElements[ intIndex ];
    if ( objElement.id ) {
      var intDifference = getDaysFrom( objElement.id );
      if ( intDifference < v_intDaysBefore || intDifference > v_intDaysAfter ) {
        setElementVisibility( objElement, false );
      }
    }
  }  
}

function setParameter( v_strName, v_strValue ) {
  eval( "me." + v_strName + " = v_strValue;" );
}

function setState( v_strName, v_strState, v_intDays ) {
  var date = new Date();
  
  date.setTime( date.getTime()+(v_intDays*24*60*60*1000) );
  document.cookie = v_strName + "=" + v_strState;
}

function clearCookie( v_strName ) {
  setCookie( v_strName, "", -1 );
}

function getCookie( v_strName ) {
  var strSearchFor = v_strName + "=";
  var arrSections = document.cookie.split( ';' );
  for (var i=0; i < arrSections.length; i++) {
    var strSection = arrSections[ i ];
    while ( strSection.charAt( 0 ) == ' ') strSection = strSection.substring( 1, strSection.length );
    if (strSection.indexOf(v_strName) == 0) return strSection.substring(v_StrName.length, strSection.length);
  }
  return null;
}

function setCookie( v_strName, v_strValue, v_intExpiryDays ) {
  if ( v_intExpiryDays ) {
    var dtmToday = new Date();
    dtmToday.setTime( dtmToday.getTime() + (v_intExpiryDays*24*60*60*1000));
    var strExpiry = "; expires="+dtmToday.toGMTString();
  } else {
    var strExpiry = "";
    document.cookie = v_strName+"="+v_strValue+strExpiry+"; path=/";
  }
}

function getHash() {
  var strHash = window.location.hash;
  if ( strHash != "" ) {
    setParameter( "section", strHash );
  }
}

function getQueries() {
  var strQuery = window.location.search;
  if ( strQuery != "" ) {
    var arrParameters = strQuery.split( '&' );
    for (var i = 0; i<arrParameters.length; i++ ) {
      var strParameter = arrParameters[ i ];
      var intAssignmentOffset = strParameter.indexOf( '=' );
      if ( intAssignmentOffset > 0 ) {
        var strKey = strParameter.substring( 1, intAssignmentOffset );
        var strValue = strParameter.substring( intAssignmentOffset + 1 );
        if ( strValue != "" ) {
          setParameter( strKey, strValue );
        }
      }
    }
  }
}

function getState( v_strName ) {
  var arrElements = getElementsByName( document.body, v_strName );
  var intNumberOfElements = arrElements.length;
  var objElement = {};
  
  for ( intIndex = 0; intIndex < intNumberOfElements; intIndex++ ) {
    objElement = arrElements[ intIndex ];
    var strState = getCookie( objElement.id );
    if ( strState == "True" ) {
      toggleVisibility( objElement.id );
  }  
  }
}

function createBookmark( v_strURL, v_strTitle) {
  if (window.sidebar) // Mozilla & Webkit browsers
    window.sidebar.addPanel(v_strTitle, v_strURL, "");
  else if(window.opera && window.print) { // Opera browsers
    var objAnchor = document.createElement('a');
    objAnchor.setAttribute('href',v_strURL);
    objAnchor.setAttribute('title',v_strTitle);
    objAnchor.setAttribute('rel','sidebar');
    objAnchor.click();
  } 
  else if(document.all) // Microsoft browsers
    window.external.AddFavorite(v_strURL, v_strTitle);
}

function launchChildRotation( v_strContainer, v_intInterval, v_strTransition ) {
  var objContainer = dom.getObject( v_strContainer );
  if ( objContainer ) {
    me.idRotation = registerTransitionBetweenChildren( objContainer, "DIV", v_intInterval, v_strTransition );
  }
}

function launchWatchHash( v_strContainer, v_intIntervalInMilliseconds ) {
  me.container = v_strContainer;
  me.interval = v_intIntervalInMilliseconds;
  watchHash();
}

function gotoSection( v_strSection ) {
  hideSectionsExcept( me.container, v_strSection, "section" );
}

function setHash( v_strHash ) {
  document.location.hash = v_strHash;
  hideSectionsExcept( me.container, v_strHash, "section" );
  if ( me.AJAXHook ) {
    me.AJAXHook( v_strHash );
  }
  launchWatchHash( me.container, me.interval );
}

function watchHash(  ) {
  if ( document.location.hash.substring(1) != me.hash ) {
    me.hash = document.location.hash;
    if ( me.hash.slice( 0, 7 ) == '#anchor' ) me.hash = me.hash.substring( 7 );
    if ( me.hash.slice( 0, 1 ) == '#' ) me.hash = me.hash.substring( 1 );
    hideSectionsExcept( me.container, me.hash, "section" );
  }
  if ( me.timeout ) { 
    window.clearTimeout( me.timeout );
  }
  me.timeout = window.setTimeout( watchHash, me.interval );
}
