//Global definitions
document.favLineOffsetX = 0;
document.favLineOffsetY = -5;
document.favCellHeight = 25;
document.favCellWidth = 75;

//IE includes the border in the width and height calculation
if (!document.all) {
    document.favCellHeight-=2;
    document.favCellWidth-=2;
}


function displayCells(msgResponse) {
    custCells = msgResponse.split ('#|#');
    for (custCell=0; custCell<custCells.length; custCell++) {
        CellInfo = custCells[custCell].split ('*|*');
        document.costumFavLineInst.setCellInfo (CellInfo);
    }
}


// FavCell object definition
//--------------------------------------------
function favCell (cellLink,favIcon,rowIndex,cellIndex) {

    //Constructing cell properties
    this.favCellObj = document.createElement("li");
    this.favCellObj.favIcon = document.createElement("img");
    divText = document.createElement("div");
    newText = document.createTextNode("");
    this.favCellObj.appendChild(divText);
    divText.appendChild(newText);
    this.favCellObj.appendChild(this.favCellObj.favIcon);
    this.favCellObj.rowIndex = rowIndex;
    this.favCellObj.cellIndex = cellIndex;

    //configure cell objects
    this.favCellObj.className = "favCell";
    if (!favIcon||favIcon=="") favIcon="/gui/holder.gif";
    if (!cellLink||cellLink=="")
    {
        cellLink="";
        this.favCellObj.vacant = true;
        this.favCellObj.favIcon.className = "DBIcon";
        this.favCellObj.favIcon.style.display = "none";
        this.favCellObj.title = "";
        this.favCellObj.style.cursor = "default";
    }
    else
    {
        this.favCellObj.vacant = false;
        this.favCellObj.favIcon.className = "favIcon";
        this.favCellObj.title = cellLink;
        this.favCellObj.style.cursor = "pointer";
    }


    this.favCellObj.cellLink = cellLink;
    this.favCellObj.cellText = newText;
    this.favCellObj.style.left = document.favLineOffsetX;
    this.favCellObj.style.top = document.favLineOffsetY;



    this.favCellObj.style.height = document.favCellHeight+'px';
    this.favCellObj.style.width = document.favCellWidth+'px';
    this.favCellObj.favIcon.src = favIcon;
    divText.className = "cellText";

   

    this.favCellObj.onclick = function () {
        if (this.cellLink!=''){
			pageTracker._trackPageview('/'+username+'/amflinks/mini3/'+this.cellLink);
			window.open(this.cellLink,"_blank");
		}
    }


}

//FavLine Object definition
//FavLine is a collection of favCell objects
//-----------------------------------------------------
function favLine (rowIndex) {
    this.favLineObj = document.createElement("ul");
    this.favLineObj.className = "favLine";
    this.favLineObj.rowIndex = rowIndex;
}

favLine.prototype.addCell = function (cellObj) {
    this.favLineObj.appendChild (cellObj.favCellObj);
}

favLine.prototype.addCellArr = function (linkArr,iconArr) {
    for (cellIndx=0; cellIndx<linkArr.length; cellIndx++) {
        favCellInst = new favCell (linkArr[cellIndx],iconArr[cellIndx],this.favLineObj.rowIndex,cellIndx);
        this.addCell(favCellInst);
    }
}


function customFav (ObjWrapper,MaxLines,CellsInLine) {
    this.favTbl = document.createElement("table");
    this.favTblHeader = document.createElement("thead");
    this.favTblBody = document.createElement("tbody");
    this.favTbl.className = "favTbl";
    ObjWrapper.appendChild (this.favTbl);
    this.favTbl.appendChild (this.favTblHeader);
    this.favTbl.appendChild (this.favTblBody);
    this.favTblBody.CellsInLine = CellsInLine;
    //Creating the custom fav header line
    trObj = document.createElement("tr");
    thObj = document.createElement("th");

    this.favTblHeader.appendChild (trObj);
    trObj.appendChild (thObj);


    this.favTblBody.MaxLines = MaxLines;
    this.CellsInLine = CellsInLine;


    this.favTblBody.addLine = function (LinkArr,IconArr,botAddition) {
        if (this.childNodes.length>=this.MaxLines) return;
        favLineInst = new favLine (this.childNodes.length);
        trObj = document.createElement("tr");
        tdObj = document.createElement("td");

        this.appendChild (trObj);
        trObj.appendChild (tdObj);
        tdObj.appendChild (favLineInst.favLineObj);
        favLineInst.addCellArr(LinkArr, IconArr);
        if (!botAddition) sendMSG ('/cellServices.php','actionType=addLine&rowIndx='+URLEncode(this.childNodes.length-1),'getNLCellInfo');
    }

    this.favTblBody.addNewLine = function (botAddition) {
        var LinkArr = new Array();
        var IconArr = new Array();
        for (cellIndx=0; cellIndx<this.CellsInLine; cellIndx++) {
            LinkArr[cellIndx] = "";
            IconArr[cellIndx] = "";
        }
        this.addLine (LinkArr,IconArr,botAddition);
    }

    customFav.prototype.setCellInfo = function (CellInfo) {
        if (!CellInfo) return;
        if (CellInfo=='') return;
        lineCollection = this.favTblBody.childNodes;
        if (lineCollection.length<=CellInfo[0]) {
            this.favTblBody.addNewLine(true);
        }
        if (CellInfo[1]>=this.favTblBody.CellsInLine) return;
        currRow = this.favTblBody.rows[CellInfo[0]];
        currCellWrapper = currRow.cells[0].firstChild;
        cellsCollection = currCellWrapper.childNodes;
        currCell = cellsCollection [CellInfo[1]];
        if (CellInfo[4]!='') {
            currCell.cellLink = CellInfo[3];
            currCell.vacant = false;
            currCell.title = CellInfo[3];
            currCell.favIcon.src = CellInfo[4];
            currCell.style.cursor = "pointer";
            currCell.favIcon.style.display = "block";
            if (CellInfo[2]=='favicon')
                currCell.favIcon.className="favIcon";
            else
            {
                currCell.favIcon.className="DBIcon";
                currCell.className="favLogoCell";
            }
        }
    }

}

//This function construct the fav line collection upon the loading of the document
function constructFavArr (MaxLines,CellsInLine) {
    costumFavLineInst = new customFav (document.getElementById("favLineWrapper"),MaxLines,CellsInLine);
    coverImgObj = document.getElementById("coverImg");
    if (coverImgObj) coverImgObj.style.display = "block";
    document.costumFavLineInst = costumFavLineInst;
    cellsInfo = document.getElementById("cellInfo");
    if (cellsInfo) displayCells(cellsInfo.value);
}




