// known problem: wehn popup is open and selected category is changed, there are problems:
// if new category has no valid link in its project... error!
// if idx > maxidx of new category

/**
    Get the selected category
*/
function get_selected_category( win )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].selected_category ) 
            return win.frames[0].document.forms["status"].selected_category.value;
}

/**
    Set the selected category
*/
function set_selected_category( win, cat )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].selected_category ) 
            win.frames[0].document.forms["status"].selected_category.value = cat;
}

/**
    Get the site language
*/
function get_language( win )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].language ) 
            return win.frames[0].document.forms["status"].language.value;
}


/**
    Set the site language
*/
function set_language( win, lang )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].language ) 
            win.frames[0].document.forms["status"].language.value = lang;
}

/**
    Get the selected project
    @param win  the frame document 
*/
function get_selected_project( win )
{
    if (win.frames[0])
        if ( win.frames[0].document.forms["status"].selected_project ) 
            return win.frames[0].document.forms["status"].selected_project.value;
}


/**
    Set the selected project
    @param win  the frame document 
*/
function set_selected_project( win, prj )
{
    if (win.frames[0])
        if ( win.frames[0].document.forms["status"].selected_project ) 
            win.frames[0].document.forms["status"].selected_project.value = prj;
}


/**
    Gets the next project index in the selected category that has a valid link
*/
function get_next_project_idx( win )
{
    var currIdx = get_selected_project( win );
    var tmp = get_projects_by_category( get_selected_category( win ) );

    if ( tmp.length <= 0 )
        return -1;
    var fi;
    if ( currIdx == "" )
        fi = 0;
        else
        fi = parseInt( currIdx );
    var lc = 0;
    while( true ) {
        if ( fi + 1 >= tmp.length ) {
            fi = -1;
            lc++;
            }
        if ( tmp[ fi + 1 ]["link"] != "" )
            return fi + 1;
        fi = fi + 1;
        if ( lc > 2 )
            break;
        }

    return -1;
}



/**
    Gets the index of the previous project in the selected category that has a valid link
*/
function get_prev_project_idx( win )
{
    var currIdx = get_selected_project( win );
    var tmp = get_projects_by_category( get_selected_category( win ) );

    if ( tmp.length <= 0 )
        return -1;
    var fi;
    if ( currIdx == "" )
        fi = 0;
        else
        fi = parseInt( currIdx );
    var lc = 0;
    while( true ) {
        if ( (fi - 1) < 0 ) {
            fi = tmp.length;
            lc++;
            }
        if ( tmp[ fi - 1 ]["link"] != "" )
            return fi - 1;
        fi = fi - 1;
        if ( lc > 2 )
            break;
        }            
    return -1;
}

/**
    Gets the next project in the selected category that has a valid link
*/
function get_next_project( win )
{
    var tmp = get_projects_by_category( get_selected_category( win ) );
    var idx = get_next_project_idx( win );
    if ( idx == -1 )
        return null;
    return tmp[idx];
}

/**
    Gets the previous project in the selected category that has a valid link
*/
function get_prev_project( win )
{
    var tmp = get_projects_by_category( get_selected_category( win ) );
    var idx = get_prev_project_idx( win );    
    if ( idx == -1 )
        return null;
    return tmp[idx];
}


/**
    Get the current project order 
*/
function get_prj_order( win )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].prj_order ) 
            return win.frames[0].document.forms["status"].prj_order.value;
}

/**
    Set the selected project order
*/
function set_prj_order( win, cat )
{
    if ( win.frames[0])
        if ( win.frames[0].document.forms["status"].prj_order ) 
            win.frames[0].document.forms["status"].prj_order.value = cat;
}

function init( win )
{
    if ( get_selected_category( win ) == "" )
        set_selected_category( win, MAUPI_ALL_Categories ); // if category is empty, use "ALL" category
    cat = get_selected_category( win );
    

    if ( cat == MAUPI_WORKS_Category )
        order_projects( "date" );
        else
        order_projects( get_prj_order( win ) );
    

    prj_cat = get_projects_by_category( cat )[get_selected_project( win )][ "category" ];
    if ( prj_cat != "" ) {
        document.writeln( '<link href="../css/'+get_cat_property( get_category_idx( prj_cat ), "css" )+'" rel="stylesheet" type="text/css"></link>' );        
        } else
        document.writeln( '<link href="../css/general.css" rel="stylesheet" type="text/css"></link>' );

    document.writeln( '<title>MAUPI - ' + get_cat_property( get_category_idx( prj_cat ), "name" ) + '</title>' );
}

// ***************************************************************************************
// ********* Browser detection ***********************************************************
// ***************************************************************************************

// Browser

var maup_agent = navigator.userAgent.toLowerCase();
var maup_major = parseInt(navigator.appVersion);
var maup_minor = parseFloat(navigator.appVersion);
var maup_ie = ((document.all) && (maup_agent.indexOf("msie") != -1)) ? true : false;
var maup_ns = ((maup_agent.indexOf('mozilla') != -1) && ((maup_agent.indexOf('spoofer') == -1) && (maup_agent.indexOf('compatible') == -1)));
var maup_ns6 = (this.maup_ns && (this.maup_major >= 5));
if ( maup_ns6 ) maup_ns = false;
var maup_mz = ((document.getElementById) && (!document.all) && (document.documentElement)) ? true : false;
var maup_op = ((document.getElementById) && (navigator.userAgent.indexOf('Opera') != -1)) ? true : false

// Browser-Version

maup_b_version = navigator.appVersion.substring(0,1);

// Bildschirm-Auflösung

maup_r_small = (screen.width < 640) ? true : false; // Kleiner Bildschirm
maup_r_640 = (screen.width >= 640) ? true : false; // Mindestens 640x480
maup_r_800 = (screen.width >= 800) ? true : false; // Mindestens 800x600
maup_r_1024 = (screen.width >= 1024) ? true : false; // Mindestens 1024x768
maup_r_1280 = (screen.width >= 1280) ? true : false; // Mindestens 1280x1024
maup_r_1600 = (screen.width >= 1600) ? true : false; // Mindestens 1600x1200

// Java?

maup_javaok = (navigator.javaEnabled()) ? true : false;


if ( maup_ie )
{
    ver=navigator.appVersion.split("MSIE");
    version=parseFloat(ver[1]);
    if ( version < 6 )
        document.location.href="wrongbrowser.html";
} 