function clearinp(i, str){
    if (i.value == str)
       i.value = '';
}
function fillinp(i, str){
   if (i.value == '')
       i.value = str;
}
function setChecked(fieldname, val){
        radioObj = document.forms['f1'].elements[fieldname];
        if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = !radioObj.checked;
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == val.toString()) {
			radioObj[i].checked = true;
		}
	}
}
// tabs 
window.onload=function(){
    initTabs();
    setInterval("reposPanel()", 100);
    updateNewComLink(true);

    var url = document.location.href;
    var st = url.indexOf('#c');
    if (st>0){
        var lid = url.substr(st+2);
        moveto = document.getElementById('li'+lid);
        if (moveto){
            pos = findPos(moveto);
            self.scrollTo(0, pos[1]-100);
        }
    } else {
        st = url.indexOf('#m');
        if (st>0){
            lid = url.substr(st+2);
            moveto = document.getElementById('m'+lid);
            if (moveto){
                pos = findPos(moveto);
                self.scrollTo(0, pos[1]-100);
            }
        }
    }
}
function reposPanel(){
    panel = document.getElementById('moving-bar');
    bph = document.getElementById('bar-place-holder');
    pos = findPos(bph);
    scrl = getScrollXY();
    if (pos[1]-scrl[1]<0){
        if (!isIE6()){
            panel.style.position = 'fixed';
            panel.style.top = "0px";
        } else {
            panel.style.position = 'absolute';
            panel.style.top = scrl[1]+"px";
        }
        panel.style.left = pos[0]+"px";
        panel.style.borderBottomColor = '#000000';
        panel.style.borderBottomStyle = 'solid';
        panel.style.borderBottomWidth = '1px';
    } else {
        panel.style.borderBottomStyle = 'none';
        panel.style.position = 'static';
    }

    //panel.innerHTML = pos[1]-scrl[1];

}
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft,curtop];
}
function isIE6(){
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
         var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
         if (ieversion>=7)
             return false;
         if (ieversion>=6)
            return true;
         if (ieversion>=5)
            return true;
    }
    return false;
}
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}
function initTabs(){
    for (var t in tabs){
        // hide inactive tabs
        if (tabs[t] != current_tab){
            tabObj = document.getElementById(tabs[t]+'_div2');
            tabObj.style.display = 'none';
        }
    }
    // set active tab image
    tabImg = document.getElementById(current_tab+'_tab');
    tabImg.style.backgroundPosition = '0px -25px';

}
function showTab(tabId){
    self.scrollTo(0, 0);
    if (tabId == 'comments'){
        setTimeout("document.f1.comment.focus()", 100);
    }
    if (tabId == current_tab)
        return;
    tabObj = document.getElementById(tabId+'_div2');
    tabObj.style.display = '';
    oldTabImg = document.getElementById(current_tab+'_tab');
    oldTabImg.style.backgroundPosition = '0px 0px';

    curTabObj = document.getElementById(current_tab+'_div2');
    curTabObj.style.display = 'none';
    current_tab = tabId;

    tabImg = document.getElementById(tabId+'_tab');
    tabImg.style.backgroundPosition = '0px -25px';

    siObj = document.getElementById('twsignin');
    if (siObj)
        siObj.innerHTML = '<a href="/twsignin.x?'+escape('/'+pollids+'/'+tabId.charAt(0))+'" title="Sign in with Twitter" class="toplinks">Sign in with Twitter</a>';
    soObj = document.getElementById('signout');
    if (soObj)
        soObj.innerHTML = '<a href="/signout.x?'+escape('/'+pollids+'/'+tabId.charAt(0))+'">Sign out</a>';

}
function toggleOther(){
    oth = document.getElementById('other-list-div');
    vol = document.getElementById('viewotherslink');
    if (!oth) return;
    if (oth.style.display != 'none'){
        oth.style.display = 'none';
        //vol.innerHTML = "<a href='javascript:toggleOther();'>show &darr;</a>";
    } else {
        oth.style.display = '';
        //vol.innerHTML = "<a href='javascript:toggleOther();'>hide &uarr;</a>";
    }
    
}
function reply(to, id, tid){
    showTab('comments');
    inrep = document.getElementById('inreply');
    inrep.innerHTML = 'replying to <a href="#c'+id+'">'+to+'</a> (<a href="javascript:undoreply();">undo</a>)';
    inrep.style.display='';
    document.f1.replyto.value = id;
    document.f1.tid.value = tid;
    setTimeout("document.f1.comment.focus()", 100);
}
function undoreply(){
    inrep = document.getElementById('inreply');
    inrep.innerHTML = '';
    inrep.style.display='none';
    document.f1.replyto.value = '';
}
function editEmail(){
    document.getElementById('emailfield').style.display='';
    document.getElementById('emailcb').style.display='none';
}
function getAjaxData(url, callback){
    var xmlHttp;
    try{
        xmlHttp=new XMLHttpRequest();
    }
    catch (e){
        // Internet Explorer
        try {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=function(){
        if(xmlHttp.readyState==4){
            callback(xmlHttp.responseText);
        }
    }
    xmlHttp.send(null);
}
function refreshComments(){
    for (var i=0; i<lastnewcoms.length; i++){
        li = document.getElementById('li'+lastnewcoms[i]);
        cl = li.getAttribute('class');
        if (!cl)
            cl = li.getAttribute('className'); // fcuking ie
        cl = cl.replace(' com-new', '');
        li.setAttribute('className', cl);
        li.setAttribute('class', cl);
    }
    document.getElementById("new-comments").innerHTML = 'loading...';
    getAjaxData("/newcomments.x?"+last_comment_id+","+pollid, commentsArrived);
}
function commentsArrived(data){
    var datas=data.split("\n\n");
    newcoms = [];
    lastnewcoms = [];
    for (var i=0; i<datas.length-1; i+=2){
        var info = datas[i].split(",");
        var newLi = document.createElement("div");
        newLi.innerHTML = datas[i+1];
        newLi.setAttribute('class', info[2]);
        newLi.setAttribute('className', info[2]);
        newLi.setAttribute('id', 'li'+info[0]);
        ul = document.getElementById("comments");
        if (info[1] == 0){
            ul.appendChild(newLi);
        }else{
            if (!compar[info[1]]){
                compar[info[1]] = new Array();
                prev = document.getElementById("li"+info[1]);
            } else {
                last = findLatestComment(info[1]);
                prev = document.getElementById("li"+last);
            }
            ul.insertBefore(newLi, prev.nextSibling);
            compar[info[1]].push(info[0]);
        }
        newcoms.push(info[0]);
        lastnewcoms.push(info[0]);
    }
    if (newcoms.length>0)
        last_comment_id = newcoms[newcoms.length-1];
    comcnt += newcoms.length;
    updateNewComLink();
    //lastnewcoms = newcoms;
    //document.getElementById("comments-count").innerHTML = comcnt+' comment'+(comcnt>1 ? "s" : "")+':';
}
function findLatestComment(id){
    if (!compar[id]){
        return id;
    } else {
        return findLatestComment(compar[id][compar[id].length-1]);
    }
}
function nextNew(){
    ncid = newcoms.shift();
    updateNewComLink(true);
    moveto = document.getElementById('li'+ncid);
    pos = findPos(moveto);
    //self.scrollTo(0, pos[1]-40);    

    slowScroll(pos[1]-50, 6);
}
function updateNewComLink(onload){
    var cnt = newcoms.length;
    var txt = '';
    if (comcnt>0)
        txt = comcnt+' comment'+(comcnt>1 ? "s" : "");
    else
        txt = '&nbsp;';
    if (cnt > 0)
        txt += " <a href='javascript:nextNew();' title='next new comment'>("+cnt+" new comment"+(cnt>1 ? "s" : "")+")</a>";
    else if(!onload)
        txt += " (no new comments)";
    document.getElementById("new-comments").innerHTML = txt;
}
function slowScroll(to, steps){
    scrl = getScrollXY();
    //alert("to:"+to+", step:"+parseInt((to-scrl[1])/steps));
    doSlowScroll(to, parseInt((to-scrl[1])/steps), steps);
}
function doSlowScroll(to, add, stepsLeft){
    if (stepsLeft>0){
        self.scrollBy(0, add);
        setTimeout("doSlowScroll("+to+", "+add+", "+(stepsLeft-1)+")", 50);
    } else {
        self.scrollTo(0, to);
    }
}
function comVote(cid, vote){
    getAjaxData("comvote.x?"+cid+","+vote, comVoted);
    document.getElementById("cv"+cid).innerHTML = 'voted';
}
function comVoted(data){
    
}