/*////////////////////////////////////////////////////////////

- common.js

[Common]
	1.roll over
	2.link
	3.page scroll
	4.change photo
	5.subwindow
	6.interview
	7.adjusts height
	8.get ad area
	9.no image
	10.shadowbox
	11.disable line-height and font-size

////////////////////////////////////////////////////////////*/

/*############################################################

[Common]

############################################################*/

/*==================================================

1.roll over
==================================================*/
$(function()
{
	// switch image
	var _switchObj = $( ".rollOverSwitch a img, img.rollOverSwitch" );
	
	$.each( _switchObj, function(){ changeBtImage( $( this ), $( this ) ); } );
	
	//------------------------------
	// fade image
	var _fadeObj = $( ".rollOverFade a img, a.rollOverFade img, .nivoSlider" );
	
	_fadeObj.each( function()
	{
		$( this ).hover(
			function()
			{
				$( this )
					.stop()
					.fadeTo( 280, 0.8 );
			},
			function()
			{
				$( this )
					.stop()
					.fadeTo( 250, 1 );
			}
		);
	});
});

function changeBtImage( target, clickableArea )
{
	var _imageCashe = new Object();
	//
	target.not( "[src*='_on.']" ).each( function( i )
	{
		var _this = this; 
		var _imgsrc = _this.src;
		var _dot = _this.src.lastIndexOf( '.' );
		var _imgsrc_on = _this.src.substr( 0, _dot ) + '_on' + _this.src.substr( _dot, 4 );
		_imageCashe[ _this.src ] = new Image();
		_imageCashe[ _this.src ].src = _imgsrc_on;
		
		//------------------------------
		clickableArea.hover(
			function() { _this.src = _imgsrc_on; },
			function() { _this.src = _imgsrc; }
		);
	});
};


/* OLD SETTING begin */
$(function()
{
	$(".bnrBlock .bnrBorder").mouseover(bnrOver);
	$(".bnrBlock .bnrBorder").mouseout(bnrOut);
	//
	$("#top_news .topNews").mouseover(newsOver);
	$("#top_news .topNews").mouseout(newsOut);
	//
	$(".btDetailBase").mouseover(detailOver);
	$(".btDetailBase").mouseout(detailOut);
	$(".nonBtDetailBase").mouseover(nonDetailOver);
	$(".nonBtDetailBase").mouseout(nonDetailOut);
});
/**
* 
**/
function bnrOver(){
	$(this).find("img").addClass("over");
	$(this).find("p").addClass("over");
}
//
function bnrOut(){
	$(this).find("img").removeClass("over");
	$(this).find("p").removeClass("over");
}
/**
* 
**/
function newsOver(){
	$(this).find("h5").addClass("over");
}
//
function newsOut(){
	$(this).find("h5").removeClass("over");
}
/**
* 
**/
function detailOver(){
	$(this).find(".flt_left img").addClass("over");
	$(this).find(".btDetail .hidetext").addClass("over");
}
//
function detailOut(){
	$(this).find(".flt_left img").removeClass("over");
	$(this).find(".btDetail .hidetext").removeClass("over");
}
/**
* 
**/
function nonDetailOver(){
	$(this).find(".flt_left img").addClass("over");
	$(this).find(".entry_link").addClass("over");
}
//
function nonDetailOut(){
	$(this).find(".flt_left img").removeClass("over");
	$(this).find(".entry_link").removeClass("over");
}
/* OLD SETTING end */

/*==================================================

2.link
==================================================*/
function clickLink( URL, target )
{
	if ( URL != "" )
	{
		if ( target == "_top" )
		{
			top.location.href = URL;
		}
		else if( target == "_blank" || target == "blank" )
		{
			window.open( URL, "" );
		}
		else
		{
			location.href = URL;
		}
	}
}

$(function()
{
	var _obj = $( ".clickableArea .clickable:has( a ), .clickable:has( a )" );
	
	_obj.css( 'cursor', 'pointer' );
	_obj.hover(
		function()
		{
			$( this ).addClass( "over" );
			$( this ).find( ".fade img" ).each(function()
			{
				$( this )
					.stop()
					.fadeTo( 100, 0.6 );
			})
		},
		function()
		{
			$( this ).removeClass( "over" );
			$( this ).find( ".fade img" ).each(function()
			{
				$( this )
					.stop()
					.fadeTo( 100, 1 );
			})
		}
	)
	.click( function( e )
	{
		// default cancel
		e.preventDefault();
		
		var _href = $( this ).find( "a" ).attr( "href" );
		var _target = $( this ).find( "a" ).attr( "target" );
		clickLink( _href, _target );
	})
});

/*==================================================

3.page scroll
==================================================*/
$.extend( $.easing,
{
	easeOutExpo: function( x, t, b, c, d ) {
		return ( t == d ) ? b + c : c * ( -Math.pow( 2, -10 * t / d ) + 1 ) + b;
	}
});

$(function()
{
	//
	// <a href="#***">
	//
	$( 'a[href*=#container]' ).click( function()
	{
		if ( location.pathname.replace( /^\//, '' ) == this.pathname.replace( /^\//, '' ) && location.hostname == this.hostname )
		{
			var $target = jQuery( this.hash );
			$target = $target.length && $target || jQuery( '[name=' + this.hash.slice(1) + ']' );
			if ( $target.length )
			{
				var targetOffset = $target.offset().top;
				$( 'html, body' ).animate( {
					scrollTop: targetOffset
				}, 1200, 'easeOutExpo' );
				return false;
			}
		}
	});
});

/*==================================================

4.change photo
==================================================*/
// old
function changePhoto(myNum){
	document.detail_photo.src = "images/" + myNum + ".jpg";
}

// new
$( function()
{
	// 画像の変更
	var changeItemPhoto = function( src, alt )
	{
		var target = $( "#itemPhoto > img" );
		target
			.stop()
			.fadeTo( 100, 0, function()
			{
				$( this )
					.attr({
						"src"		: src,
						"alt"		: alt,
						"title"	: alt
					})
					.fadeTo( "normal", 1 );
			});
	};
	
	// 画像のプリロード
	var preLoad = function ( url )
	{
		$( '<img>')
			.hide()
			.attr( 'src', url )
			.appendTo( document.body )
			.load( function(){ $( this ).remove(); } )
			.error( function(){ $( this ).remove(); } );
	}
	
	//
	var btn = $( "#photoBtn > li > a" );
	$.each( btn, function(i)
	{
		// 最初の透明度
		if ( i == 0 ) $( this ).addClass( "current" );
		else
		{
			$( this ).find( "img" ).fadeTo( 0, .6 );
			
			// 画像のプリロード
			preLoad( $( this ).attr( 'href' ) );
		}
		
		$( this ).hover(
		function(e)
		{
			if ( $( this ).hasClass( "current" ) ) return;
			
			$( this ).addClass( "current over" );
			
			changeItemPhoto( $( this ).attr( "href" ), $( this ).find( "img" ).attr( "alt" ) );
			//checkPhotoBtn();
			
			// ボタンの状態の変更
			$.each( btn, function()
			{
				if ( $( this ).hasClass( "over" ) )
				{
					$( this ).find( "img" ).fadeTo( 100, 1 );
				}
				else
				{
					$( this )
						.removeClass( "current" )
						.find( "img" ).fadeTo( 100, 0.6 );
				}
			});
		},
		function()
		{
			$( this ).removeClass( "over" );
		});
		
		// aタグのクリックイベントをキャンセル
		$( this ).click( function(e)
		{
			e.preventDefault();
		});
	});
});

/*==================================================

5.subwindow
==================================================*/
//BRAND STORY
function brand_attention(){
	window.open("/brandstory/trackback.html","BRSHOP","width=600,height=350,scrollbars=yes");
}

//CUSTOMERS VOICE
function galleryWin(URL){
	var galleryWin=window.open(URL,"BRSHOP","width=600,height=500,toolbar=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes");
	galleryWin.window.focus();
}

//Order Made Suit and Shirt
function orderWin(URL){
	var orderWin=window.open(URL,"BRSHOP","width=600,height=500,toolbar=no,scrollbars=yes,resizable=yes,menubar=yes,status=yes");
	orderWin.window.focus();
}

/*==================================================

6.interview
==================================================*/
$(function()
{
	$( ".entryKeys" ).each( function()
	{
		$( ".key:last", $( this ) ).addClass( "last" );
	} );
	
	if ( $( "#main div" ).hasClass( "interview" ) )
	{
		if ( $( "#main > .interview > *:first-child" ).is( "h3" ) )
		{
			$( "#main > .interview > *:first-child" ).addClass( "top" );
		}
		
		// キャプションがある画像を囲む "p" タグにクラスを追加
		$( $( ".caption" ).prev(), $( "#main > .interview" ) ).addClass( "hasCaption" );
		
		// "interviewee" の文章の段落が2個以上続く場合に2個目以降の "p" タグにクラスを追加
		$( $( "p" ).not( ".interviewer, .caption, :has( img ), .interviewer + p, .key p, .entryKeys + p" ), $( "#main > .interview" ) ).addClass( "indent" );
	}
});

/*==================================================

7.adjusts height
==================================================*/

/*
Copyright (c) 2007, KITAMURA Akatsuki

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Version: 2007-08-01
*/

/**
*	$.changeLetterSize.addHandler(func)
*	文字の大きさが変化した時に実行する処理を追加
==================================================*/

jQuery.changeLetterSize = {
	handlers		: [],
	interval		: 1000,
	currentSize	: 0
};

(function($)
{
	var self = $.changeLetterSize;

	/* 文字の大きさを確認するためのins要素 */
	var ins = $( '<ins>M</ins>' ).css({
		display			: 'block',
		visibility	: 'hidden',
		position		: 'absolute',
		padding			: '0',
		top					: '0'
	});

	/* 文字の大きさが変わったか */
	var isChanged = function()
	{
		ins.appendTo( 'body' );
		var size = ins[0].offsetHeight;
		ins.remove();
		if (self.currentSize == size) return false;
		self.currentSize = size;
		return true;
	};

	/* 文書を読み込んだ時点で
	   文字の大きさを確認しておく */
	$( isChanged );

	/* 文字の大きさが変わっていたら、
	   handlers中の関数を順に実行 */
	var observer = function()
	{
		if ( !isChanged() ) return;
		$.each( self.handlers, function( i, handler )
		{
			handler();
		});
	};

	/* ハンドラを登録し、
	   最初の登録であれば、定期処理を開始 */
	self.addHandler = function( func )
	{
		self.handlers.push( func );
		if ( self.handlers.length == 1 )
		{
			setInterval( observer, self.interval );
		}
	};

})(jQuery);


/**
*	$(expr).flatHeights()
*	$(expr)で選択した複数の要素について、
*	それぞれ高さを一番高いものに揃える
==================================================*/

(function($)
{
	/* 対象となる要素群の集合 */
	var sets = [];

	/* 高さ揃えの処理本体 */
	var flatHeights = function( set )
	{
		var maxHeight = 0;
		set.each(function()
		{
			var height = this.offsetHeight;
			if ( height > maxHeight ) maxHeight = height;
			
			$( this ).css( 'position', 'relative' );
			$( '.bottom', $( this ) ).each( function()
			{
				$( this ).css( 'position', 'absolute' );
			} )
		});
		set.css( 'height', maxHeight + 'px' );
	};

	/* 要素群の高さを揃え、setsに追加 */
	jQuery.fn.flatHeights = function()
	{
		if ( this.length > 1 )
		{
			flatHeights( this );
			sets.push( this );
		}
		return this;
	};

	/* 文字の大きさが変わった時に、
	   setsに含まれる各要素群に対して高さ揃えを実行 */
	$.changeLetterSize.addHandler(function()
	{
		$.each( sets, function()
		{
			this.height( 'auto' );
			flatHeights( this );
		});
	});

})(jQuery);



$(function()
{
	$( ".adjustsH > .elem" ).flatHeights();
});

/*==================================================

8.get ad area
==================================================*/
$(function()
{
	if ( $( document ).find( "#leftAd" ).length )
	{
		var _target = $( document ).find( "#leftAd" );
				
		$.get( "/sotokoto/crossdomain-proxy.php", function( data )
		{
			var _src = $( data ).find( "#contentsMainArea .photo > img" ).attr( "src" );
			var _reg = /http\:\/\/[^\/]+/i;
			
			if ( _src.match( _reg ) ) _src = _src.replace( _reg, "" );
			
			var _alt = $( data ).find( "#contentsMainArea .photo > img" ).attr( "alt" );
			var _link = $( data ).find( "#contentsMainArea .photo > a" ).attr( "href" );
			
			_target.find( "dl > dt" ).each( function()
			{
				$( "<img>" )
					.attr({
						src		: "http://www.sotokoto.net" + _src,
						alt		: _alt,
						title	: _alt
					})
					.appendTo( $( this ) );
			});
			
			_target.find( "dl > dd > a" ).attr( "href", _link );
		})
	}
});

/*==================================================

9.no image
==================================================*/
$( function()
{
	$( ".noImg" ).each( function()
	{
		var _width = $( this ).width();
		var _height = $( this ).height();
		
		$( this )
			.css({
				"position"				: "relative",
				"backgroundColor"	: "#EBEBEB"
			});
			
		$( "<img>" )
			.attr({
				"src"			: "/sotokoto/images/common/noImg.gif",
				"width"		: 56,
				"height"	: 11,
				"alt"			: "No Photo",
				"title"		: "No Photo"
				
			})
			.appendTo( $( this ) )
			.css({
				"position"	: "absolute",
				"top"				: ( _height - $( "img", $( this ) ).height() ) * .5,
				"left"			:	( _width - $( "img", $( this ) ).width() ) * .5
			});
	});
});

/*==================================================

10.shadowbox
==================================================*/
$( function()
{
	$( ".shadowbox .entry" ).each( function(i)
	{
		$( this ).find( "a:has( img )" ).each( function()
		{
			var _link = $( this ).attr( "href" );
			if ( _link.match( /\.(jpg|gif|png)$/i ) )
			{
				$( this ).attr( "rel", "shadowbox[gallery" + i + "]" );
			}
		});
	});
});

/*==================================================

11.disable line-height and font-size
==================================================*/
$( function()
{
	var _obj = $( "img:only-child, a:has( img ):only-child" );
	_obj.each( function()
	{
		$( this ).parent().css({
			"lineHeight"	: 0,
			"fontSize"		: 0
		});
	})
});

