// JavaScript Document
//分离 xxPx单位
function getPx(str) {
    var tmp =str.match(/([\d\.]*)px/i);
    return tmp[1];
}
/**
 * 扩展String
 */
String.prototype.replaceAll = function(s1, s2) {   
    return this.split(s1).join(s2);
}
/**
 * 编码
 * console.log(xjencode('http://'));
 * console.log(xjdecode(xjencode('http://')));
 */
function xjencode(str) {
    var data = $.base64.encode(str);
    data = data.replaceAll('+', '-');
    data = data.replaceAll('/', '_');
    data = data.replaceAll('=', '');
    return data;
}
function xjdecode(str) {
    str = str.replaceAll('-', '+');
    str = str.replaceAll('_', '/');
    var mod4 = str.length % 4;
    if (mod4) {
        var p = '====';
        str = str + p.substr(mod4);
    }
    return $.base64.decode(str);
}

$(document).ready(function(){
    //标题logo flash插入
    $("#rightFlashLogo").flash({
        src: _siteUrl + 'images/flash/mainLogo.swf',
        width: 670,
        height: 107,
        version: '9.0'
    });

    //新窗口打开链接 //隐藏链接虚线边框
    $('a').each(function(){
        if ( $(this).parent().hasClass('snav') || $(this).hasClass('notarget') )
        {
            return;
        }

        $(this)
        .attr('target', '_blank')	//新窗口打开链接
        .attr('hideFocus', 'true')	//隐藏焦点
        .focus(function (){
            $(this).blur()
        });
    });

    //hot article
    $("div.hotarticle li[class^=t]").click(function(){
        var patten = /^t([\d]+)/i;
        var m = $(this).attr('class').match(patten);
        if (m == null)
        {
            return;
        }

        $("div.hotarticle li[class^=t]").css('background-color', '#FFFFFF');
        $(this).css('background-color', '#C9C9CA');

        $("div.hotarticle ul[class^=d]").css('display', 'none');
        $("div.hotarticle ul.d" + m[1]).css('display', 'block');
    });

    //end
    //更新时钟
    setInterval('refreshClock()',1000);
    //end
	
    //刷新replaceUrl
    replaceUrl();
    //end

    /*    $("#ajaxStatus").ajaxStart(function(){
        $(this).show();
    });

    $("#ajaxStatus").ajaxStop(function(){
        $(this).hide();
    });*/

    //
    $("div .adSrv img").click(function(){
        window.open(_siteUrl + 'adservice/');
    });
    $("div .issue img").click(function(){
        window.open(_siteUrl + 'publish/');
    });

    /**
     * 文章页 图片
     */
    $("#ContentHandle img").each(function(index){
        var othis = $(this);
        var imgSrc = othis.attr("src");
        var title = $(".title h2").html();
        
        othis.wrap("<a href=\"" + imgSrc + "\" class=\"thickbox\" title=\"" + title + " " + (index+1) + "\" rel=\"groupImage\"></a>");
    });

});//end document ready

//取得用户cookie
function ck_getUser() {
    return $.cookie('username'); //保存用户名至cookie
}

//设置用户cookie
function ck_setUser(username) {
    $.cookie('username', username, {
        expires: 30
    }); //保存用户名至cookie
}

/**
 * 替换相对url为完整url//
 */
function replaceUrl() {
    $("*").filter(function(e){
        var patrn=/^(?:images|upload).*$/i;
        if ( patrn.exec($(this).attr('src')) ){
            $(this).attr('src', _siteUrl + $(this).attr('src') );
        }
    });
}

//下拉菜单
$("#mens").ready(function (){
    var headerLength = $("#header").width();	//头部宽度

    //下拉菜单 style
    var mensHandle = $(".mens");
    if (mensHandle) {
        $(".mens").width( headerLength );	//设置下拉菜单尺寸
    }
	
    var mainlinkObj = $("#mainlink li");	//导航栏 句柄
    var pfcObj = $("#pfc [id^='mbd']");	//浮动窗口 句柄
	
    if (mainlinkObj && pfcObj) {
        mainlinkObj.each(function (i){
            //$(this).css('width', (headerLength / mainlinkObj.length) - 1 );	//设置宽度
			
            if ( i == (mainlinkObj.length - 1) ) {	//为每个按钮增加右边间距
                $(this).css('margin-right', "0px" );
            }
			
            if ($(this).get(0).id == 'floatNav') {	//当指针指向分类的时候
                $(this).hover( function() {	//显示分类列表
                    //经过
                    pfcObj.eq(1).fadeIn('5000');
                }, function() {				//指针离开 隐藏

                    });
            } else {
                $(this).hover( function() {	//移动到其他按钮上则隐藏菜单
                    pfcObj.eq(1).fadeOut('slow');
                }, function() {
                    //null
                    });
            }
		
            $(this).find('a').hover( function() {
                //经过
                $(this).css('background-color', '#577694')		//伪类颜色
                .css('padding-top', "2px");				//边框距离
            }, function() {
                //离开
                $(this).css('background-color', 'transparent')	//原始伪类颜色
                .css('padding-top', "0px");				//边框距离
            });
        });
		
        pfcObj.each(function (i){
            $(this).css('margin-left', mainlinkObj.eq(0).width() + 1)//设置偏移量
            .mouseover(function (){	//光标经过
                $(this).css('display', 'block');
            //$(this).fadeIn('slow');
            }).mouseout(function (){		//光标离开
                $(this).css('display', 'none');
            //$(this).fadeOut('slow');
            })
            .find("li")
            .mouseover(function (){	//光标经过
                $(this).css('background-color', '#577694').css('color', '#FFFFFF');
            }).mouseout(function (){		//光标离开
                $(this).css('background-color', 'transparent').css('color', '');
            })
            .click(function(){	//点击li触发子a click
                window.open($(this).attr('title'));
            });
        });
    //定位.
    }//

});//下拉菜单end

//刷新时间
function refreshClock() {
    var obj = new DateFlash();
    $("#nowtime").html(obj.refreshCalendarClock());
}

//校验对象是否整型
function isInt(x) {
    var y=parseInt(x);
    if (isNaN(y)) return false;
    return x==y && x.toString()==y.toString();
}
 
//TAB页
function showTab(num) {
    for (i=1;i<=4;i++) {
        if (i==num) {
            $("#tab_" + i).fadeIn('slow');
        } else {
            $("#tab_" + i).css('display', 'none');
        }
    }
}

//取得10px / 1em 中整数部分
function getInt(str) {
    return str.replace(/(\d+)px/, "$1");
}


