(function($){
	
// ==============================================
//图片预览 -2015-05-25


// 大图 loading
$(".product-img").each(function(){
	$(".product-img img").load(function(){   
       $('.img-loading').hide();
    });
})

// 预览
$(function(){
	$('.small-img-item').click(function() {
		$('.small-img-scroll').trigger('next.owl.carousel', [500]);
	})	
	$(".small-img-scroll li:first-child").addClass('current');
	$(".small-img-scroll li").click(function(){
	   $(this).addClass('current').siblings().removeClass('current');
	})
	
	
	//点击小图切换大图
	$('.small-img-item').click(function(){
	   $('.product-img').append('<b class="img-loading"></b>');
	   var src_mid=$(this).find('img').attr('src_mid');
	   var src_big=$(this).find('img').attr('src_big');
	   $('#bigImg').attr('src',src_mid);
	   $('#bigImg').attr('jqimg',src_big);

	   //2015-07-27
	   $('.img-loading').show();
	   $(".product-img img").load(function(){   
          $('.img-loading').hide();
       });
	   
	})

	$('#bigImg').click(function(){
	   $('.container').addClass('container-for-gallery');
	   $('<h2 class="gallery-photo-title"></h2><section class="gallery-main"><div class="gallery-photo"></div><div class="gallery-small-imgs"></div></section>').appendTo('.photos-gallery');
	   $('.gallery-body-mask').fadeIn();
	   
	   $('.gallery-photo-title').html($('.product-title').text());
	   
	   //生成弹窗图片
	   var bigImgUrl=$(this).attr("jqimg")
	   $('<div class="photo-box"><span class="photo-prev">prev</span><span class="photo-next">next</span><img src='+bigImgUrl+'></div>').appendTo('.gallery-photo');
	   $('.small-img-scroll').find('ul').clone().appendTo('.gallery-small-imgs');
	   $('.gallery-small-imgs ul').attr('style','');
	   $('.gallery-small-imgs li').attr('style','');
	   if($('.gallery-small-imgs li').hasClass('disabled')){
	      $('.gallery-small-imgs li').removeClass('disabled');
	   }
	   
	   //弹窗居中 - 图片加载完毕之后才执行高度判断
	   function fixGallery(){
	      var gTop = ($(window).height() - $('.photos-gallery').height())/2;  
          var gLeft = ($(window).width() - $('.photos-gallery').width())/2;  
          var scrollTop = $(document).scrollTop();  
          var scrollLeft = $(document).scrollLeft();  
          $('.photos-gallery').css( { position : 'absolute', 'top' : gTop + scrollTop, left : gLeft + scrollLeft } ).fadeIn();		   
	   }	   
	   $(".photo-box img").load(function(){   
          fixGallery();
	   });
	   
	   $(window).resize(function(){
	   	   // 2015-11-25 增加条件判断
	       if($('.photos-gallery').is(':visible')){
		       fixGallery() ;
		   }
	   })	   
	   	  
	   
	   //点击小图 
	   $('.gallery-small-imgs .small-img-item').click(function(){
	      var curPhotoSrc=$(this).find('img').attr('src_big');
	      $('.gallery-photo').find('img').attr('src',curPhotoSrc);
		  $(this).parents('li').addClass('current').siblings().removeClass('current');
	   })
	   $('.gallery-small-imgs .owl-item').click(function(){
		  $(this).addClass('current').siblings().removeClass('current');
	   })
	   
	   
	   //button-prev
	   $('.photo-prev').click(function(){
		   var smallItem=$('.gallery-small-imgs li');
		   var imgNum=$('.gallery-small-imgs li').length;
		   imgI=$('.gallery-small-imgs .current').index();	
		   if(imgI>0){
		      var curPhotoSrc=smallItem.eq(imgI-1).find('img').attr('src_big');
		      smallItem.removeClass('current');
		      smallItem.eq(imgI-1).addClass('current');
	          $('.gallery-photo').find('img').attr('src',curPhotoSrc);			   
		   }
	       else{
		      var curPhotoSrc=smallItem.eq(imgNum-1).find('img').attr('src_big');
		      smallItem.removeClass('current');
		      smallItem.eq(imgNum-1).addClass('current');
	          $('.gallery-photo').find('img').attr('src',curPhotoSrc);
		   }

	   })	   
	   
	   //button-next
	   $('.photo-next').click(function(){
		   var smallItem=$('.gallery-small-imgs li');
		   var imgNum=$('.gallery-small-imgs li').length;
		   imgI=$('.gallery-small-imgs .current').index();		   
	       if(imgI<imgNum-1){
		      var curPhotoSrc=smallItem.eq(imgI+1).find('img').attr('src_big');
		      smallItem.removeClass('current');
		      smallItem.eq(imgI+1).addClass('current');
	          $('.gallery-photo').find('img').attr('src',curPhotoSrc);
		   }
		   else{
		      var curPhotoSrc=smallItem.eq(0).find('img').attr('src_big');
		      smallItem.removeClass('current');
		      smallItem.eq(0).addClass('current');
	          $('.gallery-photo').find('img').attr('src',curPhotoSrc);	   
		   }
	   })
	   
	   //关闭弹窗
	   $('.close-gallery,.gallery-body-mask').click(function(){
	      $('.photos-gallery').find('.gallery-main,.gallery-photo-title').remove();
		  $('.photos-gallery').hide();
		  $('.gallery-body-mask').fadeOut();
	      $('.container').removeClass('container-for-gallery');
	   })
	})
})



$(function(){

   //show custom size
   $(".size-form-check").click(function(){
   if($(this).attr("checked")){
       $('.size-form').show();
	   $(this).attr("checked");  	   
   }
   else{
	   $(this).removeAttr("checked",'true');  
	   $('.size-form').hide();
   }
   })  


   //select qty
   var t=$('.ipt-qty')
   $('.ipt-qty').keyup(function(){
	  var tValue=$(this).val();
      if(tValue=='' || isNaN(tValue)){
	     $(this).val('1');
	  }
	  else if(!isNaN(tValue) && tValue<1){
		 $(this).val('1');
	  }	
	  else{
		  $(this).val(parseInt(tValue));
	  }
    })
	var t=$('.ipt-qty')	
          $(".btn-plus").click(function () {
	         t.val(parseInt(t.val())+1);
          })
          $('.btn-minus').click(function(){
          if(t.val()>=2){
           t.val(parseInt(t.val())-1);
	      }
    })  	

 
  //discount for product
   var curPrice=parseFloat($('.product-summary').find('.special-price i').text().replace(',',''));
   var oPrice=parseFloat($('.product-summary').find('.old-price i').text().replace(',','')) ;
   if(curPrice<oPrice)
   {
      var discount=Math.round(curPrice/oPrice*100);
      $('.product-summary').find('.discount b').html(discount);
      $('.product-view').find('.discount b').html(discount)//for view - 2015-11-25
   }
   else{
	  $('.product-summary').find('.discount').hide()//2015-02-15
   } 
   
   $('.current-goods').each(function(){
   var curPrice=parseFloat($(this).parents('.detail-wrap').find('.product-summary').find('.special-price i').text());
   var oPrice=parseFloat($(this).parents('.detail-wrap').find('.product-summary').find('.old-price i').text());
   if(curPrice<oPrice)
   {
      var discount=Math.round(curPrice/oPrice*100);
      $(this).find('.discount b').html(discount);
   }
   else{
	  $(this).find('.discount').hide()//2015-02-15
   }
   })   
   

   //change currency
   $('.change-currency').hover(function(){
     $(this).toggleClass('currency-show');
   }) 

     
})


// ==============================================
// response
function winSizeChange()
{
	if (window.innerWidth)
		winWidth = window.innerWidth;
	else if ((document.body) && (document.body.clientWidth))
		winWidth = document.body.clientWidth;
	if (window.innerHeight)
		winHeight = window.innerHeight;
	else if ((document.body) && (document.body.clientHeight))
		winHeight = document.body.clientHeight;
	if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
	{
        winHeight = document.documentElement.clientHeight;
        winWidth = document.documentElement.clientWidth;
	}
 
	if(winWidth<=768){
	   $('.product-intro').each(function(){
		   if($(this).find('.mobile-product-view').length<1 & $(this).find('.small-img-item').length>0){
			   $('.product-intro > .product-view').append('<div class="mobile-product-view"></div>');
			  
		   }
		})	   
	   var mImgNums=$('.small-img-scroll li').length;
	   var smallItem=$('.small-img-scroll li');
	   $('.mobile-product-view').show();
	   $('.mobile-product-view').each(function(){
	      if($(this).find('.mobile-img-item').length<mImgNums){
	         for(i=0;i<mImgNums;i++){
		        var mImgUrl=smallItem.eq(i).find('img').attr('src_big');
	            $('.mobile-product-view').append('<div class="mobile-img-item">'+'<img src='+mImgUrl+'></div>');
	         }			  
		  }
	   })
		$('.mobile-product-view').owlCarousel({
			autoplay:false,
			loop:true,
			margin:0,
			dots: true,
			nav: true,
			smartSpeed:180,
			items:1
		});	

		//product detail tab		
		if($('.product-detail .detail-tabs .tab').length>0){
			$('.mobile-detail-items .mobile-detail-item').each(function(){
				var itemNum=$('.mobile-detail-items .mobile-detail-item').length;
					i=$(this).index();
					$('.detail-tabs .tab').eq(i).clone().appendTo($('.mobile-detail-items .mobile-detail-item').eq(i));
					$('.product-detail .detail-panel').eq(i).clone().appendTo($('.mobile-detail-items .mobile-detail-item').eq(i));
					// 清空style
					$('.detail-tabs .tab').removeClass('current');
					$('.detail-panel').attr('style','');
			})
			// 清空重复数据
			$('.product-detail .detail-tabs .tab').remove();
			$('.product-detail .detail-panel').remove();
			// 切换
			$('.mobile-detail-item').each(function(){
				$(this).find('.tab').click(function(){
				   var dTab=$(this).parents('.mobile-detail-item');
				   if(!dTab.hasClass('active')){
					   dTab.addClass('active');
				   }
				   else{
				       dTab.removeClass('active');
				   }
				})
			})
		}


	   
	}
	//mobile end
		
	else{
		$('.product-intro').each(function(){
		   if($(this).find('.mobile-product-view').length>0){
	          $('.mobile-product-view').remove()
			  $('.product-view .small-img-wrap').show()
		   }
		   //small image scroll 
		   $('.small-img-scroll').each(function(){
		      if($(this).find('li').length>1){
		      $(".small-img-scroll").jCarouselLite({
				 btnPrev: ".small-btn-prev",
				 btnNext: ".small-btn-next",
				 speed:100,
				 auto:false,
				 scroll:1,
				 visible:3,
				 vertical:false,
				 circular:false,
				 onMouse:true
		      });
		      }
		   })
		})
	   //hide scroll button
	   var imgCount=$('.small-img-scroll ul li').length;
	   if(imgCount<=3){
	      $('.small-btn-prev,.small-btn-next').hide();
	   }
	   else{
		  $('.small-btn-prev,.small-btn-next').show();
	   }

		  //product detail tab
			$('.mobile-detail-items .mobile-detail-item').each(function(){
				i=$(this).index();
				$('.mobile-detail-items').eq(i).find('.tab').appendTo($('.product-detail .detail-tabs'));
				$('.mobile-detail-items').eq(i).find('.detail-panel').appendTo($('.product-detail'));
			})
			if($('.detail-tabs .current').length<1){
				$('.detail-tabs .tab:first-child').addClass('current');	
			}
		    $('.product-detail .detail-tabs .tab').click(function(){
		        i=$(this).index();
		        $(this).addClass('current').siblings().removeClass('current');
		        $('.product-detail .detail-panel').eq(i).show();
		        $('.product-detail .detail-panel').not($('.product-detail .detail-panel').eq(i)).hide();
		     })  
		   $('.mobile-detail-items .tab').click(function(){
				   var dTab=$(this).parents('.mobile-detail-item');
				   if(!dTab.hasClass('active')){
					   dTab.addClass('active');
				   }
				   else{
				       dTab.removeClass('active');
				   }
			}) 



	}
}

$(document).ready(function(){
    winSizeChange();
})
//window.onresize=winSizeChange;

$(window).resize(function() {
  winSizeChange();
});




})(jQuery);