var list = null;

function initList(){
    list = {
        edit: false,
        listType: '',
        value: '',
        orderBy: 'date',
        orderByDirection: 'DESC',
        page: 0,
        category: -1,
        language: '-1',
        languageForced: false,
        user: -1,
        pages: 0,
        totalResults: 0,
        hasNext: false,
        hasPrevious: false,
        keys: []
    };
}

function isCurrentlyEditing(){
    try{
        if(keyBusy){
            for(key in keyBusy){
                if(keyBusy[key]){
                    return true;
                }
            }
        }
    }catch(err){
    }
    return false;
}

function filterCategory(index){
    if(isCurrentlyEditing())
        return;
    list.category = $("categoryFilter").options[index].value;
    list.page = 0;
    showList();
}

function filterUser(index){
    if(isCurrentlyEditing())
        return;
    list.user = $("userFilter").options[index].value;
    list.page = 0;
    showList();
}

function filterLang(index){
    if(isCurrentlyEditing())
        return;
    list.language = $("langFilter").options[index].value;
    list.page = 0;
    showList();
}

function updateListFrame(){
    $("listLoading").style.display = "none";
    $("listEmpty").style.display = list.totalResults==0?"block":"none";
    $("listItems").style.display = list.totalResults==0?"none":"block";
    $("list_currentPage").innerHTML = list.totalResults==0?"0":list.page+1;
    $("list_pages").innerHTML = list.pages;
    $("list_currentPage2").innerHTML = list.totalResults==0?"0":list.page+1;
    $("list_pages2").innerHTML = list.pages;
    
    $("dateASC").style.display = list.orderBy=="date"&&list.orderByDirection=="ASC"?"inline":"none";
    $("dateDESC").style.display = list.orderBy=="date"&&list.orderByDirection=="DESC"?"inline":"none";
    $("titleASC").style.display = list.orderBy=="title"&&list.orderByDirection=="ASC"?"inline":"none";
    $("titleDESC").style.display = list.orderBy=="title"&&list.orderByDirection=="DESC"?"inline":"none";
    
    $("list_footer").style.display = list.pages>1?"block":"none";


    $("listPaginationTop").style.display = list.pages>1?"block":"none";

    
    
    if($("categoryFilter")){
        $("categoryFilter").length = 1;
        for(var i=0;i<list.categories.length;i++){
            $("categoryFilter").options[i+1] = new Option(list.categories[i].name, list.categories[i].categoryId);
            if(list.category==list.categories[i].categoryId)
                $("categoryFilter").selectedIndex = i+1;
        }
    }
    
    if($("userFilter")){
        $("userFilter").length = 1;
        for(var i=0;i<list.users.length;i++){
            $("userFilter").options[i+1] = new Option(list.users[i].lastname+", "+list.users[i].firstname, list.users[i].userId);
            if(list.user==list.users[i].userId)
                $("userFilter").selectedIndex = i+1;
        }
    }

    if($("langFilter")){
        $("langFilter").length = 1;
        for(var i=0;i<list.languages.length;i++){
            $("langFilter").options[i+1] = new Option(list.languages[i].displayLanguage, list.languages[i].language);
            if(list.language==list.languages[i].language)
                $("langFilter").selectedIndex = i+1;
        }
    }

    //making header to use 1 or 2 lines..
    if($("list_header1")!=null){
        $("list_header0").style.display = "none";
        if(($("list_header1").getDimensions().width+$("list_header2").getDimensions().width)<$("list_header").getDimensions().width){
            $("list_header1").style.position = "";
            $("list_header1").style.visibility = "visible";
            $("list_header1").style.cssFloat = "left";
            $("list_header1").style.styleFloat = "left";
            $("list_header2").style.position = "";
            $("list_header2").style.visibility = "visible";
            $("list_header2").style.cssFloat = "right";
            $("list_header2").style.styleFloat = "right";
        }else{
            $("list_header1").style.position = "";
            $("list_header1").style.visibility = "visible";
            $("list_header2").style.position = "";
            $("list_header2").style.visibility = "visible";
            $("list_header2").style.cssFloat = "right";
            $("list_header2").style.styleFloat = "right";
        }
    }
}

function orderBy(type){
    if(isCurrentlyEditing())
        return;
    if(list.orderBy!=type){
        list.orderBy = type;
        list.orderByDirection = "ASC";
    }else{
        list.orderByDirection = list.orderByDirection=="ASC"?"DESC":"ASC";
    }
    list.page = 0;
    showList();
}

function goNext(){
    if(isCurrentlyEditing())
        return;
    if(list.page<(list.pages-1)){
        list.page++;
        showList();
    }
}
function superNext(){
    if(isCurrentlyEditing())
        return;
    if(list.page<(list.pages-1)){
        list.page = list.pages-1;
        showList();
    }
}
function goPrevious(){
    if(isCurrentlyEditing())
        return;
    if(list.page>0){
        list.page--;
        showList();
    }
}
function superPrevious(){
    if(isCurrentlyEditing())
        return;
    if(list.page>0){
        list.page = 0;
        showList();
    }
}

function showList(){
    $("listLoading").style.display = "block";
    $("listEmpty").style.display = "none";
    $("listItems").style.display = "none";
    $("listPaginationTop").style.display = "none";
    $("list_footer").style.display = "none";
    new Ajax.Updater("listItems", 
        baseHREF+"pub/list/fullList.do", 
        {
            parameters: { 
                listProjectSize: list.projectSize,
                listEdit: list.edit,
                listType: list.listType,
                value: list.value,
                category: list.category,
                language: list.language,
                languageForced: list.languageForced,
                user: list.user,
                orderBy: list.orderBy,
                orderByDirection: list.orderByDirection,
                page: list.page,
                refresh: new Date().getTime(),
                updatable: list.updatable
            },
            evalScripts: true
        }
    );
}

function showProjectInfo(key, size) {
    $("projectLoading_"+key).style.display = "block";
    $("project_"+key).style.display = "none";
    new Ajax.Updater("project_"+key, 
            baseHREF+"project/projectInfo.do", 
            {
                parameters: { 
                    workspaceKey: key,
                    type: size,
                    refresh: new Date().getTime() 
                },
                onSuccess: function(transport) {
                    try{//agilbert
                      $("projectLoading_"+key).style.display = "none";
                      $("project_"+key).style.display = "block";
                      if(typeof(keyBusy) != 'undefined')
                        keyBusy[key] = false;
                    }catch(err){
                    }                      
                },
                evalScripts: true
            }
    );
}
function showMap(key){
    window.location.href = baseHREF + 'map.do?workspaceKey='+key;
}
function hideNotViewed(key){
    $('notViewed_'+key).style.display = 'none';
    new Ajax.Request(baseHREF + 'setViewed.do', 
        {
            method: 'get',
            parameters: {"workspaceKey": key},
            onSuccess: function(response) {
                //try{
                    updateFavoriteBox();
                    updateContacts();
                //}catch(err){
                //}
            },            
            evalScripts: true
        }
    );     
}