$(document).ready(function(){
  $("#div_eclipse").css({"height":+($("body").height()+100)+"px"});
  left_category();
  productImage_jcarousel();
  selProductSku(); 
});

function productImage_jcarousel(){
    jQuery('#mycarousel').jcarousel();
    //$(".jcarousel-skin-tango .jcarousel-item:first").children(".item_top").addClass("item_top_selected");
    //$(".jcarousel-skin-tango .jcarousel-item:first").children(".productImgSmall").addClass("productImgSmall_seleted");
    $(".jcarousel-skin-tango .jcarousel-item").click(function(){
      $(".jcarousel-skin-tango .jcarousel-item").children(".item_top").removeClass("item_top_selected");
      $(".jcarousel-skin-tango .jcarousel-item").children(".productImgSmall").removeClass("productImgSmall_seleted");
      $(this).children(".item_top").addClass("item_top_selected");
      $(this).children(".productImgSmall").addClass("productImgSmall_seleted");
      $("#product_normal").attr("src",$(this).children(".productImgSmall").attr("jqimg"));
      $("#product_normal").attr("jbimgbig",$(this).children(".productImgSmall").attr("jbimgbig"));
    })
    $("#product_normal").click(function(){
      window.open ($(this).attr("jbimgbig"));
    })
    $(".middle_tab > a:first").addClass("tab_selected");
    $(".middle_tab > a").click(function(){
      $(".middle_tab > a.tab_selected").removeClass("tab_selected"); 
      $(this).addClass("tab_selected");
      if( $(this).attr("tab_link") =="all"){
        $("#tab_content > div").show();
      }else{
        $("#tab_content > div").hide();
        $("#tab_content > div."+$(this).attr("tab_link")).show();
      }
    });  
}


function sku_color_click(self){
  $(".sku_form p.sku_all_color span").removeClass("on_this");
  $(self).addClass("on_this");
  $(".sku_form p.sku_all_color label.sku_selected").html($(self).children("a").html());
  $(".sku_form p.sku_all_size label.sku_size_items").removeClass("show_this");
  $("#"+$(self).children("a").attr("sku_id")).addClass("show_this");
  $(".sku_form p.sku_all_size label.sku_selected").html("未选择");
  $(".sku_form p.sku_all_size span").removeClass("on_this");
  document.proFrm.skuid.value='';
}
function sku_size_click(self){
  $(".sku_form p.sku_all_size span").removeClass("on_this");
  $(self).addClass("on_this");
  $(".sku_form p.sku_all_size label.sku_selected").html($(self).children("a").html());
  document.proFrm.skuid.value=$(self).children("a").attr("skuid");
}
function selProductSku(){
    $(".sku_form p.sku_all_color span").click(function(){
      sku_color_click(this);
    });
    $(".sku_form p.sku_all_size span").click(function(){
      sku_size_click(this);
    });
}

var ShopProductId;
function initSku(proid){
  ShopProductId=proid;
  refreshSku();
  setInterval("refreshSku();",3600000);
}
function refreshSku(){//proid=19501;//19501,19674,诱人兔女郎黑色游戏服(lovely-kit6044)
  var url=base + '/product-status/'+ShopProductId+'?'+(new Date()).getTime();
  $('#sku_color_items').html('');
  $('#sku_size_items').html('');
  $('.buttons').html('');
  document.proFrm.skuid.value='';
  $.getJSON(url, function(data){
      var skuCount=0;
      if(data){
        if(typeof SHOP_SKU_SEL!=='undefined' && SHOP_SKU_SEL===true){
          var colorSpans=[],sizeSpans=[],i=0;
          $.each(data, function(key, value){
            var sku_id='size_of_sku_color_'+(i++);
            colorSpans.push('<span><a sku_id="');
            colorSpans.push(sku_id);
            colorSpans.push('">');
            colorSpans.push(key);
            colorSpans.push('</a></span>');
            
            sizeSpans.push('<label id="');
            sizeSpans.push(sku_id);
            sizeSpans.push('" class="sku_size_items">');
            $.each(value,function(index,v){
              sizeSpans.push('<span><a skuid="');
              sizeSpans.push(v.id);
              sizeSpans.push('">');
              sizeSpans.push(v.size);
              sizeSpans.push('</a></span>');
              skuCount++;
            });
            sizeSpans.push('</label>');
          }); 
          $('#sku_color_items').html(colorSpans.join(''));
          $('#sku_size_items').html(sizeSpans.join(''));
          selProductSku(); 
          if(i==1){
            sku_color_click($("#sku_color_items span"));
            if(skuCount==1){
              sku_size_click($("#sku_size_items span"));
            }
          }
        }else{
          $.each(data, function(key, value){
            $.each(value,function(index,v){
              document.proFrm.skuid.value=v.id;
              skuCount++;
              return false;
            });
          }); 
        }
      }
      if(skuCount>0){ 
        $('#no_cargo_handling_buttons').html('<p class="buttons"><a href="javascript:void(buy())" class="buy_button"></a></p>');
      }else{
        $('#no_cargo_handling_buttons').html('<p><span class="no_cargo_handling">目前无货，欢迎选购其它类似商品。<a href="javascript:void(showMailNotify())">到货通知我</a></span></p>');
      }
  });
}

function buy(){
 if(document.proFrm.skuid.value==''){
   alert('请选择尺码');return false;
 }
 document.proFrm.number.value=1;
 document.proFrm.submit();
 return true;
}