/**
 * jQuery.switcher
 * Copyright (c) 2008 Mindia, inc. http://co.mindia.jp/
 * Dual licensed under MIT and GPL.
 * Date: 2009/02/16
 *
 * @projectDescription To make images switch on mouse over its
 * @author Akiyoshi Tanaka akiyoshi@mindia.jp
 *
 */
 
/*
 * The follow code causes all of images including '_off' at src attribute
 * to switch '_on' image on mouse over its.
 * <code>
 * $('img').switcher();
 * </code>
 * 
 * If you want to specify postfix, use this one: 
 * <code>
 * $('img').switcher({'outPostfix': '_up', 'overPostfix': '_down'});
 * </code>
 *
 * @param object options
 */
$.fn.switcher = function(options){
  var options = jQuery.extend({
    'outPostfix': '_off',
    'overPostfix': '_on'
  }, options);
  var pattern = new RegExp('(.*)'+options['outPostfix']+'\\.(\\w+)', 'i');
  return this.each(function(){
    var outSrc = this.src;
    if (typeof(outSrc) == 'undefined'){
      return false;
    }
    if (outSrc.match(pattern)){
      var filename = RegExp.$1;
      var ext = RegExp.$2;
    }else{
      return null;
    }
    var overSrc = filename + options['overPostfix'] + '.' + ext;
    var image = new Image();
    image.src = overSrc;
    $(this).hover(
      function(){
        this.src = image.src;
        return null;
      },
      function(){
        this.src = outSrc;
        return null;
      }
    );
    return null;
  });
}