/**
 * @fileoverview
 * <b><i>core.js</i></b>
 *
 * @author Martin Hristov <martin@netclime.com>
 * @version 1.0
 */

var SECTION_GROUP       = {};

SECTION_GROUP.HasStyle = function ( sSrcClassName, sStyleToCheck ) {
   var arrClassNames = sSrcClassName.split( " " );
   for ( var i = 0; i < arrClassNames.length; i++ ) {
      if ( arrClassNames[ i ] == sStyleToCheck ) return true;
   }
   return false;
}

SECTION_GROUP.ReplaceStyle = function ( sSrcClassName, sStyleToCheck, sStyleToReplace ) {
   var arrClassNames = sSrcClassName.split( " " );
   for ( var i = 0; i < arrClassNames.length; i++ ) {
      if ( arrClassNames[ i ] == sStyleToCheck ) arrClassNames[ i ] = sStyleToReplace;
   }
   return arrClassNames.join( " " );
}

SECTION_GROUP.AddStyle = function ( sSrcClassName, sStyleToAdd ) {
   var arrClassNames = sSrcClassName.split( " " );
   for ( var i = 0; i < arrClassNames.length; i++ ) {
      if ( arrClassNames[ i ] == sStyleToAdd ) return sSrcClassName;
   }
   arrClassNames.push(sStyleToAdd);
   return arrClassNames.join( " " );
}

SECTION_GROUP.RemoveStyle = function ( sSrcClassName, sStyleToRemove ) {
   var arrClassNames    = sSrcClassName.split( " " );
   var arrClassNamesTmp = [];
   for ( var i = 0; i < arrClassNames.length; i++ ) {
      if ( arrClassNames[ i ] != sStyleToRemove ) arrClassNamesTmp.push(arrClassNames[ i ]);
   }
   arrClassNames = arrClassNamesTmp;
   return arrClassNames.join( " " );
}

SECTION_GROUP.DisplayCollapseIcon = function ( sCollapseIconCellId, hashCollapseIconClassNames ) {
   var oCollapseIconCell = document.getElementById(sCollapseIconCellId);
   if ( !oCollapseIconCell || typeof oCollapseIconCell == 'undefined' )
      return;
   oCollapseIconCell.style.backgroundImage = '';
   oCollapseIconCell.className = SECTION_GROUP.ReplaceStyle(oCollapseIconCell.className,
                                                hashCollapseIconClassNames.expand,
                                                hashCollapseIconClassNames.collapse);
}

SECTION_GROUP.DisplayExpandIcon = function ( sCollapseIconCellId, hashCollapseIconClassNames ) {
   var oCollapseIconCell = document.getElementById(sCollapseIconCellId);
   if ( !oCollapseIconCell || typeof oCollapseIconCell == 'undefined' )
      return;
   oCollapseIconCell.style.backgroundImage = '';
   oCollapseIconCell.className = SECTION_GROUP.ReplaceStyle(oCollapseIconCell.className,
                                                hashCollapseIconClassNames.collapse,
                                                hashCollapseIconClassNames.expand);
}

SECTION_GROUP.HideCollapseIcon = function ( sCollapseIconCellId ) {
   var oCollapseIconCell = document.getElementById(sCollapseIconCellId);
   if ( !oCollapseIconCell || typeof oCollapseIconCell == 'undefined' )
      return;
   oCollapseIconCell.style.backgroundImage = 'none';
}

SECTION_GROUP.SetSectionState = function ( sRootID, hashHeaderClassNames, iMode ) {
   var arrChildren = SECTION_GROUP.GetDOMChildren(document.getElementById(sRootID));
   if ( !arrChildren || typeof arrChildren == 'undefined' ) {
      return;
   }
   for ( var i = 0; i < arrChildren.length; i++ ) {
      var oChild = arrChildren[i];
      if ( typeof oChild.className != 'undefined' ) {
         if ( hashHeaderClassNames && typeof hashHeaderClassNames != 'undefined' ) {
            if ( iMode == 0 ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.active,
                                             hashHeaderClassNames.inactive);
            } else {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.inactive,
                                             hashHeaderClassNames.active);
            }
         }
      }
   }
}

SECTION_GROUP.SetActiveSectionState = function ( sRootID, hashHeaderClassNames ) {
   SECTION_GROUP.SetSectionState(sRootID, hashHeaderClassNames, 1);
}

SECTION_GROUP.RemoveActiveSectionState = function ( sRootID, hashHeaderClassNames ) {
   SECTION_GROUP.SetSectionState(sRootID, hashHeaderClassNames, 0);
}

SECTION_GROUP.SwapCollapseState = function ( sCollapseIconCellId, sRootID, hashCollapseIconClassNames, hashHeaderClassNames ) {
   var oCollapseIconCell = document.getElementById(sCollapseIconCellId);
   if ( SECTION_GROUP.HasStyle( oCollapseIconCell.className, hashCollapseIconClassNames.collapse) ) {
      SECTION_GROUP.DisplayExpandIcon(sCollapseIconCellId, hashCollapseIconClassNames);
      if ( SECTION_GROUP.Effects[sRootID] && typeof SECTION_GROUP.Effects[sRootID] != 'undefiend' ) {
         SECTION_GROUP.Animations.Slide.show(SECTION_GROUP.Effects[sRootID]);
      } else {
         SECTION_GROUP.SetActiveSectionState(sRootID, hashHeaderClassNames);
      }
   } else {
      SECTION_GROUP.DisplayCollapseIcon(sCollapseIconCellId, hashCollapseIconClassNames);
      if ( SECTION_GROUP.Effects[sRootID] && typeof SECTION_GROUP.Effects[sRootID] != 'undefiend' ) {
         SECTION_GROUP.Animations.Slide.hide(SECTION_GROUP.Effects[sRootID]);
      } else {
         SECTION_GROUP.RemoveActiveSectionState(sRootID, hashHeaderClassNames);
      }
   }
}

SECTION_GROUP.OnSectionTitleClick = function ( sUrl, sTarget, sCollapseIconCellId, sRootID, hashCollapseIconClassNames, hashHeaderClassNames, isSlidingTitle ) {
   if ( typeof sUrl != 'undefined' && sUrl != 'javascript:void(null)' ) {
      if ( document.all && isSlidingTitle )
         window.open(sUrl, sTarget);
      return;
   }
   var oCollapseIconCell = document.getElementById(sCollapseIconCellId);
   if ( oCollapseIconCell && typeof oCollapseIconCell != 'undefined' )
      SECTION_GROUP.SwapCollapseState(sCollapseIconCellId, sRootID, hashCollapseIconClassNames, hashHeaderClassNames);
}

SECTION_GROUP.GetDOMChildren = function ( oRoot ) {
   if ( !oRoot ) return;
   var arrChildren = [oRoot];
   if ( oRoot.childNodes && oRoot.childNodes.length > 0 ) {
      for ( var i = 0; i < oRoot.childNodes.length; i++ ) {
         var arrTempChildren = SECTION_GROUP.GetDOMChildren( oRoot.childNodes[i] );
         for ( var j = 0; j < arrTempChildren.length; j++ ) {
            arrChildren.push( arrTempChildren[j] );
         }
      }
   }
   return arrChildren;
}

SECTION_GROUP.PushTab = function ( iNodeID, iParentID, iPosition, iOrderIndex ) {
   var oHolder    = document.getElementById('tab_button_container' + iParentID);
   var oCaption   = document.getElementById('tab_caption_' + iNodeID);

   if ( !oHolder || !oCaption ) return;

   var attrClass        = document.createAttribute('class');
   attrClass.nodeValue  = 'tabButtonCell noactive';

   var attrId           = document.createAttribute('id');
   attrId.nodeValue     = 'tab_button_cell_' + iNodeID;

   if ( HORIZONTAL == iPosition ) {
      if ( iOrderIndex > 0 ) {
         // Create delimiter cell
         var oDelim     = document.createElement('TD');
         var delimClass = document.createAttribute('class');
         delimClass.nodeValue  = 'tabSeparator';
         oDelim.setAttributeNode(delimClass);
         oHolder.appendChild(oDelim);
      }
      // Create section cell
      var oTD = document.createElement('TD');
      oTD.setAttributeNode(attrClass);
      oTD.setAttributeNode(attrId);
      oTD.appendChild(oCaption);
      oHolder.appendChild(oTD);
   } else {
      if ( iOrderIndex > 0 ) {
         // Create delimiter cell
         var oDelimTR         = document.createElement('TR');
         var oDelimTD         = document.createElement('TD');
         var delimClass       = document.createAttribute('class');
         delimClass.nodeValue = 'tabSeparator';
         oDelimTD.setAttributeNode(delimClass);
         oDelimTR.appendChild(oDelimTD);
         oHolder.appendChild(oDelimTR);
      }
      // Create section cell
      var oTR = document.createElement('TR');
      var oTD = document.createElement('TD');
      oTD.setAttributeNode(attrClass);
      oTD.setAttributeNode(attrId);
      oTD.appendChild(oCaption);
      oTR.appendChild(oTD);
      oHolder.appendChild(oTR);
   }
}

SECTION_GROUP.ShowCaption = function ( oRoot, hashHeaderClassNames ) {

   if ( typeof oRoot.className == 'undefined' ||
        !SECTION_GROUP.HasStyle( oRoot.className, 'secTitle' ) )
   {
      return;
   }

   var arrChildren = SECTION_GROUP.GetDOMChildren(oRoot);
   for ( var i = 0; i < arrChildren.length; i++ ) {
      var oChild = arrChildren[i];
      if ( typeof oChild.className != 'undefined' ) {

         if ( SECTION_GROUP.HasStyle( oRoot.className, hashHeaderClassNames.inactive ) ) {
            if ( SECTION_GROUP.HasStyle( oChild.className, 'imgExpanded' ) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.active,
                                             hashHeaderClassNames.inactive);
            }

            if ( SECTION_GROUP.HasStyle( oChild.className, 'imgCollapsed' ) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.inactive,
                                             hashHeaderClassNames.active);
            }
         } else {
            if ( SECTION_GROUP.HasStyle( oChild.className, 'imgExpanded' ) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.inactive,
                                             hashHeaderClassNames.active);
            }

            if ( SECTION_GROUP.HasStyle( oChild.className, 'imgCollapsed' ) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.active,
                                             hashHeaderClassNames.inactive);
            }
         }

      }
   }

}

SECTION_GROUP.ShowTab = function ( sNodeID, sID, hashCollapseIconClassNames, hashHeaderClassNames ) {
   var oIcon       = null;
   var arrChildren = SECTION_GROUP.GetDOMChildren(document.getElementById(sID));

   // Inactivate all sections in the group
   if ( arrChildren ) {
      for ( var i = 0; i < arrChildren.length; i++ ) {
         var oChild = arrChildren[i];
         if ( typeof oChild.className != 'undefined' ) {
            if ( SECTION_GROUP.HasStyle(oChild.className, hashHeaderClassNames.active) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.active,
                                             hashHeaderClassNames.inactive);
            }
            SECTION_GROUP.ShowCaption(oChild, hashHeaderClassNames);

            if ( SECTION_GROUP.HasStyle( oChild.className, 'secIconCol' ) ) oIcon = oChild;
         }
      }
   }
   var oTab = document.getElementById('tab_button_cell_' + sNodeID);
   arrChildren = SECTION_GROUP.GetDOMChildren(oTab);
   if ( arrChildren ) {
      for ( var i = 0; i < arrChildren.length; i++ ) {
         var oChild = arrChildren[i];
         if ( typeof oChild.className != 'undefined' ) {
            if ( SECTION_GROUP.HasStyle(oChild.className, hashHeaderClassNames.inactive) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.inactive,
                                             hashHeaderClassNames.active);
            }
         }
      }
   }

   var oSectionWrapper  = document.getElementById('section_wrapper' + sNodeID);
   arrChildren = SECTION_GROUP.GetDOMChildren(oSectionWrapper.parentNode);
   if ( arrChildren ) {
      for ( var i = 0; i < arrChildren.length; i++ ) {
         var oChild = arrChildren[i];
         if ( typeof oChild.className != 'undefined' ) {
            if ( SECTION_GROUP.HasStyle(oChild.className, hashHeaderClassNames.inactive) ) {
               oChild.className =
                  SECTION_GROUP.ReplaceStyle(oChild.className,
                                             hashHeaderClassNames.inactive,
                                             hashHeaderClassNames.active);
            }
         }
      }
   }

}

SECTION_GROUP.getStyleProperty = function (obj, IEStyleProp, CSSStyleProp) {
   if (obj.currentStyle)
      return obj.currentStyle[IEStyleProp];
   else if (window.getComputedStyle)
      return window.getComputedStyle(obj, "").getPropertyValue(CSSStyleProp);

   return null;
}

SECTION_GROUP.OnLoad = function ( sID ) {
   var oSGTable = document.getElementById(sID);
   var bFullWidth = 0;
   if ( oSGTable.className && oSGTable.className.indexOf('sgHC') != -1 ) {
      var arrChildren = SECTION_GROUP.GetDOMChildren(oSGTable);
      if ( arrChildren ) {
         for ( var i = 0; i < arrChildren.length; i++ ) {
            if ( arrChildren[i].className && arrChildren[i].className.indexOf('sectionCell') != -1 ) {
               if ( arrChildren[i].width ) {
                  var sWidth = arrChildren[i].width;
                  sWidth = sWidth.toString();
                  if ( sWidth.indexOf('%') != -1 ) {
                     bFullWidth = 1;
                  }
               } else {
                  bFullWidth = 1;
               }
            }
         }
      }
      if ( !bFullWidth )
         oSGTable.width = '';
   }
}

SECTION_GROUP.GetAnchorName = function () {
   var anchor = '';
   if ( document.location.href.indexOf('#') != -1 )
      anchor = document.location.href.substr(document.location.href.indexOf('#')+1);
   return anchor;
}

SECTION_GROUP.BringAnchoredTabToFront = function (sgID, secNodeID) {
   var anchor_name = SECTION_GROUP.GetAnchorName();
   var section = document.getElementById('section_wrapper'+secNodeID);
   var arrChildren = SECTION_GROUP.GetDOMChildren(section);
   if ( arrChildren )
      for ( var i = 0; i < arrChildren.length; i++ )
         if ( arrChildren[i].type && arrChildren[i].type == 'anchor' &&
              arrChildren[i].name && arrChildren[i].name == anchor_name )
                 ONLOAD_FUNCTIONS[ONLOAD_FUNCTIONS.length] = "SECTION_GROUP.ShowTab("+
                                                  + secNodeID + ","
                                                  + "'" + sgID + "',"
                                                  + "{collapse: 'secIconCol', expand: 'secIconExp'},"
                                                  + "{active: 'active', inactive: 'noactive'}"
                                               + ");";
}

SECTION_GROUP.SetVisibleAccordionSectionOnInit = function (sgID, secNodeID, secIndex) {
   var anchor_name = SECTION_GROUP.GetAnchorName();
   var section = document.getElementById('section_wrapper'+secNodeID);
   var arrChildren = SECTION_GROUP.GetDOMChildren(section);
   if ( arrChildren )
      for ( var i = 0; i < arrChildren.length; i++ )
         if ( arrChildren[i].type && arrChildren[i].type == 'anchor' &&
              arrChildren[i].name && arrChildren[i].name == anchor_name )
                  SECTION_GROUP.Animations.Accordion.options.show = secIndex;
}

SECTION_GROUP.SetImagenEncoding = function ( arrImages ) {
   if ( ! arrImages || typeof arrImages == 'undefined' ) {
      return
   }
   for (var i = 0; i < arrImages.length; i++) {
      var oImage = document.getElementById(arrImages[i]);
      if ( ! oImage || typeof oImage == 'undefined' ) {
         return;
      }
      if ( oImage.src.indexOf('encoding=') == -1 ) {
         oImage.src += '&encoding=%22' + SK__PAGE_INFO.Get('Encoding') + '%22';
      }
   }
}
