/***********************************************************************************************
 * name: rjsUtility.js
 * desc: Různé pomocné funkce.
 * auth: Lukáš Mitvalský
 * Copyright © 2008 Welldone s.r.o.
 *
 * depend: independent
 ***********************************************************************************************/
 
/* Zobrazeni obrazku v novem okne. Je pouzit autoSize. */
function rsfShowPic(url, title, desc)
{
    w = window.open('','','toolbar=no,location=no,directories=no,status=no,scrollbars=no' +
        ',resizable=yes,copyhistory=no,width=0,height=0,left=0,top=0');
 
    w.document.write("<html><head><title>"+title+"</title>\n");
    w.document.write("<script language='JavaScript'>\n");
    
    w.document.write("function autoSize() {\n");
    
    w.document.write(" imgW = document.images[0].width;\n");
    w.document.write(" imgH = document.images[0].height;\n");
    w.document.write(" if (imgW > screen.Width - 50) imgW = screen.Width - 50;\n");
    w.document.write(" if (imgH > screen.Height - 50) imgH = screen.Height - 50;\n");
    
    w.document.write(" if (document.all) self.resizeTo(imgW+20,imgH+40);\n");
    w.document.write(" else if (document.getElementById) self.sizeToContent();\n");
    w.document.write(" else top.window.resizeTo(imgW,imgH+20);\n");
    
    w.document.write(" self.focus();\n");
    w.document.write("}\n");
    w.document.write("</script>\n");
    w.document.write("</head><body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0");
    w.document.write(" onLoad='autoSize();' onclick='window.close();'>\n");
    w.document.write("<img src='"+url+"' style='border-width:0px;cursor:hand;cursor:pointer;' alt='"+title+"' title='"+desc+"'>\n");
    w.document.write("</body></html>");
    w.document.close();
}

/* Ziska aktualne nejvyssi z-index na strance nebo v konkretnim objektu (pokud je zadan parametr). */
function rsfGetHighestZIndex(obj)
{  
    var highestIndex = 0;
    var currentIndex = 0;   
    var arrElements = Array();
   
    // Hledat pouze v zadanem elementu nebo na cele stránce
    if (obj) arrElements = obj.getElementsByTagName('*'); 
    else arrElements = document.getElementsByTagName('*');
   
    // Projede vsechny elementy a zjisti jejich z-index
    for (var i = 0; i < arrElements.length; i++)
    {
        currentIndex = parseInt(arrElements[i].style.zIndex);
        if (!isNaN(currentIndex) && currentIndex > highestIndex) highestIndex = currentIndex;
    }
    
    return highestIndex;
}

/* Presune zadany objekt nad vsechny ostatni objekty na strance. */
function rsfObjectOnTop(obj)
{
    if (obj)
    {
        newZIndex = rsfGetHighestZIndex() + 1;
        obj.style.zIndex = newZIndex;
    }
    else
        throw "Exception in rsfObjectOnTop(): Object could not be found.";
}
/* Presune zadany objekt nad vsechny ostatni objekty na strance. Objekt je zadan prostrednictvim jeho ID. */
function rsfObjectOnTopById(objId)
{
    var obj = document.getElementById(objId);
    rsfObjectOnTop(obj);
}

/* Nahradi vsechny vyrazy {cislo} v danem retezci zadanymi argumenty. */
function rsfStringFormat(str)
{
    var retStr = str;
    for(var i = 1; i < arguments.length; i++)
    {
        retStr = retStr.replace('{' + (i - 1) + '}', arguments[i]);
    }
    return retStr;
}