/*
var hReg = null;
*/
var bfCss = {};
bfCss.getCss = function(sSelector,sElement,bRetInt,bMatchSearch,oDocument){
  var bRetInt = (typeof(bRetInt) == 'undefined') ? 0 : 1;
  var bMatchSearch = (typeof(bMatchSearch) == 'undefined') ? 0 : bMatchSearch;
  var oDocument =  (typeof(oDocument) == 'undefined') ? document : oDocument;
  for(var i=0; i<oDocument.styleSheets.length; i++){
    for(var j=0; j<oDocument.styleSheets[i][sRules].length; j++){
      if((bMatchSearch && oDocument.styleSheets[i][sRules][j].selectorText.indexOf(sSelector) != -1) ||
         (!bMatchSearch && oDocument.styleSheets[i][sRules][j].selectorText == sSelector)
        ){
        if (sElement.indexOf('-') >= 0){
          sElement = sElement.replace(/\-(\w)/g, function (sMatch, sSub1){
            return sSub1.toUpperCase();
          });
        }
        var xRet = oDocument.styleSheets[i][sRules][j].style[sElement];
        if (bRetInt){
          var aInt;
          xRet = (aInt = xRet.match(/^[0-9]+/)) ? parseInt(aInt[0]) : 0;
        }
        return xRet;
      }
    }
  }
}
bfCss.rmvCssSelector = function(sClass,sSelector){
  for(var i=0; i<document.styleSheets.length; i++){
    for(var j=0; j<document.styleSheets[i][sRules].length; j++){
      if(document.styleSheets[i][sRules][j].selectorText.search(sClass) != -1){
        if (document.styleSheets[i][sRules][j].selectorText.search(sSelector)!=-1){
          var tmp=', .'+sSelector;
          var csstext = document.styleSheets[i][sRules][j].cssText.replace(tmp,'');
          document.styleSheets[i].deleteRule(j);
          document.styleSheets[i].insertRule(csstext,j);
          //document.styleSheets[i][sRules][j].selectorText = document.styleSheets[i][sRules][j].selectorText.replace(tmp,'');
        }
        return;
      }
    }
  }
}
bfCss.addCssSelector = function(sClass,sSelector){
  for(var i=0; i<document.styleSheets.length; i++){
    for(var j=0; j<document.styleSheets[i][sRules].length; j++){
      if(document.styleSheets[i][sRules][j].selectorText.search(sClass) != -1){
        if (document.styleSheets[i][sRules][j].selectorText.search(sSelector)==-1){
          var csstext = document.styleSheets[i][sRules][j].cssText.replace(' {',', .'+sSelector+' {');
          document.styleSheets[i].deleteRule(j);
          document.styleSheets[i].insertRule(csstext,j);
        }
        return;
      }
    }
  }
}
bfCss.changeCss = function(sSelector,sElement,sVal,bMatchSearch,oDocument,sFile){
  var bMatchSearch = (typeof(bMatchSearch) == 'undefined') ? 0 : bMatchSearch;
  var oDocument =  (typeof(oDocument) == 'undefined') ? document : oDocument;
  var sFile = (typeof(sFile) == 'undefined') ? '' : sFile;
  if (sSelector.indexOf('_hover') != -1){
    hHover = {};
  }
  if (bIE && sVal.search('!important')){
    sVal = sVal.replace(/ !important/,'');
  }
  for(var i=0; i<oDocument.styleSheets.length; i++){
    if (sFile.length == 0 || oDocument.styleSheets[i].href.indexOf(sFile) >= 0){
      for(var j=0; j<oDocument.styleSheets[i][sRules].length; j++){
        if((bMatchSearch && oDocument.styleSheets[i][sRules][j].selectorText.indexOf(sSelector) != -1) ||
           (!bMatchSearch && oDocument.styleSheets[i][sRules][j].selectorText == sSelector)
          ){
          var sOld = oDocument.styleSheets[i][sRules][j].style[sElement];
          if (typeof(oDocument.styleSheets[i][sRules][j].style.setProperty) == 'function'){
            //var bImportant = (sVal.indexOf('!important') != -1 || oDocument.styleSheets[i][sRules][j]..cssText.indexOf('!important')) ? 1 : 0;
            var bImportant = (sVal.indexOf('!important') != -1  ) ? 1 : 0;
            var sVal = (bImportant) ? sVal.substr(0,sVal.length-11) : sVal;
            var hElementTrans = {
              'background':'background',
              'backgroundColor':'background-color',
              'backgroundPosition':'background-position',
              'backgroundRepeat':'background-repeat',
              'backgroundImage':'background-image',
              'border':'border',
              'borderStyle':'border-style',
              'borderLeftStyle':'border-left-style',
              'borderRightStyle':'border-right-style',
              'borderTopStyle':'border-top-style',
              'borderBottomStyle':'border-bottom-style',
              'borderWidth':'border-width',
              'borderLeftWidth':'border-left-width',
              'borderRightWidth':'border-right-width',
              'borderTopWidth':'border-top-width',
              'borderBottomWidth':'border-bottom-width',
              'borderLeftColor':'border-left-color',
              'borderRightColor':'border-right-color',
              'borderTopColor':'border-top-color',
              'borderBottomColor':'border-bottom-color',
              'borderColor':'border-color',
              'marginTop':'margin-top',
              'marginBottom':'margin-bottom',
              'marginLeft':'margin-left',
              'marginRight':'margin-right',
              'paddingLeft':'padding-left',
              'paddingRight':'padding-right',
              'paddingTop':'padding-top',
              'paddingBottom':'padding-bottom',
              'fontSize':'font-size',
              'fontFamily':'font-family',
              'fontWeight':'font-weight',
              'fontStyle':'font-style',
              'textAlign':'text-align',
              'lineHeight':'line-height'
            };
            if (sVal.length == 0){
              oDocument.styleSheets[i][sRules][j].style.removeProperty((typeof(hElementTrans[sElement]) != 'undefined') ? hElementTrans[sElement] : sElement);
            }else{
              oDocument.styleSheets[i][sRules][j].style.setProperty(((typeof(hElementTrans[sElement]) != 'undefined') ? hElementTrans[sElement] : sElement),sVal,((bImportant == 1) ? 'important' : ''));
              if (sVal != oDocument.styleSheets[i][sRules][j].style[sElement] && sVal != sOld){
                oDocument.styleSheets[i][sRules][j].style.setProperty(((typeof(hElementTrans[sElement]) != 'undefined') ? hElementTrans[sElement] : sElement),sVal,'important');
              }
            }
            // geschmeidiger Cheat, der den FF dazu bringt, den Border erneut zu rendern *hust*
            if (sElement.indexOf('border') != -1){
              var oStyleObj = null;
              for(var a=0; a<oDocument.styleSheets.length; a++){
                for(var b=0; b<oDocument.styleSheets[a][sRules].length; b++){
                  if(oDocument.styleSheets[a][sRules][b].selectorText == '.table_plain'){
                    var oStyleObj = oDocument.styleSheets[a][sRules][b].style;
                    break;
                    break;
                  }
                }
              }
              function resetBorderCollapse(){
                oStyleObj.setProperty('border-collapse','collapse','');
              }
              if (oStyleObj != null){
                oStyleObj.setProperty('border-collapse','separate','');
                setTimeout(resetBorderCollapse,10);
              }
            }

          }else{
            if (sVal.length == 0){
              var sCssRule = sElement.replace(/\-(\w)/g, function (sMatch, sSub1){
                return sSub1.toUpperCase();
              });
              var hSplit = {
                'margin':  1,
                'padding': 1,
                'border':  1
              };
              if (typeof(hSplit[sCssRule]) == 'undefined'){
                oDocument.styleSheets[i][sRules][j].style.removeAttribute(sCssRule);
              }else{
                oDocument.styleSheets[i][sRules][j].style.removeAttribute(sCssRule+'Top');
                oDocument.styleSheets[i][sRules][j].style.removeAttribute(sCssRule+'Right');
                oDocument.styleSheets[i][sRules][j].style.removeAttribute(sCssRule+'Bottom');
                oDocument.styleSheets[i][sRules][j].style.removeAttribute(sCssRule+'Left');
              }
            }else{
              oDocument.styleSheets[i][sRules][j].style[sElement] = sVal;
            }
          }
          return;
        }
      }
    }
  }
}
