// 画像切り替えスクリプト
//透明度効果を使う場合(hover="t")の設定
//マウスオーバーした時の透明度(0〜100)
var trans="70";


//画像＆背景画像切り替えを使う場合(hover="i")の設定
//切り替え画像のファイル名につく接尾語
var newhimg="_on";
//明示的なカレント画像を使う場合(hover="ic")のファイル名につく接尾語
var newhcrimg="_cr";


var overtCss = {
"filter": "alpha(opacity=trans)",
"-moz-opacity":trans*0.01,
"opacity":trans*0.01
}
var outtCss = {
"filter": "alpha(opacity=100)",
"-moz-opacity":"1",
"opacity":"1"
}
var thisurl=location.href;

  $(document).ready(function(){
	$("[hover]").each(function(){
							   //リンク先の検索
							   var box=$(this);
							   var a_this="";
							   //var aplace=""
							   //外側にaがある場合
							   if(box.parents('a').attr("href")){
								   a_this=box.parents('a').attr("href");
								   //aplace = box.parents('a');
							   //内側にaがある場合
							   }else if(box.find("a").attr("href")){
								   a_this=box.find("a").attr("href");
								   //aplace = box.find("a");
							   }
							   if(a_this){
								   //このボタンのリンク先
								   var a_this=path2url(a_this);
								   //現在のページと同じ場合はカレントフラグ
								   if(a_this!=thisurl){a_this="";}
							   }
							   
							   //hover=tの挙動(透明度)
							   if(box.attr('hover')=="t" || box.attr('hover')=="tc"){
								 //hover=tcでもtでもカレント表示
								 if(a_this!=""){
									 box.css(overtCss)
									 //a要素を削除
									 //aplace.replaceWith(aplace.html());
								 }else{
									 box.mouseover(function(){
																box.css(overtCss)
																})
									 box.mouseout(function(){
																box.css(outtCss)
																})
								 }
								 
								//hover=iの挙動(画像オーバー＆背景オーバー)
							   }else if(box.attr('hover')=="i" || box.attr('hover')=="ic"){
								   //画像タイプか背景タイプか判断
								   if(box.get(0).tagName.match(/^img$/i)){
									   //画像タイプの場合
									   //元画像を保存
									   var previmg=box.attr('src');
								   }else{
									   //背景タイプの場合
									   //元画像を保存
									   var previmg=box.css('background-image').replace(/url\(['"]?(.+?)['"]?\)/, "$1")
								   }
								 var newimg=previmg.replace(/(.+?)(\.jpg|\.gif|\.png)/, "$1"+newhimg+"$2")
								 var newcrimg=previmg.replace(/(.+?)(\.jpg|\.gif|\.png)/, "$1"+newhcrimg+"$2")
								 //画像をスワップ
								 var thisoverswap=new Image();
								 var thisoutswap=new Image();
								 if(document.images){
								 thisoverswap.src=newimg;
								 thisoutswap.src=previmg;
								 }
								 
								 
								 //画像タイプの場合の画像変更
								 if(box.get(0).tagName.match(/^img$/i)){
									   //画像タイプの場合
									   //自動カレント表示
									   if(a_this!="" && box.attr('hover')=="i"){
										   box.attr('src', newimg);
										   //明示的なカレント表示の場合
									   }else if(a_this!="" && box.attr('hover')=="ic"){
										   box.attr('src', newcrimg);
									   }else{
										   box.mouseover(function(){
																  box.attr('src', newimg);
																  })
										   box.mouseout(function(){
																 box.attr('src', previmg);
																 })
									   }
									   
								//背景タイプの画像変更
								 }else{
									 if(a_this!="" && box.attr('hover')=="i"){
										 box.css('background-image', 'url("'+newimg+'")');
										 //明示的なカレント表示の場合
									 }else if(a_this!="" && box.attr('hover')=="ic"){
										 box.css('background-image', 'url("'+newcrimg+'")');
									 }else{
										 box.mouseover(function(){
																box.css('background-image', 'url("'+newimg+'")');
																})
										 box.mouseout(function(){
															   box.css('background-image', 'url("'+previmg+'")');
															   })
									 }
								 }
							   }
							   })



})

//相対＆絶対パスをURLに変換するfunction 拝借:http://blog.3ot.net/design/javascript/20090605000251.html
function path2url(path){
  if(path.match(/^\/.+$/)){
    path = location.protocol + '//' + location.hostname + path;
  }
  p = 0, arr = [];
  r = window.location.href;
  path = (path + '').replace('\\', '/');
  if(path.indexOf('://') !== -1){
    p = 1;
  }
  if(!p){
    path = r.substring(0, r.lastIndexOf('/') + 1) + path;
  }
  arr = path.split('/');
  path = [];
  for(k in arr){
    if (arr[k] == '.') {
      continue;
    }
    if (arr[k] == '..') {
      if (path.length > 3) {
        path.pop();
      }
      }else{
        if((path.length < 2) || (arr[k] !== '')){
        path.push(arr[k]);
      }
    }
  }
  return path.join('/').replace(/^file:\/\//, 'file:///');
}
