///////////////////////////////////////////////////////////////////////
// For friend links to scroll on index.html
var friend_link_scroll = {
    numPerTime:1, 
    intervel:5000, 
    currentNo:0,
    timer:null,
    start:function(){
        if(friend_link_scroll.timer == null)
            friend_link_scroll.timer=setInterval(toScrollFriendLink, friend_link_scroll.intervel);
    },
    stop:function(){
        clearInterval(friend_link_scroll.timer);
        friend_link_scroll.timer = null;
    }
};

function toScrollFriendLink(){
    var total_friends = document.getElementsByName('total_friend_link_a');
    var friends = document.getElementsByName('friend_link_a');
    
    //Scroll to next
    friend_link_scroll.currentNo = (friend_link_scroll.currentNo+friend_link_scroll.numPerTime)%total_friends.length;
    
    for(var i=0;i<friends.length;++i){
        friends[i].href = total_friends[(i+friend_link_scroll.currentNo)%total_friends.length].href;
        friends[i].style.backgroundImage = total_friends[(i+friend_link_scroll.currentNo)%total_friends.length].style.backgroundImage;
    
    }
    
}

//////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////
// For activities to scroll on index.html
var activity_scroll = {
    numPerTime:1, 
    intervel:2000, 
    currentNo:0,
    selectedNo:0,
    timer:null,
    total_act_con_arr:[],
    start:function(){
        if(activity_scroll.timer == null)
            activity_scroll.timer=setInterval(toScrollAcitivity, activity_scroll.intervel);
    },
    stop:function(){
        clearInterval(activity_scroll.timer);
        activity_scroll.timer = null;
    },
    scroll_left:function(){
        this.stop();
        this.selectedNo = -1;
        toScrollAcitivity();
        this.start();
    },
    scroll_right:function(){
        this.stop();
        this.currentNo = this.currentNo - 16;
        this.selectedNo = -1;
        toScrollAcitivity();
        this.start();
    }
};

function toScrollAcitivity(){
    //These are total content to replay the current content
    var total_activities_img = document.getElementsByName('total_activities_img'); // for activity logos to scroll
    var total_act_con = document.getElementById('total_activities_content'); // for activity main content to scroll
    
    //This is current content
    var act_img = document.getElementsByName('act_img');    // activity logos,right part
    var top_act = document.getElementById('top_act');       // activity main content,Left part
    
    
    // push the content divs into Array:activity_scroll.total_act_con_arr
    if(activity_scroll.total_act_con_arr.length < 1){
        for(var i=0;i<total_act_con.childNodes.length;++i){
            if(total_act_con.childNodes[i].tagName == "DIV"){
                activity_scroll.total_act_con_arr[activity_scroll.total_act_con_arr.length] = total_act_con.childNodes[i];
            }
        }
    }
    while(activity_scroll.currentNo < 0 && activity_scroll.total_act_con_arr.length > 0)
        activity_scroll.currentNo += activity_scroll.total_act_con_arr.length;
    //Scroll to next
    activity_scroll.selectedNo = (activity_scroll.selectedNo+activity_scroll.numPerTime)%act_img.length;
    
    if(activity_scroll.selectedNo == 0){
        var tmp_index;
        activity_scroll.currentNo = (activity_scroll.currentNo+act_img.length)%activity_scroll.total_act_con_arr.length;
        for(var i=0;i<act_img.length;++i){
            tmp_index = (i+activity_scroll.currentNo)%total_activities_img.length;
            act_img[i].src=total_activities_img[tmp_index].src;
            act_img[i].alt=total_activities_img[tmp_index].alt;
            ++i;
            tmp_index = (i+activity_scroll.currentNo)%total_activities_img.length;
            act_img[i].src=total_activities_img[tmp_index].src;
            act_img[i].alt=total_activities_img[tmp_index].alt;
        }
        
    }
    var tmpNo = (activity_scroll.selectedNo % 4)*2+parseInt(activity_scroll.selectedNo / 4);
    select_certain_act_img(tmpNo);
    tmpNo = (activity_scroll.currentNo + tmpNo)%activity_scroll.total_act_con_arr.length;
    top_act.innerHTML = activity_scroll.total_act_con_arr[tmpNo].innerHTML;
    
}

function display_special_act_con(act_no){
    if(typeof(activity_scroll) == 'undefine')
        return;
    // push the content divs into Array:activity_scroll.total_act_con_arr
    if(activity_scroll.total_act_con_arr.length < 1){
        var total_act_con = document.getElementById('total_activities_content'); // for activity main content to scroll
        for(var i=0;i<total_act_con.childNodes.length;++i){
            if(total_act_con.childNodes[i].tagName == "DIV"){
                activity_scroll.total_act_con_arr[activity_scroll.total_act_con_arr.length] = total_act_con.childNodes[i];
            }
        }
    }
    
    var top_act = document.getElementById('top_act');       // activity main content,Left part
    
    act_no = act_no % activity_scroll.total_act_con_arr.length;
    top_act.innerHTML = activity_scroll.total_act_con_arr[act_no].innerHTML;

}

function select_certain_act_img(no){
    var act_img = document.getElementsByName('act_img');    // activity logos,right part
    for(var i=0;i<act_img.length;++i){
         if(i == no){
             act_img[i].style.border = 'solid orange';//'medium double orange'
             act_img[i].style.borderWidth = '1px';
         }else{
             act_img[i].style.border = 'solid white';
             act_img[i].style.borderWidth = '1px';
         }
    }

}

//////////////////////////////////////////////////////////////////////////////////////////
function initial_scroll_event(){
    var friends = document.getElementsByName('friend_link_a');
    for(var i=0;i<friends.length;++i){
        myAttachEvent(friends[i],'mouseover',friend_link_scroll.stop);
        myAttachEvent(friends[i],'mouseout',friend_link_scroll.start);
    }
    
    var act_img = document.getElementsByName('act_img');    // activity logos,right part

    for(var i=0;i<act_img.length;++i){
        if (act_img[i].addEventListener)
            act_img[i].addEventListener('click',function(e){activity_mouseclick(e)}, false);
        else if (act_img[i].attachEvent) 
            act_img[i].attachEvent("onclick", function(e){activity_mouseclick(e)});

        myAttachEvent(act_img[i],'mouseover',activity_mouseover);
        myAttachEvent(act_img[i],'mouseout',activity_mouseout);
    }    

}

function activity_mouseover(){
    activity_scroll.stop();
}

function activity_mouseclick(e){
    activity_scroll.stop();
    var element = getEventElement(e);  //in nav_tab.js
    var act_no = null;
    var act_img = document.getElementsByName('act_img');    // activity logos,right part
    for(var i=0;i<act_img.length;++i){
        if(act_img[i] == element){
            act_no = i;
            break;
        }
    }
    select_certain_act_img(act_no);
    display_special_act_con(activity_scroll.currentNo-activity_scroll.currentNo%2+act_no);
}

function activity_mouseout(){
    activity_scroll.start();
    
}
////////////////////////////////////////////////////OTHER
function myAttachEvent(object,eventName,action){
    if (object.addEventListener)
        object.addEventListener(eventName,action, false);
    else if (object.attachEvent) 
        object.attachEvent("on"+eventName, action);

}

