//商品規格 var prod_detail = function(option){ var sets = { json64ID : 'json64', Spec_Code1_ValID: 'Spec_Code1_Val', Spec_Code2_ValID: 'Spec_Code2_Val', Spec_TypeID : 'Spec_Type', Prod_TypeID : 'Prod_Type', Prod_Sell_ID : 'Prod_Sell_ID', my_price : 'my_price', stock_info_span : 'stock_info_span', Prod_ImgID : 'Prod_Img', spec_div1ID : 'detail_colorbox', spec_items1ID : 'spec_items1', spec_name1ID : 'spec_name1', spec_focus1 : 'color_focus', spec_default1 : 'color_default', spec_div2ID : 'detail_sizebox', spec_items2ID : 'spec_items2', spec_name2ID : 'spec_name2', spec_focus2 : 'size_focus', spec_default2 : 'size_default', spec_not2 : 'size_not', amountID : 'Amount', Add_Cart_BTID : 'Add_Cart_BT', Add_Inform_BTID : 'Add_Inform_BT', bonus_parentID : 'bonus_parentID', bonusID : 'bonusID', url1 : '/products/ajax/ajax_add_prod1.php', Add_Button_SpanID: 'Add_Button_Span' }; $.extend(sets, option); var _this = this; this.json64ID = sets.json64ID; this.Spec_Code1_ValID = sets.Spec_Code1_ValID; this.Spec_Code2_ValID = sets.Spec_Code2_ValID; this.Spec_TypeID = sets.Spec_TypeID; this.Prod_TypeID = sets.Prod_TypeID; this.Prod_Sell_ID = sets.Prod_Sell_ID; this.my_price = sets.my_price; this.stock_info_span = sets.stock_info_span; this.Prod_ImgID = sets.Prod_ImgID; this.spec_div1ID = sets.spec_div1ID; this.spec_items1ID = sets.spec_items1ID; this.spec_name1ID = sets.spec_name1ID; this.spec_name1ID = sets.spec_name1ID; this.spec_focus1 = sets.spec_focus1; this.spec_default1 = sets.spec_default1; this.spec_div2ID = sets.spec_div2ID; this.spec_items2ID = sets.spec_items2ID; this.spec_name2ID = sets.spec_name2ID; this.spec_focus2 = sets.spec_focus2; this.spec_default2 = sets.spec_default2; this.spec_not2 = sets.spec_not2; this.amountID = sets.amountID; this.Add_Cart_BTID = sets.Add_Cart_BTID; this.Add_Inform_BTID = sets.Add_Inform_BTID; this.bonus_parentID = sets.bonus_parentID; this.bonusID = sets.bonusID; this.url1 = sets.url1; this.Add_Button_SpanID = sets.Add_Button_SpanID; this.jDatas = Object; this.Spec_Code1_Val = String; this.Price1 = Number; this.init = function(){ var hash = window.location.hash.replace('#', ''); //解碼json字串 _this.jDatas = _this.jsonDecode($("#" + _this.json64ID).val()); //讀取價格 _this.Price1 = parseFloat($("#" + _this.my_price).text()); //focus規格代碼1 _this.Spec_Code1_Val = hash || $('#' + _this.Spec_Code1_ValID).val(); //focus規格代碼2 _this.Spec_Code2_Val = $('#' + _this.Spec_Code2_ValID).val(); //規格 switch($('#' + _this.Spec_TypeID).val()){ case '1': _this.Spec_Type1(); break; case '2': _this.Spec_Type2(); break; case '3': _this.Spec_Type3(); break; }; //設定事件 _this.AddCart(); //加入購物車 }; //加入購物車 this.AddCart = function(){ $("#" + _this.Add_Cart_BTID).live("click", function(){ switch($('#' + _this.Prod_TypeID).val()){ case '1': //商品 case '2': //贈品 _this.ProdType1($("#" + _this.Prod_Sell_ID).val()); break; case '3': //特惠組 case '4': //組合包 _this.ProdType3($("#" + _this.Prod_Sell_ID).val()); break; }; }); }; //貨到通知視窗 $("#" + _this.Add_Inform_BTID).live("click", function(){ // $("body").append('
'); // $("#Add_Inform_Div").load('/products/ajax/ajax_inform.php', {Prod_Sell_ID: $("#" + _this.Prod_Sell_ID).val()}).css({ // position : 'absolute', // left : $(this).offset().left - 200, // top : $(this).offset().top // }); //改為跳轉到聯絡我們頁面 window.location.href = '/聯絡我們/action-contact.htm'; }); //Ajax this.ajaxRequest = function(url, send, callback, dataType){ $.ajax({ type : "POST", url : url, dataType : dataType, async : true, data : send, success : function(ret){ if($.isFunction(callback))callback(ret); } }); }; //商品 this.ProdType1 = function(id){ if(parseInt($("#" + _this.amountID).val()) <= 0){ alert(jsLang.請選擇數量); return false; } else{ var flag = _this.ProdTypeAdd(); if(!flag)return false; _this.ajaxRequest(_this.url1, '&ID=' + id + "&Amount=" + parseInt($("#" + _this.amountID).val()), function(ret){ switch(ret){ case 'RET_STOCK_ERROR': alert(jsLang.商品庫存不足); break; case 'RET_SUCCESS': alert(jsLang.加入購物車成功); var topCARTObj = new topCART(); topCARTObj.detail(id, $("#" + _this.amountID).val()); break; case 'RET_DATABASE_ERROR': alert(jsLang.資料庫忙線中); break; }; $("#" + _this.amountID).val('0'); }, 'html'); }; }; //加購商品 this.ProdTypeAdd = function(){ var counts = parseInt($("#add_counts_span").text()); var amount_counts = 0; $(".Add_Amount").each(function(){ if(!$(this).val())$(this).val('0'); amount_counts += parseInt($(this).val()); }); if(amount_counts < counts){ if(confirm('您尚有' + parseInt(counts - amount_counts) + '件未選擇加購。確定送出購物車嗎?')){ var flag = true; } else{ var flag = false; } } else{ var flag = true; }; if(flag){ $(".Add_Prod_Sell_ID").each(function(idx){ _this.ajaxRequest(_this.url1, '&ID=' + $(this).val() + "&Amount=" + parseInt($(".Add_Amount:eq(" + idx + ")").val()), function(ret){ switch(ret){ case 'RET_STOCK_ERROR': alert(jsLang.商品庫存不足); break; case 'RET_SUCCESS': break; case 'RET_DATABASE_ERROR': alert(jsLang.資料庫忙線中); break; }; }, 'html'); }); $(".Act_Add_Tr").remove(); }; return flag; }; //特惠組,組合包 this.ProdType3 = function(id){ if(parseInt($("#" + _this.amountID).val()) <= 0){ alert(jsLang.請選擇數量); return false; } else{ var flag = _this.ProdTypeSell(); if(!flag)return false; _this.ajaxRequest(_this.url1, '&ID=' + id + "&Amount=" + parseInt($("#" + _this.amountID).val()), function(ret){ switch(ret){ case 'RET_STOCK_ERROR': alert(jsLang.商品庫存不足); break; case 'RET_SUCCESS': alert(jsLang.加入購物車成功); var topCARTObj = new topCART(); topCARTObj.detail(id, $("#" + _this.amountID).val()); break; case 'RET_DATABASE_ERROR': alert(jsLang.資料庫忙線中); break; }; $("#" + _this.amountID).val('0'); }, 'html'); }; }; //特惠組,組合包商品 this.ProdTypeSell = function(){ var counts = parseInt($("#add_counts_span").text()); var amount_counts = 0; $(".Add_Amount").each(function(){ if(!$(this).val())$(this).val('0'); amount_counts += parseInt($(this).val()); }); if(amount_counts < counts){ alert('您尚有' + parseInt(counts - amount_counts) + '件商品未選擇,請選擇完整後在加入購物車'); var flag = false; } else{ var flag = true; }; if(flag){ $(".Add_Prod_Sell_ID").each(function(idx){ _this.ajaxRequest(_this.url1, '&ID=' + $(this).val() + "&Amount=" + parseInt($(".Add_Amount:eq(" + idx + ")").val()), function(ret){ switch(ret){ case 'RET_STOCK_ERROR': alert(jsLang.商品庫存不足); break; case 'RET_SUCCESS': break; case 'RET_DATABASE_ERROR': alert(jsLang.資料庫忙線中); break; }; }, 'html'); }); $(".Act_Add_Tr").remove(); }; return flag; }; //解base64和json this.jsonDecode = function(str){ return JSON.parse(base64_decode(str)); }; //無規格 this.Spec_Type1 = function(){ $("#" + _this.spec_div1ID).hide(); $("#" + _this.spec_div2ID).hide(); //設定第一筆商品ID _this.setProdSellID(_this.jDatas.Prod_Sell_ID); //設定數量 _this.setAmount(_this.jDatas.Stock); //設定購物金 _this.setBonus(_this.jDatas.Get_Bonus); }; //單規格 this.Spec_Type2 = function(){ $("#" + _this.spec_div1ID).show(); $("#" + _this.spec_div2ID).hide(); var ret1 = _this.setSpec1(); //設定規格1 _this.setProdSellID(ret1.prod_sell_id); //設定第一筆商品ID _this.setAmount(ret1.stock); //設定數量 _this.setImg(ret1.pic_path); //設定第一張圖片 _this.setSpecName(_this.spec_name1ID, ret1.spec_name1); //設定規格名稱1 _this.setPrice(ret1.hops_price); //設定計算跳段價差 _this.setBonus(ret1.get_bonus); //設定購物金 //點選規格1 $(".spec1_BT").click(function(){ var spec_code1 = $(this).attr('spec_code1'); _this.setProdSellID(_this.jDatas[spec_code1].Prod_Sell_ID); //設定第一筆商品ID _this.setAmount(_this.jDatas[spec_code1].Stock); //設定數量 _this.setImg(_this.jDatas[spec_code1].Pic_Path); //設定第一張圖片 _this.setSpecName(_this.spec_name1ID, _this.jDatas[spec_code1].Spec_Name1); //設定規格名稱1 _this.setPrice(_this.jDatas[spec_code1].Hops_Price); //設定計算跳段價差 _this.setBonus(_this.jDatas[spec_code1].Get_Bonus); //設定購物金 //focus點選 $(".spec1_BT").removeClass(_this.spec_focus1).addClass(_this.spec_default1); $(this).addClass(_this.spec_focus1); //換底下圖片 var prod_no = _this.jDatas[spec_code1].Prod_No; $("#products_detail_imsmallbox").load('/products/ajax/ajax_prod_pic.php', {Spec_Type1: '2', Prod_No: prod_no, Spec_Code1: spec_code1}); }); }; //雙規格 this.Spec_Type3 = function(){ $("#" + _this.spec_div1ID).show(); $("#" + _this.spec_div2ID).show(); var ret1 = _this.setSpec1(); //設定規格1 var ret2 = _this.setSpec2(ret1.spec_code1); //設定規格2 _this.setProdSellID(ret2.prod_sell_id); //設定第一筆商品ID _this.setAmount(ret2.stock); //設定數量 _this.setImg(ret2.pic_path); //設定第一張圖片 _this.setSpecName(_this.spec_name1ID, ret1.spec_name1); //設定規格名稱1 _this.setSpecName(_this.spec_name2ID, ret2.spec_name2); //設定規格名稱2 _this.setPrice(ret2.hops_price); //設定跳段價差 _this.setBonus(ret2.get_bonus); //設定購物金 //點選規格1 $(".spec1_BT").click(function(){ var spec_code1 = $(this).attr('spec_code1'); var ret2 = _this.setSpec2(spec_code1); //設定規格2 _this.setProdSellID(ret2.prod_sell_id); //設定第一筆商品ID _this.setAmount(ret2.stock); //設定數量 _this.setImg(ret2.pic_path); //設定第一張圖片 _this.setSpecName(_this.spec_name1ID, _this.jDatas[spec_code1].Spec_Name1); //設定規格名稱1 //觸發點選規格2 $(".spec2_BT:eq(0)").trigger('click'); //focus點選 $(".spec1_BT").removeClass(_this.spec_focus1).addClass(_this.spec_default1); $(this).addClass(_this.spec_focus1); //換底下圖片 var prod_no = _this.jDatas[spec_code1].Prod_No; $("#products_detail_imsmallbox").load('/products/ajax/ajax_prod_pic.php', {Spec_Type1: '2', Prod_No: prod_no, Spec_Code1: spec_code1}); }); //alert(1); //點選規格2 $(".spec2_BT").die("click").live('click', function(){ var spec_code1 = $(this).attr('spec_code1'); var spec_code2 = $(this).attr('spec_code2'); if(spec_code1 && spec_code2){ var Sepc2 = _this.jDatas[spec_code1]['Spec2']; _this.setProdSellID(Sepc2[spec_code2].Prod_Sell_ID); //設定商品ID _this.setAmount(Sepc2[spec_code2].Stock); //設定數量 _this.setSpecName(_this.spec_name2ID, Sepc2[spec_code2].Spec_Name2); //設定規格名稱2 _this.setPrice(Sepc2[spec_code2].Hops_Price); //設定計算跳段價差 _this.setBonus(Sepc2[spec_code2].Get_Bonus); //設定購物金 //focus點選 $(".spec2_BT").removeClass(_this.spec_focus2) $(".spec2_BT").addClass(_this.spec_default2); $(this).addClass(_this.spec_focus2); }; }); }; //設定規格1 this.setSpec1 = function(){ var i = 0; var json = _this.jDatas; for(s in json){ //庫存0不顯示 //if(json[s].Stock <= 0)continue; i++; if(_this.Spec_Code1_Val == s){ var span_class = _this.spec_focus1; var prod_sell_id = json[s].Prod_Sell_ID; var stock = json[s].Stock; var spec_code1 = s; var pic_path = json[s].Pic_Path; var spec_name1 = json[s].Spec_Name1; var hops_price = json[s].Hops_Price; var get_bonus = json[s].Get_Bonus; } else if(i == 1 && _this.Spec_Code1_Val == ''){ var span_class = _this.spec_focus1; var prod_sell_id = json[s].Prod_Sell_ID; var stock = json[s].Stock; var spec_code1 = s; var pic_path = json[s].Pic_Path; var spec_name1 = json[s].Spec_Name1; var hops_price = json[s].Hops_Price; var get_bonus = json[s].Get_Bonus; } else{ var span_class = _this.spec_default1; }; $("#" + _this.spec_items1ID).append('' + json[s].Spec_Name1 + ''); }; return { 'spec_code1' : spec_code1, 'prod_sell_id' : prod_sell_id, 'stock' : stock, 'pic_path' : pic_path, 'spec_name1' : spec_name1, 'hops_price' : hops_price, 'get_bonus' : get_bonus }; }; //設定規格2 this.setSpec2 = function(spec_code1){ var i = 0; var json = _this.jDatas[spec_code1].Spec2; $("#" + _this.spec_items2ID).find('.spec2_BT').remove(); for(s in json){ i++; if(_this.Spec_Code2_Val == s){ var span_class = _this.spec_focus2; var prod_sell_id = json[s].Prod_Sell_ID; var stock = json[s].Stock; var spec_name2 = json[s].Spec_Name2; var hops_price = json[s].Hops_Price; var get_bonus = json[s].Get_Bonus; } else if(i == 1 && _this.Spec_Code2_Val == ''){ var span_class = _this.spec_focus2; var prod_sell_id = json[s].Prod_Sell_ID; var stock = json[s].Stock; var spec_name2 = json[s].Spec_Name2; var hops_price = json[s].Hops_Price; var get_bonus = json[s].Get_Bonus; } else{ var span_class = _this.spec_default2; }; (!json[s].Spec_Name2)? json[s].Spec_Name2 = s: ""; if(json[s].Stock > 0){ $("#" + _this.spec_items2ID).append('' + json[s].Spec_Name2 + ''); } else{ $("#" + _this.spec_items2ID).append('' + json[s].Spec_Name2 + ''); }; }; var pic_path = _this.jDatas[spec_code1].Pic_Path; return { 'prod_sell_id' : prod_sell_id, 'stock' : stock, 'pic_path' : pic_path, 'spec_name2' : spec_name2, 'hops_price' : hops_price, 'get_bonus' : get_bonus }; }; //設定第一筆商品ID this.setProdSellID = function(v){ $("#" + _this.Prod_Sell_ID).val(v); //加入商品的按鈕 $("#" + _this.Add_Button_SpanID).load('/products/ajax/ajax_bt.php', {Prod_Sell_ID: v}); }; //設定第一張圖片 this.setImg = function(src){ $("#" + _this.Prod_ImgID).attr('src', src); $("#" + _this.Prod_ImgID).attr('bigsrc', src.replace('prodpic/D_', 'prodpic/')); }; //設定規格名稱 this.setSpecName = function(id, str){ $("#" + id).html(str); }; //設定數量 this.setAmount = function(num){ (num <= 0 || !num)? $("#" + _this.stock_info_span).show(): $("#" + _this.stock_info_span).hide(); (num >= 5)? num = 5: ""; if($('#' + _this.Prod_TypeID).val() == '3' || $('#' + _this.Prod_TypeID).val() == '4'){ num = 1; }; $("#" + _this.amountID).children(':gt(0)').remove(); for(i = 1; i <= num; i++){ if(i == 1)var s = 'selected="selected"'; else var s = ""; $("#" + _this.amountID).append(''); }; }; //設定購物金 this.setBonus = function(num){ (num > 0)? $("#" + _this.bonus_parentID).show(): $("#" + _this.bonus_parentID).hide(); $("#" + _this.bonusID).html(num); }; //設定價格 this.setPrice = function(price){ if(price){ $("#" + _this.my_price).html(_this.Price1 + parseFloat(price)); }; }; };