/*

Last UpDate 2009-10-14

*/

$(function(){
	//現在居るファイル名	   
	var currentFile = location.href.split('/').pop();
	
	//ua取得
	var ua = navigator.userAgent;
	
	//classが、'nav'で終わるulの、最後のliに、'last'クラスを付ける
	$('ul[class$="nav"]').each(function(){
		$('li:last',this).addClass('last');
	});
	
	//classが、'list'で終わるulの、偶数の要素に、'even'クラスを付ける
	$('ul[class$="list"] li:nth-child(even)').addClass('even');
	
	//classが、'table'で終わるテーブルの、偶数trに、'even'クラスを付ける
	$('table[class$="table"] tr:nth-child(even)').addClass('even');
	
	//rollover _off の付いた画像をマウスオーバーで _on の付いた画像に変える
	$('a img').each(function(){
		var imgSrc = $(this).attr('src');
		//smartRollover
		if(imgSrc.match(/(.*)_off(\.gif|\.jpg)/g)){
			var repSrc = RegExp.$1+'_on'+RegExp.$2;
			$(this).hover(function(){
				$(this).attr('src',repSrc);
				$(this).css({opacity: '1',filter: 'alpha(opacity=100)'});
			},function(){
				$(this).attr('src',imgSrc);
			});
		//ウィンク効果
		}else{
			$(this).hover(function(){
				var parentId = $(this).parent().attr("id");
				if(parentId != "logo"){
					if(imgSrc.indexOf('_off') == -1 && imgSrc.indexOf('_on') == -1){
						$(this).css({
							opacity: '0.2',
							filter: 'alpha(opacity=20)'
						});
						$(this).fadeTo('slow',1.0);
					}else{
						$(this).css({opacity: '1',filter: 'alpha(opacity=100)'});
					}
				}
			});
		}
	});
	
	if(ua.indexOf('IE 6') > -1 || ua.indexOf('IE 7') > -1){
		//サムネールを内包する、.table-cell内で、display:table-cell;vertical-align:middle;のように表示する
		$('.table-cell').each(function(){
			var x = $('img',this).width();
			var y = $('img',this).height();
			if(x > y){
				$(this).css({display:'block'});
				var boxY = $(this).height();
				var rePadding = Math.floor((boxY - y) / 2);
				var reHeight = boxY - rePadding;
				$(this).css({paddingTop:rePadding+'px',height:reHeight+'px'});
			}
		});
	}
	
	//rollover使用時、カレントページのliに、'current'クラスを付ける
	$('li').each(function(){
		var imgSrc = $(' a img',this).attr('src');
		if(imgSrc != undefined){
			if(imgSrc.indexOf('_on') > -1){
				$(this).addClass('current');
			}
		}
	});
	
	//submit押した感&ウィンク
	$('form p.submit input').mousedown(function(){
		$(this).css({position:'relative',top:'1px',left:'1px'});
	}).mouseup(function(){
		$(this).css({position:'static'});
	}).mouseout(function(){
		$(this).css({position:'static'});
	}).hover(function(){
		$(this).css({opacity:0.2});
		$(this).fadeTo('slow',1.0);
	});
	
	//lightBox
	try{
		$('.lb a[href$=".jpg"],.lb a[href$=".png"],.lb a[href$=".gif"]').lightBox({
			overlayBgColor: '#000',
			overlayOpacity: 0.8,
			imageLoading:   'js/images/lightbox-ico-loading.gif',
			imageBtnPrev:   'js/images/lightbox-btn-prev.gif',
			imageBtnNext:   'js/images/lightbox-btn-next.gif',
			imageBtnClose:  'js/images/lightbox-btn-close.gif',
			imageBlank:     'js/images/lightbox-blank.gif'
		});
	}catch(error){
	}
	
	//高さ調整
    $.fn.autoHeight = function(line){
        var self = $(this);

        function descend (a,b){ return b-a; }

        var n = 0,
            hMax,
            hList = new Array(),
            hListLine = new Array();
            hListLine[n] = 0;

        // 要素の高さを取得
        self.each(function(i){
            var h = $(this).height();
            hList[i] = h;
            if (line > 1) {
                // lineごとの最大値を格納していく
                if (h > hListLine[n]) {
                    hListLine[n] = h;
                }
                if ( (i > 0) && (((i+1) % line) == 0) ) {
                    n++;
                    hListLine[n] = 0;
                };
            }
        });

        // 取得した高さの数値を降順に並べ替え
        hList = hList.sort(descend);
        hMax = hList[0];
        
        // 高さの最大値を要素に適用
        var browser = $.browser.version;
        if (line > 1) {
            for (var j=0; j<hListLine.length; j++) {
                for (var k=0; k<line; k++) {
                    if (browser == '6.0') {
                        self.eq(j*line+k).height(hListLine[j]);
                    } else {
                        self.eq(j*line+k).css('height',hListLine[j]);
                    }
                }
            }
        } else {
            if (browser == '6.0') {
                self.height(hMax);
            } else {
                self.css('height',hMax);
            }
        }
    };
})(jQuery);


