/**
 * 
 * @authors Your Name (you@example.org)
 * @date    2017-08-02 17:23:52
 * @version $Id$
 */

var langInit = function () {
	var langObj = {
		cn: '简体',
		gb: '繁体',
		en: 'EN'
	};
	var currentLang = TD.util.getQuery('lang');

	// 根据当前语言设置导航栏的语言显示
	if (currentLang !== '') {
		$('.nav-lang-current').html('<div class="nav-lang-item">' + langObj[currentLang] + '</div>')
		$('.lang-' + currentLang).hide();
	} else {
		$('.lang-cn').hide();
	};

	// 事件委托
	$('.nav-lang-hidden').on('click', function () {
		var target = window.event.target || window.event.srcElement;

		if (target.nodeName.toLowerCase() === 'li') {
			// 识别选取的语言并转换成xx格式
			var nextLang = $(target).attr('class').match(/lang\-(cn|gb|en)/g).toString().replace('lang-', '');

			TD.util.setCookie('lang',nextLang,7);

			if (currentLang !== '') {
				location.href = location.href.replace('lang=' + currentLang, 'lang=' + nextLang);
			} else if (TD.util.getQuery('=')) {
				location.href += '&lang=' + nextLang;
			} else {
				location.href += '?lang=' + nextLang;
			};
		};
	});
};

var pageInit = function () {
	langInit();

	$('.m-maofm').fullpage({
		easingcss3: 'cubic-bezier(.62,.01,0,1)',
		onLeave: function (index, nextIndex, direction) {
            console.log(index, nextIndex, direction);
            var sectionNow = $('.m-maofm' + '>:nth-child(' + index + ')').find('.video');
            var sectionNext = $('.m-maofm' + '>:nth-child(' + nextIndex + ')').find('.video');

            if (nextIndex === 2 || nextIndex === 10) {
            	sectionNext.get(0).currentTime = 0;
            	sectionNext.get(0).play();
            } else if (index === 2 || index === 10) {
            	sectionNow.get(0).pause();
            }
        }
		// scrollOverflow: true
	});

	TD.imgPreload(function () {
		$('.m-loading').fadeOut(300);
	});
	// $('.m-loading').fadeOut(300);

	$('.maofm-2 .btn-videoplay').on('click', function () {
		$('.maofm-2 .video-wrap-2').show();
		$('.maofm-2 .video').get(0).pause();
		$('.maofm-2 .video-2').get(0).play();
	});

	$('.maofm-2 .video-wrap-2').on('click', function (e) {
		console.log(e.target.nodeName);
		if (e.target.nodeName === 'VIDEO') {
			return;
		};
		$('.maofm-2 .video-wrap-2').hide();
		$('.maofm-2 .video-2').get(0).pause();
	});

	$('.maofm-10 .btn-videoplay').on('click', function () {
		$('.maofm-10 .video-wrap-2').show();
		$('.maofm-10 .video').get(0).pause();
		$('.maofm-10 .video-2').get(0).play();
	});

	$('.maofm-10 .video-wrap-2').on('click', function (e) {
		if (e.target.nodeName === 'VIDEO') {
			return;
		};
		$('.maofm-10 .video-wrap-2').hide();
		$('.maofm-10 .video-2').get(0).pause();
	});

	var ratio = $('.video-wrap .video').width() / $('.video-wrap .video').height();
	console.log(ratio);
	if (window.innerWidth / window.innerHeight < ratio) {
		$('.video-wrap .video').css('width', 'auto');
		$('.video-wrap .video').css('height', '100%');
	}

	var rszEvt = 'orientationchange' in window ? 'orientationchange' : 'resize';
	window.addEventListener(rszEvt, function () {
		console.log('resize');
		if (window.innerWidth / window.innerHeight < ratio) {
			$('.video-wrap .video').css('width', 'auto');
			$('.video-wrap .video').css('height', '100%');
		} else {
			$('.video-wrap .video').css('width', '100%');
			$('.video-wrap .video').css('height', 'auto');
		}
	});
}

$(document).ready(function () {
	pageInit();
})
