//Entfernt Leerzeichen vor und nach Strings
function trim(str) {
    str+="";
    
    c=str.charAt(0);
    while(c==" " || c=="\t" || c=="\n" || c=="\f" || c=="\r") 
    {
        str=str.substring(1,str.length);
        c=str.charAt(0);
    }
    
    c=str.charAt(str.length-1);
    while(c==" " || c=="\t" || c=="\n" || c=="\f" || c=="\r") 
    {
        str=str.substring(0,str.length-1);
        c=str.charAt(str.length-1);
    }
    
    return str;
}

//Liefert die Cursorposition
var mouseX = 0;
var mouseY = 0;
function getPosition(e) {
    e = (e) ? e : ((window.event) ? window.event : "");
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
    mouseX = cursor.x;
    mouseY = cursor.y;
    
    return cursor;
}

//Zeigt Subnavigation
var active = false;
function shownavi(id)
{
    navis = new Array();
    
    navis[0] = document.getElementById("subnavi_startseite");
    navis[1] = document.getElementById("subnavi_roster");
    navis[2] = document.getElementById("subnavi_community");
    navis[3] = document.getElementById("subnavi_miscbase");
    navis[4] = document.getElementById("subnavi_imprint");

    for(i = 0; i < navis.length;i++)
        navis[i].style.display = "none";
    
    navi = document.getElementById("subnavi_" + id);
    navi.style.display = "block";
    navi.onmousemove = getPosition;
    
    active = true;
}

//Verstecke Subnavigation
function hidenavitimed(id, time)
{
    active = false;
    setTimeout("hidenavi('" +id + "')", time * 1000);
}

function hidenavi(id)
{
    if(!active)
    {
        navi = document.getElementById("subnavi_" + id);
        navi.style.display = "none";
    }
}

//Zeigt Popupnavigationen
function showpopupnavi(id)
{
    navi = document.getElementById("popupnavi_" + id);
    if(navi.style.display == "block")
        var hide = true;
    else
        var hide = false;
    
    hidepopupnavis();

    if(!hide)
      navi.style.display = "block";
}

//Versteckt alle Popupnavigationen
function hidepopupnavis()
{
    document.getElementById("popupnavi_kategorie").style.display = "none";
    document.getElementById("popupnavi_poster").style.display = "none";
    document.getElementById("popupnavi_comments").style.display = "none";
}

function hidepopupnavistimed(time)
{
    setTimeout("hidepopupnavis()", (time * 1000));
}

//Ändert die aktuelle Einstellungen einer Popupnavigation
function changepopupvalue(popupnavi, value)
{
    hidepopupnavis();
    input = document.getElementById("popup_" + popupnavi);
    input.value = value;
    hidelastnews();
}

//Ändert die Ansicht der Lastnews
function hidelastnews()
{
    news = document.getElementsByName("lastnews_headline");
    searchkategorie = trim(document.getElementById("popup_kategorie").value);
    searchposter = trim(document.getElementById("popup_poster").value);
    searchcomments = trim(document.getElementById("popup_comments").value);
    searchcomments = trim(searchcomments.split("mehr als "));
    searchcomments = trim(searchcomments[1]);
    
    var counter = 0;
    var show, values, kategorie, poster, comments, id;
    for(i = 0; i < news.length; i++)
    {
        show = true;
    
        values = news[i].value.split("|");
        kategorie = trim(values[0]);
        poster = trim(values[1]);
        comments = trim(values[2]);
        id = trim(values[3]);

        if(searchkategorie != "Kategorie" && kategorie != searchkategorie)
            show = false;
        
        if(searchposter != "Poster" && poster != searchposter)
            show = false;
        
        if(searchcomments != "Comm..." && parseInt(comments) <= parseInt(searchcomments))
            show = false;

        if(show == true && counter < 11)
        {
            document.getElementById("lastnews_headline_" + id).style.display = "block";
            counter++;
        } else
            document.getElementById("lastnews_headline_" + id).style.display = "none";
    }
}

//Image Preloading
function preloadimages()
{
    images = new Array();
    var i = 0;
    
    
    images[0] = "include/designs/shipandaz/images/links_bg_startseite_hover.jpg";
    images[1] = "include/designs/shipandaz/images/links_bg_roster_hover.jpg";
    images[2] = "include/designs/shipandaz/images/links_bg_community_hover.jpg";
    images[3] = "include/designs/shipandaz/images/links_bg_miscbase_hover.jpg";
    images[4] = "include/designs/shipandaz/images/links_bg_imprint_hover.jpg";
    images[5] = "include/designs/shipandaz/images/btn_deinprofil_hover.jpg";
    images[6] = "include/designs/shipandaz/images/btn_next2_hover.jpg.jpg";
    images[7] = "include/designs/shipandaz/images/btn_back2_hover.jpg.jpg";
    
    for(i = 0; i < images.length; i++)
    {
        imageObj = new Image();
        imageObj.src = images[i];
    }
}

function preloadimages2(images)
{
    for(i = 0; i < images.length; i++)
    {
        imageObj = new Image();
        imageObj.src = images[i];
    }
}

//Ändert den Navigationshintergrund
function changenavi(id)
{
    var background = "";
    var navi = document.getElementById('navigation');
    
    if(id == 0)
      background = "";
    else
      background = "_" + id + "_hover";
      
    navi.style.backgroundImage = "url(include/designs/shipandaz/images/links_bg" + background + ".jpg)";
}

//Wechelst die Lastwar Ergebnisse
function switchlastresult(how)
{
    squads = document.getElementsByName('lastwar');
    crrLastwarInput = document.getElementById('crr_lastwar');
    crrLastwar = parseInt(crrLastwarInput.value);
 
    if(squads.length > 1)
    {
       if(how == 1)
       {
         if(crrLastwar + 1 <= squads.length - 1)
           newLastwar = crrLastwar + 1;
         else
           newLastwar = 0;
       } else
       {
         if(crrLastwar - 1 >= 0)
           newLastwar = crrLastwar - 1;
         else
           newLastwar = squads.length - 1;
       }
       
       document.getElementById(squads[crrLastwar].value).style.display = "none";
       document.getElementById(squads[newLastwar].value).style.display = "block";
       crrLastwarInput.value = newLastwar;
    }
}

//Wechelst die Squads in der Squadansicht
function switchsquad(how)
{
    squads = document.getElementsByName('squad_squadview');
    crrSquadInput = document.getElementById('crr_squad');
    crrSquad = parseInt(crrSquadInput.value);
 
    if(squads.length > 1)
    {
       if(how == 1)
       {
         if(crrSquad + 1 <= squads.length - 1)
           newSquad = crrSquad + 1;
         else
           newSquad = 0;
       } else
       {
         if(crrSquad - 1 >= 0)
           newSquad = crrSquad - 1;
         else
           newSquad = squads.length - 1;
       }
       
       document.getElementById(squads[crrSquad].value).style.display = "none";
       document.getElementById(squads[newSquad].value).style.display = "block";
       document.getElementsByName("squad_squadlink")[crrSquad].style.display = "none";
       document.getElementsByName("squad_squadlink")[newSquad].style.display = "inline";
       crrSquadInput.value = newSquad;
    }
}

//Wechelt das Memberbild in der Squadansicht
function change_memberimage(which, how)
{
    img = document.getElementById(which);

    extension = img.src;
    src = img.src;
    
    extension = extension.substr(extension.length - 4, 4);

    if(how == 1)
    {
        src = src.substr(0, src.length - 4);
        path = src + "_hover" + extension;
    } else
    {
        src = src.substr(0, src.length - 10);
        path = src + extension;
    }
    
    img.src = path;
}

//Squadmember scroll
var scrollactive;
var scrollboxID;

function scrollbox()
{
    var box = document.getElementById(scrollboxID);
    speed = 0;
    var cursorX = mouseX - box.offsetParent.offsetLeft - box.offsetLeft;
    
    if(cursorX < 0)
      cursorX = 0;
      
    cursorX -= box.offsetWidth / 2;
    
    if(cursorX < -50 || cursorX > 50)
        if(cursorX < 0)
          speed = (cursorX + 50) * 0.02;
        else
          speed = (cursorX - 50) * 0.02;
    
    box.scrollLeft = box.scrollLeft + speed;
}

function scrollboxStart(id)
{
    scrollboxID = id;
    document.getElementById(id).onmousemove = getPosition;
    scrollbox();
    scrollactive = setInterval("scrollbox()", 10);
}

function scrollboxStop()
{
    clearInterval(scrollactive);
}

//Ändert die aktuell angewählte Frage beim Poll
function setanswer(id, sort)
{
    vote_radio = document.getElementById("vote_radio");
    selected = document.getElementById("selected_answer");
    checkboxes = document.getElementsByName("pollanswer");
    img = checkboxes[id];
    
    for(i = 0; i < checkboxes.length; i++)
        checkboxes[i].src = 'include/designs/shipandaz/images/poll_checkbox.gif';
    
    vote_radio.value = sort;
    selected.value = id;
    img.src = 'include/designs/shipandaz/images/poll_checkbox_checked.gif';
}
