$(document).ready(function() {

    // 互斥变量，停止自动播放
    var auto_play = true;
    // 点击后永不播放
    var clicked = false;
    var auto_play_timeout;
    
	$('.nav li').hover(function (){
		$(this).children('em').children('a').addClass('hover');
		$(this).children('ul').fadeIn('normal');
	},function (){
		$(this).children('em').children('a').removeClass('hover');
		$(this).children('ul').fadeOut('normal');
	});

	$('.thumbnail li').hover(function (){
		$(this).addClass('hover');
	},function (){
		$(this).removeClass('hover');
	});

	$('.thumbnail li').each(function(index) {
		$(this).bind("mouseover.thumb", function() {
            if (clicked == false) {
                var li = $(this);
        //		$('.desc_overlay').hide();
        //		$('.desc').hide();
				$('.cover').show();
				$('.cover li').eq(index).show().siblings().hide();
				$('.cover li img').eq(index).fadeIn('show').siblings().fadeOut('slow');
            }
		});

		$(this).bind("mouseout.thumb", function() {
            if (clicked == false) {
				$('.cover li img').hide();
            }
		});

        $(this).mouseover(function() {
            if (clicked == false) {
                auto_play = false;
                $(this).siblings().removeClass('actived');
                clearTimeout(auto_play_timeout);
            }
        });
        $(this).mouseout(function() {
            if (clicked == false) {
                auto_play = true;
                thumb_index--;
                change_thumb();
            }
        });
	});

	$('.thumbnail li').each(function(index) {
		$(this).click(function() {
			var li = $(this);
			li.addClass('actived current').siblings().removeClass('actived current');
			$(this).parents('.thumbnail').addClass('translucent');
			$('.cover').show();
			$('.cover li').eq(index).show().siblings().hide();
			$('.cover li img').eq(index).fadeIn('show').siblings().fadeOut('slow');
			$('.cover li h3').hide();
			$('.desc_overlay').show();
			$('.desc').show();
			$('.desc li').eq(index).show().siblings().hide();
			init_scrollbar($('.desc li').eq(index).find('.entry'));
			clicked = true;
		});
	});

	$('.thumbnail li').each(function(index) {
		$(this).click(function() {
			var li = $(this);
			li.addClass('actived current').siblings().removeClass('actived current');
			$(this).parents('.thumbnail').addClass('translucent');
			$('.preview').show();
			$('.preview li').eq(index).show().siblings().hide();
		});
	});
	
	$('.preview .close').click(function (){
		$('.overlay, .preview').hide();
	//	$('.thumbnail').removeClass('translucent');
		$('.thumbnail li').removeClass('actived current');
	});

    // scrollbar

    function init_scrollbar(elm) {
        var $entry_slide = elm.find('.entry-slide-content');
        var $entry_scrollbar = elm.find('.scrollbar');
        var $entry_scrollbar_face = elm.find('.scrollbar-face');
        var entry_height = elm.height();
        var entry_slide_height = $entry_slide.height();

        if(entry_slide_height/entry_height > 1){
            $entry_scrollbar_face.height(entry_height/entry_slide_height * 100 + '%');

            var heightPs = entry_slide_height/entry_height;
            entry_scrollbar_face_height = $entry_scrollbar_face.height();
            $entry_scrollbar_face.mousedown(function(evt){
                    $(document.body).css('-moz-user-select', 'none');
                    $(document.body).css('-webkit-user-select', 'none');
                    $(document.body).attr('unselectable', 'on');
                    document.body.onselectstart = function(){return false;};
                    $(this).data('moused', true);
                    var marginTop = parseInt($(this).css('marginTop'))
                    var offset = $(this).offset();
                    $(this).data('xy', [evt.pageX - offset.left, evt.pageY - marginTop]);
                    $(this).data('_face_height', $(this).height());
            });

            $(document).bind('mousemove.gdg', function(evt){
            //$entry_scrollbar_face.mousemove(function(evt) {
                var moused = $entry_scrollbar_face.data('moused');
                var xy = $entry_scrollbar_face.data('xy');
                if(moused && xy){
                    // 多滚动条同时出现时，变量会出现错误，在此再次进行计算
                    entry_scrollbar_face_height = $entry_scrollbar_face.data('_face_height');
                    var t = evt.pageY - xy[1];
                    if (t <= -1) t = 0;
                    if (t >= entry_height - entry_scrollbar_face_height) t = entry_height - entry_scrollbar_face_height;
                    $entry_scrollbar_face.css('marginTop', t + 'px');
                    $entry_slide.css('top', -t*heightPs + 'px');
                }
            });

            $(document).bind('mouseup.gdg', function(evt){
            //$entry_scrollbar_face.mouseup(function(evt) {
                var moused = $entry_scrollbar_face.data('moused');
                if(moused){
                    $(document.body).css('-moz-user-select', 'all');
                    $(document.body).css('-webkit-user-select', 'auto');
                    $(document.body).attr('unselectable', 'yes');
                    document.body.onselectstart = '';
                    $entry_scrollbar_face.data('moused', false);
                }
            });
        }else{
            $entry_scrollbar_face.parent().hide();
            $entry_scrollbar_face.height('0%');
        }
    }
    //var $entry = $('#entry');
    var $scrollbar_panel = $(".scrollbar-panel");
    $scrollbar_panel.each(function(i) {
        init_scrollbar($(this));
    });

    // 缩略图轮播索引
    var thumb_index = 0;
    var max_thumb_index = 0;
    // 切换缩略图显示
    function change_thumb() {
        if (auto_play) {
            var thumb_div = $(".thumbnail-autoplay");
            var thumb_li = thumb_div.find("li");
            max_thumb_index = thumb_li.length;
            if (thumb_index >= max_thumb_index || thumb_index < 0) {
                thumb_index = 0;
            }
            
            var actived_thumb = thumb_div.find('.actived');
            if (actived_thumb.length > 0) {
                actived_thumb.removeClass('actived');
                actived_thumb.trigger('mouseout.thumb');
            }
            var thumb = thumb_li.eq(thumb_index);
            thumb.addClass('actived');
            thumb.trigger('mouseover.thumb');
            thumb_index++;

            auto_play_timeout = setTimeout(change_thumb, 5000);
        }
    }
    change_thumb();
    //setInterval(change_thumb, 5000);
});
