// 画像切り替えスクリプト
//透明度効果を使う場合(hover="t")の設定
//マウスオーバーした時の透明度(0～100)
var trans="80";


//画像＆背景画像切り替えを使う場合(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],.alpha_btn").each(function(){
	$("[hover]").each(function(){
	//リンク先の検索
	var box=$(this);
	var a_this="";
	//var aplace=""
	//外側にaがある場合
	if($(this).parents('a').attr("href")){
		a_this=$(this).parents('a').attr("href");
		//aplace = $(this).parents('a');
	//内側にaがある場合
	}else if($(this).find("a").attr("href")){
		a_this=$(this).find("a").attr("href");
		//aplace = $(this).find("a");
	}
	if(a_this){
		//このボタンのリンク先
		var a_this=path2url(a_this);
		//現在のページと同じ場合はカレントフラグ
		if(a_this!=thisurl){a_this="";}
	}
	
	//hover=tの挙動(透明度)
	if($(this).attr('hover')=="t" || $(this).attr('hover')=="tc"){
		//hover=tcでもtでもカレント表示
		if(a_this!=""){
			$(this).css(overtCss)
			//a要素を削除
			//aplace.replaceWith(aplace.html());
		}else{
			$(this).mouseover(function(){
				$(this).css(overtCss)
			})
			$(this).mouseout(function(){
				$(this).css(outtCss)
			})
		}
		
	//hover=iの挙動(画像オーバー＆背景オーバー)
/*,.allbtn>ul>li>a>img,.allbtn img*/
	}else if($(this).attr('hover')=="i" || $(this).attr('hover')=="ic"　){
		//画像タイプか背景タイプか判断
		if($(this).get(0).tagName.match(/^img$/i)){
			//画像タイプの場合
			//元画像を保存
			var previmg=$(this).attr('src');
		}else{
			//背景タイプの場合
			//元画像を保存
			var previmg=$(this).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($(this).get(0).tagName.match(/^img$/i)){
			//画像タイプの場合
			//自動カレント表示
			if(a_this!="" && $(this).attr('hover')=="i"){
				$(this).attr('src', newimg);
				//明示的なカレント表示の場合
			}else if(a_this!="" && $(this).attr('hover')=="ic"){
				$(this).attr('src', newcrimg);
			}else{
				$(this).mouseover(function(){
					$(this).attr('src', newimg);
				})
				$(this).mouseout(function(){
					$(this).attr('src', previmg);
				})
			}
			
		//背景タイプの画像変更
		}else{
			if(a_this!="" && $(this).attr('hover')=="i"){
				$(this).css('background-image', 'url("'+newimg+'")');
				//明示的なカレント表示の場合
			}else if(a_this!="" && $(this).attr('hover')=="ic"){
				$(this).css('background-image', 'url("'+newcrimg+'")');
			}else{
				$(this).mouseover(function(){
				$(this).css('background-image', 'url("'+newimg+'")');
				})
				$(this).mouseout(function(){
					$(this).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:///');
}