(function($){
 
 $.fn.addImgSelected = function(){
  return this.each(function(){
   if(this.src&&!this.src.match(/-selected\.(gif|jpg|png)$/)){
    this.src.match(/\.(gif|jpg|png)$/);
    var ext = RegExp.$1;
    this.src = this.src.replace('-over.'+ext, '.'+ext);
    this.src = this.src.replace('.'+ext, '-selected.'+ext);
   }
  });
 };
 
 $.fn.removeImgSelected = function(){
  return this.each(function(){
   if(this.src){
    this.src.match(/\.(gif|jpg|png)$/);
    var ext = RegExp.$1;
    this.src = this.src.replace('-selected.'+ext, '-over.'+ext);
   }
  });
 };
 
 $.fn.addImgOver = function(){
  return this.each(function(){
   if(this.src&&!this.src.match(/-over\.(gif|jpg|png)$/)&&!this.src.match(/-selected\.(gif|jpg|png)$/)){
    this.src.match(/\.(gif|jpg|png)$/);
    var ext = RegExp.$1;
    this.src = this.src.replace('.'+ext, '-over.'+ext);
   }
  });
 };
 
 $.fn.removeImgOver = function(){
  return this.each(function(){
   if(this.src){
    this.src.match(/\.(gif|jpg|png)$/);
    var ext = RegExp.$1;
    this.src = this.src.replace('-over.'+ext, '.'+ext);
   }
  });
 };
 
 $.fn.overimage = function(){
  return this.each(function(){
   if(this.overimage_already==true){ return; }
   if(!this.src){ return; }
   
   //カーソル化
   $(this).css('cursor','pointer');
   
   //先読み込み
   if(this.src.match(/\.(gif|jpg|png)$/)){
    var ext = RegExp.$1;
    var url1 = this.src.replace('.'+ext, '-over.'+ext);
    $('<img />').hide().attr('src', url1).appendTo(document.body).load(function(){ $(this).remove(); }).error(function(){ $(this).remove(); });
    var url2 = this.src.replace('.'+ext, '-selected.'+ext);
    $('<img />').hide().attr('src', url2).appendTo(document.body).load(function(){ $(this).remove(); }).error(function(){ $(this).remove(); });
   }
   
   $(this).hover(
    function(){ $(this).addImgOver(); },
    function(){ $(this).removeImgOver(); }
   );
   this.overimage_already = true;
  });
 };
 
})(jQuery);

$(document).ready(function() {
 $('.overimage').overimage();
 $('.selectedObject').addImgSelected();
 $('.selectedObject').parents("li").addClass("selectedListParents");
});

