// tmFixPngs
// Searches for pngs and enables the proprietary AlphaImageLoader filter allowing to transpareny layer to be displayed properly in IE6. Works for both normal and background images 
//
// Param(s): 
//     area_of_effect
//       Limits the area of the DOM effected < UNTESTED!!
// Returns: 
//     none
function tmFixPngs(area_of_effect) {
  var shim = '/images/x.gif';
  var root = "";
    
  // TODO: test area of effect functionality!
  if(area_of_effect) {
    root = "#"+area_of_effect+" ";
  }
  
  // replace IMG PNGs
  $(root+"img[src$='.png']").each(function() {
    var $$  = $(this);
    var css_position = $$.css("position");

    if(css_position == "absolute") {
      css_position = "absolute";
    } else {
      css_position = "relative";
    }
    
    var hack = { 
      filter : function(src) {
        return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod=crop,src='"+src+"')";
      }
    };
    
    $$.css({
      filter:hack.filter($$.attr("src")), 
      width:$$.width(), 
      height:$$.height(), 
      position:css_position
    }).attr("src", shim);
  });
  
  // replace background PNGs
  $(root+"a, "+root+"div, "+root+"span, "+root+"input, "+root+"button, "+root+"p, "+root+"h3").each(function() {
    var $$  = $(this);
    var src = $$.css("background-image");
      
    if(src.match(/\.png/i)) {     
      var mode = 'scale';
      var src_trimmed = src.substring(5,src.length-2); 
 
      if ($$.css("background-repeat") == "no-repeat") {
        mode = 'crop';
      }

      var css_position = $$.css("position");
      

      if(css_position == "absolute") {
        css_position = "absolute";
      } else {
        css_position = "relative";
      }
 
      var hack = { 
        filter : function(src, mode) {
          return "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,sizingMethod="+mode+",src='"+src_trimmed+"')";
        }
      };
      
      $$.css({
        filter:hack.filter(src, mode), 
        width:$$.width(), 
        height:$$.height(), 
        position:css_position,
        backgroundImage:"url('"+shim+"')" 
      });
    }
  }); 
}

// tmLinkIcons
// Adds classes to external links and links pointing to certain filetypes
//
// Param(s): 
//   area_of_effect
//     ID of the area of the DOM to limit t6his process to
//   exclude_class
//     Class applied to links which are to be excluded from this process
// Returns: 
//   n/a
function tmLinkIcons(area_of_effect, exclude_class) {
  // default area of effect
  var root = "";
  
  // define filestypes
  var filetypes = []
  filetypes[0] = Array("pdf"); // pdf
  filetypes[1] = Array("xls", "xlsx"); // excel spreadsheet
  filetypes[2] = Array("doc", "docx"); // word doc
  filetypes[3] = Array("ppp", "ppt"); // powerpoint
  filetypes[4] = Array("mp3", "wav", "flac", "ogg", "wma"); // audio
  filetypes[5] = Array("mpg", "avi", "mov", "wmv", "flv"); // video
  filetypes[6] = Array("jpeg", "jpg", "gif", "png", "bmp", "tif"); // image
  filetypes[7] = Array("zip", "rar", "ace", "tz", "gz", "7zip", "7z"); // archive
  // to add a new filetype: filetypes[x] = Array("xxx", "xxx", "xxx");  then make sure you add the class with the array key below
  
  // filetype_class keys match up to filetypes array above. the value below is the class that will be applied to the link
  var filetype_class = {0:'pdfLink', 
                        1:'xlsLink', 
                        2:'docLink', 
                        3:'pppLink', 
                        4:'audioLink', 
                        5:'videoLink', 
                        6:'imageLink', 
                        7:'archiveLink'}; 
  
  // TODO: test area of effect functionality!
  if(area_of_effect) {
    root = "#"+area_of_effect+" ";
  }
  
  // add icons to extenal links
  $(root+"a:not(.link_exc):not(:has(img))").filter(function() {
    // if the domain of the link target if different to the current domain, apply an external link class to the link
    return this.hostname && this.hostname !== location.hostname;
  }).addClass('extLink'); 
  
  // add icon to file links - loop thru filetypes
  $.each(filetypes, function(key, file_exts) { 
    // loopthru extensions per filetype
    $.each(file_exts, function(){ 
      // add class to links that do not have exclude class and that do not contain an image
      $(root+"a[href$='."+this+"']:not(."+exclude_class+"):not(:has(img))").addClass(filetype_class[key]);  
    });
  });
}

// tmTrimAll
// Removes whitespace from beginning and end of supplied string
//
// Param(s): 
//     sString. 
//       The string to be trimmed
// Returns: 
//     The trimmed string 
function tmTrimAll(sString) {
  while (sString.substring(0,1) == ' ') {
    sString = sString.substring(1, sString.length);
  }
  
  while (sString.substring(sString.length-1, sString.length) == ' ') {
    sString = sString.substring(0,sString.length-1);
  }
  
  return sString;
}

// trimAll
// alias of tmTrimAll - all references to this function need to be removed and replaced with tmTrimAll
//
// Param(s): 
//     sString. 
//       The string to be trimmed
// Returns: 
//     The trimmed string 
function trimAll(sString) {
  return tmTrimAll(sString);
}