﻿$(document).ready(function() {
    if (userID > 0) {
        LoadToolbar(); //载入底部工具栏
    }

    //LoadCheckBrowser(); //载入底部判断浏览器版本

    //载入头部背景图片
    if ($('.head-background').length > 0) {
        var headBGIndex = GetCookie('HeadBGIndex');
        if (headBGIndex != undefined && headBGIndex != null && headBGIndex != "" && headBGIndex != "null") {
            $('#btnSaveHeadBG').addClass('btnRandHeadBG').attr('title', '随机显示当前背景');
            $('#btnDeleteHeadBG,#btnPrevHeadBG,#btnNextHeadBG,#btnSaveHeadBG').attr('bgindex', headBGIndex);
            $('.head-background').css('background-image', 'url(/Files/Themes/' + headBGIndex + '/' + screen.width + '.jpg)');
        } else {
            SetHeadBackground();
        }

        //删除背景
        $('#btnDeleteHeadBG').unbind().click(function() {
            var bgIndex = parseInt($(this).attr('bgindex'));
            if (bgIndex > 1) {
                $.ajax({
                    type: "POST",
                    url: '/Home/DeleteHeadBGAjax/',
                    data: { 'bgIndex': bgIndex },
                    success: function(data) {
                        if (data == "success")
                            SetHeadBackground();
                        else
                            ShowTips(data);
                    },
                    error: function(xmlHttpRequest, error) {
                        AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
                    }
                });
            }
        });

        //上一张背景
        $('#btnPrevHeadBG').unbind().click(function() {
            var bgIndex = parseInt($(this).attr('bgindex'));
            var headBGIndex = GetCookie('HeadBGIndex');
            if (bgIndex > 1) {
                bgIndex--;
                $('#btnDeleteHeadBG,#btnPrevHeadBG,#btnNextHeadBG,#btnSaveHeadBG').attr('bgindex', bgIndex);
                $('.head-background').css('background-image', 'url(/Files/Themes/' + bgIndex + '/' + screen.width + '.jpg)');
                if (headBGIndex == bgIndex) {
                    $('#btnSaveHeadBG').addClass('btnRandHeadBG').attr('title', '随机显示当前背景');
                } else {
                    $('#btnSaveHeadBG').removeClass('btnRandHeadBG').attr('title', '固定显示当前背景');
                }
            }
        });

        //下一张背景
        $('#btnNextHeadBG').unbind().click(function() {
            var bgIndex = parseInt($(this).attr('bgindex'));
            var headBGIndex = GetCookie('HeadBGIndex');
            if (bgIndex > 0 && bgIndex < 1000) {
                bgIndex++;
                $('#btnDeleteHeadBG,#btnPrevHeadBG,#btnNextHeadBG,#btnSaveHeadBG').attr('bgindex', bgIndex);
                $('.head-background').css('background-image', 'url(/Files/Themes/' + bgIndex + '/' + screen.width + '.jpg)');
                if (headBGIndex == bgIndex) {
                    $('#btnSaveHeadBG').addClass('btnRandHeadBG').attr('title', '随机显示当前背景');
                } else {
                    $('#btnSaveHeadBG').removeClass('btnRandHeadBG').attr('title', '固定显示当前背景');
                }
            }
        });

        //固定显示当前背景
        $('#btnSaveHeadBG').unbind().click(function() {
            var bgIndex = parseInt($(this).attr('bgindex'));

            if ($(this).attr('class').indexOf('btnRandHeadBG') != -1) {
                SetCookie('HeadBGIndex', null);
                $(this).removeClass('btnRandHeadBG').attr('title', '固定显示当前背景');
            }
            else {
                SetCookie('HeadBGIndex', bgIndex);
                $(this).addClass('btnRandHeadBG').attr('title', '随机显示当前背景');
            }
        });
    }
});

//载入底部工具栏
function LoadToolbar() {
    $.ajax({
        type: "POST",
        url: commonControllerUrl + '/GetControlHtmlAjax/',
        data: { 'viewName': 'Toolbar' },
        success: function(data) {
            $('body').append(data);
        },
        error: function(xmlHttpRequest, error) {
            AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
        }
    });
}
function LoadCheckBrowser() {
    if (GetCookie("wohuiaBorwserVersion") == null) {
        //判断浏览器
        if ($.browser.msie) {
            if (isIE6) {
                $.ajax({
                    type: "POST",
                    url: commonControllerUrl + '/GetControlHtmlAjax/',
                    data: { 'viewName': 'BrowserTips' },
                    success: function(data) {
                        HideLoading();
                        $('body').prepend(data);
                    },
                    error: function(xmlHttpRequest, error) {
                        HideLoading();
                        AlertTips("系统提示：", "对不起，系统故障，我们将尽快处理！", 2);
                    }
                });
            }
        }
    }
}

//设置头部背景
function SetHeadBackground() {
    var screenWidth = screen.width;
    switch (screenWidth) {
        case 1024:
        case 1152:
        case 1280:
        case 1366:
        case 1440:
        case 1600:
        case 1680:
        case 1920:
            var bgIndex = GetRandomNum(1, 1000);
            $('#btnDeleteHeadBG,#btnPrevHeadBG,#btnNextHeadBG,#btnSaveHeadBG').attr('bgindex', bgIndex);
            $('.head-background').css('background-image', 'url(/Files/Themes/' + bgIndex + '/' + screenWidth + '.jpg)');
            break;
        default:
            $('#btnDeleteHeadBG,#btnPrevHeadBG,#btnNextHeadBG,#btnSaveHeadBG').attr('bgindex', 0);
            $('.head-background').css('background-image', 'url(/Images/Skins/head/defaultbg.png)');
    }
}

//取指定范围内的随机数
function GetRandomNum(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    return (Min + Math.round(Rand * Range));
}
