//////////////////////////////////////////////////////////////////////////////////////
// jQuery for 367net.com
// ---------------------------------------------------------
// @name ready.js
// @author Steven
// @version 0.1
// @date April 21, 2009
// @copyright 2010 SLC Development Co. Ltd
//////////////////////////////////////////////////////////////////////////////////////
$(function(){
  //products.asp页面设置元素的高度
  $('#pro_details > .img').height($('#pro_details').height());

  //产品图片幻灯效果
  if($(".picShow, .imgbox").length){
    $.getScript("/js/jq.imgbox.js", function(){
      $(".picShow > a, .imgbox > a").imgbox();

      $("body#products .imgbox > a:has('img')").imgbox({
        'overlayShow'   : true,
        'overlayOpacity': '0.7',
        'alignment'     : 'center'
      });

      /*$("#about .picShow a, #showroom .imgbox a").imgbox({
        'zoomOpacity'  : true,
      });*/
    });
  }

  $('#picbox').click(function(){return false;});

  //IE6 only
  if($.browser.msie && $.browser.version<7){//找出IE6浏览器
    //修正IE6的固定定位问题
    $("#guarantee").fixed({left:"5px", top:"5px"});
    $("#time").fixed({right:"5px", top:"5px"});
    $("#extra1").fixed("rightbottom");
    $('#pro.housing div.pro img').css({height:"100%", width:"auto", "max-width":"100%"});
  }

  $('#showroom .btn span').hover(function(){
    $(this).addClass('hover');
  },function(){
    $(this).removeClass('hover');
  });

  //selectPage for all products pages
  $("select[name='page']").change(function(){
    selectPage('parent',this,0);
  });

  /*Tab 选项卡 标签*/
  var $tab_title = $("#tab > .tab_title > a");
  //var $h1_text = $("h1").text();
  $tab_title.hover(function(){
    $('body')[0].className = this.className;
    var index =  $tab_title.index(this);  // 获取当前点击的<li>元素 在 全部li元素中的索引。
    $("#tab > .tab_content").eq(index).show().siblings("#tab > .tab_content").hide();
  });//.filter("[title="+$h1_text+"]").trigger("click");

  //隐藏型号列表最后的竖线
  $("#types > dd:last").addClass("last");

  //验证VIP注册表单
  if($('#vip_form').length){
    $.getScript("/js/jq.validate.js", function(){
      //电话号码验证
      jQuery.validator.addMethod("tel", function(value, element) {
        value = value.replace(/\s+/g, "");
        var patrn = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;//必须以数字开头，除数字外，可含有“-”
        return this.optional(element) || value.match(/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/) || patrn.test(value);
      }, "Please specify a valid tel number");

      //联系电话码验证
      jQuery.validator.addMethod("phone", function(value, element) {
        value = value.replace(/\s+/g, "");
        var mobile = /^((0|\+44)7(5|6|7|8|9){1}\d{2}\s?\d{6})$/
        var tel = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;
        var patrn = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;//必须以数字开头，除数字外，可含有“-”
        return this.optional(element) || value.match(tel) || value.match(mobile) || patrn.test(value);
      }, "Please specify a valid phone number");
      $('#vip_form').validate({
        rules: {
          country: "required",
          company: "required",
          name: {
            required: true,
            minlength: 2
          },
          email: {
            required: true,
            email: true
          },
          //tel: "required",
          tel: {
            required: true,
            phone: true
          },
          fax: {
            required: true,
            tel: true
          }
        },

        messages: {
          country: "Please enter your country",
          company: "Please enter your company name",
          name: {
            required: "Please enter your name",
            minlength: "Your name must consist of at least 2 characters"
          },
          email: "Please enter a valid email address",
          tel: {
            required: "Please enter your tel number",
            minlength: "Please specify a valid phone number"
          },
          fax: {
            required: "Please enter your fax number",
            minlength: "Please specify a valid fax number"
          }
        }
      });
    });
  }
});

function selectPage(targ,selObj,restore){//v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

/*##################页面加载完执行##################*/
$(window).load(function(){
  //元素居中
  if($('#pro_details').length){
    $.getScript("/js/jq.center.js", function(){
      if($.browser.msie && $.browser.version<8) $('#pro_details > .img img').center();
      $('#pro_related .img > img').center();
    });
  }

  /*======新产品列表滚动效果======*/
  var $hot_pros = $("#hot_pros > ul");
  var scrollTimer;
  $hot_pros.hover(function(){
    clearInterval(scrollTimer);
  }, function(){
    scrollTimer = setInterval(function(){
      var scrollHeight = $hot_pros.children("li:first").height();//获取行高
      $hot_pros.animate({marginTop: -scrollHeight + "px"}, 200, function(){
        $(this).css("margin-top", 0).children("li:lt(2)").appendTo($(this));
      });//把元素移到最后
    }, 5000);
  }).trigger("mouseleave");

  /*======产品展示图片滚动效果======*/
  var page = 1;
  var i = 5;//每版放5个图片
  var scrollerTimer;
  var $prolist = $("#showroom > div.prolist > ul");
  var $btn =  $("#showroom > div.btn > span");
  var len = $prolist.children("li").length;
  var page_count = Math.ceil(len / i);//只要不是整数，就往大的方向取最小的整数
  var page_width = $prolist.parent().width();//获取框架内容的宽度,不带单位

  $('#showroom').hover(function(){
    clearInterval(scrollerTimer);
  }, function(){
    scrollerTimer = setInterval(function(){
      scroll_next();
    }, 6000);
  }).trigger("mouseleave");

  //next按钮
  $btn.filter(".next").click(scroll_next = function() {
    if(!$prolist.is(":animated")) {
      if(page == page_count) {//已经到最后一个版面了,如果再向后，必须跳转到第一个版面。
        $prolist.animate({ left: 0 }, 1000);//通过改变left值，跳转到第一个版面
        page = 1;
      }else {
        $prolist.animate({ left: '-=' + page_width }, 1000);//通过改变left值，达到每次换一个版面
        page++;
      }
    }
  });

  //prev按钮
  $btn.filter(".prev").click(function() {
    if(!$prolist.is(":animated")) {
      if(page == 1) {//已经到第一个版面了,如果再向前，必须跳转到最后一个版面。
        $prolist.animate({ left: '-=' + page_width*(page_count-1) }, 1000);//通过改变left值，跳转到最后一个版面
        page = page_count;
      }else {
        $prolist.animate({ left: '+=' + page_width }, 1000);//通过改变left值，达到每次换一个版面
        page--;
      }
    }
  });
});
