/**
 * SUKB
 * tools.js
 * Divers outils javascript
 * 20/11/2008
 */
 
/*******************************************************************************
 *  Copyright (C) 2008, Jimmy Rudolf <jimmy.rudolf@athaliasoft.fr>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/  
 
function SetHeight(readonly)
{
    var winWidth, winHeight, d=document;
    
    if (typeof window.innerWidth!='undefined') 
    {
        winWidth = window.innerWidth;
        winHeight = window.innerHeight;
    } 
    else if ( d.documentElement && typeof d.documentElement.clientWidth!='undefined' && d.documentElement.clientWidth!=0 ) 
    {
        winWidth = d.documentElement.clientWidth;
        winHeight = d.documentElement.clientHeight;
    } 
    else if ( d.body && typeof d.body.clientWidth!='undefined') 
    {
        winWidth = d.body.clientWidth;
        winHeight = d.body.clientHeight;
    }
    
    var main = document.getElementById("main");
    
    var left_pane = document.getElementById("left_pane");
    var categories_list = document.getElementById("categories_list");
    
    var right_pane = document.getElementById("right_pane");
    var article_view = document.getElementById("article_view");
    var article_content = document.getElementById("article_content");
    var article_content_frame = document.getElementById("article_content___Frame");
    
    main.style.height = 
        main.style.maxHeight = 
        right_pane.style.height = 
        left_pane.style.height = 
        right_pane.style.maxHeight = 
        left_pane.style.maxHeight = 
        winHeight - 32 + "px";
    
    if(readonly == '')
    {
        categories_list.style.height = 
            categories_list.style.maxHeight = 
            winHeight - 269 + "px";
    
        article_view.style.height = 
            article_view.style.maxHeight = 
            winHeight - 301 + "px";
    
        article_content.style.height = 
            article_content.style.maxHeight = 
            winHeight - 401 + "px";
    
        if(article_content_frame != null)
        {
            article_content_frame.style.height = 
                article_content_frame.style.maxHeight = 
                winHeight - 328 + "px";
        }
    }
    else
    {
        categories_list.style.height = 
            categories_list.style.maxHeight = 
            winHeight - 156 + "px";
    
        article_view.style.height = 
            article_view.style.maxHeight = 
            winHeight - 270 + "px";
    
        article_content.style.height = 
            article_content.style.maxHeight = 
            winHeight - 370 + "px";
    }
}

function CheckSelectedItems(items)
{
    var checked = true;
    
    if(items.length == 0)
    {
        checked = false;
    }
    else
    {
        for(id in items)
        {
            if(items[id] == false)
            {
                checked = false;
            }
        }
    }
    
    if(!checked)
    {        
        alert(Lang["MustSelect1Item"]);
    }
    
    return checked;
}

function DeleteSelectedItems(items, category_id)
{
    var c = confirm(Lang["ConfirmSuppress"]);
    
    if(c)
    {
        var itemsToDelete = "";
        
        for(id in items)
        {
            if(items[id] == true)
            {
                itemsToDelete += id + ",";
            }
        }
        
        location.href = "?action=delete_articles&articles_id=" + itemsToDelete + "&category_id=" + category_id;
    }
}

function DeleteCategory(id)
{
    var c = confirm(Lang["ConfirmSuppressCategory"]);
    
    if(c)
    {
        location.href = "?action=delete_category&category_id=" + id;
    }
}

function MoveItemsTo(category_id, items)
{
    var itemsToMove = "";
        
    for(id in items)
    {
        if(items[id] == true)
        {
            itemsToMove += id + ",";
        }
    }
    
    location.href = "?action=move_articles&articles_id=" + itemsToMove + "&category_id=" + category_id;
}

function SetMenuPosition(menu, menu_item)
{
    // Merci QuirksMode !
    var curleft = curtop = 0;

    if (menu_item.offsetParent) 
    {
        do 
        {
        	curleft += menu_item.offsetLeft;
        	curtop += menu_item.offsetTop;
        } 
        while (menu_item = menu_item.offsetParent);
    }
    
    curleft = curleft - 17;
    
    menu.style.left = curleft + "px";
}
