//cluster.js

//global variable to hold crushes displayed on the page
var objCrushes=[];

//crushes beyond the first five are displayed in a paginated, smaller format
//they are updated by a interval that runs continously, checking to see if further updates are required
var smallInterval=null;
var smallOffset=5; //initial offset: start at the sixth crush; increments by 50 per page being advanced
var smallCounter=0; //which elements past the 5th crush are we currently working on

//FUTURE add "next" button that advances the small offset and wipes out existing text in name-? etc.


//refresh the account data
function refreshAccount() {
    var e=$("account-name");
    while (e.firstChild)
        e.removeChild(e.firstChild);
    e.appendChild(document.createTextNode(possessive(account)+" cluster"));

    $("account-link-crushes").href="/crushes/"+encodeURIComponent(account);
    $("account-link-crushes").title=possessive(account)+" crushes";

    $("account-link-twitter").href="http://twitter.com/"+encodeURIComponent(account);
    $("account-link-twitter").title=possessive(account)+" tweets";

    var divImage=$("account-image");
    while (divImage.firstChild)
        divImage.removeChild(divImage.firstChild);

    var e=$("cluster-score");
    while (e.firstChild)
        e.removeChild(e.firstChild);

    e.appendChild(document.createTextNode("Nobody has declared a crush on "+account+" yet"));


    //reload profile image
    var req=new Ajax.Request('/do/profile?id='+encodeURIComponent(account), {
        method: 'get',
        asynchronous: true,
        onSuccess: function(transport) {
            refreshAccountImage(transport.responseXML);
        },
        onFailure: function() {
            //do nothing; the image will simply not be refreshed
        }
    });
} //refreshAccount


function refreshAccountImage(xml) {
    var objProfiles=parseProfiles(xml);

    if (objProfiles.length!=1)
        return;

    var objProfile=objProfiles[0];

    var imageUrl=objProfile.imageUrl;

    var e=document.createElement("img");
    e.src=imageUrl;
    e.alt=objProfile.id;

    var divImage=$("account-image");
    divImage.appendChild(e);
} //refreshAccountImage


//reloads the crushes that make up the cluster from the backend
//on a successful return, refreshes the display
function reloadCluster() {
    var req=new Ajax.Request('/do/cluster?do=get&id='+encodeURIComponent(account), {
        method: 'get',
        asynchronous: true,
        onSuccess: function(transport) {
            objCrushes=parseCrushes(transport.responseXML);

            refreshCluster();
        },
        onFailure: function() {
            //do nothing; there simply won't be any crushes
        }
    });
} //reloadCluster


//refresh the information displayed in the UI
//erases the contents of the existing fields and populates them with the current data
function refreshCluster() {
    if (smallInterval!=null)
        window.clearInterval(smallInterval);

    //sort: most recent crush first
    objCrushes.sort(function(a,b) {
        if (a.ts*1<b.ts*1)
            return 1;
        else if (a.ts*1>b.ts*1)
            return -1;
        else
            return 0;
    });

    var clusterScore=0;

    for (var a=0;a<objCrushes.length&&a<5;a++) {
        var objCrush=objCrushes[a];

        var e=$("image-"+a);
        while (e.firstChild)
            e.removeChild(e.firstChild);

        var e=$("name-"+a);
        while (e.firstChild)
            e.removeChild(e.firstChild);
        e.appendChild(document.createTextNode(objCrush.from));

        $("link-"+a+"-crushes").href="/crushes/"+encodeURIComponent(objCrush.from);
        $("link-"+a+"-crushes").title=possessive(objCrush.from)+" crushes";

        $("link-"+a+"-cluster").href="/cluster/"+encodeURIComponent(objCrush.from);
        $("link-"+a+"-cluster").title=possessive(objCrush.from)+" cluster";

        $("link-"+a+"-twitter").href="http://twitter.com/"+encodeURIComponent(objCrush.from);
        $("link-"+a+"-twitter").title=possessive(objCrush.from)+" tweets";

        var e=$("msg-"+a);
        while (e.firstChild)
            e.removeChild(e.firstChild);
        e.appendChild(document.createTextNode(objCrush.msg));
        e.title=objCrush.msg;

        $("cluster-item-"+a).style.display="list-item";
    } //objCrushes


    //update cluster score
    var e=$("cluster-score");
    while (e.firstChild)
        e.removeChild(e.firstChild);

    e.appendChild(document.createTextNode("The following "+objCrushes.length+" tweeple have declared a crush on "+account+":"));


    //ask for profile data
    var idParams="";

    for (var a=0;a<objCrushes.length&&a<5;a++) {
        var objCrush=objCrushes[a];

        if (objCrush.from!="") {
            if (idParams!="")
                idParams+="&";
            idParams+="id="+encodeURIComponent(objCrush.from);
        } //id
    } //objCrushes

    var req=new Ajax.Request('/do/profile?'+idParams, {
        method: 'get',
        asynchronous: true,
        onSuccess: function(transport) {
            refreshCrushImages(transport.responseXML);
        },
        onFailure: function() {
            //do nothing; there simply won't be any images
        }
    });


    //start the interval that will fill in the smaller crush areas
    if (objCrushes.length>5) {
        $("cluster-small").style.visibility="visible";

        window.setTimeout(function() {
            smallInterval=window.setInterval(refreshSmallCluster,100);
        },1000);
    } else {
        $("cluster-small").style.visibility="hidden";
    } //objCrushes.length
} //refreshCluster


function refreshCrushImages(xml) {
    var objProfiles=parseProfiles(xml);


    for (var a=0;a<objProfiles.length;a++) {
        var objProfile=objProfiles[a];

        //update all profiles that match the accountId
        for (var b=0;b<objCrushes.length&&b<5;b++) {
            var objCrush=objCrushes[b];

            if (objCrush.from.toLowerCase()==objProfile.id.toLowerCase()) {
                //found a matching profile

                var divImage=$("image-"+b);

                while (divImage.firstChild)
                    divImage.removeChild(divImage.firstChild);

                var imageUrl=objProfile.imageUrl;
                imageUrl=imageUrl.replace(/_normal\./,"_bigger.");

                var e=document.createElement("img");
                e.src=imageUrl;
                e.alt=objProfile.id;

                divImage.appendChild(e);
            } //id match
        } //objCrushes
    } //objProfiles
} //refreshCrushImages


//function called by an interval timer that runs to fill in crush information past the first 5
//automatically creates additional DOM elements in the small list if there aren't sufficient entries present
//shows up to 50 entries at a time
function refreshSmallCluster() {
    if (smallOffset+smallCounter<objCrushes.length&&smallOffset+smallCounter<50) {
        //we have more crushes to display
        var idxCrush=smallOffset+smallCounter;
        var objCrush=objCrushes[idxCrush];


        //test if UI has already been created
        var e=$("name-"+idxCrush);
        if (e==null) {
            //need to create the UI
            var liCrush=document.createElement("li");

            var eCrush=document.createElement("div");
            eCrush.className="crush";


            //name
            var e=document.createElement("div");
            e.className="name";
            e.id="name-"+idxCrush;

            eCrush.appendChild(e);


            //links
            var e=document.createElement("div");
            e.className="links";
            e.id="links-"+idxCrush;

            //cluster
            var a=document.createElement("a");
            a.href="#";
            a.id="link-"+idxCrush+"-cluster";

            var i=document.createElement("img");
            i.src="/media/b-cluster.png";
            i.alt="Cluster";

            a.appendChild(i);
            e.appendChild(a);

            //crushes
            var a=document.createElement("a");
            a.href="#";
            a.id="link-"+idxCrush+"-crushes";

            var i=document.createElement("img");
            i.src="/media/b-crushes.png";
            i.alt="Crushes";

            a.appendChild(i);
            e.appendChild(a);

            //twitter
            var a=document.createElement("a");
            a.href="#";
            a.id="link-"+idxCrush+"-twitter";

            var i=document.createElement("img");
            i.src="/media/b-twitter.png";
            i.alt="Twitter";

            a.appendChild(i);
            e.appendChild(a);

            eCrush.appendChild(e);


            //msg
            var e=document.createElement("div");
            e.className="msg";
            e.id="msg-"+idxCrush;

            eCrush.appendChild(e);


            liCrush.appendChild(eCrush);


            $("cluster-small-list").appendChild(liCrush);
        } //e==null


        //ui should be there; update with proper info
        var e=$("name-"+idxCrush);
        while (e.firstChild)
            e.removeChild(e.firstChild);

        e.appendChild(document.createTextNode(objCrush.from));


        var e=$("msg-"+idxCrush);
        while (e.firstChild)
            e.removeChild(e.firstChild);

        e.appendChild(document.createTextNode(objCrush.msg));
        e.title=objCrush.msg;


        var e=$("link-"+idxCrush+"-crushes");
        e.href="/crushes/"+encodeURIComponent(objCrush.from);
        e.title=possessive(objCrush.from)+" crushes";

        var e=$("link-"+idxCrush+"-cluster");
        e.href="/cluster/"+encodeURIComponent(objCrush.from);
        e.title=possessive(objCrush.from)+" cluster";

        var e=$("link-"+idxCrush+"-twitter");
        e.href="http://twitter.com/"+encodeURIComponent(objCrush.from);
        e.title=possessive(objCrush.from)+" tweets";


        smallCounter++;
    } //more to go?
} //refreshSmallCluster

//end
