/*
 * yuga.js 0.3.0 - 優雅なWeb制作のためのJS
 *
 * Copyright (c) 2007 Kyosuke Nakamura (kyosuke.jp)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2006-10-30
 * Modified:  2007-06-18
 *
 * jQuery 1.1.2
 * ThickBox 3
 * Interface 1.2 (Effects scroll)
 */
 
//定義
var ary = location.href.split('/');
var aL = ary.length;
var baseL = 4;

//テストサイト用
if(document.domain == "test.akindo2000.net"){
	baseL++;
	
}//

//階層分、URI生成
var j = aL - baseL;
var isLinkAry = new Array;
for(i=0; i<=j; i++){
        isLinkAry[i] = ary.slice(0,ary.length-i);
        isLinkAry[i] = isLinkAry[i].join('/');
}
//ここに追記
isLinkAry[0] = isLinkAry[0].replace(/\#.*$/,"");//'#'があったら削除

var yuga = {
	preloader: {
		loadedImages: [],
		load: function (url){
			var img = this.loadedImages;
			var l = img.length;
			img[l] = new Image();
			img[l].src = url;
		}
	},
	// URIを解析したオブジェクトを返すfunction
	URI: function(s){
		this.originalPath = s;
		//絶対パスを取得
		this.getAbsolutePath = function(path){
			var img = new Image();
			img.src = path;
			path = img.src;
			img.src = '#';
			path = path.replace(/index.*$/,"");//'index'があったら削除
			path = path.replace(/\/$/,"");//'/'があったら削除
			return path;
		};
		this.absolutePath = this.getAbsolutePath(s);
		//同じ文書にリンクしているかどうか
		this.len = isLinkAry.length;
		this.isSelfLink = false;
		while(this.len--){
			this.isSelfLink = this.isSelfLink || (this.absolutePath == isLinkAry[this.len]);
		}
		//this.isSelfLink = (this.absolutePath == location.href);
		//絶対パスを分解
		/*
		var a = this.absolutePath.split('://');
		this.schema = a[0];
		var d = a[1].split('/');
		this.host = d.shift();
		var f = d.pop();
		this.dirs = d;
		this.file = f.split('?')[0].split('#')[0];
		var fn = this.file.split('.');
		this.fileExtension = (fn.length == 1) ? '' : fn.pop();
		this.fileName = fn.join('.');
		var fq = f.split('?');
		this.query = (fq[1]) ? fq[1].split('#')[0] : '';
		var ff = f.split('#');
		this.fragment = (ff[1]) ? ff[1].split('?')[0] : '';
		*/
	}
};

$(function(){
	/*
	if(document.domain == "test.akindo2000.net"){
		$('a[@href^="/"]').add('link[@href^="/"]').each(function(){
			var dirname = $(this).attr('href');
			$(this).attr({ href: '/kikuchi_kensetsu'.concat(dirname)});
		});					 
		$('img[@src^="/"]').each(function(){
			var dirname = $(this).attr('src');
			$(this).attr({ src: '/kikuchi_kensetsu'.concat(dirname)});
		});
	}*/

	//リンク画像はロールオーバーを設定
	$('a.btn').add('#globalNav li a').add('#subNav li a').each(function(){
		this.originalSrc = $(this).find('img').attr('src');
		this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpg|\.png)/, "_on$1");
		yuga.preloader.load(this.rolloverSrc);
	}).hover(function(){
		$(this).find('img').attr('src',this.rolloverSrc);
	},function(){
		$(this).find('img').attr('src',this.originalSrc);
	});

	//現在のページへのリンク
	$('#globalNav li a').add('#subNav li a').each(function(){
		var href = new yuga.URI(this.getAttribute('href'));
		if (href.isSelfLink && !href.fragment) {
			$(this).parent().addClass('current');
			//img要素が含まれていたら現在用画像（_cr）に設定
			$(this).each(function(){
				//ロールオーバーが設定されていたら削除
				$(this).unbind('mouseover');
				$(this).unbind('mouseout');
				$(this).unbind('each');
				$(this).unbind('hover');
				this.currentSrc = $(this).find('img').attr('src').replace(/(\.gif|\.jpg|\.png)/, "_cr$1");
				$(this).find('img').attr('src',this.currentSrc);
			});
		}
	});
	//現在のページへのリンク localNav
	$('#localNav li a').add('#localNav2 li a').add('.orderNav li a').add('.orderNav dd a').each(function(){
		var href = new yuga.URI(this.getAttribute('href'));
		if (href.isSelfLink && !href.fragment) {
			$(this).parent().addClass('current');
		}
	});
	//外部リンクは別ウインドウを設定
	$('a[href^="http://"]').add('a[href^="https://"]').not('[href^="http://www.kikuchi-kensetsu.co.jp/"]').not('[href^="http://kikuchi-kensetsu.co.jp/"]').not('[href^="https://kikuc795.securesites.com/"]').click(function(){
		window.open(this.href, '');
		return false;
	}).addClass('externalLink');
	
	//奇数、偶数を自動追加
	$('form table').each(function(){
		$(this).find('tr:odd').addClass('even');
		$(this).find('tr:even').addClass('odd');
	});
	$('body.faq #mainContent #questions').each(function(){
		$(this).find('li:odd').addClass('even');
		$(this).find('li:even').addClass('odd');
	});
	
	//奇数、偶数を自動追加
	$('body#sitIndex #mainContent').each(function(){
		$(this).find('div.sitSection:odd').addClass('odd');
		$(this).find('div.sitSection:even').addClass('even');
	});
	//:first-child, :last-childをクラスとして追加
	$('body#sitIndex #mainContent ul').each(function(){
		$(this).find('li:first-child').addClass('firstChild');
		$(this).find('li:last-child').addClass('lastChild');
	});
	$('#mainContent table').each(function(){
		$(this).find('tr:first-child').addClass('firstChild');
		$(this).find('tr:last-child').addClass('lastChild');
	});

	//高さそろえる
	$('body.blog .heightLineBox li').each(function(){
		//$(this).addClass('heightLine');
		$(this).flatHeights();
		//($('body.blog')) ? $(this).css('margin-bottom',-150) : $(this).css('margin-bottom',-120);
		
	});
	
	//lightBox
	$('#mainContent .gallery li a').add('#goodslist li a').lightBox();
	$('#mainContent .gallery li a').add('#goodslist li a').each(function(){
		$(this)	.find('img').after('<p class="num" style="position: absolute;top:0;left:0;color: #fff;background: #312323;font-size:10px;padding:1px 3px;">+Zoom</p>')	;									 
		$(this).hover(function(){
			$(this).find('p').css('background-color','#e1320a')
		},function(){
			$(this).find('p').css('background-color','#312323')
		});
	});
	
	//RollOverエリアの設定
	$('.ros img').each(function(){
		$(this)	.addClass('ro');		
	});
//	$('#mainContent a[@href$=".jpg"]').add('#mainContent a[@href$=".gif"]').add('#mainContent a[@href$=".png"]').lightBox();

/*lightBox
	$('a[@href$=".jpg"], a[@href$=".gif"], a[@href$=".png"]').each(function(){
			$(this).lightBox();
	});
	$('#gallery a').each(function(){
			$(this).lightBox();
	});
	
	*/
	//dd要素を出したり消したり
	/*
	$('#faq dl.list').each(function(){
		$(this).children('dd').hide();
		$(this).before('<p class="alignR"><a>回答をすべて表示</a></p>');
	});
	$('#faq p.alignR a').click(function(){
		if($(this).parent().next().children('dd').is(':visible')){
			$(this).parent().next().children('dd').hide();
			$(this).text('回答をすべて表示');
		}else{
			$(this).parent().next().children('dd').show();
			$(this).text('回答をすべて非表示');
		}
	});
	$('#faq dl.list dt').click(function(){
		if($(this).next().is(':visible')){
			$(this).next().hide();
			if(!$(this).nextAll('dd').is(':visible')){
				$(this).parent().prev().children().text('回答をすべて表示');
			}
		}else{
			$(this).next().show();
			$(this).parent().prev().children().text('回答をすべて非表示');
		}
	});
	
	//googlemapをiframeで表示
	$('#outline #mainContent table dl dd a.map').each(function(){
		this.gmapURI = $(this).attr('href');
		$(this).removeAttr('href');
	}).click(function(){
		if($(this).hasClass('show')){
			$(this).next().remove();
			$(this).text('地図を表示');
			$(this).removeClass('show')
		}else{
			$(this).after('<iframe frameborder="0" src="'+this.gmapURI+'"></iframe>');
			$(this).text('地図を非表示');
			$(this).addClass('show')
		}
	});
	*/
	//するするアニメーション ※要scrollTo.js
	$('a[href^="#"]').each(function(){
		this.target = $(this).attr('href');
	}).click(function(){
		$.scrollTo( this.target, {speed:800} );
		return false;
	}); 

});

