// 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): 
//     nome
// Returns: 
//     none
function tmLinkIcons() {
  $('.ctl_Content a:not(a:has(img))').filter(function() {
    return this.hostname && this.hostname !== location.hostname;
  }).addClass('extLink');

  $(".ctl_Content a[href$='.pdf']").addClass('pdfLink');
  $(".ctl_Content a[href$='.pdf']:has(img)").removeClass('pdfLink'); // remove class for linked images - can't find a better way to do this. annoying...
  
  $(".ctl_Content a[href$='.xls']").addClass('xlsLink');
  $(".ctl_Content a[href$='.xls']:has(img)").removeClass('xlsLink');
  
  $(".ctl_Content a[href$='.doc']").addClass('docLink');
  $(".ctl_Content a[href$='.doc']:has(img)").removeClass('docLink');
  
  $(".ctl_Content a[href$='.ppp'],.ctl_Content a[href$='.ppt']").addClass('pppLink');   
  $(".ctl_Content a[href$='.ppp']:has(img),.ctl_Content a[href$='.ppt']:has(img)").removeClass('pppLink');   
  
  $(".ctl_Content a[href$='.mp3'],.ctl_Content a[href$='.wav'],.ctl_Content a[href$='.wma'],.ctl_Content a[href$='.ogg']").addClass('audioLink'); 
  $(".ctl_Content a[href$='.mp3']:has(img),.ctl_Content a[href$='.wav']:has(img),.ctl_Content a[href$='.wma']:has(img),.ctl_Content a[href$='.ogg']:has(img)").removeClass('audioLink');   
  
  $(".ctl_Content a[href$='.mpg'],.ctl_Content a[href$='.avi'],.ctl_Content a[href$='.mov'],.ctl_Content a[href$='.flv']").addClass('vidLink');
  $(".ctl_Content a[href$='.mpg']:has(img),.ctl_Content a[href$='.avi']:has(img),.ctl_Content a[href$='.mov']:has(img),.ctl_Content a[href$='.flv']:has(img)").removeClass('vidLink');  
}

// 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);
}