
$(window).load(function() {
//$(function() {
	setSize();
	
	$(window).resize(function(){
		setSize();

		// scroll
		setSlider();
	});

	// scroll
	setSlider();

	// lightbox
//	$('.picts li a').lightBox();

});

function setSize() {
    var minimum = false;
    
	var userAgent = window.navigator.userAgent.toLowerCase();
	var appVersion = window.navigator.appVersion.toLowerCase();

/*
	if (userAgent.indexOf("msie") > -1) {
		if (appVersion.indexOf("msie 6.0") > -1) {
			alert("IE6");
		}
		else if (appVersion.indexOf("msie 7.0") > -1) {
			alert("IE7");
		}
		else if (appVersion.indexOf("msie 8.0") > -1) {
			alert("IE8");
		}
		else {
			alert("Unknown");
		}
	}
	else if (userAgent.indexOf("firefox") > -1) {
		alert("Firefox");
	}
	else if (userAgent.indexOf("opera") > -1) {
		alert("Opera");
	}
	else if (userAgent.indexOf("chrome") > -1) {
		alert("Google Chrome");
	}
	else if (userAgent.indexOf("safari") > -1) {
		alert("Safari");
	}
	else {
		alert("Unknown");
	}
*/

    // ***********************
	// window size
    // ***********************
    var winW = window.innerWidth;
    var winH = window.innerHeight;
	if(!window.innerWidth) {
		// for ie
		winW = document.body.clientWidth;
	}
	// 高さを保持
	if(winH > 0) globalH = winH;

    // ***********************
	// background size
    // ***********************
	// コンテンツ表示領域（footerぞ退く）
    var setW = winW;    // とりあえず幅は100%
    var setH = Math.round(winW*1080/1920) - 40;    // 高さは画面幅から縦横比で求めたものからfooterとかぶる分を引く
    var setT = 0;
    var setL = Math.round((winW-setW)/2);
	if(!window.innerWidth) {
		// for ie
	    setH = setH - 180;
	}


	// コンテンツエリアがウインドウの高さを超える場合は
	// 幅を基準に高さを算出する
	if(winH == 0) {
		// for ie
		// winHが取得できなかったときはあらかじめ保持しておいた高さをセット
		winH = globalH;
	}
	if((setH) > winH) {
		setH = winH - 40;
	}

    // min-sizeを設定
    if(setW < 1024) {
    	setW = 1024;
    	setH = 600 - 60;
    	setL = 0;
    	if((setH + 60) < 600) {
    		minimum = true;
    	}
    }

	// 背景画像の大きさを設定
    var backW = setW;
    var backH;
    // 背景の高さはとりあえず幅は100%でそれを元に決める
    backH = Math.round(winW*1080/1920);
    if(winW < 1024) {
        backH = Math.round(1024*1080/1920);
    }

//        'bottom': 0,
    $('#background').css({
        'position': 'fixed',
        'top': 0,
        'left': setL,
        'height': backH,
        'width': backW,
        'z-index': -1,
        'margin': '0 0 0 0'
    });

    // ***********************
    // container size
    // ***********************
    var containerW = setW;
    var containerH = setH - 30;
    $('#container').css({
        'width': containerW,
        'height': containerH,
        'position': 'absolute',
        'top': 0,
        'left': setL
    });
    // ***********************
    // the left content
    // ***********************
    var contentW = Math.round(setW*0.5) - 60;	// 横幅全体の半分からマージンを引いた値
    var contentH = containerH-80;	// コンテナサイズから上下のマージンを引いた値
    $('#content').css({
        'width': contentW,
        'height': contentH
    });

    var conHeadW = contentW-20;
    $('.content_header_center').css({
    	'width': conHeadW
    });
    $('.content_header_title').css({
    	'width': contentW
    });

    $('.content_inner_category_left').css({
        'height': setH - 137 - 100 - 60
    });

    $('.v_center').css({
        'width': Math.round(contentW - 70 - 30 - 70)
    });

    $('#footer').css({
        'width': setW,
        'position': 'absolute',
        'top': setH - 15,
        'left': setL
    });

    // ***********************
    // the right content
    // ***********************
    var con2H = setH - 100;
    $('#content_2').css({
        'width': contentW,
        'height': con2H
    });

    $('.content_inner_category_right').css({
        'height': con2H - 80
    });


    /* discography page setting
    ----------------------------------- */
    $('#post-51 div.entry-content').css({
        'height': con2H - 30
    });

    /* profile page setting
    ----------------------------------- */
//        'height': Math.round(setH*0.8)
    $('.content_inner_page').css({
        'height': con2H - 20
    });
    
//    var profileImgW = contentW - 80;
//    var profileImgW = 452 - 80; // 固定
//    var profileImgH = Math.round((profileImgW - 40) * 3/4);
    var profileImgW = 580; // 固定
    var profileImgH = 405;
    if(profileImgW > contentW - 80) {
        profileImgW = contentW - 80;
        profileImgH = Math.round((profileImgW - 40) * 3/4);
    }
    $('.img-profile').css({
        'width': profileImgW,
        'height': profileImgH
    });

	// 写真の下部分のコンテナ
//        'width': contentW - 10 - 40,
    $('.under_the_img').css({
        'width': profileImgW+10,
        'height': con2H - 80 - profileImgH + 20
    });

	// 写真の下部分のコンテナ内の文章
//    $('.under_the_img #mcs_container .customScrollBox .container .content p').css({
//        'width': contentW - 10 - 40 - 20
//    });
    var ouh = $('#content_2').height();
    var inh = $('#mcs_container_right .customScrollBox .container .content').height();
//    alert(ouh + ' ' + inh);

    // ***********************
    // top page
    // ***********************
//    var conW = Math.round(setW*0.5*0.9);
//    $('#content_top_page').css({
//        'width': contentW,
//        'height': Math.round(setH*0.3)
//    });

//    var setCILB = Math.round(setH*0.8 - 137 - (30 + 80 + 15 + 20) - 10 - 10 - 10);
    var setCILB = Math.round(contentH - 137 - 60 - 158);
    if(minimum) {
        setCILB = Math.round(setH*0.8 - 137 - 60 - 150);
    }
    $('#top_content_inner_left_bottom').css({
        'height': setCILB
    });

    $('.post-description').css({
        'width': contentW -165
    });

//	$('ul.thumbs').css({
//	    'float' : 'right',
//	    'margin' : '0px 30px 0px 0px'
//	});
//	$('ul.thumbs li').css({
//	    'float' : 'left',
//	    'padding' : '0',
//	    'margin' : '0px 5px 0px 5px',
//	    'list-style' : 'none url("") outside'
//	});

//	    'margin' : '0 0 0 20px',
	$('.picts').css({
	    'margin' : '0 0 0 0',
	    'padding' : '0 0 0 0',
	    'position': 'relative',
	    'background' : 'rgba(14,22,20,0.9)',
	    'width' : contentW - 40
	});
	
//	var thumbW = Math.round((contentW - 40)/4) - 20;
	var thumbW = 80;	// 写真はwidth=80で固定
	var thumbH = Math.round(thumbW * 2/3);
	$('ul.picts li').css({
	    'width' : thumbW,
	    'height' : thumbH
	});

	$('ul.picts li img').css({
	    'width' : thumbW,
	    'clip' : 'rect(0px,'+thumbW+'px,'+thumbH+'px,0px)'
	});

    $('#content_inner_top_right').css({
        'height': setH - 100
    });


    // ***********************
	// movie style
    // ***********************
    var movieW = (contentW - 60) / 2 - 12;
    var movieH = movieW * 364 / 560;
	$('.movieBox').css({
		    'width' : movieW,
		    'height' : movieH
	});
	$('.movie_left').css({
		    'width' : movieW,
		    'height' : movieH
	});


    // ***********************
	// 12 pictures
    // ***********************
    if($('#twelvePictsBoxWrapper')) {
		$('#twelvePictsBoxWrapper').css({
		    'width' : contentW-76
		});
		$('#twelvePictsBox').css({
		    'width' : contentW - 76
		});
		marginLeft = 10;	// 実は計算したわけではない
		$('#twelvePictsBox ul').css({
		    'margin-left': marginLeft
		});


	}


}

function setSlider() {
	var setH;
	var setConH;
	var setConInH;

	if($("#mcs_container")) {
		// 内容量によってスライダーの長さを設定
		setConH = $("#mcs_container").height();
		setConInH = $("#mcs_container .customScrollBox .container .content").innerHeight();

		$("#mcs_container").mCustomScrollbar("vertical",0,"",1.05,"fixed","yes","yes", getScrollSpeed(setConH, setConInH));

		// 内容が全て表示される範囲でなるべくスライダーが大きくなるよう設定
		setH = getSetH(setConH, setConInH);

		$("#mcs_container .dragger").css({
			'height':setH,
			'line-height':setH
		});
	}
	if($("#mcs_container_left")) {
		// 内容量によってスライダーの長さを設定
		setConH = $("#mcs_container_left").height();
		setConInH = $("#mcs_container_left .customScrollBox .container .content").innerHeight();

		$("#mcs_container_left").mCustomScrollbar("vertical",0,"",1.05,"fixed","yes","yes", getScrollSpeed(setConH, setConInH));

		// 内容が全て表示される範囲でなるべくスライダーが大きくなるよう設定
		setH = getSetH(setConH, setConInH);

		$("#mcs_container_left .dragger").css({
			'height':setH,
			'line-height':setH
		});
	}
	if($("#mcs_container_right")) {
		// 内容量によってスライダーの長さを設定
		setConH = $("#mcs_container_right").height();
		setConInH = $("#mcs_container_right .customScrollBox .container .content").innerHeight();

		$("#mcs_container_right").mCustomScrollbar("vertical",0,"",1.05,"fixed","yes","yes", getScrollSpeed(setConH, setConInH));

		// 内容が全て表示される範囲でなるべくスライダーが大きくなるよう設定
		setH = getSetH(setConH, setConInH);

		$("#mcs_container_right .dragger").css({
			'height':setH,
			'line-height':setH
		});
    }
}

function getScrollSpeed(setConH, setConInH) {
	if(setConH == null || setConInH == null) {
		return 10;
	}
	var setH = Math.round(setConH * setConH/setConInH);
	if(setConH/setConInH < 0.15) {
		setH = 10;
	} else if(setConH/setConInH < 0.2) {
		setH = 10;
	} else if(setConH/setConInH < 0.4) {
		setH = 50;
	} else if(setConH/setConInH < 0.5) {
		setH = 75;
	} else if(setConH/setConInH < 0.6) {
		setH = 100;
	} else if(setConH/setConInH < 0.7) {
		setH = 150;
	} else if(setConH/setConInH < 0.8) {
		setH = 200;
	} else {
		setH = 200;
	}

	// 極端に高さが小さい場合（数値は適当）
	if(setConH < 250) {
		setH = 200;
	}
	if(setConH < 150) {
		setH = 400;
	}

	return setH;
}


function getSetH(setConH, setConInH) {
	if(setConH == null || setConInH == null) {
		return 60;
	}
	var setH = Math.round(setConH * setConH/setConInH);
//	alert(setConH + ' ' + setConH/setConInH);

	if(setConH > 250) {
		if(setConH/setConInH < 0.15) {
			setH = setH;
		} else if(setConH/setConInH < 0.2) {
			setH = setH * 0.6;
		} else if(setConH/setConInH < 0.3) {
			setH = setH * 0.4;
		} else if(setConH/setConInH < 0.4) {
			setH = setH * 0.35;
		} else if(setConH/setConInH < 0.5) {
			setH = setH * 0.3;
		} else if(setConH/setConInH < 0.6) {
			setH = setH * 0.3;
		} else if(setConH/setConInH < 0.7) {
			setH = setH * 0.29;
		} else if(setConH/setConInH < 0.8) {
			setH = setH * 0.3;
		} else {
			setH = setH * 0.4;
		}
	} else {
		// 極端に高さが小さい場合（数値は適当）
//		alert(true);
		if(setConH/setConInH < 0.15) {
			setH = setH * 2;
		} else if(setConH/setConInH < 0.2) {
			setH = setH * 1.5;
		} else if(setConH/setConInH < 0.25) {
			setH = setH * 1;
		} else if(setConH/setConInH < 0.3) {
			setH = setH * 0.8;
		} else if(setConH/setConInH < 0.4) {
			setH = setH * 0.7;
		} else if(setConH/setConInH < 0.5) {
			setH = setH * 0.4;
		} else if(setConH/setConInH < 0.6) {
			setH = setH * 0.3;
		} else {
			setH = setH * 0.3;
		}
	}
	
	return setH;
}
