/*js of science——wxy 2017.3.21*/
$(document).ready(function(){
	TwoImgMargin(".detail-img-half");
	ThreeImgMargin(".img-three");



	// 为侧导航绑定事件
	$(".content-nav>li").each(function(){
		if($(this).attr("class")=="has-children"){// 含有子项目
			// console.log($(this).attr("class"));
            var that=this,url,name,imgUrls,text;
			$(this).children().children("li").bind("click",function(){
                showPresentChildrenContent(this,".content-detail");
			    if($(that).attr("id")=="product-center"){
                    url="/product/";
                    name=$(this).attr("name");
                    imgUrls="/image?image=";
                    text=$(this).text();
                    console.log(name,this);
                    if(!(name=="线上高端定制产品")){
                        showProductContent(url,name,imgUrls,text);
                    }

                }else if($(that).attr("id")=="wood-veneer"){
                        url="/board/ajax/";
                        name=$(this).attr("name");
                        imgUrls="/image?image=/home/img/board/";
                        console.log(name,this);
                        showContentCenter(url,name,imgUrls);
                }



			});
		}else {//不含子项目
            if ($(this).closest(".has-children").length == 0) {
                $(this).bind("click", function () {
                    showPresentContent(this, ".content-detail");
                });
            }
        }
	});

    $(".content-nav li").each(function(){
		$(this).bind("click",function(){
			AddNavStyle(this);
		});
    });


	// 选中内容

	// console.log(window.location.search.substr(1).split("=")[0]);
		if(window.location.search.substr(1)){
            var IndexSignArr=window.location.search.substr(1).split("&&");
            if(IndexSignArr.length==1){
                getContentIndexSign=IndexSignArr[0].split("=")[1];
                getContentIndexSignName=IndexSignArr[0].split("=")[0];
			}else{
                getContentIndexSign=IndexSignArr[0].split("=")[1];
                getSubContentIndexSign=IndexSignArr[1].split("=")[1];
                getContentIndexSignName=IndexSignArr[1].split("=")[0];
			}
		}else{
            getContentIndexSign=0;
			if($(".content-nav li:nth-child(1)").hasClass("has-children")){
                getSubContentIndexSign=0;
                getContentIndexSignName="setSubContentIndexSign"
			}

		}



	//刷新显示第一个内容模块（公司简介）
    showContent();

	TableStyle();

	//根据url参数加载
    if(window.location.hash==='#/product'){
        $(".content-nav>li").eq(1).children().children('li').eq(0).click();
    }
});


// 侧导航
// 一级导航内容显示
function showPresentContent(element){
	var contentIndex=$(element).index();
	setContentIndexSign=contentIndex;
	setSubContentIndexSign=undefined;
	// console.log(setContentIndexSign,setSubContentIndexSign);
	$(".content-detail").eq(contentIndex).addClass("show-present-content").siblings().removeClass("show-present-content");
}
// 二级导航内容显示
function showPresentChildrenContent(element){
	var contentIndex=$(element).parent().parent("li").index();
	var SubContentIndex=$(element).index();
	// console.log("子导航"+contentIndex,SubContentIndex);
	setContentIndexSign=contentIndex;
	setSubContentIndexSign=SubContentIndex;
	$(".content-detail").eq(setContentIndexSign).children().eq(setSubContentIndexSign).addClass("show-present-content").parents().addClass("show-present-content").siblings().removeClass("show-present-content").end().end().siblings().removeClass("show-present-content");
}

//产品中心
function showProductContent(url,name,imgUrls,text){
    var contents,results="",nodeDiv="",total="";


    $.get(url+name).done(function (data) {

        $.each(data,function (index, ele) {
            // nodeDiv="<div class='totalTitle'>"+text+"</div>";
            if(window.location.pathname==="/tianjin/chinese"){
                $.each(ele,function (i, con) {
                    var imgUrl=imgUrls+con.path;
                    results+= "<div class='imageFile'>"+
                        "<img class='imagePic' src="+imgUrl+">" +
                        "<p class='imageTitle'>"+con.name+"</p>"+
                        "</div>";
                })
            }else{
                $.each(ele,function (i, con) {
                    var imgUrl=imgUrls+con.path;
                    results+= "<div class='imageFile'>"+
                        "<img class='imagePic' src="+imgUrl+">" +
                        "<p class='imageTitle'>"+con.english_name+"</p>"+
                        "</div>";
                })
            }

        });

            total+=results;

        $(".content-detail").eq(setContentIndexSign).children().eq(setSubContentIndexSign).empty().append(total);

    }).fail(function () {
        console.error("服务器错误");
    })
}


//木饰面样板的二级导航
function showContentCenter(url,name,imgUrls) {
	var contents,results="",nodeDiv="",total="",code="";

    $.get(url+name).done(function (data) {

        $.each(data,function (index, ele) {
            // nodeDiv="<div class='totalTitle'>"+ele.title+"</div>";
            contents=JSON.parse(ele.content);
            if(window.location.pathname==="/tianjin/chinese"){
                $.each(contents,function (i, con) {
                    var imgUrl=imgUrls+ele.name+"/"+ele.page+"/"+con.pic_name;
                    results+= "<div class='imageFile'>"+
                        "<img class='imagePic' src="+imgUrl+">" +
                        "<p class='imageTitle'>"+con.title+"</p>"+
                        "</div>";
                })
            }else{
                $.each(contents,function (i, con) {
                    var imgUrl=imgUrls+ele.name+"/"+ele.page+"/"+con.pic_name;
                    results+= "<div class='imageFile'>"+
                        "<img class='imagePic' src="+imgUrl+">" +
                        "<p class='imageTitle'>"+con.english_title+"</p>"+
                        "</div>";
                })
            }

        });

        var QrCode="/code/"+name+".png";
        code="<div class='qrCode_content'><img class='qrCode' src="+QrCode+"><p>扫码即可观看</p></div>";
        total+=nodeDiv+results+code;

        $(".content-detail").eq(setContentIndexSign).children().eq(setSubContentIndexSign).empty().append(total);

    }).fail(function () {
		console.error("服务器错误");
    })
}




// // 初始化样式（刷新、中英切换）
function showContent(){
    var element;
    if(getContentIndexSignName=="setSubContentIndexSign"){
        element=".content-nav li:nth-child("+(Number(getContentIndexSign)+1) +") li:nth-child("+(Number(getSubContentIndexSign)+1)+")";
        // ShowPresentChildrenSty(getContentIndexSign,getSubContentIndexSign);
    }else{
        element=".content-nav li:nth-child("+(Number(getContentIndexSign)+1)+")";
        // ShowPresentSty(getContentIndexSign);
    }
    $(element).trigger("click");
}


// 表格样式
function TableStyle(){
	$(".show-info").children("tr:even").css("background","#eee");
	$(".show-info tr:odd").css("background","#fff");
	$(".show-info tr:first").css("background","#ccc");
}

// 两个小图并排显示，左边的图加margin
function TwoImgMargin(element){
	$(element+":even").addClass("detail-img-margin");
}
// 三张图 最后一张去掉margin
function ThreeImgMargin(element){
	// 因为img-three前面恰好有奇数个元素，所以三的倍数为3n
	$(element+":nth-child(3n)").css("margin-right","0px");
}


//切换中英文时调到响应字栏目
// var contentIndex=0;

//证书轮播
var swiper = new Swiper('.swiper-container', {
    slidesPerView: 4,
    spaceBetween: 30,
    slidesPerGroup: 2,
    loop: true,
    loopFillGroupWithBlank: true,
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
    autoplay: true
});

//鼠标覆盖停止自动切换

$('.wrap').mouseover(function () {
    swiper.autoplay.stop();
}).end().mouseout(function () {
    swiper.autoplay.start();
});

$('.swiper-button-next').mouseover(function () {
    swiper.autoplay.stop();
}).end().mouseout(function () {
    swiper.autoplay.start();
});

$('.swiper-button-prev').mouseover(function () {
    swiper.autoplay.stop();
}).end().mouseout(function () {
    swiper.autoplay.start();
});

