var vResp;
function displayMostBullDetail (divEnt, usrId, link, reasons, brief) {
    if (usrId) {
        var a = document.createElement('a');
        a.style.fontSize = '14px';
        a.style.fontWeight = 'bold';
        a.setAttribute('href', 'usr_profile.php?usrid='+usrId);
        a.appendChild(document.createTextNode(usrId));
        divEnt.appendChild(a);
    }
    
    if (link) {
        divEnt.appendChild(document.createTextNode('  my blog: '));
        var a = document.createElement('a');
        a.style.fontSize = '14px';
        a.setAttribute('href', 'http://'+link);

        var short_text = (link.length > 23) ?
            (link.substring(0, 23)+'...') : link;
        a.appendChild(document.createTextNode(short_text));
        divEnt.appendChild(a);
    }

    if (reasons) {
        var eachReason = reasons.split("^230^");
        var i=0, brief_reason;
        
        divEnt.appendChild(document.createElement('br'));
        while (eachReason[i]) {
            if (brief && (eachReason[i].length > 50)) {
                brief_reason = eachReason[i].substring(0, 50)+'...';
            } else {
                brief_reason = eachReason[i];
            }
            displayText(divEnt, brief_reason);
            i++;
        }
    }
}

function mostBullDetailResp (req, p_arr) {
    var divId = p_arr[1];
    var category = p_arr[2];
    var bullish, i=2, j=0;
    var divMb, div = document.getElementById(divId);
    
    vResp = req.responseText;
    var resp = vResp.split(DELIMITER);
    
    // alert(resp[0]+' '+resp[1]+' '+resp[2]);

    h2 = document.createElement('h3');
    h2.className = "center";

    var str = "Bull vs. Bear";
    h2.appendChild(document.createTextNode(str));
    div.appendChild(h2);

    bullBearHdr (div);
    displayMostBull(div, resp[i++], resp[i++], resp[i++],
                    resp[i++], resp[i++]);

    div.appendChild(document.createElement('hr'));

    divFullDesc = document.createElement('div');
    divFullDesc.className = 'divFullNoB';        
    divFullDesc.style.textAlign='left';
    divFullDesc.style.padding='2px';
    divFullDesc.onclick = function() {
        var fChild;
        while (fChild = divFullDesc.firstChild) {
            divFullDesc.removeChild(fChild);
        }
        divFullDesc.className = 'divFullNoB';        
        this.style.backgroundColor = 'white';
    }

    divFullDesc.onmouseover = function() {
        if (this.firstChild) {
            this.style.backgroundColor = '#f0f0f0';
        }
    }
    divFullDesc.onmouseout = function() {
        this.style.backgroundColor = 'white';
    }

    div.appendChild(divFullDesc);

    var divBull = document.createElement('div');
    divBull.className = 'divBull';
    var divBear = document.createElement('div');
    divBear.className = 'divBear';
    div.appendChild(divBull);
    div.appendChild(divBear);

    while (resp[i] && resp[i]=='USRID') {
        bullish = resp[i+2];

        divMb = document.createElement('div');
        divMb.className = 'divFull3D';
        divMb.id = j++;
        divMb.onmouseover = function() {
            if (this.parentNode) {
                this.style.backgroundColor = (this.parentNode.className == 'divBull') ?
                '#dff5bf' : '#efd1bf';
            }
        }
        divMb.onmouseout = function() {
            this.style.backgroundColor = 'white';
        }
        divMb.onclick = function() {
            var reparse = vResp.split(DELIMITER);
            var id = 7+(5*this.id);

            var fChild;
            while (fChild = divFullDesc.firstChild) {
                divFullDesc.removeChild(fChild);
            }
            divFullDesc.className = 'divFullNoB';        
            this.style.backgroundColor = 'white';

//            if (divFullDesc.firstChild) {
//                return;
//            }
            displayMostBullDetail(divFullDesc, reparse[id+1], reparse[id+3],
                                  reparse[id+4], 0);
            var p = document.createElement('p');
            p.appendChild(document.createTextNode('click to close'));
            p.style.fontStyle = 'italic';
            p.style.textAlign = 'right';
            divFullDesc.appendChild(p);
            divFullDesc.className = 'divFull3D'; 
        }
        if (bullish==1) {
            divBull.appendChild(divMb);
        } else {
            divBear.appendChild(divMb);
        }
        
        displayMostBullDetail(divMb, resp[i+1], resp[i+3], resp[i+4], 1);
        i +=5;
    }

    divFull = document.createElement('div');
    divFull.className = 'divFullNoB';
    div.appendChild(divFull);

    displayPages('mbPages', p_arr[0], resp[0], resp[1]);
}

function mostBullDetailReq (divId, sym, page) {
    var url = 'most_bull_handler.php';
    var url_param = '?sym='+sym+'&page='+page;    
    var p_arr;
    
    url += url_param;
    p_arr = new Array('mbd.php'+url_param, divId, sym);
    xmlHttpReq(xmlHttpObj(), url, true, null, mostBullDetailResp, p_arr);
}

function mostBullFor (divId, page) {
    var symObj = document.getElementById('key');
    if (!symObj) {
        return;
    }
    sym = symObj.value;
    window.location = 'http://'+getServerName()+'/mbd.php?sym='+sym;
}

function displayMostBull (div, symbol, num_bulls, num_bears, num_links,
                          num_reasons) {
    var i=0;
    var tbl = document.createElement('table');
    var row = tbl.insertRow(tbl.rows.length);
    var a;
    
    tbl.className = 'tfmtMb';
    div.appendChild(tbl);

    // bulls
    var cell = row.insertCell(i++);
    cell.rowSpan=2;
    cell.className = 'num';

    a = document.createElement('a');
    a.setAttribute('href', 'mb_vote.php?sym='+symbol+'&senti=1');
    a.appendChild(document.createTextNode(num_bulls));    
    cell.appendChild(a);
    cell.style.backgroundColor = '#dff5bf';
    cell.onmouseover = function() {
        this.style.backgroundColor = '#008000';
    }
    cell.onmouseout = function() {
        this.style.backgroundColor = '#dff5bf';
    }
    cell.onclick = function() {
        document.location.href='mb_vote.php?sym='+symbol+'&senti=1';
    }
    
    // symbol
    cell = row.insertCell(i++);
    cell.style.textAlign = 'center';
    cell.style.fontSize = '18px';
    cell.style.fontWeight = 'bold';
    cell.onmouseover = function() {
        this.style.backgroundColor = '#d0d0d0';
    }
    cell.onmouseout = function() {
        this.style.backgroundColor = 'white';
    }
    cell.onclick = function() {
        document.location.href='mbd.php?sym='+symbol;
    }
    a = document.createElement('a');
    a.setAttribute('href', 'mbd.php?sym='+symbol);
    a.appendChild(document.createTextNode(symbol));
    cell.appendChild(a);
    
    // bear
    cell = row.insertCell(i++);
    cell.rowSpan=2;
    cell.className = 'num';
    a = document.createElement('a');
    a.setAttribute('href', 'mb_vote.php?sym='+symbol+'&senti=2');
    a.appendChild(document.createTextNode(num_bears));    
    cell.appendChild(a);
    cell.style.backgroundColor = '#efd1bf';
    cell.onmouseover = function() {
        this.style.backgroundColor = '#b00000';
    }
    cell.onmouseout = function() {
        this.style.backgroundColor = '#efd1bf';
    }
    cell.onclick = function() {
        document.location.href='mb_vote.php?sym='+symbol+'&senti=2';
    }
    
    var row = tbl.insertRow(tbl.rows.length);
    // num_links
    i=0;
    cell = row.insertCell(i++);
    cell.style.textAlign = 'center';
    cell.onmouseover = function() {
        this.style.backgroundColor = '#d0d0d0';
    }
    cell.onmouseout = function() {
        this.style.backgroundColor = 'white';
    }
    cell.onclick = function() {
        document.location.href='mbd.php?sym='+symbol;
    }
    var a = document.createElement('a');
    a.setAttribute('href', 'mbd.php?sym='+symbol);
    a.appendChild(document.createTextNode(num_links+' links   '+
                                          num_reasons+' reasons'));
    cell.appendChild(a);    
}

function bullBearHdr (div) {
    var tbl = document.createElement('table');
    var row = tbl.insertRow(tbl.rows.length);
    var cell = row.insertCell(0);
    var img = document.createElement('img');
    img.src = "images/bull.jpg";
    cell.appendChild(img);
    cell.style.width="23%";
    cell = row.insertCell(1);
    cell.style.width="54%";
    cell = row.insertCell(2);
    cell.style.width="23%";
    img = document.createElement('img');
    img.src = "images/bear.jpg";
    cell.appendChild(img);
    tbl.className = 'tfmtMb';

    div.appendChild(tbl);
}

function mostBullResp (req, p_arr) {
    var divId = p_arr[1];
    var category = p_arr[2];
    var resp = req.responseText.split(DELIMITER);
    var i=2;
    var div = document.getElementById(divId);
    
    // alert(resp[0]+' '+resp[1]+' '+resp[2]);

    bullBearHdr(div);
    
    while (resp[i]) {
        displayMostBull(div, resp[i++], resp[i++], resp[i++],
                        resp[i++], resp[i++]);
    }

    displayPages('mbPages', p_arr[0], resp[0], resp[1]);
}

function mostBullReq (divId, category, page) {
    var url = 'most_bull_handler.php';
    var url_param = '?c='+category+'&page='+page;    
    var p_arr;
    
    url += url_param;
    p_arr = new Array('mb.php'+url_param, divId, category);
    xmlHttpReq(xmlHttpObj(), url, true, null, mostBullResp, p_arr);
}

function chkMostBull () {
    var symObj = document.getElementById("symbol");
    if (!symObj.value) {
        alert('Symbol is missing');
        symObj.focus();
        return false;
    }
    var sentObj = document.getElementById("mb");

    if ((sentObj.value !=1) && (sentObj.value !=2)){
        alert('Bullish or Bearish? Sentiment is missing');
        sentObj.focus();
        return false;
    }

    return true;
}