/*
* @url: http://www.yuki.or.jp/
* @name: 共通JS
* @file: main.js
* @date: 2011.03.17
* @auther: Yoshiaki MATSUMURA
*/

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*
jQuery.fn.extend({
  scrollTo : function(speed, easing) {
    if(!$(this)[0].hash || $(this)[0].hash == "#") {
      return false;
    }
    return this.each(function() {
      var targetOffset = $($(this)[0].hash).offset().top;
      $('html,body').animate({scrollTop: targetOffset}, speed, easing);
    });
  }
});
//*/

// 汎用関数の置き換え
var gid = function(id){return document.getElementById(id);}
var windowHeight= function(){return $(window).height();}
var windowWidth    = function(){return $(window).width();}

// ユーザーエージェント判別
var getAgent,agent;
getAgent = window.navigator.userAgent.toLowerCase();
if(getAgent.indexOf('msie 6') > -1){ agent = { 'ua' :'ie', 'v' : 6}}
if(getAgent.indexOf('msie 7') > -1){ agent = { 'ua' :'ie', 'v' : 7}}
if(getAgent.indexOf('msie 8') > -1){ agent = { 'ua' :'ie', 'v' : 8}}
if(getAgent.indexOf('msie 9') > -1){ agent = { 'ua' :'ie', 'v' : 9}}
if(getAgent.indexOf('opera') > -1){ agent = { 'ua' :'opera'}}
if(getAgent.indexOf('firefox') > -1){ agent = { 'ua' :'firefox'}}
if(getAgent.indexOf('chrome') > -1){ agent = { 'ua' :'chrome'}}
if((getAgent.indexOf('chrome') < 0) && (getAgent.indexOf('safari') > -1)){ agent = { 'ua' :'safari'}}

//alert(agent.ua +'/'+agent.v);

// スクロール位置取得
function getScrollPosition() {
var obj = new Object();
obj.x = document.documentElement.scrollLeft || document.body.scrollLeft;
obj.y = document.documentElement.scrollTop || document.body.scrollTop;
return obj;
}

// 各値の定義
var winAttr = ['def','window','image'];
winAttr.def = ['height','width','aspect'];
winAttr.window = new Array();
winAttr.image = [];
// 背景イメージのデフォルトサイズを定義
winAttr.def.height = 600;//768
winAttr.def.width = 1000;//1024
winAttr.def.aspect = winAttr.def.width / winAttr.def.height;
winAttr.def.maxHeight = 600;//769
winAttr.def.maxWidth = 1000;//1024
var bgColor = "#a58756";
var bgUrl = "#a58756 url(../_lib/_css/_img/bg_texture-06.jpg) -50% top repeat";

// ウィンドウサイズ変更後調整
//var bgImg = gid('bgImg');
var offset;
var windowResize =function()
{
// window の高さと幅を取得
winAttr.window.height= $(window).height();
winAttr.window.width= $(window).width();
winAttr.window.aspect= winAttr.window.width / winAttr.window.height;

winAttr.image.orientation= (winAttr.def.aspect > winAttr.window.aspect) ? 'portrate' : 'landscape';
winAttr.image.vertical    = (winAttr.def.height > winAttr.window.height) ? false : true;
winAttr.image.horizon    = (winAttr.def.width  > winAttr.window.width)  ? false : true;
winAttr.image.big        = (!winAttr.image.horizon || !winAttr.image.vertical) ? false : true;



// センタリング
var centering = function(){
var bgImg = gid('mainImage');
var mainImageWidth = (winAttr.image.big) ? winAttr.window.height / 3 * 5: 1000;
//bgImg.width = (bgImg.width == '') ? mainImageWidth : bgImg;
bgImg.width = mainImageWidth;
//alert(mainImageWidth);
if((agent.ua == 'ie' )&&(agent.v < 9)){
var footerMargin = (winAttr.window.width - $('#header').width() ) / 2;
$('#footer,#globalNavi').css('margin-left', footerMargin);
}
//alert(bgImg.width);
var wGap = (winAttr.window.width - mainImageWidth) / 2;
var hGap = (winAttr.window.height - mainImageWidth) / 2;


wGap = ((agent.ua == 'ie')&&(agent.v < 9)) ? wGap -20: wGap;

$('#bgImg').css('margin-left',wGap);
wGap = (wGap <0) ? 0 : wGap;

if((agent.ua != 'ie' )||((agent.ua == 'ie')&&(agent.v == 9))){
var headerWidth = (winAttr.window.height * winAttr.def.aspect >= winAttr.window.width) ? 0 : wGap;
headerWidth = (winAttr.image.orientation == 'landscape') ? headerWidth : headerWidth;
$('#footer,#globalNavi,#globalNaviBg').css({
marginLeft: wGap
});
$('#header').css({
marginLeft: headerWidth
});
}

else if((agent.ua == 'ie' )&&(agent.v == 6)){
var headerWidth = (winAttr.window.height * winAttr.def.aspect >= winAttr.window.width) ? 0 : wGap;
headerWidth = (winAttr.image.orientation == 'landscape') ? headerWidth +20: headerWidth;
var shadowGap = winAttr.window.width - bgImg.width;
shadowGap = (shadowGap < 0) ? 0 : shadowGap / 2;
wGap = (shadowGap <= 20) ? wGap + shadowGap: wGap +20;
$('#header,#globalNaviBg').css({
marginLeft: wGap
});
$('#footer').css({
width: '100%'

});
}
 else if((agent.ua == 'ie' )&&(agent.v <= 7)&&(winAttr.image.big)){
var headerWidth = (winAttr.window.height * winAttr.def.aspect >= winAttr.window.width) ? 0 : wGap;
headerWidth = (winAttr.image.orientation == 'landscape') ? headerWidth +20: headerWidth;
var shadowGap = winAttr.window.width - bgImg.width;
shadowGap = (shadowGap < 0) ? 0 : shadowGap / 2;
wGap = (shadowGap <= 20) ? wGap + shadowGap: wGap +20;
$('#footer,#header,#globalNaviBg').css({
marginLeft: wGap
});
} else {
var headerWidth = (winAttr.window.height * winAttr.def.aspect >= winAttr.window.width) ? 0 : wGap;
headerWidth = (winAttr.image.orientation == 'landscape') ? headerWidth +20: headerWidth;
var shadowGap = winAttr.window.width - bgImg.width;
shadowGap = (shadowGap < 0) ? 0 : shadowGap / 2;
wGap = (shadowGap <= 20) ? wGap + shadowGap: wGap +20;
//$('#subMenu').html(shadowGap);
$('#footer,#globalNavi,#header,#globalNaviBg').css({
marginLeft: wGap
});
}
}

//alert (winAttr.window.height);

// 背景イメージのリサイズ
var headerWidth;
var headerMargin = 0;
var tmpWidth = winAttr.window.height * winAttr.def.aspect;
if(tmpWidth >= winAttr.window.width){
headerWidth = winAttr.window.width;
} else {
headerWidth = tmpWidth;
}

if ((winAttr.image.orientation == 'landscape')&&(winAttr.image.big)){

$('#bgImg').css({
height: winAttr.window.height + 'px',
width: tmpWidth + 'px'
});
$('#footer,#globalNavi,#globalNaviBg').css({
width: headerWidth + 'px'
});
$('#header').css({
width: headerWidth + 'px',
position: 'absolute'
});

} else if((winAttr.image.orientation == 'portrate')&&(winAttr.image.vertical)){

$('#bgImg').css({
height: winAttr.window.height + 'px',
width: tmpWidth + 'px'
});
$('#footer,#globalNavi,#globalNaviBg').css({
width: headerWidth + 'px'
});
$('#header').css({
width: headerWidth + 'px',
position: 'absolute'
});
} else if((winAttr.image.orientation == 'landscape')&&(!winAttr.image.big)){

$('#bgImg').css({
height: winAttr.def.height + 'px',
width: winAttr.def.width + 'px'
});
$('#footer,#globalNavi,#globalNaviBg').css({
width: winAttr.def.width + 'px'
});
$('#header').css({
width: winAttr.def.width + 'px',
position: 'absolute'
});
}
//*/
centering();

//alert(agent.v + '/' +getAgent);
// ie 6以下での処理
offset = getScrollPosition();
if((agent.ua == 'ie')&&(agent.v < 7)){

var fPosY = winAttr.window.height + offset.y -29;
var cPosY = winAttr.window.height + offset.y -51;
var nPosY = offset.y + 79;
var hisPosY = offset.y + 120;

var hPosY = offset.y;
$('#header').css({
martinTop : hPosY
});
$('#bg').css({
top : hPosY,
position: 'absolute'
});
if(document.body.id !='home'){fPosY -= 113};
$('#footer').css({
position: 'absolute',
top: fPosY,
marginLeft : 0
});
}


//スクロール時のヘッダー固定表示
var headerOffset = offset.y -110;
var innerWidth = $('#main').width();
/*
if(offset.y >= 110){
//$('#main').css('margin-top','138px');
$('#header').css({
position: 'fixed',
top: '-110px'
//,width: innerWidth + 'px'
//top:'100px'
});
} else {
//$('#main').css('margin-top','0');
$('#header').css({
position: 'absolute',
//,width: 'auto'
top: '0px'
});
}
*/
//+++++++++++++++++++++++++++++++++ $('body').show();

//スクロール時のササブメニューの固定表示
var subOffset = offset.y -110;
if((agent.ua == 'ie')&&(agent.v == 6)){
return;
} else if((agent.ua == 'ie')&&(agent.v == 7)){
if(offset.y >= 110){
$('.rightCol').css('margin-left','290px')
$('#subMenu').css({
position: 'fixed',
top:'26px',
marginLeft:'-955px'
});
$('#globalNavi,#globalNaviBg').css({
position: 'fixed',
top: '0px',
marginTop: '0'
});
} else {
$('.rightCol').css('margin-left','25px')
$('#subMenu').css({
position: 'static'
,marginLeft:'25px'
});
$('#globalNavi,#globalNaviBg').css({
position: 'absolute',
top: '0',
marginTop: '112px'
});
}
} else {
if(offset.y >= 110){
$('.rightCol').css('margin-left','265px')
$('#subMenu').css({
position: 'fixed',
top:'26px'
});
$('#globalNavi,#globalNaviBg').css({
position: 'fixed',
top: '0px',
marginTop: '0'
});
} else {
$('.rightCol').css('margin-left','25px')
$('#subMenu').css({
position: 'static'
});

$('#globalNavi,#globalNaviBg').css({
position: 'absolute',
top: '0',
marginTop: '112px'
});
}
}
showMain();
}


function showInfo(){
//$('#copyright').fadeIn(500);

$('.section').css({
marginTop:'200px',
display:'block'
});
if(agent.ua != 'ie'){
$('.section').css('opacity',0);
$('.section').animate({
marginTop : 50,
opacity:1
},800,'swing')
} else {
$('.section').animate({
marginTop : 50
},800,'swing',function(){

});
}
}
function showRight(){
//$('#copyright').fadeIn(500);

$('.rightCol').css({
marginTop:'150px'
,display:'block'
});
if(agent.ua != 'ie'){
$('.rightCol').css('opacity',0);
$('.rightCol').animate({
marginTop : 25,
opacity:1
},800,'swing')
} else {
$('.rightCol').animate({
marginTop : 25
},800,'swing',function(){

});
}
}
function showMain(){
$('html').css('background', bgUrl);
if(agent.ua != 'ie'){
$('body').fadeIn(1000,function(){
$('html').css('background', bgUrl);
});
} else {
$('body').show();
}

}
function hideRight(href){
if ( $('.rightCol').html() == null){
window.location.href = href;
} 
//alert($('.rightCol').html());
if(agent.ua != 'ie'){
$('.rightCol').css('opacity',1);
$('.rightCol').animate({
marginTop : 300,
opacity:0
},300,'swing',function(){
window.location.href = href;
});
} else {
$('.rightCol').animate({
marginTop : 300
},300,'swing',function(){
window.location.href = href;
});
}

}
function fontSize(tmp){
//alert(tmp);
var thisFontSize = $('.rightCol').css('font-size');
//alert(tmp +'.'+ thisFontSize);
var nextFontSize;
if((tmp == 'b')&&(thisFontSize == '12px')){nextFontSize = '15px';}
if((tmp == 'b')&&(thisFontSize == '15px')){nextFontSize = '18px';}
if((tmp == 'b')&&(thisFontSize == '18px')){nextFontSize = '18px';}

if((tmp == 's')&&(thisFontSize == '12px')){nextFontSize = '12px';}
if((tmp == 's')&&(thisFontSize == '15px')){nextFontSize = '12px';}
if((tmp == 's')&&(thisFontSize == '18px')){nextFontSize = '15px';}

if(tmp == 'cookie'){nextFontSize = $.cookie('fontSize');}

$('p, .rightCol').css({
fontSize: nextFontSize,
lineHeight: 1.8
});
$.cookie('fontSize',nextFontSize, { path: '/',expires: 7 });
}

function contrast(tmp){
if(agent.ua == 'ie'){
if(tmp == 'l'){
$('.section, .section a, .section a.hover, .section h1 img, .section h2 img, .sublist li img, .section h1 span, .section h2 span, .subList li span,#globalNavi').removeClass('contrast');
} else if(tmp == 'c'){
$('.section, .section a, .section a.hover, .section h1 img, .section h2 img, .sublist li img, .section h1 span, .section h2 span, .subList li span,#globalNavi').addClass('contrast');
}
} else {
if(tmp == 'l'){
$('.section, .section a, .section a.hover, .section h1 img, .section h2 img, .sublist li img, .section h1 span, .section h2 span, .subList li span,#globalNavi').removeClass('contrast');
} else if(tmp == 'c'){
$('.section, .section a, .section a.hover, .section h1 img, .section h2 img, .sublist li img, .section h1 span, .section h2 span, .subList li span,#globalNavi').addClass('contrast');
}
}
$.cookie ('contrast', tmp, { path: '/',expires: 7 });
}
function cookieCheck(){
var infoBtn_cookie = $.cookie('infoBtn');
//alert($.cookie('infoBtn'));
if (typeof infoBtn_cookie != 'undefined'){
if(infoBtn_cookie == 'false'){
$('#rightPane').css({right: -150});
$('#showBtn').css({right: 5});
}
} else {
$.cookie ('infoBtn', true ,{ path: '/',expires: 7 });
}

var contrast_cookie = $.cookie('contrast');
if (typeof contrast_cookie != 'undefined'){
if(contrast_cookie == 'c'){
contrast('c');
} else {
contrast('l');
}
} else {
$.cookie ('contrast', 'l', { path: '/',expires: 7 });
}


var fontSize_cookie = $.cookie('fontSize');
if (typeof fontSize_cookie != 'undefined'){
fontSize('cookie');
//alert(fontSize_cookie);
} else {
$.cookie('fontSize','12px', { path: '/',expires: 7 });
}
//alert($('.sizer').css('font-size'));

}
function showBtnEvent(){
var elm = this.parentNode;
$('#rightPane').animate({
right: 0
},500,'swing');
$('#showBtn').animate({
right: -50
},500,'swing');
$.cookie ('infoBtn', true, { path: '/',expires: 7 });
}
// DOM READY SECTION
function subListShowCheck(id){

var thisIdNo = id.split('_');
var bodyClass = $('body').attr('class').split(' ');
var bodyClassNo = bodyClass[1].split('_');
//alert(thisIdNo[1] + '/'+subMenuSlideFlag);

//alert(thisIdNo[1] +'/'+ parseInt(bodyClassNo[1]));

//alert(bodyClassNo[1] * 1);
if (subMenuSlideFlag == thisIdNo[1]){
return 'true';
//alert('1')
} else if((thisIdNo[1] == bodyClassNo[1] * 1)||
($('#'+id+' ul').text() == '')) {
//alert('true');
return 'true';
}
//alert(subMenuSlideFlag+'/'+thisIdNo[1]);//#############################
return 'false';
}
var subMenuSlideFlag = 0;
var mouseIs = 0;
function showSubList(id){
if(subListShowCheck(id) == 'true') return false;
var thisIdNo = id.split('_')[1];
subMenuSlideFlag = thisIdNo;

$('#'+id+' ul').slideDown(300,function(){subMenuSlideFlag = 0;});
}
var subNaviFlag = '';
function showSubNavi(id){
if(subNaviFlag == id) return false;
$('.globalNaviInner > li > a').removeClass('hover');
$('.gSubNavi').hide();
subNaviFlag = id;

$('#'+id+' > a').addClass('hover');
$('#'+id+' .gSubNavi').slideDown(300,'swing');
}
function closeSubNavi(){
if(subNaviFlag == '') return false;
$('.gSubNavi').hide();
$('.globalNaviInner > li > a').removeClass('hover');
subNaviFlag = '';
}
// Tooltip 表示
function simple_tooltip(target_items, name, css){
 $(target_items).each(function(i){
$("body").append("<div class='"+css+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
var my_tooltip = $("#"+name+i);

if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){

$(this).removeAttr("title").mouseover(function(){
my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
}).mousemove(function(kmouse){
var border_top = $(window).scrollTop();
var border_right = $(window).width();
var left_pos;
var top_pos;
var offset = 20;
if(border_right  >= my_tooltip.width() + kmouse.pageX -offset){
left_pos = kmouse.pageX - offset * 2;
} else{
left_pos = border_right-my_tooltip.width()-offset;
}

if(border_top + (offset *2) >= kmouse.pageY - my_tooltip.height()){
top_pos = border_top +offset;
} else{
top_pos = kmouse.pageY-my_tooltip.height()-offset;
}

my_tooltip.css({left:left_pos, top:top_pos});
}).mouseout(function(){
my_tooltip.css({left:"-9999px"});
});

}

});
}
$(function(){
windowResize();

//setTimeout('windowResize()',100);

//リサイズ・スクロール時にコールバック
$(window).resize(function(){windowResize();});
$(window).scroll(function(){windowResize();});

//オリエンテーションチェンジ
if(window.addEventListener){
window.addEventListener("orientationchange",function(){
windowResize();
},false);
} else if(window.attachEvent){
window.attachEvent("orientationchange",function(){
windowResize();
},false);
}
//setTimeout('windowResize()',1000);

//フォントサイズ変更
//swfobject.registerObject("topFlash");
$('#fontSizeS').click(function(){
fontSize('s');
return false;
});
$('#fontSizeB').click(function(){
fontSize('b');
return false;
});
$('#contrlastL').click(function(){
contrast('l');
return false;
});
$('#contrlastC').click(function(){
contrast('c');
return false;
});

//ライトペン表示・非表示
$('.closeBtn').click(function(){
var elm = this.parentNode;
$(elm).animate({
right: -100
},500,'swing');
$('#showBtn').animate({
right: 5
},500,'swing');
$.cookie ('infoBtn', false, { path: '/',expires: 7 });
//alert($.cookie('infoBtn'));
});
$('#showBtn').hover(function(){
showBtnEvent();
//alert($.cookie('infoBtn'));
});


$('.subList > li').hover(function(){

//mouseIs = this.id;
//if((subMenuSlideFlag > 0)||($('#'+this.id+' ul').text() == '')) return;
//alert (this.id);
if(subListShowCheck(this.id) == 'true') return false;
var thisIdNo = this.id.split('_')[1];
subMenuSlideFlag = thisIdNo;

$('#'+this.id+' ul').slideDown(250,function(){
subMenuSlideFlag = 0;
});
},function(){
//if((subMenuSlideFlag == 1)||($('#'+this.id+' ul').text() == '')) return;
//if((subListShowCheck(this.id) == 'true')||(subMenuSlideFlag > 0)) return false;
if((subListShowCheck(this.id) == 'true')&&(subMenuSlideFlag == 0)) return false;
var thisIdNo = this.id.split('_')[1];
subMenuSlideFlag = thisIdNo;
$('#'+this.id+' ul').slideUp(600,function(){
//$('#'+this.id+' ul').css({display:'none'},function(){
subMenuSlideFlag = 0;
});
});
//*/
/*
$('.subList > li').hover(function(){
if(subMenuSlideFlag > 0) return false;
var thisIdNo = this.id.split('_')[1];
subMenuSlideFlag = thisIdNo;

$('.subList li ul').slideUp(300,function(){});
$('#'+this.id+' ul').slideDown(300,function(){
subMenuSlideFlag = 0;
});
});
//*/
$('div').not('#globalNavi,.gSubNavi').hover(function(){closeSubNavi()});
$('#globalNavi .gnaviBtn').hover(function(){showSubNavi(this.id)});

$('.pageEvent li a,.popup').click(function(){
$('.pageEvent li a').removeClass('active');
$(this).addClass('active');
var eventId = this.href;
var eventIdNo = eventId.split('#');
//alert(eventIdNo[1]);
$('.'+eventIdNo[1] +' > a').addClass('active');
$('.privacy > li,#attached1,#attached2, hr.k, #eventStage > .toggle').fadeOut(200,'swing');
$('#'+eventIdNo[1]).fadeIn(500,'swing',function(){
var pos = getScrollPosition();
//alert(pos.y);
$('html,body').animate({scrollTop: 0}, 500, 'swing');
});
//alert(eventIdNo[1]);


return false;
})
$('a').click(function(){

var lastStr = this.href.substr(this.href.length-1,1);
//alert(lastStr);
if(($(this).parent().hasClass('indA'))||($(this).hasClass('indA'))||(lastStr == '#')){
return;
} else {
//hideRight(this.href);
//return false;
}
});

if((agent.ua == 'ie')&&(agent.v == 6)){
$.each($('img'),function(){
var src = this.src;
var fileName = src.substr(src.lastIndexOf('/') +1 );
var typeNo = fileName.lastIndexOf('.');
var imgType = fileName.substr(typeNo);
//alert(imgType);
if(imgType == '.png'){
//alert(fileName);
var nextType = (fileName.substr(0,typeNo)) +'.gif';
//alert(nextType);
//this.src = nextType;
}
});
//var imgType = $('img').attr('src');//split('.');
}
//コンテンツ非表示
$('#hideMain').click(function(){
$('#header,#globalNavi,#globalNaviBg,#main,#footer').fadeOut(1000,'swing',function(){
if((agent.ua == 'ie')&&(agent.v == 6)){
$('.section').css('display','none');
}
});
return false;
});
//コンテンツ表示
$('#showMain').click(function(){
$('#header,#globalNavi,#globalNaviBg,#main,#footer').show(0,function(){
if(agent.ua == 'ie'){
$('#header,#globalNaviBg,#footer').css({
filter:'progid:DXImageTransform.Microsoft.Gradient(startColorStr=#88331c00,endColorStr=#88331c00);'
});
}
return false;
});
return false;
});
$("area[rel^='prettyPhoto']").prettyPhoto({
custom_markup:'<div width="800px" height="500px"></div>'
});
$('.examList li a[rel^="prettyPhoto"]').prettyPhoto();
cookieCheck();

if((agent.ua != 'ie')||(agent.v != 6)){
simple_tooltip("abbr","abbr","tooltip");
simple_tooltip("img","img","tooltip");
}


$('body').show();
setTimeout('showMain()',250);
});


