﻿var entryData;
var message;
var jsTabArray;
var censur = false;
var siteID;

function handleShowUser(result){
    setEntryList( eval( '(' + result.responseText + ')' ) );
}
function handleShowRelated(result){
    setEntryList( eval( '(' + result.responseText + ')' ) );
}
function showUser(id){
    new Ajax.Request('/section/media/MediaAjax.ashx?m=getuserentries&userID=' + entryData.userID + '&count=' + entryData.count + '&type=' + entryData.mediaType, {onSuccess: handleShowUser });
    EntryTabSelect(jsTabArray, id);
}
function showFavorites(id){
    new Ajax.Request('/section/media/MediaAjax.ashx?m=getfavoritelist&id=' + entryData.id + '&count=' + entryData.favoriteCount, {onSuccess: handleFavoriteList});
    EntryTabSelect(jsTabArray, id);
}
function showRelated(id){
    new Ajax.Request('/section/media/MediaAjax.ashx?m=getrelatedentries&id=' + entryData.id + '&count=' + entryData.count + '&type=' + entryData.mediaType + '&keywords=' + entryData.tags + '&cat=' + entryData.categoryID, {onSuccess: handleShowRelated});
    EntryTabSelect(jsTabArray, id);
}
function handleFavoriteList(result){
    var list = eval( '(' + result.responseText + ')' )
    if(list.length == 0){
        var noEntriesDiv = document.createElement("div");
        noEntriesDiv.className = 'noEntries';
        noEntriesDiv.appendChild(document.createTextNode(message.noFavoritesFound));
        removeChilds('entriesContainer');
        getE('entriesContainer').appendChild(noEntriesDiv);
    }
    else{
        var entryTable = document.createElement("table");
        entryTable.setAttribute("cellspacing", "10")
        var entryTableBody = document.createElement("tbody");
        for(var i = 0; i < list.length; i++){    
            var entryObj = list[i];
            var entryRow = document.createElement("tr");
            
            var imageCell = document.createElement("td");
            var imageLink = document.createElement("a");
            imageLink.setAttribute("href", '/section/user/profile/?id=' + entryObj.UserID + '&type=4')
            var thumb = new Image();
            if(entryObj.PictureApproved && entryObj.HasImage){
                thumb.src = entryData.userImagePath + entryObj.PictureID.substr(0,3) + "/" + entryObj.PictureID + ".jpg?updated" + escape(entryObj.PictureUpdatedTime);
            }
            else{
                thumb.src = '/gfx/design/user/user_nopicture_48_' + siteID + '.gif';
            }
            thumb.style.width = entryData.userImageWidth + 'px';
            thumb.style.height = entryData.userImageHeight + 'px';
            thumb.style.borderWidth = '0px';
            imageLink.appendChild(thumb);
            imageCell.appendChild(imageLink);
            entryRow.appendChild(imageCell);
            
            var nameCell = document.createElement("td");
            var nameLink = document.createElement("a");
            nameLink.setAttribute("href", '/section/user/profile/?id=' + entryObj.UserID + '&type=4')
            nameLink.appendChild(document.createTextNode(entryObj.Username));
            nameCell.appendChild(nameLink);
            entryRow.appendChild(nameCell);
            
            entryTableBody.appendChild(entryRow);
        }
        entryTable.appendChild(entryTableBody);
    
        removeChilds('entriesContainer');
        
        getE('entriesContainer').appendChild(entryTable);
    }
}
function setEntryList(list){
    if(list.length == 0){
        setNoEntries();
        return;
    }
    var entryTable = document.createElement("table");
    entryTable.setAttribute("cellspacing", "0")
    var entryTableBody = document.createElement("tbody");
    for(var i = 0; i < list.length; i++){
        var entryObj = list[i];
        var entryRow = document.createElement("tr");

        var imageCell = document.createElement("td");
        imageCell.style.paddingRight = '10px';
        //imageCell.style.paddingBottom = '10px';
        
        var thumbDiv = document.createElement('div');
        thumbDiv.style.width = entryData.thumbWidth + 'px';
        
        var posDiv = document.createElement('div');
        posDiv.style.position = 'absolute';
        posDiv.style.zIndex = '1';
        
        var imageLink = document.createElement("a");
        imageLink.setAttribute("title", entryObj.Title)
        if(entryData.mediaType == entryData.photoMediaType){
            imageLink.setAttribute("href", '/' + entryData.linkType + '/' + entryObj.SeriesID + '/' + entryObj.ID);
        }
        else{
            imageLink.setAttribute("href", '/' + entryData.linkType + '/' + entryObj.ID);
        }
        
        var spacerThumb = new Image();
        spacerThumb.src = '/gfx/spacer.gif';
        spacerThumb.style.width = entryData.thumbWidth+'px';
        spacerThumb.style.height = entryData.thumbHeight+'px';
        spacerThumb.style.borderWidth = '0px';
        if(entryData.mediaType != entryData.photoMediaType){
            spacerThumb.className = 'videoThumb';
        }        
        imageLink.appendChild(spacerThumb);
        posDiv.appendChild(imageLink);
        thumbDiv.appendChild(posDiv);
        
        var thumb = new Image();
        if(censur){
          thumb.src = '/gfx/fakeImages/fixedthumbs.jpg';
        }
        else{
          thumb.src = entryData.thumbPath + entryObj.FileID.substr(0,3) + '/' + entryObj.FileID + '.jpg?updated' + escape(entryObj.CreatedDate);
        }
        thumb.style.width = entryData.thumbWidth+'px';
        thumb.style.height = entryData.thumbHeight+'px';
        thumbDiv.appendChild(thumb);
        
        var bottomDiv = document.createElement('div');
        bottomDiv.style.width = entryData.thumbWidth + 'px';
        bottomDiv.innerHTML = '&nbsp;';
        if(entryData.mediaType == entryData.photoMediaType) {
          bottomDiv.className = 'controlEntryTitlePhotoBG';
        }
        else{
          bottomDiv.className = 'controlEntryTitleVideoBG';
        }
        thumbDiv.appendChild(bottomDiv);
        
        imageCell.appendChild(thumbDiv);
        entryRow.appendChild(imageCell);
        
        var descriptionCell = document.createElement("td");
        descriptionCell.setAttribute("valign", "top");
        var cellDiv = document.createElement("div");
        cellDiv.style.verticalAlign = 'top';
        cellDiv.style.height = entryData.thumbHeight + 'px';
        
        var titleDiv = document.createElement("div");
        titleDiv.className = 'title';
        titleDiv.style.width = (289 - entryData.thumbWidth) + 'px';
        titleDiv.style.overflow = 'hidden';
        var titleLink = document.createElement("a");
        titleLink.setAttribute("title", entryObj.Title)
        if(entryData.mediaType == entryData.photoMediaType){
            titleLink.setAttribute("href", '/' + entryData.linkType + '/' + entryObj.SeriesID + '/' + entryObj.ID);
        }
        else{
            titleLink.setAttribute("href", '/' + entryData.linkType + '/' + entryObj.ID);
        }
        var title = document.createTextNode(entryObj.Title);
        titleLink.appendChild(title);
        titleDiv.appendChild(titleLink);
        cellDiv.appendChild(titleDiv);
        //descriptionCell.appendChild(titleDiv);
        
        var description = document.createElement("div");
        description.className = 'description';
        description.innerHTML = entryObj.Description;
        cellDiv.appendChild(description);
        //descriptionCell.appendChild(description);
        var timestamp = document.createElement("div");
        timestamp.className = 'timestamp';
        timestamp.appendChild(document.createTextNode(entryObj.CreatedDate));
        cellDiv.appendChild(timestamp);
        descriptionCell.appendChild(cellDiv);
        entryRow.appendChild(descriptionCell);        
        
        entryTableBody.appendChild(entryRow);
    }    
    entryTable.appendChild(entryTableBody);
    
    removeChilds('entriesContainer');
    
    getE('entriesContainer').appendChild(entryTable);
}
function removeChilds(id){
    while (getE(id).firstChild) {
      getE(id).removeChild(getE(id).firstChild);
    }
}
function setNoEntries(){
    var noEntriesDiv = document.createElement("div");
    noEntriesDiv.className = 'noEntries';
    noEntriesDiv.appendChild(document.createTextNode(entryData.noEntries));
    removeChilds('entriesContainer');
    getE('entriesContainer').appendChild(noEntriesDiv);
}