﻿var _incool = {
};
_incool.i = {
};
var Incool = {
    MyID: incoolMyID,
    ROOT: incoolROOT,
    RESROOT: 'http://res.incool.com/',
    IMG: incoolIMG,
    UploadService: incoolUploadService,
    UpSongFileUrl: incoolUpSongFileUrl,
    UpCopyrightPicUrl: incoolUpCopyrightPicUrl,
    UpIcoolPicUrl: 'http://res.incool.com/icoolpic/',
    ProxyUrl: '/proxy/',
    PLAYERURL: 'http://www.incool.com/inzone/song.asp?id=',
    PLAYERALBUMURL: 'http://www.incool.com/inzone/album_detail.asp?id=',
    AlertHtml: '<div style="padding-top:100px; text-align:center; display:none" id="dvIncoolAlert">' +
	            '<div class="alert_shadow">' +
                    '<div class="alert_content">' +
                        '<div class="alert_title"><span class="alert_tishi pink12_b">提示</span><span class="alert_close"><a href="javascript:void(0)" class="pink">[关闭]</a></span></div>' +
                        '<div class="alert_message"></div>' +
                        '<div class="alert_queding"><input type="button" class="button4 gray12_66_b" value="确 定" /></div>' +
                    '</div>' +
	            '</div>' +
             '</div>',
    AlertInfoHtml: '<div class="open_window_in">' +
                      '<div class="window_title">' +
                            '<div ><span class="biaoti_l pink12_b"></span><span class="biaoti_r"><a href="javascript:void(0)" class="pink">[关闭]</a></span></div>' +
                      '</div>' +
                      '<div style=" width:450px; text-align:center; margin:40px auto" class="pink14_b"><img src="' + incoolIMG + 'i_ok_i_red.gif" width="18" height="32" style="vertical-align:bottom" />&nbsp;&nbsp;<span class="_message"></span></div>' +
    		            '<div style="width:180px ; margin:0px auto; padding-bottom:30px; text-align:center"><input type="button" class="button8" value="关闭" /></div>' +
                        '<br style=" clear:both" />' +
                    '</div>',
    ConfirmHtml: '<div class="open_window_in">' +
                      '<div class="window_title">' +
                            '<div ><span class="biaoti_l pink12_b"></span><span class="biaoti_r"><a href="javascript:void(0)" class="pink">[关闭]</a></span></div>' +
                      '</div>' +
                      '<div style=" width:450px; text-align:center; margin:40px auto" class=" pink14_b"><img src="' + incoolIMG + 'i_ok_i_red.gif" width="18" height="32" style="vertical-align:bottom" />&nbsp;&nbsp;<span class="_message"></span></div>' +
    		            '<div style="width:180px ; margin:0px auto; padding-bottom:30px; text-align:center"><input type="button" class="buttonok" value="确定" /> <input type="button" class="buttoncancel" value="取消" /></div>' +
                        '<br style=" clear:both" />' +
                    '</div>',
    SuccessHtml: '<div class="open_window_in">' +
                      '<div class="window_title">' +
                            '<div ><span class="biaoti_l pink12_b"></span><span class="biaoti_r"><a href="javascript:void(0)" class="pink">[关闭]</a></span></div>' +
                      '</div>' +
                      '<div style=" width:450px; text-align:center; margin:40px auto" class="green_big_14"><img src="' + incoolIMG + 'i_ok_ok.gif" width="32" height="32" style="vertical-align:bottom" />&nbsp;&nbsp;<span class="_message"></span></div>' +
    		            '<div style="width:180px ; margin:0px auto; padding-bottom:30px; text-align:center"><input type="button" class="button8" value="确定" /></div>' +
                        '<br style=" clear:both" />' +
                    '</div>',
    LoadingHtml: '<div class="open_window_in">' +
                      '<div class="window_title">' +
                            '<div ><span class="biaoti_l pink12_b"></span><span class="biaoti_r"></span></div>' +
                      '</div>' +
                      '<div style=" width:450px; text-align:center; margin:40px auto" class="green_12"><img src="' + incoolIMG + 'loding.gif" width="14" height="14" style="vertical-align:bottom" />&nbsp;&nbsp;<span class="_message"></span></div>' +
    		            '<div style="width:180px ; margin:0px auto; padding-bottom:30px; text-align:center"></div>' +
                        '<br style=" clear:both" />' +
                    '</div>',
    //普通提示
    Alert1: function(message, title) {
        //alert(message);
        if (!$('#dvIncoolAlert').length) {
            $('body').append(Incool.AlertHtml);
        }

        var dvalert = $('#dvIncoolAlert');

        $('.alert_message', dvalert).html(message.replace(new RegExp('\\n', 'g'), '</br>').replace(new RegExp(' ', 'g'), '&nbsp;'));

        Incool.ShowBlockUI(dvalert);

        dvalert.find('a.pink').unbind('click').click(function() {
            dvalert.hide();
            $.unblockUI();
        });
        dvalert.find('input').unbind('click').click(function() {
            dvalert.hide();
            $.unblockUI();
        });
        //        $('#incoolalert').text(message);
        //        dvalert.attr('style', 'position:absolute;top: 100px;left:' + ($(window).width() - dvalert.width()) / 2 + 'px');
        //        dvalert.show();
    },
    Alert2: function(message, title) {
        alert(message);
    },
    Alert: function(message, title, type, callback) {
        if (!title) {
            title = '提示';
        }

        var _dvAlertName = 'dvIncoolAlert';
        if (type == 'success') {
            _dvAlertName = 'dvIncoolSuccess';
        }
        else if (type == 'loading') {
            _dvAlertName = 'dvIncoolLoading';
        }

        var dvalert = $('#' + _dvAlertName);
        if (!dvalert.length) {
            dvalert = $('<div>').attr('id', _dvAlertName).addClass('open_window').css({ display: 'none' });
            $('body').append(dvalert);
            dvalert.html(type == 'success' ? Incool.SuccessHtml : (type == 'loading' ? Incool.LoadingHtml : Incool.AlertInfoHtml));
        }

        $('._message', dvalert).html(message.replace(new RegExp('\\n', 'g'), '</br>').replace(new RegExp(' ', 'g'), '&nbsp;'));
        $('.biaoti_l', dvalert).text(title);

        Incool.ShowBlockUI(dvalert);

        dvalert.find('a.pink').unbind('click').click(function() {
            dvalert.hide();
            if (callback && $.isFunction(callback)) {
                $.unblockUI({ onUnblock: callback });
            }
            else $.unblockUI();
        });
        dvalert.find('input').unbind('click').click(function() {
            dvalert.hide();
            if (callback && $.isFunction(callback)) {
                $.unblockUI({ onUnblock: callback });
            }
            else $.unblockUI();
        });
    },
    //确认提示
    Confirm: function(message, title, callback) {
        if (callback) {
            var dvconfim = $('#dvIncoolConfirm');
            if (!dvconfim.length) {
                dvconfim = $('<div>').attr('id', 'dvIncoolConfirm').addClass('open_window').css({ display: 'none' });
                $('body').append(dvconfim);
                dvconfim.html(Incool.ConfirmHtml);
            }

            $('._message', dvconfim).html(message.replace(new RegExp('\\n', 'g'), '</br>').replace(new RegExp(' ', 'g'), '&nbsp;'));
            $('.biaoti_l', dvconfim).text(title);

            Incool.ShowBlockUI(dvconfim);

            dvconfim.find('a.pink', 'input').unbind('click').click(function() {
                dvconfim.hide();
                $.unblockUI();
            });
            dvconfim.find('input').unbind('click').click(function() {
                dvconfim.hide();
                if ($(this).attr('class') == 'buttonok') {
                    if ($.isFunction(callback)) {
                        $.unblockUI({ onUnblock: callback });
                    }
                    else $.unblockUI();
                }
                else $.unblockUI();
            });
        }
        else {
            return confirm(message);
        }
    },
    //错误提示
    Error: function(message, title) {
        alert(Incool.E[0]);
    },
    E: ['服务器繁忙,请重试', '请先登录', '确定要删除？', '您发布的内容需经过审核才能显示。'],
    Reg: {
        tel: /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/,
        mobile: /^((\(\d{2,3}\))|(\d{3}\-))?(13|15|18)\d{9}$/,
        qq: /^[1-9]\d{4,10}$/,
        email: /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/,
        url: /^http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/

    },
    EscapeHTML: function(str) {
        return str.replace(/&/g, '&amp;').replace(/>/g, '&gt;').replace(/</g, '&lt;').replace(/"/g, '&quot;').replace(/'/g, '&#39;');
    },
    ClearHTML: function(str) {
        a = str.indexOf("<");
        b = str.indexOf(">");
        len = str.length;
        c = str.substring(0, a);
        if (b == -1)
            b = a;
        d = str.substring((b + 1), len);
        str = c + d;
        tagCheck = str.indexOf("<");
        if (tagCheck != -1)
            str = Incool.ClearHTML(str);
        return str;
    },
    getArticleCoverUrl: function(src, size) {
        if (src == '' || src == "default.jpg")
            return Incool.RESROOT + "articlecover/" + size + "/" + "default.jpg";
        else
            return Incool.RESROOT + "articlecover/" + size + "/" + src;
    },
    getUserFaceUrl: function(src, size) {
        if (src == '' || src == "default.gif")
            return Incool.RESROOT + "face/" + size + "/" + "default.gif";
        else
            return Incool.RESROOT + "face/" + size + "/" + src;
    },
    getMainPicUrl: function(src, size, sortname) {
        if (src == '' || src == "default.jpg")
            return Incool.RESROOT + sortname + "/" + size + "/" + "default.jpg";
        else
            return Incool.RESROOT + sortname + "/" + size + "/" + src;
    },
    Play: function(key) {
        window.open(Incool.PLAYERURL + key, "SongPlay");
        //Incool._PlayTrace(key, 0);
        //self.focus();
    },
    PlayAlbum: function(key) {
        window.open(Incool.PLAYERALBUMURL + key, "SongPlay");
        //Incool._PlayTrace(key, 0);
        //self.focus();
    },
    Down: function(id) {
        var url = "" + id;

        window.open(url, 'DOWNLOAD', 'width=450,height=400,resizable=1,scrollbars=1,toolbar=0,location=0,menubar=0');
    },
    TryLogin: function() {
        if (Incool.MyID > 0)
            return true;
        else
            return Incool.Login();
    },
    Login: function(url, isNotConfirm) {
        //if (isNotConfirm == true)
            location.href = "http://c.incool.com/passport/login.aspx?return=" + (url || escape(document.URL.toString()));
        return false;
    },
    Regist:function()
    {
        window.location.href = "http://c.incool.com/passport/reg.aspx?return=" + escape(document.URL);
    },
    Logout: function() {
        window.location.href = "http://c.incool.com/passport/logout.aspx?return=" + escape(document.URL);
    },
    Close: function(objName) {
        $(objName).hide();
    },
    CloseBlockUI: function(objName, isHide) {
        if (isHide)
            $(objName).hide();
        else
            $(objName).remove();
        $.unblockUI();
    },
    ShowBlockUI: function(objName) {
        $.blockUI({
            message: objName,
            css: {
                top: '100px',
                left: ($(window).width() - objName.width()) / 2 + 'px'
            },
            overlayCSS: {
                backgroundColor: ''
            }
        });
    },
    CheckAgree: function(e, btnObj) {
        if ($(e).attr("checked") == true) {
            $(btnObj).removeAttr('disabled');
        }
        else {
            $(btnObj).attr('disabled', 'disabled');
        }
    },
    ContentTipInit: function(txt, tip, maxlength) {
        //字数限制事件
        txt.keyup(function() {
            var t = maxlength - this.value.length;
            if (t < 0) t = 0;
            tip.text(t.toString());
        });
    },
    Comment: {
        Add: function(cid, ctype, cuserid, objcontent) {
            if (Incool.TryLogin()) {
                if (objcontent.val().length > 200) {
                    Incool.Alert("输入内容超出了字数限制");
                } else if (objcontent.val() == '') {
                    Incool.Alert("请填写内容");
                }
                else {
                    $.ajax({
                        dataType: 'json',
                        type: 'post',
                        url: Incool.ROOT + "Proxy/Action.aspx",
                        data: { "act": "comment.add",
                            "cid": cid,
                            "ctype": ctype,
                            'cuserid': cuserid,
                            "content": escape(objcontent.val())
                        },
                        success: function(r) {
                            if (r.s == 0) {
                                _page(1);
                            }
                            else {
                                Incool.Alert(Incool.E[0]);
                            }
                        }
                    });
                }
            }
        },
        Del: function(cid,ctype,commentid, e) {
            if (Incool.Confirm("确定要删除该条留言吗?")) {
                $.ajax({
                    type: 'post',
                    url: Incool.ROOT + "Proxy/Action.aspx",
                    data: { "act": "comment.del",
                    "cid": cid,
                        "ctype":ctype,
                        "id": commentid
                    },
                    success: function(r) {
                        if (r.s >= 0) {
                            $(e).parents('div.gbook_list').remove();
                        }
                    }
                });
            }
        }
    },
    CommentRvt: {
        Add: function(cid,ctype, pid, cuserid, objcontent, obj1, idx) {
            if (Incool.TryLogin()) {
                if (objcontent.val().length > 200) {
                    Incool.Alert("输入内容超出了字数限制");
                } else if (objcontent.val() == '') {
                    Incool.Alert("请填写内容");
                }
                else {
                    $.ajax({
                        type: 'post',
                        url: Incool.ROOT + "Proxy/Action.aspx",
                        data: { "act": "comment.rvt.add",
                            "cid":cid,
                            "ctype":ctype,
                            "pid": pid,
                            'cuserid': cuserid,
                            "content": escape(objcontent.val())
                        },
                        success: function(r) {
                            if (r.s == 0) {
                                _page(idx);
                            }
                            else {
                                Incool.Alert(Incool.E[0]);
                            }
                        }
                    });
                }
            }
        },
        Del: function(cid,ctype,commentrvtid, e) {
            if (Incool.Confirm("确定要删除该条回复吗?")) {
                $.ajax({
                    type: 'post',
                    url: Incool.ROOT + "Proxy/Action.aspx",
                    data: { "act": "comment.rvt.del",
                    "cid": cid,
                        "ctype":ctype,
                        "id": commentrvtid
                    },
                    success: function(r) {
                        if (r.s >= 0) {
                            $(e).parents('div.gbook_reply').remove();
                        }
                    }
                });
            }
        }
    }
};

$(function() {
    $.ajaxSetup({
        async: false,
        cache: false,
        dataType: 'json',
        data: {  },
        beforeSend: function() {
//            var _top = $(document).scrollTop();
//            $("#_AJAX_HINT").css("top", _top).show();
        },
        complete: function() {
            //$("#_AJAX_HINT").hide();
        }
    });

    $.blockUI.defaults.overlayCSS.cursor = "default";
});
function showmessagewindow() {
    $('.m_window').show();
};
function closemessagewindow() {
    $('.m_window').hide();
};
function formatByte(num) {
    if (isNaN(num)) {
        return false;
    }
    num = parseInt(num);
    var unit = [" B", " KB", " MB", " GB"];
    for (var i = 0; i < unit.length;i += 1) {
        if (num < 1024) {
            num = num + "";
            if (num.indexOf(".") != -1 && num.indexOf(".") != 3) {
                num = num.substring(0, 4);
            }
            else {
                num = num.substring(0, 3);
            }
            break;
        }
        else {
            num = num / 1024;
        }
    }
    return num + unit[i];
};

jQuery.format = function jQuery_dotnet_string_format(text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; ++token) {
        //iterate through the tokens and replace their placeholders from the text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"), arguments[token + 1]);
    }
    return text;
};

String.prototype.format = function() {
    var args = arguments;
    return this.replace(/{(\d{1})}/g, function() {
        return args[arguments[1]];
    });
};
Date.prototype.format = function(format) {
    var o = {
        "M+": this.getMonth() + 1, //month 
        "d+": this.getDate(),    //day 
        "h+": this.getHours(),   //hour 
        "m+": this.getMinutes(), //minute 
        "s+": this.getSeconds(), //second 
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter 
        "S": this.getMilliseconds() //millisecond 
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
    (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
      RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
};

var SWFUpload;
