// JavaScript Document
function init() {
    for(var i=0; i<window.onloadListeners.length;i++) {
        var func = window.onloadListeners[i];
        func.call();
    }
}
window.onloadListeners = new Array();
window.addOnLoadListener = function(listener) {
    window.onloadListeners[window.onloadListeners.length] = listener;
}

/* on document mouse up */
window.onmouseupListeners = [];
window.addOnMouseUpListener = function(listener) {
    window.onmouseupListeners[window.onmouseupListeners.length] = listener;
}

function initOnMouseUp() {
    window.document.onmouseup = function() {
        for(var i=0; i<window.onmouseupListeners.length;i++) {
            var func = window.onmouseupListeners[i];
            func.call();
        }
    }
}
/* on document mouse up */

String.prototype.trim = function() {
    return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

String.prototype.replaceAll = function(AFindText,ARepText,i){ 
    raRegExp = new RegExp(AFindText,"g"+i); 
    return this.replace(raRegExp,ARepText) 
}

Array.prototype.removeItem = function(index) {
    if(index==0) {
        this.shift();
        return this;
    }else if(index>=this.length || index<0) {
    return this;
}else {
return this.slice(0, index).concat(this.slice(index+1, this.length));
}
}

function switchStyle(s) {
    if (!document.getElementsByTagName) return;
    var el = document.getElementsByTagName("link");
    for (var i = 0; i < el.length; i++ ) {
        if (el[i].getAttribute("rel").indexOf("style") != -1 && el[i].getAttribute("title")) {
            el[i].disabled = true;
            if (el[i].getAttribute("title") == s) el[i].disabled = false;
        }
    }
}
//
function switchBox(editor, switcher){
    new Effect[Element.visible(editor) ? 
        'BlindUp' : 'BlindDown'](editor, {duration: 0.25});
    new Effect[Element.visible(switcher) ? 
        'BlindUp' : 'BlindDown'](switcher, {duration: 0.25}); 
}

/*search box*/
function do_filter() { 
    var f_u_name = $F('f_u_name').trim();
    // fulltext search
    if(searchType && searchType=='f') {
        if($('ad_holder').style.display=='') {
            ad_search();
        }
        var f_what = $F('f_t_name').trim();
        //f_u_name=='' && 
        if(f_u_name=='' && f_what==''){
            return false;
        }
        var vm = $('my_name') ? "0" : "1";
        re_all = new RegExp(/^all$/i);
        re_my = new RegExp(/^my$/i);
        if(f_u_name.match(re_all) && f_what==''){
            return false;
        }
        self.location = '/search/who='+f_u_name
        + '&what='+f_what
        // tag search 
    }else {
    var f_t_name = $F('f_t_name').trim(); 
    // all
    re_all = new RegExp(/^all$/i);
    re_my = new RegExp(/^my$/i);
    if(f_u_name.match(re_all)) {
        if(f_t_name!='') {
            self.location = '/tag/'+f_t_name
        }else {
        self.location = '/community'  
    }
    // my
}else if(f_u_name.match(re_my)) {
f_u_name = $F('my_name');
if(f_t_name!='') {
    self.location = '/user/'+f_u_name+'/'+f_t_name
}else {
self.location = '/user/'+f_u_name
}
// /user/tag  
}else if(f_u_name!='' && f_t_name!='') {
self.location = '/user/'+f_u_name+'/'+f_t_name
// /tag
}else if(f_u_name=='' && f_t_name!='') {
self.location = '/tag/'+f_t_name  
// /user
}else if(f_u_name!='' && f_t_name=='') {
self.location = '/user/'+f_u_name  
}else {
self.location = '/community'
}
} // fulltext search
}

function check_filter(id, img_id) {
    if($(id).style.display=='') {
        $(img_id).src = '/images/arr_left.gif'
        Element.hide(id)
    }else {
    $(img_id).src = '/images/arr_bottom.gif'
    Element.show(id)
}
}
var defSearchType;
function ad_search() {  
    var adSearchBox = $('ad_holder');
    if(adSearchBox.style.display=='none') { 
        setAdSearchBoxOffsets(adSearchBox);
        adSearchBox.style.display = "";
        Element.show('ad_holder_shadow');
        $('ad_tags').value = '';
        $('ad_url').value = '';
        $('ad_title').value = '';
        $('ad_text').value = '';
        $('ad_comments').value = '';
        $('ad_highlights').value = '';
        $('ad_anywhere').value = $F('f_t_name');
        $('ad_nowhere').value = '';
        $('ad_anywhere').focus();
        defSearchType = searchType;
        searchType = 'f';
        $('tagSearch').disabled = true;
    }else {
    searchType = defSearchType;
    encode_ad_syntax();
    adSearchBox.style.display = "none";
    Element.hide('ad_holder_shadow');
    $('f_t_name').focus();
    $('tagSearch').disabled = false;
}
}

//
function encode_ad_syntax() {
    var p_what = "";
    // encode tags
    
    var p_tags = $F('ad_tags').trim();
    if(p_tags!='') {
        p_tags = "tag:("+p_tags+") ";
        p_what += p_tags;
    }
    
    // encode title
    var p_url = $F('ad_url').trim();
    if(p_url!='') {
        p_url = "url:("+p_url+") ";
        p_what += p_url;
    }
    
    // encode title
    var p_title = $F('ad_title').trim();
    if(p_title!='') {
        p_title = "title:("+p_title+") ";
        p_what += p_title;
    }
    // encode text
    var p_text = $F('ad_text').trim();
    if(p_text!='') {
        p_text = "text:("+p_text+") ";
        p_what += p_text;
    }
    // encode comments
    var p_comments = $F('ad_comments').trim();
    if(p_comments!='') {
        p_comments = "c:("+p_comments+") ";
        p_what += p_comments;
    }
    // encode highlights
    var p_highlights = $F('ad_highlights').trim();
    if(p_highlights!='') {
        p_highlights = "h:("+p_highlights+") ";
        p_what += p_highlights;
    }
    // encode anywhere
    var p_anywhere = $F('ad_anywhere').trim();
    if(p_anywhere!='') {
        p_anywhere = p_anywhere+" ";
        p_what += p_anywhere;
    }
    
    // encode anywhere 2
    var p_anywhere_2 = $F('ad_anywhere_2').trim();
    if(p_anywhere_2!='') {
        p_anywhere_2 = p_anywhere_2+" ";
        p_what += p_anywhere_2;
    }
    
    // encode nowhere
    var p_nowhere = $F('ad_nowhere').trim();
    if(p_nowhere!='') {
        p_nowhere = "-("+p_nowhere+")"; 
        p_what += p_nowhere;
    }
    $('f_t_name').value = p_what;
}

function decode_ad_syntax() {
    return;
    var p_what = $F('f_t_name').trim();
    if(p_what=='') {
        return;
    }
    for(i=0; i<p_what.length; i++) {
        alert(p_what[i]);
    }
    // decode tags
    var re = new RegExp(/tags?:\(.*\)/i);
    var t_match_a = p_what.match(re);
    if(t_match_a) {
        var t_match_str = t_match_a[0];
        p_what -= t_match_str;
        var p_tags = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
        $('ad_tags').value = p_tags;
    }else {
    $('ad_tags').value = "";    
}

// decode title
var re = new RegExp(/titles?:\(.*\)/i);
var t_match_a = p_what.match(re);
if(t_match_a) {
    var t_match_str = t_match_a[0];
    p_what -= t_match_str;
    var p_title = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
    $('ad_title').value = p_title;
}else {
$('ad_title').value = "";    
}

// decode text
var re = new RegExp(/text?:\(.*\)/i);
var t_match_a = p_what.match(re);
if(t_match_a) {
    var t_match_str = t_match_a[0];
    p_what -= t_match_str;
    var p_text = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
    $('ad_text').value = p_text;
}else {
$('ad_text').value = "";    
}

// decode comments
var re = new RegExp(/comments?:\(.*\)/i);
var t_match_a = p_what.match(re);
if(t_match_a) {
    var t_match_str = t_match_a[0];
    p_what -= t_match_str;
    var p_comments = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
    $('ad_comments').value = p_comments;
}else {
$('ad_comments').value = "";    
}

// decode highlights
var re = new RegExp(/highlights?:\(.*\)/i);
var t_match_a = p_what.match(re);
if(t_match_a) {
    var t_match_str = t_match_a[0];
    p_what -= t_match_str;
    var p_highlights = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
    $('ad_highlights').value = p_highlights;
}else {
$('ad_highlights').value = "";    
}

// decode nowhere
var re = new RegExp(/-\(.*\)/i);
var t_match_a = p_what.match(re);
if(t_match_a) {
    var t_match_str = t_match_a[0];
    p_what -= t_match_str;
    var p_nowhere = t_match_str.substring(t_match_str.indexOf(':(')+2, t_match_str.length-1);
    $('ad_nowhere').value = p_nowhere;
}else {
$('ad_nowhere').value = "";    
}

// decode anywhere
var t_match_str = p_what.trim();
$('ad_anywhere').value = t_match_str;

}

function change_search_type(s_type) {
    if(s_type=='f') {
        Element.hide('f_t_name'); 
        Element.show('f_what');
        $('f_what').value = $F('f_t_name');
    }else {
    Element.show('f_t_name');
    Element.hide('f_what');
    Element.hide('ad_holder');
}
}
/*search box*/



function calculateOffest(field, attr) {
    var offset = 0;
    while(field) {
        offset += field[attr]; 
        field = field.offsetParent;
    }
    return offset;
} 

function setWhoListOffsets(whoList) {
    var whoFiled = $("f_u_name");
    var end = whoFiled.offsetWidth;
    var left = calculateOffest(whoFiled, "offsetLeft");
    var top = calculateOffest(whoFiled, "offsetTop") + whoFiled.offsetHeight;
    whoList.style.left = left + "px";
    whoList.style.top = top + "px";
}   

function setAdSearchBoxOffsets(adSearchBox) {
    var whatFiled = $("f_t_name");
    var end = whatFiled.offsetWidth;
    var left = calculateOffest(whatFiled, "offsetLeft");
    var top = calculateOffest(whatFiled, "offsetTop") + whatFiled.offsetHeight;
    adSearchBox.style.left = left-6 + "px";
    adSearchBox.style.top = top-47 + "px";
}

function check_who() {
    var whoList = $("who_list");
    if(whoList.style.display=="") {
        whoList.style.display = "none" ;
    }else { 
    setWhoListOffsets(whoList);            
    whoList.style.display = "";
}
}

function fill_who(id, u_name) {
    $(id).value = u_name;
    check_who();
} 

function displayAvatar(size, avatar) {
    for(var i=0; i<avatar.length; i++) { 
        if(avatar[i].offsetWidth>avatar[i].offsetHeight) {
            avatar[i].style.width = size+"px";
        }else if(avatar[i].offsetWidth<avatar[i].offsetHeight) {     
        avatar[i].style.height = size+"px";
    }else {
    avatar[i].style.width = size+"px";
}
avatar[i].style.visibility = "visible";              
}
}

function switchSkinOptions() {
    var skinPanel = $("skinPanel");
    if(skinPanel.style.display=="") {
        skinPanel.style.display = "none";
    }else {
    setSkinPanelOffset(skinPanel)
    skinPanel.style.display = "";
}
}

function setSkinPanelOffset(skinPanel) {
    var skinOptions = $("skinOptions");
    var end = skinOptions.offsetWidth;
    var left = calculateOffest(skinOptions, "offsetLeft");
    var top = calculateOffest(skinOptions, "offsetTop") + skinOptions.offsetHeight;
    skinPanel.style.left = left-80 + "px";
    skinPanel.style.top = top + "px";
}


function initSkinSwitcher() {
    var index = _GLOBAL_VAR['skin_index'];
    var skinA = _GLOBAL_VAR['skin_name'];
    var skinOptions = $("skinOptions");
    var skinPanel = document.createElement("div");
    skinPanel.id = "skinPanel";
    skinPanel.style.display = "none";
    var panelNoticer = document.createElement("div");
    panelNoticer.id="panelNoticer";
    panelNoticer.innerHTML = WORD_27;
    var closer = document.createElement("img");
    closer.id = "closer";
    closer.src="/images/v2/closer.gif";
    closer.onclick = switchSkinOptions;
    panelNoticer.appendChild(closer);
    skinPanel.appendChild(panelNoticer);
    for(var i=0; i<5; i++) {
        var skin = document.createElement("a");
        skin.id="skin_"+i;
        skin.className = skinA[i];
        skin.title = skinA[i];
        skin.onclick = switchSkin;
        if(i==index) {
            skin.className += " current";
            var currentMark = document.createElement("img");
            currentMark.id = "currentMark";
            currentMark.src = "/images/v2/tempCurrentMark.png";
            currentMark.className = "currentMark";
            skin.appendChild(currentMark);
        }
        var temp = document.createElement("img");
        temp.className = "tempPreview";
        temp.src="/images/v2/temp_"+skinA[i]+".gif";
        skin.appendChild(temp);
        skinPanel.appendChild(skin);
    }
    var skinSwitcher = document.createElement("span");
    skinSwitcher.id = "skinSwitcher";
    skinSwitcher.className = "skinSwitcher";
    skinSwitcher.onclick = switchSkinOptions;
    skinSwitcher.innerHTML = WORD_26;
    skinOptions.appendChild(skinPanel);
    skinOptions.appendChild(skinSwitcher);
}
function markCurrentSkin(index) {
    if(index==_GLOBAL_VAR['skin_index']) return false;
    var skin = $("skin_"+index);
    skin.className += " current";
    var preSkin = $("skin_"+_GLOBAL_VAR['skin_index']);
    preSkin.className = preSkin.className.replace("current", "");
    _GLOBAL_VAR['skin_index'] = index;
    var el = $("currentMark");
    var currentMark = el.parentNode.removeChild(el);
    skin.appendChild(currentMark);
}

function switchSkin() {
    var index = this.id.split('_')[1];
    if(_GLOBAL_VAR['skin_index']==index) {
        switchSkinOptions();  
        return false;
    }
    var url = '/group_mana/switch_skin/'+index;
    new Ajax.Request(url, 
        {asynchronous:true, 
            evalScripts:true, 
            onLoading:function(request){switchSkinLoading(request, index)}, 
            onComplete:function(request){switchSkinComplete(request, index)}
        }
    ); 
    return false;
}

function switchSkinLoading(request, index) {
    
}

function switchSkinComplete(request, index) {
    if(request.status=="200") {
        switchSkinOptions();
        var logo = $("logoImg");
        var preSkin = _GLOBAL_VAR['skin_name'][_GLOBAL_VAR['skin_index']];
        var currentSkin = _GLOBAL_VAR['skin_name'][index];
        logo.src = logo.src.replace(/v2\/.*\//, "v2/"+currentSkin+"/");
        var skinLink = $("skinLink_"+currentSkin+"_"+0);
        if(skinLink) {
            // use cache
        }else {
        var i = 0;
        var preSkinLink = $("skinLink_"+preSkin+"_"+i);
        while(preSkinLink) {
            var link = document.createElement("link");
            link.id = "skinLink_"+currentSkin+"_"+i;
            link.rel= "stylesheet";//"alternate stylesheet";
            link.href = preSkinLink.href.replace(/v2\/.*\//, "v2/"+currentSkin+"/");
            link.type = "text/css";
            link.media="screen";
            document.body.appendChild(link);
            i++;
            preSkinLink = $("skinLink_"+preSkin+"_"+i);
        }
    }
    doSwitchSkin(preSkin, currentSkin);
    markCurrentSkin(index);
}else {
alert("switchSkinFailed");
}
}
function doSwitchSkin(preSkin, currentSkin) {
    var i = 0
    var skinLink = $("skinLink_"+currentSkin+"_"+i);
    // use cache
    if(skinLink) {
        var logo = $("logoImg");
        logo.src = logo.src.replace(/v2\/.*\//, "v2/"+currentSkin+"/");
        while(skinLink) {
            var preSkinLink = $("skinLink_"+preSkin+"_"+i);
            preSkinLink.disabled = true;
            skinLink.disabled = false;
            i++;
            skinLink = $("skinLink_"+currentSkin+"_"+i);
        }
        return true;
        // skin no cache
    }else {
    return false;
}
}

function initSideBarBoxSwitchAble() {
    try{
        var index = 0;
        var header = $("sideBarBoxTitle_"+index);
        var eocMark = $("eocMark_"+index);
        var content = $("sideBarBoxContent_"+index);
        while(header) {
            header.onclick = switchSideBarBox;
            if(content.style.display=="") {
                eocMark.className += " expand";
            }else {
            eocMark.className += " collapse";
        }
        index++;
        //alert($("sideBarBoxTitle_"+index));
        header = $("sideBarBoxTitle_"+index);
        eocMark = $("eocMark_"+index);
        content = $("sideBarBoxContent_"+index);
        //alert(index);
        //alert(header);
    }
}
catch(e)
{
    alert(e);
}
}

function switchSideBarBox() {
    var index = this.id.split("_")[1];
    var content = $("sideBarBoxContent_"+index);
    new Effect[Element.visible(content) ?
        'BlindUp' : 'BlindDown'](content, {duration: 0.25});
    var eocMark = $("eocMark_"+index);
    if(content.style.display=="") {
        eocMark.className = eocMark.className.replace("expand", "collapse");
    }else {
    eocMark.className = eocMark.className.replace("collapse", "expand");
}
}

function switchBox2(box1, box2){
    new Effect[Element.visible(box1) ? 
        'BlindUp' : 'BlindDown'](box1, {duration: 0.25});
    new Effect[Element.visible(box2) ? 
        'BlindUp' : 'BlindDown'](box2, {duration: 0.25}); 
}

function flashNotice2(content, noticerID, className) {
    if(typeof(noticerID)=="undefined") noticerID = "tailNoticer";
    if(typeof(className)=="undefined") className = "tailNoticer";
    var noticer = $(noticerID);
    if(noticer) {
        noticer.style.display = "";
        noticer.innerHTML = content;
        noticer.className = className;
        noticer.onclick = quickFadeFlashNoticer;
        setTimeout("new Effect.Fade('"+noticerID+"')",DURATION_1);
    }
}

function hideNotice2(noticerID) {
    if(typeof(noticerID)=="undefined") noticerID = "tailNoticer";
    var noticer = $(noticerID);
    if(noticer) noticer.style.display = "none";
}

function quickFadeFlashNoticer() {
    setTimeout("new Effect.Fade('"+this.id+"')",DURATION_2);
}

//function showCentralNoticer(content, noticerID) {
//    if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
//    var noticerBox = $(noticerID+"_box");
//    var noticerContent = $(noticerID+"_content");
//    if(!noticerBox) {
//        noticerBox = document.createElement("div");
//        noticerBox.className = "centralNoticerBox";
//        noticerBox.id = noticerID+"_box";
//        noticerContent = document.createElement("div");
//        noticerContent.id = noticerID+"_content";
//        noticerContent.className = "centralNoticerContent";
//        noticerBox.appendChild(noticerContent);
//        document.body.appendChild(noticerBox);
//    }
//    var width = document.body.offsetWidth;
//    var height = document.body.offsetHeight;
//    noticerBox.style.display = "block";
//    noticerBox.style.left = width/2 - (noticerBox.offsetWidth/2) + "px";
//    noticerContent.innerHTML = content;
//    noticerBox.style.top = "160px";
//    if(noticerBox.offsetHeight<100) {
//        noticerBox.style.height = "100px";
//    }
//}
//function updateCentralNoticer(content, noticerID) {
//    if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
//    var noticerBox = $(noticerID+"_content");
//    noticerBox.innerHTML = content;
//}
//function hideCentralNoticer(delay) {
//    if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
//    if(typeof(delay)=='undefined') delay = 1;
//    delay = delay*1000;
//    //$(noticerID+"_box").style.display="none";
//    setTimeout("new Effect.Fade('"+noticerID+"_box')",delay)
//}

function showCentralNoticer(content, noticerID) {
  if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
  if(typeof(content)=='undefined') content = "Processing...";
  var noticerBox = $(noticerID+"_box");
  var noticerContent = $(noticerID+"_content");
  if(!noticerBox) {
    noticerBox = document.createElement("div");
    noticerBox.className = "centralNoticerBox";
    noticerBox.id = noticerID+"_box";
    noticerContent = document.createElement("div");
    noticerContent.id = noticerID+"_content";
    noticerContent.className = "centralNoticerContent";
    noticerBox.appendChild(noticerContent);
    document.body.appendChild(noticerBox);
  }
  //TODO
  noticerContent.update(content);
  var width = document.body.offsetWidth;
  var left = width/2 - $(noticerID+"_box").getWidth()/2;
  noticerBox.setStyle({"left":left+"px", "top":0, "height":"20px"}).show();
  
}
function updateCentralNoticer(content, noticerID) {
  if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
  var noticerBox = $(noticerID+"_content");
  noticerBox.innerHTML = content;
}
function hideCentralNoticer(delay) {
  if(typeof(noticerID)=='undefined') noticerID = "centralNoticer";
  if(typeof(delay)=='undefined') delay = 1;
  delay = delay*1000;
  $(noticerID+"_box").style.display="none";
}

function Validator(){
    this.isEmail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    this.isEmail2 = /^.+<\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*>$/;
    this.isTags = /^[^%&]+$/;
    this.isTag = /^[^%&\s]*$/;
    this.isUsername = /^[a-zA-Z]([0-9a-zA-Z\-_]{5,15})$/;
    this.isUrl = /^https?:\/\/.+$/;
    this.IsEmail = function (email) {
        re = this.isEmail;
        return (re.test(email));
    };
    this.IsEmail2 = function (email) {
        re = this.isEmail2;
        return (re.test(email));
    };
    this.IsTags = function (tags) {
        re = this.isTags;
        return (re.test(tags));
    };
    this.IsTag = function (tag) {
        re = this.isTag;
        return (re.test(tag));
    };
    this.IsUsername = function (username) {
        re = this.isUsername;
        return (re.test(username))
    };
    this.IsPwd = function (pwd) {
        if(pwd.length>=6 && pwd.length<=32) {
            return true;
        }else {
        return false;
    }
};
this.IsRealname = function(realname) {
    if(realname.length>=1 && realname.length<=64) {
        return true;
    }else {
    return false;
}
}
this.IsTitle = function(title) {
    if(title.length<=255) {
        return true;
    }else {
    return false;
}
}
this.IsSignature = function(signature) {
    if(signature.length<=255){
        return true
    }else {
    return false;
}
}
this.IsUrl = function(url) {
    re = this.isUrl;
    return (re.test(url))
}
};

function appendTip(tipperID, content, position) {
    var tipper = $(tipperID);
    tipper.content = content;
    tipper.tipPosition = position;
    tipper.onmouseover = showFloatTip;
    tipper.onmouseout = hideFloatTip;
}

function showFloatTip() {
    var floatTipID = "floatTip";
    var floatTip = getFloatTip(floatTipID);
    floatTip.innerHTML = "<p>"+this.content+"</p>";
    setFloatTipOffsets(this, floatTip, this.tipPosition);
    floatTip.style.display = "block";
}
function hideFloatTip() {
    var floatTipID = "floatTip";
    var floatTip = getFloatTip(floatTipID);
    floatTip.style.display = "none";
}
function getFloatTip(floatTipID) {
    var floatTip = $(floatTipID);
    return floatTip ? floatTip : createFloatTip(floatTipID);
}
function createFloatTip(floatTipID) {
    var box = document.createElement("div");
    box.id = floatTipID;
    box.className = box.id;
    box.style.display = "none";
    document.body.appendChild(box);
    return box;
}

/* cookie */
function getCookieDomain() {
    return D_DOMAIN;
}

function Cookie(document, name, hours, path, domain, secure) {
    this.$document = document;
    this.$name = name;
    if(hours)
        this.$expiration = new Date((new Date).getTime() + hours*3600000);
    else this.$expiration = null;
    if(path) this.$path = path; else this.$path = null;
    if(domain) this.$domain = domain; else this.$domain = null;
    if(secure) this.$secure = secure; else this.$secure = null;  
}

Cookie.prototype.store = function() {
    var cookieval = "";
    for(var prop in this) {
        if((prop.charAt(0) =='$' ) || ((typeof this[prop]) == 'function' ))
            continue;
        if(cookieval != "") cookieval += '|';
        cookieval += prop + ':' + escape(this[prop]);
    }
    
    var cookie = this.$name + '=' + cookieval;
    if(this.$expiration)
        cookie += '; expires=' + this.$expiration.toGMTString();
    if(this.$path) cookie += '; path=' + this.$path;
    if(this.$domain) cookie += '; domain=' + this.$domain;
    if(this.$secure) cookie += '; secure';
    this.$document.cookie = cookie;
}

Cookie.prototype.load = function() {
    var allcookies = this.$document.cookie;
    if(allcookies == '') return false;
    
    var start = allcookies.indexOf(this.$name + '=');
    if(start==-1) return false;
    start += this.$name.length + 1;
    var end = allcookies.indexOf(';', start);
    if(end==-1) end = allcookies.length;
    var cookieval = allcookies.substring(start, end);
    
    var a = cookieval.split('|');
    for(var i=0; i<a.length; i++){
        a[i] = a[i].split(':');
    }
    
    for(var i=0; i<a.length; i++) {
        this[a[i][0]] = unescape(a[i][1]);
    }
    
    return true
}

Cookie.prototype.remove = function() {
    var cookie;
    cookie = this.$name + '=';
    if(this.$path) cookie += '; path=' + this.$path;
    if(this.$domain) cookie += '; domain=' + this.$domain;
    cookie += '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
    this.$document.cookie = cookie;
}
/* cookie */


/* avatar */
function user_avatar_uri(fileName, size) {
    var uri = USER_AVATAR_URI.replace("?", fileName).replace("_size", "_"+size);
    return uri;
}
function group_avatar_uri(fileName, size) {
    var uri = GROUP_AVATAR_URI.replace("?", fileName).replace("_size", "_"+size);
    return uri;
}
/* avatar */

function report() {
    var w = window.open(BOOKMARK_HOST+"/report/index", null, "width=380,height=500, left=0, top=0, status=no,resizable=no");
}

function doSearch(searchType, ad,group_name) {
    if(typeof(searchType)=="undefined") searchType="T";
    if(typeof(group_name)=="undefined") group_name="";
    var reAll = new RegExp(/^all$/i);
    var reMy = new RegExp(/^my$/i);
    var scope = $F("dSearchScope").trim();
    var keyword = $F("dSearchKeyword").trim();
    var url;
    if(searchType=="T") {
        if(scope=="" || scope.match(reAll)) {
            if(keyword==_GLOBAL_VAR['keywordTip']) return false;
            url = BOOKMARK_HOST+"/tag/"+keyword;
        }else if(scope.match(reMy)){
        if(keyword==_GLOBAL_VAR['keywordTip'])
            url = BOOKMARK_HOST+"/user/"+_GLOBAL_VAR['my_name'];
        else
            url = BOOKMARK_HOST+"/user/"+_GLOBAL_VAR['my_name']+"/"+keyword;
    }else {
    if(keyword==_GLOBAL_VAR['keywordTip'])
        url = BOOKMARK_HOST+"/user/"+scope;
    else
        url = BOOKMARK_HOST+"/user/"+scope+"/"+keyword;
}
}else if(searchType=="GT"){
if(keyword==_GLOBAL_VAR['keywordTip'])
    url = GROUP_HOST+"/"+group_name+"/bookmark/tag";
else
    url = GROUP_HOST+"/"+group_name+"/bookmark/tag/"+keyword;
}
else if(searchType=="F") {
    if(keyword==_GLOBAL_VAR['keywordTip'])
        url = BOOKMARK_HOST+"/search/ad="+ad+"&who="+scope+"&what=";
    else
        url = BOOKMARK_HOST+"/search/ad="+ad+"&who="+scope+"&what="+keyword;
}else if(searchType=="GF"){
if(keyword==_GLOBAL_VAR['keywordTip'])
    url = GROUP_HOST+"/search?group_name="+group_name+"&who="+scope+"&what=";
else
    url = GROUP_HOST+"/search?group_name="+group_name+"&who="+scope+"&what="+keyword;
}
else {
    return false;
}
self.location = url;
return false;
}
function doFSearch(type,group_name) {
    if(typeof(type)=="undefined") type="all";
    if(typeof(group_name)=="undefined") group_name="";
    var ad = 0;
    if(type=="my") {
        $("dSearchScope").value = "My";
    }else if(type=="all") {
    $("dSearchScope").value = "All";
}else if(type=="ad") {
ad = 1;
}else if(type=="group"){
$("dSearchScope").value = "Group";
doSearch("GF",ad,group_name);
return;
}
doSearch("F", ad);
}

function fillScope(scope) {
    $("dSearchScope").value = scope;
    switchScopeList();
}

function switchScopeList() {
    var scopeList = $("scopeList");
    if(scopeList.style.display=="none") {
        scopeList.style.display = ""
        setScopeListOffsets(scopeList);
    }else {
    scopeList.style.display = "none";
}
return false;
}

function setScopeListOffsets(scopeList) {
    var who = $("dSearchScope");
    var end = who.offsetWidth;
    var left = calculateOffest(who, "offsetLeft");
    var top = calculateOffest(who, "offsetTop") + who.offsetHeight;
    scopeList.style.left = left + "px";
    scopeList.style.top = top + "px";
}
function blurKeyword() {
    var keyword = $("dSearchKeyword");
    if(keyword.value.trim()=="") {
        keyword.value = _GLOBAL_VAR['keywordTip'];
    }
}
function focusKeyword() {
    var keyword = $("dSearchKeyword");
    if(keyword.value.trim()==_GLOBAL_VAR['keywordTip']) {
        keyword.value = ""
    }
}

function setCaretPos(ele, pos) {
    if(ele.createTextRange) {
        /* Create a TextRange, set the internal pointer to
        a specified position and show the cursor at this
        position
        */
        var range = ele.createTextRange();
        range.move("character", pos);
        range.select();
    } else if(ele.selectionStart >= 0) {
    /* Gecko is a little bit shorter on that. Simply
    focus the element and set the selection to a
    specified position
    */
    ele.focus();
    ele.setSelectionRange(pos, pos);
}
}

function switchSearchMoreList() {
    var list = $("searchMore");
    if(list.style.display=="none") {
        list.style.display = ""
        setSearchMoreOffsets(list);
    }else {
    list.style.display = "none";
}
return false;
}

function setSearchMoreOffsets(list) {
    var switcher = $("searchMoreLink");
    var end = switcher.offsetWidth;
    var left = calculateOffest(switcher, "offsetLeft");
    var top = calculateOffest(switcher, "offsetTop") + switcher.offsetHeight;
    list.style.left = left-75 + "px";
    list.style.top = top+3 + "px";
}

function markUserStatus() {
    var usernameEls = $("main").select('.dUserName');
    var usernames = [];
    //    usernameEls.each(function(u) {
    //        usernames.push(u.name.toLowerCase());
    //    });
    usernameEls.each(function(u) {
        Try.these(
            function() {usernames.push(u.getAttribute("username"));},
            function() {usernames.push(u.name.toLowerCase());},
            function() {usernames.push(u.title.toLowerCase());}
        );
    });
    var url="/site/check_user_status";
    var params = "json_infos="+usernames.toJSON();
    new Ajax.Request(url, 
        {asynchronous:true, 
            evalScripts:false, 
            method:'get', 
            onComplete:function(request){checkUserStatusCompleted(request, usernameEls)},
            parameters:params
        }); 
        //console.log(userNames);
    }
    
    function checkUserStatusCompleted(request, usernameEls) {
        if(request.status=="200") {
            var status = request.responseText.evalJSON();
            usernameEls.each(function(u) {
                //var username = u.name.toLowerCase();
                var username = '';
                Try.these(
                    function() {username = u.getAttribute("username");},
                    function() {username = u.name.toLowerCase();},
                    function() {username = u.title.toLowerCase();}               
                );
                var s = status[username];
                alert(s);
                doMarkUserStatus(u, username, s);
                //console.log(userName+":"+s);
            });
        }
    }
    function doMarkUserStatus(el, username, status) {
        //        try{
        //            var tempC = document.createElement("div");
        //            var link = document.createElement("a");
        //            link.title = username.capitalize()+"'s Profile";
        //            link.href = "/profile/"+username;
        //            link.className = "userStatusMark";
        //            var mark = document.createElement("img");
        //            var icon = status==1 ? "online.gif" : "offline.gif";
        //            mark.src = "/images/v2/"+icon;
        //            mark.style.border = "none";
        //            mark.alt = status==1 ? "online" : "offline";
        //            link.appendChild(mark);
        //            tempC.appendChild(link);
        //            new Insertion.Before(el, tempC.innerHTML);
        //        }catch(e) {
        //        console.log(e);
        //    }
        try{
            if(status==1) {
                el.style.background = "url(/images/online_status.gif) no-repeat scroll left -30px";
            }else {
            el.style.background = "url(/images/online_status.gif) no-repeat scroll left top";
        }
        el.style.paddingLeft = "11px";
        el.style.width = "37px";
    }catch(e) {}
}


/***************group*********************/
function doTopicSearch(group_name){
    var keyword = $F("dSearchKeyword").trim();
    url = GROUP_HOST+"/search_topics?group_name="+group_name+"&what="+keyword;
    self.location = url;
    return false;
}

/**
* Returns HTML code for "plain text"
*/
String.prototype.zm_toPlainHTML = function (maxLen) {
    if (maxLen == undefined) {
        maxLen = -1;
    }
    var s = this.gsub(/\r\n/, '\n');
    var s = s.gsub(/\t/, ' ');
    if (maxLen > -1) {
        s = s.substr(0, maxLen);
    }
    s = s.gsub(/\n/, '-@####@-');
    s = s.gsub(/ /, '-@#==#@-');
    s = s.escapeHTML();
    s = s.gsub('-@####@-', '<br/>');
    s = s.gsub('-@#==#@-', '&nbsp;');
    return s;
};

function aboutDiigoFourm(){
    var w = window.open(ABOUTDIIGOFORUM, null, "width=700,height=500, left=0, top=0, status=1,scrollbars=1,resizable=1");
}
