﻿if (Drupal.jsEnabled) {
    $(function() {
        //$(document).ready(function() {
        var clips = $('#videobar ul li');
        $.ajax({
            type: "POST",
            url: "../../WebServerControl/Eart.UIL.AJAX.ProductVideo_Index.asp",
            data: "DataResultCount=6&RequestPageCount=1",
            success: function(msg) {
                //alert(msg.length);
                msg = msg.split("$%_%$");
                //alert(msg[1]);
                //if (msg.length > 0) {
                if (msg[1] > 0) {

                    //return;
                    clips.parent().html(msg[0]);
                    //alert(msg[2])
                    pagerCount(1);
                    resultCount(msg[1]);
                } else {
                    alert('没有视频被发现。');
                }
            }
        });

        /***** make a category selectable ********/
        $('.videobar-category a').click(function(event) {
            event.preventDefault(); //prevent default action
            $(this).blur(); //remove focus
            $('.videobar-category').removeClass('active');
            $(this).parent().addClass('active');
            var categoryHolder = $('#videobar-category');
            var category = $(this).parent().attr('id').replace('videobar-category-', '');
            categoryHolder.html(category);
            var clips = $('#videobar ul li');
            pagerCount(0); //reset counter
            requestVideos(clips, 0); //send request for initial videos
        });

        $('#videobar-next').click(function() {
            if (resultCount() == 6) {
                //hide the existing feeds
                var clips = $('#videobar ul li');
                clips.fadeOut();
                clips.html('');
                clips.addClass('gui-loading');
                clips.fadeIn();
                var count = pagerCount();
                count++;
                //alert(count);
                requestVideos(clips, count);
            } else {
                alert('没有发现更多视频。');
            }
        });
        $('#videobar-previous').click(function() {
            var count = pagerCount();
            if (count > 1) {
                //hide the existing feeds
                var clips = $('#videobar ul li');
                clips.fadeOut();
                clips.html('');
                clips.addClass('gui-loading');
                clips.fadeIn();
                count--;
                //alert(count);
                requestVideos(clips, count);
            } else {
                alert("您已经到达了列表的首页，请点击向后按钮查看更多视频。");
            }
        });
        function requestVideos(clips, count) {
            var category = $('#videobar-category').html();
            //url = '/g/gblocks/videobar/' + category + '/' + count.toString();
            //url = "WebServerControl/Eart.UIL.AJAX.ProductVideo_Index.asp"
            //alert(count);
            /*
            $.getJSON(url, function(data) {
            alert(data)
            if (data.html.length > 0) {
            clips.parent().html(data.html.toString());
            pagerCount(count);
            resultCount(data.resultcount.toString());
            } else {
            alert('没有视频被发现。');
            }
            });
            */
            $.ajax({
                type: "POST",
                url: "../../WebServerControl/Eart.UIL.AJAX.ProductVideo_Index.asp",
                data: "DataResultCount=6&RequestPageCount=" + count.toString(),
                success: function(msg) {
                    //alert(msg.length);
                    msg = msg.split("$%_%$");
                    //alert(msg[1]);
                    //if (msg.length > 0) {
                    if (msg[1] > 0) {

                        //return;
                        clips.parent().html(msg[0]);
                        //alert(msg[2])
                        pagerCount(count);
                        resultCount(msg[1]);
                    } else {
                        alert('没有视频被发现。');
                    }
                }
            });
        }

        function pagerCount(count) {
            if (count != null) {
                $('#videobar-count').html(count.toString());
            } else {
                var count = Number($('#videobar-count').html());
                return count;
            }
        }
        function resultCount(count) {
            if (count != null) {
                $('#videobar-result-count').html(count.toString());
            } else {
                var count = Number($('#videobar-result-count').html());
                return count;
            }
        }

    });

}
