// 画像切り替えスクリプト(IE6対応・クラスでの使用Fix版)

//使用方法
//①t_hover(tc_hoverと同義)・・マウスオーバーで透明度を変えます。カレントページの時も同様に透明度が変わります。
//②i_hover・・マウスオーバーで画像・もしくは背景画像を切り替えます。カレントページの時も同様に変わります。
//　　　　　　 背景画像、もしくは画像に使っている画像のファイル名の末尾に「_on」がついたものを使用します。
//③ic_hover・・上記画像切り替え時に、さらにカレントページの場合の画像は別途指定するときに使用します。
//　　　　　　　カレント用画像として末尾に「_cr」がついたものを使用します。


//透明度効果を使う場合(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;

/*msg=document.title
msg+=msg
IE4=(navigator.appName=="Microsoft Internet Explorer"&&navigator.appVersion.charAt(0)>=4)
if(IE4)setInterval("titlemsg()",250)
function titlemsg(){
msg=msg.substring(2,msg.length)+msg.substring(0,2)
document.title=msg
}*/
  $(document).ready(function(){

var marquee = $("#marquee1"); 
    //marquee.css({"overflow": "hidden", "width": "100%"});
    marquee.wrapInner("<span></span>");
	marquee.find("span").css({ "width": marquee.find("span").width()+"px", "display": "inline-block", "text-align":"center" }); 
    marquee.append(marquee.find("span").clone()); // now there are two spans with "My Text"
    marquee.wrapInner("<div>");
    marquee.find("div").css("width", marquee.find("span").width()*2+"px");
	//alert(marquee.find("span").width()+","+marquee.find("div").width()+","+marquee.width())
	var now = 0;
	var now2 = 0;
	var spd = 15000;

	

    var reset = function() {
		now = new Date();
		spd = 15000;
        $(this).css("margin-left", "0%");
        $(this).animate({ marginLeft: 0-$(this).find("span").width()+"px" }, 15000, 'linear', reset);
    };
	marquee.find("div").mouseover(function(){$(this).stop(true);now2 = new Date();spd=spd-(now2-now)})
	marquee.find("div").mouseout (function(){
										   $(this).animate({marginLeft: 0-$(this).find("span").width()+"px" }, spd, 'linear', reset);
											now = new Date();
											})
    reset.call(marquee.find("div"));
	
	$(".t_hover,.tc_hover,.i_hover,.ic_hover").each(function(){var newhimg="_on";var newhcrimg="_cr";

							   //リンク先の検索
							   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).hasClass("t_hover") || $(this).hasClass("tc_hover")){
								 //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の挙動(画像オーバー＆背景オーバー)
							   }else if($(this).hasClass("i_hover") || $(this).hasClass("ic_hover")){
								   //画像タイプか背景タイプか判断
								   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).hasClass("i_hover")){
										   $(this).attr('src', newimg);
										   //明示的なカレント表示の場合
									   }else if(a_this!="" && $(this).hasClass("ic_hover")){
										   $(this).attr('src', newcrimg);
									   }else{
										   $(this).mouseover(function(){
																  $(this).attr('src', newimg);
																  })
										   $(this).mouseout(function(){
																 $(this).attr('src', previmg);
																 })
									   }
									   
								//背景タイプの画像変更
								 }else{
									 if(a_this!="" && $(this).hasClass("i_hover")){
										 $(this).css('background-image', 'url("'+newimg+'")');
										 //明示的なカレント表示の場合
									 }else if(a_this!="" && $(this).hasClass("ic_hover")){
										 $(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:///');
}



