﻿; (function($) { $.fn.extend({ "totalWidth": function() { var tmpWidth = 0; $(this).each(function() { tmpWidth += $(this).outerWidth(true); }); return tmpWidth; }, "totalHeight": function() { var tmpHeight = 0; $(this).each(function() { tmpHeight += $(this).outerHeight(true); }); return tmpHeight; } }); $.fn.YlMarquee = function(o) { o = $.extend({ speed: 60, step: 3, vertical: false, width: 0, height: 0, visible: 0, textMode: false }, o || {}); var wrap = $(this), ul = $("ul", wrap), li = $("li", ul), v = o.visible, step = o.step, liNum = li.size(), visibleLi = li.slice(0, v); var whiteSpace, floatStyle, displayStyle, liSize, ie7HackCss, marginStyle, paddingStyle, wrapSize, visibleLiSize, i, scrollSize, cssPro; if (o.vertical) { whiteSpace = "normal"; floatStyle = "none"; displayStyle = "block"; wrapSize = o.height; } else { whiteSpace = "nowrap"; floatStyle = "left"; displayStyle = "inline"; wrapSize = o.width; ie7HackCss = o.textMode ? "*float:none;" : ""; } wrap.css({ position: "relative", overflow: "hidden" }); ul.css({ position: "relative", "white-space": whiteSpace, overflow: "hidden", "list-style-type": "none", margin: "0", padding: "0" }); li.css({ "white-space": whiteSpace, "display": displayStyle, overflow: "hidden" }); li.attr("style", li.attr("style") + ";" + "float:" + floatStyle + ";" + ie7HackCss); liSize = o.vertical ? li.totalHeight() : li.totalWidth(); o.vertical ? ul.height(liSize) : ul.width(liSize); visibleLiSize = o.vertical ? visibleLi.totalHeight() : visibleLi.totalWidth(); if (wrapSize == 0) { wrapSize = visibleLiSize; } o.vertical ? wrap.height(wrapSize) : wrap.width(wrapSize); if (wrapSize < liSize) { ul.append(li.clone()); var newLi = $("li", ul), newLiSize = o.vertical ? newLi.totalHeight() : newLi.totalWidth(); newLi.attr("style", newLi.attr("style") + ";" + "float:" + floatStyle + ";"); o.vertical ? ul.height(newLiSize) : ul.width(newLiSize); scrollSize = o.vertical ? newLi.slice(0, liNum).totalHeight() : newLi.slice(0, liNum).totalWidth(); var MyMar = setInterval(marquee, o.speed); ul.hover(function() { clearInterval(MyMar); }, function() { MyMar = setInterval(marquee, o.speed); }); } function marquee() { if (o.vertical) { if (wrap.scrollTop() >= scrollSize) { wrap.scrollTop(wrap.scrollTop() - scrollSize + step); } else { i = wrap.scrollTop(); i += step; wrap.scrollTop(i) } } else { if (wrap.scrollLeft() >= scrollSize) { wrap.scrollLeft(wrap.scrollLeft() - scrollSize + step); } else { i = wrap.scrollLeft(); i += step; wrap.scrollLeft(i); } } }; }; })(jQuery);
$("#nav").ready(function() {
  $("#CrawlUrl").focus(function() {
      $("#CrawlPromt").removeClass("red").addClass("green").text("输入中国购物网站的商品链接地址就可以代购！");
      if ($("#CrawlUrl").val() == "http://") {
          $("#CrawlUrl").val("");
      }
  }).blur(function() {
      $("#CrawlPromt").removeClass("green");
      if ($.trim($("#CrawlUrl").val()).length <= 0) {
          $("#CrawlUrl").val("http://");
      }
  });
  $("#CrawlUrl").keydown(function(d) {
    if (d.keyCode == 13) {
      e();
      return false;
    }
  });
  $("#CrawlBtn").click(e);
  $("#closeBtn").click(AddItemClose);
  $("#Tools").mouseenter(function(){$("#ToolsList").fadeIn();});
  $("#ToolsList").mouseleave(function(){$("#ToolsList").fadeOut();});
});


var e = function() {
  var d = $.trim($("#CrawlUrl").val());
  if (d.length <= 0 || d == "http://") {
      $("#CrawlPromt").removeClass("green").addClass("red").text("请您输入商品链接地址！");
      return false;
  }
  var i = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
  if (d.indexOf("http://") < 0 && d.indexOf("https://") < 0) {
      d = "http://" + d;
  }
  if (!i.test(d)) {
      $("#CrawlPromt").removeClass("green").addClass("red").text("您输入的链接地址不正确，请核实后再填写！");
      return false;
  }
  $("#CrawlUrl").val("");
  //alert(d);
  FastAddShow(d);
  return false;
};




//输入商品网址后提交方法
function addpanel_submit() {
  var url = $("#itemUrl").val();
  var reg = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
  if (url.length <= 0) {
      $("#promptInfo").attr("class", "addpanel_wrong");
      $("#promptInfo p").text("请输入您想代购商品的详细页网址！");
  }
  else {
    if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1)
        url = "http://" + url;
    if (reg.test(url)) {
       FastAddShow(url);
    }
    else {
      $("#promptInfo").attr("class", "addpanel_wrong");
      $("#promptInfo p").text("输入的网址不正确，请核实后再填写！");
    }
  }
}


function InitP2() {
  return;
  $("#proAlert").attr("class", "").text("恭喜您！商品信息抓取成功，您可以修改购买数量和填写商品备注！");
  $("#productUrl").val("");
  $("#productName").val("").attr("class", "addpanel_k").removeAttr("disabled").unbind("focus").unbind("blur");
  $("#productPrice").val("").attr("class", "").removeAttr("disabled").unbind("focus").unbind("blur");
  $("#productSendPrice").val("").attr("class", "").unbind("focus");
  $("#productNum").val("1");
  $("#productImg").hide();
  $("#isAuction").hide();
  $("#productImg img").attr("src", "");
  $("#productRemark").attr("class", "addpanel_still").text("请选填颜色、尺寸等要求！");
  $("#successBtn").attr("disabled", "disabled").attr("class", "addpanel_next_no");
  $("#vipPriceS").remove();
}

function p3Init() {
  $("#p3_img").attr("src", "/add/newimages/noimg80.gif");
}


function bulidP3() {
  $.ajax({
    type: "POST",
    url: "/ajax/fast_ajax.php?action=state",
    dataType: "text",
    contentType: "application/json;utf-8",
    data: "{}",
    timeout: 20000,
    error: function(a, b, c) { },
    success: function(data) {
        value = data.split("#"); $("#p3_cartNum").text(value[1].toString()); $("#p3_total").text(value[2].toString());
    }
  });
  
  $("#p3_img").attr("src", $("#productImg img").attr("src"));
  $("#p3_proName").text($("#productName").val());
  $("#p3_price").text($("#productPrice").val());
  $("#p3_sendPrice").text($("#productSendPrice").val());
  $("#p3_num").text($("#productNum").val());
}

function btnClsClick() {
  hideall();
  InitP2();
  if ($("#p3 div").length >= 1) {
      $("#p3").hide();
  }
  $(".address_").attr("class", "addpanel_address");
  $("#itemUrl").removeAttr("disabled").val("");
  $("#promptInfo").attr("class", "addpanel_dhk").find("img").remove();
  $("#promptInfo p").text("请将您想代购商品的详细页网址粘贴到输入框中提交!");
  $("#addpanel_submit").removeAttr("disabled").attr("class", "addpanel_tijiao"); 
  $("#p1").fadeIn();
  $("#itemUrl").focus();
}
//执行抓取操作

function hideall()
{
  $("#p0").hide();
  $("#p1").hide();
  $("#p2").hide();
  $("#p3").hide();
}
function FastAddShow(url) {
  hideall();
  $(".addpanel_overlay").height($(document).height()).show();
  $(".addpanel_dialog").show();
  $("#p0").fadeIn();

  if(url==null){
    hideall();
    $("#p1").fadeIn();
    $("#itemUrl").focus();
  }else{
    $.ajax({
      url: '/good/get/',
      type: 'post',
      data: {'url':url},
      success: function(data){
        $("#p2").html(data);
        hideall();
        $("#p2").fadeIn();
      },
      error: function(){
        $("#p2").load('/good/nodata/',{'url':url});
        hideall();
        $("#p2").fadeIn();
      }
    });
  }
}

function FastAddSubmit(){
  $("#addpanel_submit").click(); 
}


function AddItemClose() {
  $(".addpanel_dialog").hide();
  $(".addpanel_overlay").hide();
  if ($("#p2 div").length >= 1) {
      $("#p2").hide();
      InitP2();
  }
  if ($("#p3 div").length >= 1) {
      $("#p3").hide();
      p3Init();
  }
  $(".addpanel_address_").attr("class", "addpanel_address");
  $("#itemUrl").removeAttr("disabled").val("");
  $("#promptInfo").attr("class", "addpanel_dhk").find("img").remove();
  $("#promptInfo p").text("请将您想代购商品的详细页网址粘贴到输入框中提交!");
  $("#addpanel_submit").removeAttr("disabled").attr("class", "addpanel_tijiao");
  $("#p1").show();
}

var browser = navigator.appName;
var b_version = navigator.appVersion;
var version = parseFloat(b_version);
navigator.compitable

var addItem_productInfo = {
    "Name": "",
    "Href": "",
    "Picture": "",
    "Thumbnail": "",
    "chicun": "",
    "yanse": "",
    "ShopName": "",
    "ShopHref": "",
    "Price": -1,
    "VIPPrice1": -1,
    "VIPPrice2": -1,
    "VIPPrice3": -1,
    "BuyNum": -1, //此属性暂时无用
    "Freight": -1,
    "IsAuction": false
};

var OnFail = function(XMLHttpRequest, textStatus, errorThrown) {
  alert("网络错误，请稍后再试");
};

var OnSuccess = function(resault) {
  hideall();
  if (resault) {
    p3Init();
    bulidP3();
    $("#p3").show();
  }
  else {
    alert("添加商品失败");
    btnClsClick();
  }
}

function successBtn() {
  if ($("#productPrice").attr("class") != "addpanel_hui") {
      addItem_productInfo.Price = parseFloat($("#productPrice").val());
  }
  typevalue=$("#BuySelf").attr("checked");
  addItem_productInfo.chicun=$("#chicun").val();//获取尺寸信息
  addItem_productInfo.yanse=$("#yanse").val();//获取颜色信息
  addItem_productInfo.type=1;//默认是代购
  addItem_productInfo.expressno="";//默认快递号码是空
  if(typevalue==true){
   if ($.trim($("#expressno").val()).length <= 0) {
    $("#expressno").focus();
      return;
    }else{
      addItem_productInfo.type=2;//获取是否是代发
      addItem_productInfo.expressno=$.trim($("#expressno").val());//获取颜色信
    }
  }
  if ($.trim($("#productName").val()).length <= 0) {
      return;
  }
  if ($.trim($("#productPrice").val()).length <= 0) {
      return;
  }

  var datasource = addItem_productInfo;

  $.ajax({
      type: "POST",
      url: "/ajax/fast_ajax.php?action=add",
      dataType: "json",
      data:"adddata="+encodeURIComponent("{\"name\":'" + HtmlEncode($("#productName").val()) + "',\"href\":'" + $("#productUrl").val() + "',\"picture\":'" +  $('#productImgThumb').attr('src') + "',\"thumbnail\":'" + $('#productImgThumb').attr('src') + "',\"chicun\":'" + datasource.chicun + "',\"yanse\":'" + datasource.yanse + "',\"shopName\":'" + $("#shopName").val() + "',\"shopHref\":'" + $("#shopUrl").val() + "',\"price\":" + parseFloat(datasource.Price) + ",\"vipPrice1\":" + datasource.VIPPrice1 + ",\"vipPrice2\":" + datasource.VIPPrice2 + ",\"vipPrice3\":" + datasource.VIPPrice3 + ",\"buyNum\":" + $("#productNum").val() + ",\"freight\":" + $("#productSendPrice").val() + ",\"isAuction\":" + datasource.IsAuction + ",\"remark\":'" + $("#productRemark").val()  + "',\"type\":" + datasource.type  + ",\"expressno\":'" + datasource.expressno + "'}"),
      timeout: 10000,
      error: OnFail,
      success: OnSuccess
  });
}


function toGetPro() {
  var url = $("#myAllyesbuy_itemUrl").val();
  if (url.length <= 0) { $('#itemUrlTip').attr("class", "wrong").html("<p>请输入您想代购商品的详细页网址！</p>"); return; }
  if (url.indexOf("http://") <= -1 && url.indexOf("https://") <= -1) url = "http://" + url;
  if (new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?").test(url)) {
    url = $("#myAllyesbuy_itemUrl").val();$("#CrawlUrl").val(url);
    var d = $.trim($("#CrawlUrl").val());
    if (d.length <= 0 || d == "http://") {
        $("#CrawlPromt").removeClass("green").addClass("red").text("请您输入商品链接地址！");
        return false;
    }
    var i = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?");
    if (d.indexOf("http://") < 0 && d.indexOf("https://") < 0) {
        d = "http://" + d;
    }
    if (!i.test(d)) {
        $("#CrawlPromt").removeClass("green").addClass("red").text("您输入的链接地址不正确，请核实后再填写！");
        return false;
    }
    $("#CrawlUrl").val("");
    FastAddShow(d);
    return false;
  }
  else { $('#itemUrlTip').attr("class", "wrong").html("<p>输入的网址不正确，请核实后再填写！</p>"); return; }
}

     
/*
 *  USEFUL FUNC
 * 
*/

function HtmlEncode(text) {return text.replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\\/g, "").replace(/'/g, "|");}
function getViewportHeight() { if (window.innerHeight != window.undefined) { return window.innerHeight } if (document.compatMode == "CSS1Compat") { return document.documentElement.clientHeight } if (document.body) { return document.body.clientHeight } return window.undefined }
function clearinput(elem){if($(elem).hasClass('addpanel_red')){$(elem).val(''); $(elem).attr("class", "");}}
function clearonce(elem){if($(elem).hasClass('once')){$(elem).val(''); $(elem).removeClass("once");}}

