var src='media/video.mp4';
var videoPlayer=new MMD.VideoPlayer(
    {
        videoElement:document.getElementById('video'),//[必填],video元素;
        src:src,//[必填],video src;
        loop:false,//[可选],是否循环,默认false,true为循环;
        muted:false,//[可选],是否静音,默认false;
        poster:'',//[可选],video默认图片;
        timesParam:[ 
        	{name:'skip',time: 1},
        	{name:'end',time: 12}
            ],//[可选],video currenttime时间点;
        onTimes:function(name){
        	switch(name){
        		case 'skip':
        			$('.loading').addClass('fadeDown');
        			setTimeout(function(){
        				$('.loading').hide();
        			},3000)
        			break;
				case 'end':
					document.getElementById('ending').style.display='block';
					break;
        	}
        },//[可选],video currenttime回调;
        onStart:function(){  	
        	$('.loading').addClass('ani')
        },//[可选],video第一个画面出现回调;
        onEnd:function(){
            $('.btn-skip').hide();
            document.getElementById('ending').style.display='block';
            $('.bg-end').css('display','block')
            console.log('video end');
        }//[可选],video播放完成回调;
    }
);

if(videoPlayer.isVideoCanAutoPlay)
{
    videoPlayer.pause();
}

// loading
var imgPath = 'image/';
var arrImgOne = ['btn.png','loading.jpg','end.jpg','flash1.png','flash2.png','piao1.png','piao2.png','piao3.png','tab1.png','tab2.png','title.png','dia-img.jpg','dia-txt.png','flicker.png','dia-bg.png','dia-close.png','share-tips1.png','share-tips2.png'];
var percent = 0;

function loadFn(arrimg) {
    for (var i = 0; i < arrimg.length; i++) {
        arrimg[i] = (imgPath + arrimg[i]);
    };
    var loadImage = function (path, callback) {
        var img = new Image();
        img.onload = function () {
            img.onload = null;
            callback(path);
        }
        img.src = path;
    }

    var imgLoader = function (imgs, callback) {
        var len = imgs.length, i = 0;
        while (imgs.length) {
            loadImage(imgs.shift(), function (path) {
                callback(path, ++i, len);
            })
        }
    }

    imgLoader(arrimg, function (path, curNum, total) {
        percent = curNum / total;
        // console.log(percent)
        if (percent == 1) {	
   			$('#btn_start').css('display','block');
   			$('#btn_start').on('click',function(){
                $('#btn_start').off("click");
        		$(this).addClass('fadeDown')
        		bgm.play();
                videoPlayer.play();
                tcssF(".btn.page0");
                PTTSendClick("btn", "page0", "点击播放视频动画");
        	})
        }
    })
};
loadFn(arrImgOne);

// bgm
function music(mic,lop){
    var url =  mic;
    var audio = document.createElement("audio");
    audio.preload="auto";
    if(lop) {
    	audio.loop = lop;
    }
    var source = document.createElement("source");
    audio.id = "audio";
    source.type = "audio/mpeg";
    source.src = url;
    audio.appendChild(source);
    return audio;
}

var bgm= music('media/bgm.mp3','loop');


// ending
$('.ending .btn-box a').on('click',function(){
	var idx = $('.ending .btn-box a').index($(this));
	console.log(idx)
	$('.ending .btn-box a').removeClass('on').eq(idx).addClass('on');
})

// 弹窗
function showDia(id){
	$(id).css('display','block');
}
function hideDia(id){
	$(id).css('display','none');
}

//分享提示层
$(".ending .btn1").on('click', function () {
    if(navigator.userAgent.indexOf("TIEM Ingame Browser/") != -1&&typeof(ingameSdkShare) == "object"){
        ingameSdkShare.showUI();
    }else {
        if (tcssObj.browser == "msdk") { // 判断游戏环境所需 js
            $(".share-yx").css("display", "block");
        } else {
            $(".share-wx").css("display", "block");
        }
    }
    PTTSendClick("btn", "share", "分享");
});
$(".share-yx,.share-wx").on('click', function() {
    $(this).css('display','none');
});


// 查看视频
function vidFn(vid) {
	var video = new tvp.VideoInfo();
        video.setVid(vid);
        player = new tvp.Player();
        player.create({
            width: "100%",
            height: "100%",
            video: video,
            modId: "_video",
            isHtml5UseAirPlay: true,
            isHtml5UseFakeFullScreen: true,
            vodFlashSkin: "https://imgcache.qq.com/minivideo_v1/vd/res/skins/TencentPlayerMiniSkin.swf",
            autoplay: true//true为自动播放、false为不自动播放
        });

}
function popVid(vid){
	$('#_video').html('');
	vidFn(vid);
	$("#wrap").css('display','block')
	showDia('#dia4')
    bgm.pause();
    tcssF(".btn.videoon");
                    PTTSendClick("btn", "videoon", "播放皮肤视频");
}

function hideVid(){
	$("#wrap").css('display','none')
	$('#dia4').css('display','none');
	$('#_video').html('');
	bgm.play();
}

// 页面禁止下拉
$(window).on('scroll.elasticity',function(e) {
    e.preventDefault();
}).on('touchmove.elasticity',function(e) {
    e.preventDefault();
});

$(window).on('load',function(){
	$('.loading').show();
	bgm.play();
	document.addEventListener("WeixinJSBridgeReady", function () {
        bgm.play();
    }, false);
})

// 强制横竖屏
var set = null;hs = false;
$wrap = $("#wrap");
function orientationF() {
    var html = document.documentElement;
    var w = html.clientWidth, h = html.clientHeight;
    if (h < w) {//横屏
        html.style.fontSize = w / 1334 * 100 + "px";
        $wrap.css({ "width": "auto","height":"100%", "webkitTransform": "none", "transform": "none" });
        hs = false;
        $(".popbox").css({ "height": "100%", "top": 0 });
    }
    else {//竖屏
        html.style.fontSize = h / 1334 * 100 + "px";
        $wrap.css({ "width": h,"height":w, "webkitTransform": "rotate(90deg) translate(0,-" + w + "px)", "transform": "rotate(90deg) translate(0,-" + w + "px)" });
        hs = true;
    }
}

function orientationfn() {
    clearTimeout(set);
    orientationF();
    set = setTimeout(function () {
        orientationF()
    }, 300);
}
window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", orientationfn, false);
orientationfn();





//分享
//依赖tcsspvp.js
shareF(
"【王者荣耀】桃花源记：走进武陵仙君的尘封回忆", //标题
"桃李春风，不如一眼回眸", //描述，可以为空
"image/share.jpg"//图标，必须用https
);