﻿$(function(){
	var cookie = readCookie("style");
	var title = cookie ? cookie : getPreferredStyleSheet();
	setActiveStyleSheet(title);
	
	hover(".","gallery a");
	
	hover(".","hover");
	hover("#","searchbtn");
	hover(".","li-hover li a");
	hover(".","td-hover td a");
	
	setScrollAnimation();
	
});



function popup(href){
	window.open(href, 'popup', 'width=600, height=760, menubar=yes, toolbar=yes, scrollbars=yes');
}

/* ---------------------------------------------------------------------------- */
//文字サイズ変更
$(window).unload(function(){
  var title = getActiveStyleSheet();
  createCookie("style", title, 0);
});


function setActiveStyleSheet(title) {
	jQuery("link[rel*='stylesheet'][title]").each(function(){
		this.disabled = true;
		this.disabled = jQuery(this).attr("title")!=title;
	});
}


function getActiveStyleSheet() {
	var titlename = null;
	jQuery("link[rel*='stylesheet'][title]").each(function(){
		if(!this.disabled)
			titlename = jQuery(this).attr("title");
	});
	return titlename;
}


function getPreferredStyleSheet() {
	var titlename = null;
	jQuery("link[rel^='stylesheet'][title]").each(function(){
			titlename = jQuery(this).attr("title");
	});
	return titlename;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

/* ---------------------------------------------------------------------------- */



//imgのsrcに_onを付け外しする
$(function(){
	$("img.rollover").mouseover(function(){
		$(this).attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/,"$1_on$2"));
	}).mouseout(function(){
		$(this).attr("src",$(this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/,"$1$2"));
	}).each(function(){
		$("<img>").attr("src",$(this).attr("src").replace(/^(.+)(\.[a-z]+)$/,"$1_on$2"));
	});
});



//ページ内リンクでIDの個所へスクロール
function setScrollAnimation(){
	var speed = "300";
	
	$('a').click(function() {
		var jumpTo = $(this).attr('href');
		if(jumpTo == '#pagetop') {
			$('html,body').animate({ scrollTop: 0 }, speed)
			return false;
		}
		else if(jumpTo == '#') {
			return false;
		}
		else if(jumpTo.charAt(0) == '#') {
			$('html,body').animate({ scrollTop: $(jumpTo).offset().top }, speed)
			return false;
		}
	});
}


//サイト内検索
$(function(){
	var msg = "入力してください";

	$(".serch").val(msg).css("color","#ccc").one("focus",function(){
		$(this).val("").css("color","#000");
	}).blur(function(){
		if($(this).val()==""){
			$(this).val(msg).css("color","#ccc").one("focus",function(){
				$(this).val("").css("color","#000");
			});
		}
	})
});

//mouseoverによる透過
function hover(p, tgt){
    $(p+tgt).hover(function(){
       $(this).stop().fadeTo(100, 0.5); // マウスオーバーで透明度を50%にする
    },function(){
       $(this).stop().fadeTo(100, 1.0); // マウスアウトで透明度を100%に戻す
    });
}

