function Browser() {
    var ua, s, i;

    this.isIE    = false;  // Internet Explorer
    this.isOP    = false;  // Opera
    this.isNS    = false;  // Netscape
    this.version = null;

    ua = navigator.userAgent;

    s = "Opera";
    if (window.opera) {
        this.isOP = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    // Treat any other "Gecko" browser as Netscape 6.1.

    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }

    s = "MSIE";
    if ((i = ua.indexOf(s))) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }
}
var browser = new Browser();


function get_window_size()
{
    /* http://www.howtocreate.co.uk/tutorials/javascript/browserwindow */
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    
    var x = new Object();
    x.width = myWidth;
    x.height = myHeight;
    return x;    
}

function get_scrolled_area() {
    xy = new Object();
    xy.x = 0;
    xy.y = 0;
    
    if(document.documentElement && typeof(document.documentElement.scrollLeft) == "number") {
        xy.x = Math.max(xy.x, document.documentElement.scrollLeft);
        xy.y = Math.max(xy.y, document.documentElement.scrollTop);
    }
    if(document.body && typeof(document.body.scrollLeft) == "number") {
        xy.x = Math.max(xy.x, document.body.scrollLeft);
        xy.y = Math.max(xy.y, document.body.scrollTop);
    }
    if(typeof(window.scrollX) == "number") {
        xy.x = Math.max(xy.x, window.scrollX);
        xy.y = Math.max(xy.y, window.scrollY);
    }
    if(typeof(window.pageXOffset) == "number") {
        xy.x = Math.max(xy.x, window.pageXOffset);
        xy.y = Math.max(xy.y, window.pageYOffset);
    }
    
    xy.top = xy.y;
    xy.left = xy.x;
    return xy;
}

function get_scrolled_size()
{
    var xy;
    xy = new Object();
    xy.width = 0;
    xy.height = 0;
    
    var window_size = get_window_size();
    var document_size = get_document_size();
    var scrolled_area = get_scrolled_area();
    
    /* Skrolano */
    xy.width = scrolled_area.x;
    xy.height = scrolled_area.y;
    
    /* Prozor */
    xy.width += window_size.width;
    xy.height += window_size.height;
    
    /* Dokument */
    xy.width = Math.min(xy.width, document_size.width);
    xy.height = Math.min(xy.height, document_size.height);
    
    /*
    var maxX, maxY
    if (browser.isIE) {
        maxX = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft) +
            (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
        maxY = Math.max(document.documentElement.scrollTop, document.body.scrollTop) +
            (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
    }
    if (browser.isOP) {
        maxX = document.documentElement.scrollLeft + window.innerWidth;
        maxY = document.documentElement.scrollTop  + window.innerHeight;
    }
    if (browser.isNS) {
        maxX = window.scrollX + window.innerWidth;
        maxY = window.scrollY + window.innerHeight;
    }
    
    xy.width = maxX;
    xy.height = maxY;
    */
    
    //alert(xy.width + "x" + xy.height);
    return xy;
}

function get_document_size()
{
    var xy = new Object();
    xy.width = 0;
    xy.height = 0;
    
    if(document.body.scrollHeight) {
        xy.width = Math.max(xy.width, document.body.scrollWidth);
        xy.height = Math.max(xy.height, document.body.scrollHeight);
    }
    if(document.body.parentNode.scrollHeight) {
        xy.width = Math.max(xy.width, document.body.parentNode.scrollWidth);
        xy.height = Math.max(xy.height, document.body.parentNode.scrollHeight);
    }
    
    return xy;
}

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}


function format_bytes(bytes){
    if (bytes >= 1073741824) {
        return Math.round(bytes / 1073741824 * 100) / 100 + " GiB";
    } else if (bytes >= 1048576) {
        return Math.round(bytes / 1048576 * 100) / 100 + " MiB";
    } else if (bytes >= 1024) {
        return Math.round(bytes / 1024 * 100) / 100 + " KiB";
    } else {
        return bytes + " B";
    }
}

function scroll_to_element(theElement) {
    if(theElement == null || theElement == undefined) {
        return;
    }
    var selectedPosX = 0;
    var selectedPosY = 0;
    
    while(theElement != null){
        selectedPosX += theElement.offsetLeft;
        selectedPosY += theElement.offsetTop;
        theElement = theElement.offsetParent;
    }

    var scrolled = get_scrolled_area();
    if(scrolled.top < selectedPosX) {
        return;
    }
    
    window.scrollTo(selectedPosX,selectedPosY);
}

function is_applet_loaded(applet_id) {
    try {
        var element = document.getElementById(applet_id);
        if (!element.isActive()) {
            return false;
        }
        return true;
    } catch(err) {
        return false;
    }
}