﻿/**
 * Cookie
 */

//var urlPathContent = "//news.dahebao.cn/dahe/appweb";//  /pc
//var urlPathContent = "//www.dahebao.cn/dahe/appweb";//  /pc
var urlPathContent = "//www.dahebao.cn/news";//  /pc
var Cookie = {
    set: function (name, value) {
        var expires = new Date(new Date().getTime() + (3600 * 24));
        document.cookie = name + "1=" + value + "; expires=" + expires;
    },
    get: function (name) {
        var arr = document.cookie.match(new RegExp("(^| )" + name + "1=([^;]*)(;|$)"));
        if (arr != null) { return unescape(arr[2]); }
        return null;
    }
};

/**
 * 转换时间
 */
function calcTime(timestamp){
    if(!timestamp){return}
    var day = new Date(timestamp);
    var now = new Date();
    var result = now.getTime() - day.getTime();
    if(result < 60000){
        return '刚刚';
    }else if(60000 <= result && result < 3600000){
        return Math.floor(result / 60000) + '分钟前';
    }else if(3600000 <= result && result < 86400000){
        return Math.floor(result / 3600000) + '小时前';
    }else{
        return moment(timestamp).format('MM月DD日  hh:mm');
    }
}

/**
 * 函数节流
 */
function throttle(func, interval){
    if(!interval){
        var interval = 300;
    }
    var timeout;
    var startTime = new Date();
    return function (event) {
        clearTimeout(timeout);
        var curTime = new Date();
        var _this = this
        if(curTime - startTime <= interval) {
            //小于规定时间间隔时，用setTimeout在指定时间后再执行
            timeout = setTimeout(function() {
                func.call(_this, event);
            }, interval)
        }else{
            //重新计时并执行函数
            startTime = curTime;
            func.call(this, event);
        }
    }
}


/**
 * 获取地址栏参数
 */
function getUrlParms(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)
    return unescape(r[2]);
    return null;
}

/**
 * 发表评论随机ID
 */
function GUID() {
    this.date = new Date();   /* 判断是否初始化过，如果初始化过以下代码，则以下代码将不再执行，实际中只执行一次 */
    if (typeof this.newGUID != 'function') {   /* 生成GUID码 */
        GUID.prototype.newGUID = function () {
            this.date = new Date(); var guidStr = '';
            sexadecimalDate = this.hexadecimal(this.getGUIDDate(), 16);
            sexadecimalTime = this.hexadecimal(this.getGUIDTime(), 16);
            for (var i = 0; i < 9; i++) {
                guidStr += Math.floor(Math.random() * 16).toString(16);
            }
            guidStr += sexadecimalDate;
            guidStr += sexadecimalTime;
            while (guidStr.length < 32) {
                guidStr += Math.floor(Math.random() * 16).toString(16);
            }
            return this.formatGUID(guidStr);
        }
        /* * 功能：获取当前日期的GUID格式，即8位数的日期：19700101 * 返回值：返回GUID日期格式的字条串 */
        GUID.prototype.getGUIDDate = function () {
            return this.date.getFullYear() + this.addZero(this.date.getMonth() + 1) + this.addZero(this.date.getDay());
        }
        /* * 功能：获取当前时间的GUID格式，即8位数的时间，包括毫秒，毫秒为2位数：12300933 * 返回值：返回GUID日期格式的字条串 */
        GUID.prototype.getGUIDTime = function () {
            return this.addZero(this.date.getHours()) + this.addZero(this.date.getMinutes()) + this.addZero(this.date.getSeconds()) + this.addZero(parseInt(this.date.getMilliseconds() / 10));
        }
        /* * 功能: 为一位数的正整数前面添加0，如果是可以转成非NaN数字的字符串也可以实现 * 参数: 参数表示准备再前面添加0的数字或可以转换成数字的字符串 * 返回值: 如果符合条件，返回添加0后的字条串类型，否则返回自身的字符串 */
        GUID.prototype.addZero = function (num) {
            if (Number(num).toString() != 'NaN' && num >= 0 && num < 10) {
                return '0' + Math.floor(num);
            } else {
                return num.toString();
            }
        }
        /*  * 功能：将y进制的数值，转换为x进制的数值 * 参数：第1个参数表示欲转换的数值；第2个参数表示欲转换的进制；第3个参数可选，表示当前的进制数，如不写则为10 * 返回值：返回转换后的字符串 */GUID.prototype.hexadecimal = function (num, x, y) {
            if (y != undefined) { return parseInt(num.toString(), y).toString(x); }
            else { return parseInt(num.toString()).toString(x); }
        }
        /* * 功能：格式化32位的字符串为GUID模式的字符串 * 参数：第1个参数表示32位的字符串 * 返回值：标准GUID格式的字符串 */
        GUID.prototype.formatGUID = function (guidStr) {
            var str1 = guidStr.slice(0, 8) + '-', str2 = guidStr.slice(8, 12) + '-', str3 = guidStr.slice(12, 16) + '-', str4 = guidStr.slice(16, 20) + '-', str5 = guidStr.slice(20);
            return str1 + str2 + str3 + str4 + str5;
        }
    }
}

// 请求数据
function request(url, parm, callback){
    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: parm,
        crossDomain: true == !(document.all),
        timeout: 6000,
        success: function(data){
            if(data.state == 1){
                callback(data.data);
            }else{
                callback(data)
            }
        },
        error: function(err){
            console.log(err);
        }
    });
}

// 显示更多nav
$('.header_nav').on('click', function(e){
    if(e.target.className.indexOf('more') !== -1 ){
        var isFold = $(e.target).hasClass('hover');
        var headerMore = $('.header_more');
        if(!isFold){
            var headerMoreW = headerMore.width();
            var headerMoreH = headerMore.height();
            headerMore.css({'marginLeft': -headerMoreW/2-1})
            headerMore.animate({'bottom': -headerMoreH, 'opacity': 1});
            $(e.target).addClass('hover');
        }else{
            $(e.target).removeClass('hover');
            headerMore.animate({'bottom': 0, 'opacity': 0});
            headerMore.css({'marginLeft': -headerMoreW})
        }
    }
});

// 鼠标悬浮展示客户端
$('.header_btn_client').on('mouseenter', function(){
    $('.header_appQrcode').css({'opacity': 1}).animate({'top':'125px','z-index': 1001 });
});
$('.header_appQrcode').on('mouseleave', function(){
    $(this).css({'opacity': 0}).animate({'top': 0,'z-index': -1});
})

// 移动端展开搜索栏
$('.header_btn_search .icon-search').on('click', function(){
    $('.header_search').animate({'top': '70px', 'opacity': 1});
    $(this).hide();
    $('.header_btn_search .icon-guanbi').show();
});

// 移动端关闭搜索栏
$('.header_btn_search .icon-guanbi').on('click', function(){
    $('.header_search').animate({'top': 0, 'opacity': 0});
    $(this).hide();
    $('.header_btn_search .icon-search').show();
});

// 切换频道
$('.header').on('click', function(e){
    var isToggleChannel = e.target.className.indexOf('nav_item') !== -1;
    if($('.list').length &&　isToggleChannel){
        var channelid = $(e.target).attr('data-channelid');
        $('.list_index').attr('data-channelid', channelid);
    }
});

// 切换频道选中样式
$('.header_nav').on('click', function(e){
    var isToggleChannel = e.target.className.indexOf('nav_item') !== -1;
    if(isToggleChannel){
        $(e.target).parent().addClass('active').siblings().removeClass('active');
    }
});

// 点击返回顶部
$('#toTop').on('click', function(){
    $('body,html').animate({scrollTop:0},800);
});

// 搜索关键词
$('#searchInput').on('submit', function(){
    var keyVal = $.trim($(this).find('input').val());
    if(keyVal){
        var url = encodeURI(encodeURI('classify.html?keywords=' + keyVal))
        location.href = url;
    }
    return false;
});

$('.header_top .icon-search').on('click', function(){
    var keyVal = $.trim($('#searchInput').find('input').val());
    if(keyVal){
        var url = encodeURI(encodeURI('classify.html?keywords=' + keyVal))
        location.href = url;
    }
    return false;
})


// 页面滚动
$(window).on('scroll', function(){
    var scrollTop = $(this).scrollTop();
    return scrollTop >= 700 ? $('#toTop').show().animate({'opacity': 1}) : $('#toTop').hide();
});

function contentClick(id) {
    var urlnew =  urlPathContent + '/' +id + '?cid=' + id;
    
        window.open(urlnew);
    // changeURL('id=' + id);
    // $('.mainlib').load('content.html?id=' + id);
    // $("#mainFrame").attr("src", 'content.html?id=' + id);
    }
