/*
css_utils.js.php

Copyright (c) Gareth Hadfield 2008
*/


function setOpacity(aElement, aOpacity){
  // aOpacity is %

  if(isNaN(aOpacity)){
    aOpacity = 100;
  }

  if(IS_IE){
    if(aOpacity==100){
      aElement.style.filter = "";
    }
    else{
      aElement.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+aOpacity+")";
    }

  }

  // This is included even in the IE code because it is used to store the curernt opacity value
  aElement.style.opacity = (aOpacity / 100);
}

function getOpacity(aElement){
  aOpacity = (parseFloat(aElement.style.opacity) * 100);

  return(aOpacity);
}

function find_css_attribute(aCSSArray, aAttributeName){
  // return the index in aCSSArray for the attribute aAttributeName
  var result;
  for(var i=0; i<aCSSArray.length; i++){
    if(aCSSArray[i][0]==aAttributeName){
      result = i;
      break;
    }
  }
  return(result);
}

function css_array(aCSSText){
  // return an array[index][0=name/1=value] representing the given css text
  // text is just the inner part of the css and does not include selectors eg:
  // color:red;

  if((aCSSText == undefined) || (aCSSText == "")){
    return(new Array());
  }

  // remove /*...*/ comments
//  aCSSText = aCSSText.replace(/\/\*[\s\S]*\*\//g, "");

  // escape the : in url() parts
  aCSSText = aCSSText.replace(/url\((.*):(.*)\)/g, "url($1" + escape(":") + "$2)"); // todo use urlencode()?

  if(aCSSText.substr(aCSSText.length-1) == "\n"){
    // remove the ending \n
    aCSSText = aCSSText.substr(0, aCSSText.length-1);
  }

  if(aCSSText.substr(aCSSText.length-1) == ";"){
    // remove the ending ;
    aCSSText = aCSSText.substr(0, aCSSText.length-1);
  }

  var aCSSArray = aCSSText.split(";");
  for(var iCss=0; iCss<aCSSArray.length; iCss++){
    aCSSArray[iCss] = aCSSArray[iCss].split(":");
    aCSSArray[iCss][0] = trim_css(aCSSArray[iCss][0]); // trim spaces and \n from name
  }

  return(aCSSArray);
}

function css_text(aCSSArray){
  // reverse css_array() - return given array as css text
  var tempcssArray = new Array;

  for(var iCss=0; iCss<aCSSArray.length; iCss++){
    tempcssArray[iCss] = aCSSArray[iCss].join(":");
  }

  var result = add_semi_colon(tempcssArray.join(";"));

/*
  if((result.length>0) && (result.substr(result.length-1) != ";")){
    // add the ending ;
    result += ";";
  }
*/
  // unescape the %3A in url() parts
  result = result.replace(/url\((.*)\%3A(.*)\)/g, "url($1:$2)");

  return(result);
}

function full_css_array(aCSSText){
  // return an array[index][0=selector_name/1=css_info] representing the given css text
  // text is full css including selectors eg:
  // .CLASSNAME{color:red;}

  if((aCSSText == undefined) || (aCSSText == "")){
    return(new Array());
  }

  aCSSText = trim_css(aCSSText);

  var aCSSArray = aCSSText.split("}"); // separate entities

  for(var i=0; i<aCSSArray.length; i++){
    aCSSArray[i] = aCSSArray[i].split("{"); // separate selecctor/info
  }

  return(aCSSArray);
}

function trim_css(aText){
  return(trim(aText.replace(/\n/g, "").replace(/\r/g, "")));
}

function strip_html(aText){
  // simple html strip - strips everything between <>
  return(aText.replace(/\<[^\>]*\>/g, ""));
}

function add_semi_colon(aString){
  // adds a ; to the end of the string if the string isnt blank and there isnt one there already
  var result = aString;
  if((result!="") && (result.substr(result.length-1)!=";")){
    result += ";";
  }
  return(result);
}

function class_info_from_css_text(aSubClass, aCSSText){
  // search through the given css text for the class aSubClass
  // and return all the style info for that class as a string
  var result = "";

  var aClasses = aCSSText.split("}");
  for(var i=0; i<aClasses.length; i++){
    aClasses[i] = aClasses[i].split("{");
    if(aClasses[i][0]!=undefined){
      aClasses[i][0] = trim_css(aClasses[i][0]);
    }
    if(aClasses[i][1]!=undefined){
      aClasses[i][1] = trim_css(aClasses[i][1]);
    }
    //debug(aClasses[i][0].toLowerCase());
    //debug(aSubClass.toLowerCase());
    if(aClasses[i][0].toLowerCase()==aSubClass.toLowerCase()){
      if(aClasses[i][1] != undefined){
        result += add_semi_colon(aClasses[i][1]);
      }
    }
  }

  return(add_semi_colon(result));
}

function full_rule_text(aRule){
  return(aRule.selectorText.toLowerCase() + "{" + aRule.style.cssText.toLowerCase() + "}");
}

function get_css_text(aStyleElement){
  // return as text all the css rules contained in style element
  var result = "";
  var aRules;
  if(aStyleElement.cssRules!=undefined){
    aRules = aStyleElement.cssRules;
  }
  else{
    if(aStyleElement.rules!=undefined){
      aRules = aStyleElement.rules;
    }
  }

  if(aRules != undefined){
    for(var iRule=0; iRule<aRules.length; iRule++){
      result += full_rule_text(aRules[iRule]) + "\n";
    }
  }
  return(result);
}

function get_css_classes(aStyleElement){
  // return an array containing all the css class names (the selectors)
  // in the given style element

  var result = new Array();
  var aRules;

  if(aStyleElement.cssRules!=undefined){
    aRules = aStyleElement.cssRules;
  }
  else{
    if(aStyleElement.rules!=undefined){
      aRules = aStyleElement.rules;
    }
  }

  if(aRules != undefined){
    for(var iRule=0; iRule<aRules.length; iRule++){
      if(aRules[iRule].selectorText!=undefined){
        result.push(aRules[iRule].selectorText);
      }
    }
  }
  return(result);
}

function remove_selector(aCssArray){
  // remove any entries that dont have a selector
  // also remove the selector char
  var result = Array();
  var aCss;
  for(var i=0; i<aCssArray.length; i++){
    aCss = aCssArray[i];
    if(aCss.substr(0,1)=="."){
      aCss = aCss.substr(1);
      result[result.length]=aCss;
    }
  }
  return(result);
}

function all_linked_css_classes(aRemoveSelector){
  // return an array containing all the css class names (the selectors)
  // in all the linked css documents
  
  if(aRemoveSelector==undefined){
    aRemoveSelector = false;
  }

  var result = new Array();
  if(document.styleSheets!=undefined){
    var aLinks = document.styleSheets;
    for(var i=0; i<aLinks.length; i++){
      result = result.concat(get_css_classes(aLinks[i]));
    }
  }
  result.sort();
  result = remove_duplicates(result);
  if(aRemoveSelector){
    result = remove_selector(result);
  }
  return(result);
}

function all_linked_css_text(){
  // return as text all the css rules contained in all the linked css
  // documents

  var result = "";
  if(document.styleSheets!=undefined){
    var aLinks = document.styleSheets;
    for(var i=0; i<aLinks.length; i++){
      result += get_css_text(aLinks[i]);
    }
  }

  return(result);
}

function class_info_from_linked_css(aSubClass){
  // search through linked css files for the class aSubClass
  // and return all the style info for that class as a string
  return(class_info_from_css_text(aSubClass, all_linked_css_text()));
}

function class_info_from_style_tags(aSubClass){
  // search through style tags for the class aSubClass
  // and return all the style info for that class as a string

  var result = "";
  var aStyleTags = all_tags("style");
  for(var i=0; i<aStyleTags.length; i++){
    var aStyleTag = aStyleTags[i];
    result += class_info_from_css_text(aSubClass, aStyleTag.innerHTML);
  }

  return(result);
}

function getCSSClassInfo(aSubClass){
  // search through style tags and linked css files for the class aSubClass
  // and return all the style info for that class as a string
  var result = "";
  result += class_info_from_linked_css(aSubClass) + class_info_from_style_tags(aSubClass);
  return(add_semi_colon(result));
}

function remove_position(aCSSText){
  // return the given css text without any postion attribute
  var aCssArray = css_array(aCSSText);
  var result = new Array();
  for(i=0; i<aCssArray.length; i++){
    var aCssName = aCssArray[i][0];
    if(aCssName != "position"){
      result.push(aCssArray[i]);
    }
  }
  return(css_text(result));
}

function get_parent_style_info(aElement){
  var result = "";
  if(aElement.parentNode != undefined){
    var aCSSText = getAttributeText(aElement, "style");
    if(aCSSText != undefined){
      result += add_semi_colon(aCSSText);
    }
    result = get_parent_style_info(aElement.parentNode) + result;
  }
  return(result);
}

function getTargetCSS(aTarget, aName){
  var result;
  if(aTarget.getCSSAttribute!=undefined){
    result = aTarget.getCSSAttribute(aName);
  }
  else{
    result = eval("aTarget.style."+aName);
  }
  return(result);
}

function setTargetCSS(aTarget, aName, aValue){
  if(aTarget.setCSSAttribute!=undefined){
    aTarget.setCSSAttribute(aName, aValue);
  }
  else{
    eval("aTarget.style." + aName + "=\"" + aValue + "\";");
  }
}
