if ( typeof($) != 'function'){
    function $(id){
        return typeof(id)=='string'?document.getElementById(id):id;
    }
}

var calObject = {
    contentObj : $('brief'),
    data : null,
    showTimer : null,
    interval : 600,
    hideTime : 1500,
    hideTImer : null,
    node:null,
    show : function(id,o){
        
        var data = this.data;
        if ( typeof(data[id]) != null ){
            var items = data[id];
            var content = '';
            for(property in items){
                if ( isNaN(parseInt(property)) ){
                    continue;
                }
                var url = items[property]['url'];
                var title = items[property]['title_cal'];
                if ( url ){
                    content = content + '<a href="'+ url + '" target="_blank">' + title + '</a><br/>';
                } else {
                    content = content + title + '<br/>';
                }
            }
            $('brief_content').innerHTML = content;
            var e = o;
            var x = e.offsetLeft,y = e.offsetTop;   
            while(e = e.offsetParent){ 
                x += e.offsetLeft;   
                y += e.offsetTop;
            } 
            var brief = this.contentObj;
//                    alert(y);
            if ( brief.offsetHeight == 0 )
            {
                brief.style.left = x + o.offsetWidth + 'px';
                brief.style.display = 'block';
                brief.style.top = y - brief.offsetHeight + 35 + 'px';
            }
            else
            {
                brief.style.left = x + o.offsetWidth + 'px';
                brief.style.top = y - brief.offsetHeight + 35 + 'px';
                brief.style.display = 'block';
            }
            
            
        } else {
            
        }
    },
    setToHide : function(){
        var obj = this;
        if ( obj.showTimer ){
            clearTimeout(obj.showTimer);
        }
        this.hideTimer = window.setTimeout(function(){obj.hide();}, obj.hideTime);
    },
    clearHideAction : function(){
        if ( this.hideTimer ){
            clearTimeout(this.hideTimer);
        }
    },
    hide : function(){
        this.contentObj.style.display = 'none';
    }
};
/**
* 刷新日期函数
*
*/
function reqCal(){
    // 月份
    $('brief_title').innerHTML = $('cal_game').options[$('cal_game').selectedIndex].text;
    // 标题
    Ajax.getJSON('/calendar.php?date=' + encodeURIComponent($('cal_date').innerHTML) + '&gid=' + $('cal_game').options[$('cal_game').selectedIndex].value,'',function(o,response){
        eval('var fff='+response+';');
        $('cal_container').innerHTML = fff['html'];
//                    eval('calObject.data='+fff['data']+';');
        calObject['data'] = fff['data'];
    });
}
function toggleDate(type){
    var text = $('cal_date').innerHTML;
    var ret = text.split("-");
    
    var year = parseInt(ret[0],10);
    var mon = parseInt(ret[1],10);
    if ( isNaN(year) || isNaN(mon) ){
        return;
    }
    if ( type == 1 ){
        // 加
        if ( mon == 12 ){
            ++year;
            mon = 1;
        }else{
            ++mon;
        }
    }else{
        // 减
        if ( mon == 1 ){
            --year;
            mon = 12;
        }else{
            --mon;
        }
    }
    if ( mon.toString().length == 1 ){
        mon = '0' + mon.toString();
    }
//                alert(year + '-' + mon);
    $('cal_date').innerHTML = year + '-' + mon;
    // 请求
    reqCal();
}
function showBrief(id,o){
//                getAbsPoint(o);
    calObject.clearHideAction();
    if ( calObject.showTimer ){
        clearTimeout(calObject.showTimer);
    }
    calObject.showTimer = setTimeout(function(){calObject.show(id,o);},calObject.interval);
}
function   getAbsPoint(obj)   
{   
    oRect   =   obj.getBoundingClientRect(); 
}
(function(){
     // 初始化日历
     if ( window.addEventListener ){
         window.addEventListener('load', reqCal, false);
     }else if( window.attachEvent){
         window.attachEvent('onload', reqCal);
     }else{
         window.onload = reqCal;
     }
 })();
 