/**
*	DM_ThbShow.js (jQuery.js required)
*
*	@version	0.10
*	@author	Yosuke Hiyoshi
*	@require	jquery.js
*
*	[Update history]
*	# May. 12 2010 (Version 0.10)	-	開発開始
*
*/
(function(jQuery){
//	jQuery(function() {
//		//jQuery.thb_show.init();
//		jQuery.thb_show.preload();
//	});

	jQuery.thb_show = {
		/**
		 * [初期処理]
		 * 
		 * @param	void
		 * @return	void
		 */
		init: function(){
			jQuery('.thb_show_trigger_w120h94').click(function(){
				jQuery.thb_show.swap_image(this);
				return false;
			});
		},
		/**
		 * [画像変更]
		 * 
		 * @param	Object caller_elem 呼び出し元要素
		 * @return	void
		 */
		swap_image: function(caller_elem){
			var img_src = jQuery(caller_elem).attr('href');
			jQuery('#thb_show_target').empty();
			jQuery('#thb_show_target').append('<img />');
			var new_img = jQuery('#thb_show_target').children('img');
			jQuery(new_img).attr('src', img_src);
			jQuery('p#caption_photo').empty();
			if(jQuery('p#caption_photo') && jQuery(caller_elem).attr('title')){
				jQuery('p#caption_photo').html(jQuery(caller_elem).attr('title'));
			}
			jQuery.thb_show.preload();
		},
		/**
		 * [画像プリロード]
		 * 
		 * @param	void
		 * @return	void
		 */
		preload: function(){
			var preload_images = Array();
			var i = 0;
			var thb_img_width;
			var thb_img_height;
			var margin_top;

			var thb_show_preload_images;
			
			var resize_func = function(img, w, h){
				jQuery(img).removeAttr("width").removeAttr("height");
				thb_img_width = img.width;
				thb_img_height = img.height;

				if(w > thb_img_width && h > thb_img_height) {
					mgSide = Math.floor((w - thb_img_width)/2);
					mgSide = mgSide + "px";
					$(img).css('margin-left',mgSide);
				} else {
					
					// 横長画像の場合
					if(thb_img_width > thb_img_height){
						margin_top = Math.floor((h - thb_img_height*(w / thb_img_width)) / 2);
						margin_top = margin_top + 'px';
						jQuery(img).css({
								'margin-top' : margin_top,
								'margin-bottom' :  margin_top,
								'width' : w
							});
					}else{
						jQuery(img).css('height', h);
						jQuery(img).parents('li').css('text-align', 'center');
					}
				}
			};
			
			jQuery('.thb_show_trigger').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 100, 100);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 100, 100);
					});
				}
				
				
//				jQuery(thb_show_preload_images).load(function(){
/*
 *				console.log("a" + jQuery(thb_show_preload_images).get(0).width);
 *				console.log("b" + jQuery(thb_show_preload_images).width());
 */
					// 画像サイズ取得
/*					jQuery(this).removeAttr('width').removeAttr('height')
					thb_img_width = this.width;
					thb_img_height = this.height;

					// 横長画像の場合
					if(thb_img_width > thb_img_height){
						alert("here");
						margin_top = Math.floor((100 - thb_img_height*(100/thb_img_width)) / 2);
						margin_top = margin_top + 'px';
						jQuery(this)
							.css('margin-top', margin_top)
							.css('margin-bottom', margin_top)
							.css('width', '100px');
					}else{
						jQuery(this).css('height', '100px');
						jQuery(this).parents('li').css('text-align', 'center');
					}
				});
*/
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});
			
			jQuery('.thb_show_trigger_w360h270').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 360, 270);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 360, 270);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_w120h94').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 120, 94);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 120, 94);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_w197h160').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 197, 160);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 197, 160);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_70').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 70, 70);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 70, 70);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_114').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 114, 114);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 114, 114);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_w100h130').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 100, 130);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 100, 130);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

			jQuery('.thb_show_trigger_w410h300').each(function(){
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 410, 300);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 410, 300);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});
			
			$('.img_resize').each(function() {
				thb_show_preload_images = jQuery(this).children('img:first');
				var img = thb_show_preload_images.get(0);
				if( img.complete ){
					resize_func(img, 160, 160);
				}else{
					thb_show_preload_images.load(function(){
						resize_func(this, 160, 160);
					});
				}
				
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(thb_show_preload_images).attr('src');

				i++;
			});

/*		jQuery('.thb_show_trigger_70').each(function(){
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(this).children('img:first').attr('src');
				if(jQuery(this).children('img').length > 0){
					// 画像サイズ取得
					thb_img_width = preload_images[i].width;
					thb_img_height = preload_images[i].height;
				//alert(thb_img_width);
				//alert(thb_img_height);
					// 横長画像の場合
					if(thb_img_width > thb_img_height){
						margin_top = Math.floor((70 - thb_img_height*(70/thb_img_width)) / 2);
						margin_top = margin_top + 'px';
						jQuery(this).children('.thb_resizable_70').css('margin-top', margin_top);
						jQuery(this).children('.thb_resizable_70').css('width', '70px');
					}else{
						jQuery(this).children('.thb_resizable_70').css('height', '70px');
						jQuery(this).css('text-align', 'center');
					}
				}
				i++;
			});

			jQuery('.thb_show_trigger_114').each(function(){
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(this).children('img:first').attr('src');
				if(jQuery(this).children('img').length > 0){
					// 画像サイズ取得
					thb_img_width = preload_images[i].width;
					thb_img_height = preload_images[i].height;
				//alert(thb_img_width);
				//alert(thb_img_height);
					// 横長画像の場合
					if(thb_img_width > thb_img_height){
						margin_top = Math.floor((114 - thb_img_height*(114/thb_img_width)) / 2);
						margin_top = margin_top + 'px';
						jQuery(this).children('.thb_resizable_114').css('margin-top', margin_top);
						jQuery(this).children('.thb_resizable_114').css('width', '114px');
					}else{
						jQuery(this).children('.thb_resizable_114').css('height', '114px');
						jQuery(this).css('text-align', 'center');
					}
				}
				i++;
			});

			jQuery('.thb_show_trigger_w100h130').each(function(){
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(this).children('img:first').attr('src');
				if(jQuery(this).children('img').length > 0){
					// 画像サイズ取得
					thb_img_width = preload_images[i].width;
					thb_img_height = preload_images[i].height;
				//alert(thb_img_width);
				//alert(thb_img_height);
					// 横長画像の場合
					if(thb_img_width > thb_img_height){
						margin_top = Math.floor((130 - thb_img_height*(100/thb_img_width)) / 2);
						margin_top = margin_top + 'px';
						jQuery(this).children('.thb_resizable_w100h130').css('margin-top', margin_top);
						jQuery(this).children('.thb_resizable_w100h130').css('width', '100px');
					}else{
						jQuery(this).children('.thb_resizable_w100h130').css('height', '130px');
						jQuery(this).css('text-align', 'center');
					}
				}
				i++;
			});

			jQuery('.thb_show_trigger_w410h300').each(function(){
				preload_images[i] = new Image();
				preload_images[i].src = jQuery(this).children('img:first').attr('src');
				if(jQuery(this).children('img').length > 0){
					// 画像サイズ取得
					thb_img_width = preload_images[i].width;
					thb_img_height = preload_images[i].height;
				//alert(thb_img_width);
				//alert(thb_img_height);
					
				// 横長画像の場合
				if(thb_img_width > thb_img_height){
					margin_top = Math.floor((300 - thb_img_height*(410/thb_img_width)) / 2);
					margin_top = margin_top + 'px';
					jQuery(this).children('.thb_resizable_w410h300').css('margin-top', margin_top);
					jQuery(this).children('.thb_resizable_w410h300').css('width', '410px');
				}else{
					jQuery(this).children('.thb_resizable_w410h300').css('height', '300px');
					jQuery(this).css('text-align', 'center');
				}
					
					
					
				}
				i++;
			});
*/
		}
	};

	jQuery(document).ready(function() {
		jQuery.thb_show.init();
		jQuery.thb_show.preload();
	});

})(jQuery);

