/*
bxGallery v1.1
Plugin developed by: Steven Wanderski
http://bxgalleryplugin.com
http://stevenwanderski.com

Released under the GPL license:
http://www.gnu.org/licenses/gpl.html
*/

(function($){
	var showing=0;
	$.fn.extend({
		bxgallery:function(options){
			var defaults={
				maxwidth:'',maxheight:'',thumbwidth:58,thumbcrop:false,croppercent:.35,thumbplacement:'bottom',thumbcontainer:'',opacity:.7,load_text:'',load_image:'../images/load_image.gif',wrapperclass:'outer'
			}
			if(options.set_showing){
				showing=options.set_showing;
				return ;
			}
			var options=$.extend(defaults,options);
			var o=options;
			var cont='';
			var caption='';
			var $outer='';
			var $orig=this;
			var tall=0;
			var wide=0;
			var i=0;
			var k=$orig.find('img').size();
			var current;preload_img();
			

			function preload_img(){
				$orig.hide();
				if(o.load_text!=''){
					$orig.before('<div id="loading">'+o.load_text+'</div>');
				}
				else{
					$orig.before('<div id="loading"><img src="'+o.load_image+'" /></div>');
				}
				$orig.parent().find('#loading').css({
					'textAlign':'center','width':o.maxwidth
				});
				$orig.find('img').each(function(){
					var the_source=$(this).attr('src');
					var the_img=new Image();
					the_img.onload=function(){
						preload_check();
					};
					the_img.src=the_source;
				});
			}
			
			function get_showing(){
				return showing;
			}
			
			function preload_check(){
				i++;if(i==k){
					init();
				}
			}
			
			function init(){
				set_layout();
				set_main_img();
				place_thumbcontainer();
				set_thumbs();
			}

			function set_layout(){
				$orig.parent().find('#loading').hide();
				$orig.show();
				$orig.wrap('<div class="'+o.wrapperclass+'"></div>');
				$outer=$orig.parent();
				$orig.find('li').css({
					'position':'absolute'
					});
				}

				function set_main_img(){
					$orig.find('img').each(function(){
						var $this=$(this);
						var $imgheight=$this.height();
						var $imgwidth=$this.width();
						if($this.attr('title')!=''){caption=$this.attr('title');
						$this.parent().append('<div class="caption">'+caption+'</div>');
					}
				if(o.maxwidth!=''){
					if($this.width()>o.maxwidth){
						$this.width(o.maxwidth);
						$this.height(($imgheight/$imgwidth)*o.maxwidth);
					}
				}
				if(o.maxheight!=''){
					if($this.height()>o.maxheight){
						$this.height(o.maxheight);
						$this.width(($imgwidth/$imgheight)*o.maxheight);
					}
				}
				if($this.height()+$this.parent().find('.caption').height()>tall){
					tall=$this.height()+$this.parent().find('.caption').height();
				}
				if($this.width()>wide){
					wide=$this.width();
				}
				
				cont+='<li><img src="'+$this.attr('src')+'" width="111" height="111" /></li>';
			});
			$orig.find('li:not(:first)').hide();
			//$orig.height(tall);
			//$orig.width(wide);
			$outer.find('.caption').width(wide);
		}
		
		function place_thumbcontainer(){
			if(o.thumbplacement=='top'){
				$outer.prepend('<ul class="thumbs clearfix">'+cont+'</ul>');
				$outer.find('.thumbs').css({'overflow':'auto'});
			}
			else if(o.thumbplacement=='left'){
				$outer.prepend('<ul class="thumbs clearfix">'+cont+'</ul>');
				$orig.css({'float':'left'});
				$outer.find('.thumbs').css({'float':'left'});
			}
			else if(o.thumbplacement=='bottom'){
				$outer.append('<ul class="thumbs clearfix">'+cont+'</ul>');
			}
			else if(o.thumbplacement=='right'){
				$outer.append('<ul class="thumbs clearfix">'+cont+'</ul>');
				$orig.css({'float':'left'});
				$outer.find('.thumbs').css({
					'float':'left'
				});
			}
			$outer.append('<div style="clear:both"></div>');
			if(o.thumbcontainer!=''){
				$outer.find('.thumbs').width(o.thumbcontainer);
			}
		}
		function set_thumbs(){
			$outer.find('.thumbs li').each(function(){
				var $this=$(this);
				var $img=$this.find('img');
				var $imgwidth=$img.width();
				var $imgheight=$img.height();
				if(o.thumbcrop){
					$img.width($imgwidth*o.croppercent);
					$img.height(($imgheight/$imgwidth)*$img.width());
					$this.css({
						'float':'left','overflow':'hidden','cursor':'pointer'});
					}
					else{
					$img.width(o.thumbwidth);
					$img.height(($imgheight/$imgwidth)*o.thumbwidth);
					$this.css({
						'float':'left','cursor':'pointer'
					});
					$this.height($img.height())
				;}
			$this.click(function(){
				var x=$outer.find('.thumbs li').index($this);
				showing=get_showing();
				if(showing!=x){
					$orig.find('li').fadeOut();
					$orig.find('li').eq(x).fadeIn();showing=x;
				}
			});
		});
		var $thumb=$outer.find('.thumbs li');
		$thumb.eq(0).addClass('on');
		$thumb.not('.on').fadeTo(0,o.opacity);
		$thumb.click(function(){var t=$(this);
		var i=$thumb.index(this);
		if(current!=i){
			$thumb.removeClass('on');
			t.addClass('on');
			$thumb.not('.on').fadeTo(200,o.opacity);
			current=i;
		}
	})
	.hover(function(){
		$(this).stop().fadeTo(200,1);
	},
	function(){
		$(this).not('.on').stop().fadeTo(200,o.opacity);
	});
}
}
});
})
(jQuery);


$(document).ready(function(){
	$('.goods_main').bxgallery({
		maxwidth: '',              // if set, the main image will be no wider than specified value (in pixels)
		maxheight: '',             // if set, the main image will be no taller than specified value (in pixels)
		thumbwidth: 111,           // �T���l�C���̕�
		thumbcrop: false,          // if true, thumbnails will be a perfect square and some of the image will be cropped
		croppercent: .35,          // if thumbcrop: true, the thumbnail will be scaled to this 
								   // percentage, then cropped to a square
		thumbplacement: 'bottom',  // placement of thumbnails (top, bottom, left, right)
		thumbcontainer: '',        // width of the thumbnail container div (in pixels)
		opacity: .7,               // opacity level of thumbnails
		load_text: '',             // if set, text will display while images are loading
		load_image: '../images/load_image.gif',
		                           // image to display while images are loading
		wrapperclass: 'outer'      // classname for outer wrapping div
	});
});
