var Stil = "Standard";
var Keks = "Layout";
var Tage = 30;
var oldVideoHeight;

function switchStyle(title)
{
    try{
        window.clearTimeout(boxtimer);
    }catch(e){}
    if(title == "klein" && gWidth != iWidth) {return;}
    if(title == "gross" && gWidth == iWidth) {return;}
        
    if (!document.getElementsByTagName) { return; }

    // Stylesheet setzen
    var css = document.getElementsByTagName("link");
    for (var i = 0; i < css.length; i++ ) 
    {        
        if (css[i].getAttribute("rel").indexOf("style") != -1 && css[i].getAttribute("title")) 
        {
            css[i].disabled = true;
            if (css[i].getAttribute("title") == title) { css[i].disabled = false; }
        }        
    }
    
    resizePage(title, true);  
}

function resizePage(title, switchCSS, loadScroller)
{
    setColor(title);
    
    if(gWidth == 1280)
    {
        if(title == "klein") { iWidth="1024"; }
        if(title == "gross") { iWidth="1280"; }
    }
    
    if(gWidth == 1680)
    {
        if(title == "klein") { iWidth="1280"; }
        if(title == "gross") { iWidth="1680"; }
    }
    
    var box =               document.getElementById('box');
    var box_braun =         document.getElementById('box_braun');
    var box_news =          document.getElementById('box_news');
    var box_member =        document.getElementById('box_member');
    var box_suche =         document.getElementById('suchbox');
    
    var boxrand =           document.getElementById('boxrand');
    var boxrand_braun =     document.getElementById('boxrand_braun');
    var boxrand_news =      document.getElementById('boxrand_news');
    var boxrand_member =    document.getElementById('boxrand_member');
    var boxrand_suche =     document.getElementById('suchbox_rand');
   
    var bigbox =            document.getElementById('bigbox');
    var boxposition =       document.getElementById('boxposition');
    var scrollbox =         document.getElementById('scrollbox');
    var boxcontent =        document.getElementById('boxcontent')
    
    var videoframe =        document.getElementById('if');
    
    if(videoframe)  
    { 
        if(videoframe.offsetHeight!=oldVideoHeight && oldVideoHeight > 0) { window.location.reload(); }
        oldVideoHeight = videoframe.offsetHeight;
    }
    
    switch(iWidth)
    {
        case "1024": 
            if(typeof(relvalue) != 'undefined') 
            { if(gWidth == "1280") {absboxwidth=655;} else {absboxheight=483; topmargin=180; absboxwidth=655;} }
            else 
            {   absboxwidth=655;
                if(document.getElementById('titel')) 
                     { topmargin=14;  absboxheight=469; if(title=="klein") {topmargin=107;}}
                else { topmargin=180; absboxheight=303; if(title=="klein") {topmargin=273;}}    
            } break;
        case "1280": 
            if(typeof(relvalue) != 'undefined') 
            { if(gWidth == "1680") {absboxwidth=781;} else {absboxheight=575; topmargin=213; absboxwidth=781;} }
            else 
            {   absboxwidth=781;
                if(document.getElementById('titel')) 
                     { topmargin=16;  absboxheight=560; if(title=="klein") {topmargin=106;}}
                else { topmargin=213; absboxheight=363; if(title=="klein") {topmargin=303;}}
            } break;
        case "1680": 
            if(typeof(relvalue) != 'undefined') 
            { absboxheight=666; absboxwidth=832; topmargin=246;} 
            else 
            {   absboxwidth=832;
                if(document.getElementById('titel')) 
                     { topmargin=19;  absboxheight=647; }
                else { topmargin=246; absboxheight=420; }
            } break;
    }
    
    /*if(typeof(newscount) != 'undefined' && 
       typeof(aid) != 'undefined')
    {
        if(aid == 1)
        {
            absboxheight = (absboxheight/2) * newscount;
            topmargin += (absboxheight-2);
        }
    }*/
        
    if(document.getElementById('boxcontent')) 
    {
        if(document.getElementById('boxcontent').style.display == "none") 
        {
            minimizeBox(topmargin, absboxwidth, absboxheight, null);
        }
    }

    if(scrollbox) { scrollbox.style.width=null; scrollbox.style.height=null; }
        
    // Member
    if(box_member && boxrand_member)
    {
        var smallbox=false;
        
        if(document.getElementById('optiker_adresse')) 
        { 
            if(document.getElementById('optiker_adresse').style.display != "block") {smallbox=true;} 
        }    
        else { setKarte(iWidth); }
        
        switch(iWidth)
        {
            case "1024": if(smallbox) {memberheight=139; margintop=221;} else {memberheight=286;margintop=74;} break;            
            case "1280": if(smallbox) 
                              { if(gWidth > iWidth) {memberheight=178; margintop=237;} else { memberheight=150; margintop=210;}} 
                         else { if(gWidth > iWidth) {memberheight=343; margintop=67; } else { memberheight=343; margintop=15; }} break;
            case "1680": if(smallbox) {memberheight=180; margintop=235;} else {memberheight=396;margintop=17;} break;                    
        }
        
        if(!smallbox) 
        {
            if(bigbox.style.display == "block") { margintop = box_member.offsetHeight-memberheight; };
            if(margintop > 0) { margintop+=3; } else { margintop-=3; }
            if(document.all && document.documentMode != 8) 
            {
               bigbox.style.marginTop = (bigbox.offsetTop + margintop)+"px";
            } 
            else { if(navigator.appVersion.indexOf("MSIE 7.0") > -1) { bigbox.style.marginTop = (16 + margintop)+"px"; } }
        }
        else { bigbox.style.marginTop=margintop+"px"; }
        
        box_member.style.height = memberheight+"px";
        boxrand_member.style.height = (memberheight-2)+"px";
        
        if($('optiker_akustik')) 
        { 
            if(auswahlid == "23" && iWidth == "1024") 
            {
                bigbox.style.marginTop = "0px";
                box_member.style.height = memberheight+60+"px";
                boxrand_member.style.height = (memberheight+60-2)+"px";
                
                $('akustik_geschaeft2').innerHTML = $('akustik_geschaeft2').innerHTML.replace("&nbsp;", "<br/>");
                $('akustik_geschaeft3').innerHTML = $('akustik_geschaeft3').innerHTML.replace("&nbsp;", "<br/>");
                $('akustik_geschaeft4').innerHTML = $('akustik_geschaeft4').innerHTML.replace("&nbsp;", "<br/>");
            }
            else
            {
                $('akustik_geschaeft2').innerHTML = $('akustik_geschaeft2').innerHTML.replace("<BR>", "&nbsp;");
                $('akustik_geschaeft3').innerHTML = $('akustik_geschaeft3').innerHTML.replace("<BR>", "&nbsp;");
                $('akustik_geschaeft4').innerHTML = $('akustik_geschaeft4').innerHTML.replace("<BR>", "&nbsp;");
            }
        }
    }

    // Box
    if(box && boxrand)
    {
        if(typeof(relvalue) != 'undefined') { setRelValue(); }
        
        setStandart(iWidth);
        setPanorama(iWidth);
        setBroschuere(iWidth);
        setSmallNews(iWidth);
        
        /*if(typeof(relvalue) != "undefined" ||
           typeof(newscount)!= "undefined")
        {        
            scrollbox.style.width = null;
            scrollbox.style.height = null;
        }*/

        if(typeof(relvalue) != 'undefined') 
        {
            box.style.height = relvalue;
            boxrand.style.height = (relvalue-2);
            
            var bottomspace = 90;
            if(typeof(addbottomspace) != 'undefined') 
            {
                if(addbottomspace)
                {
                    if(iWidth == "1024") { bottomspace += 15; }
                    if(iWidth == "1280") { bottomspace += 20; }
                    if(iWidth == "1680") { bottomspace += 25; }
                }
            }
            
            if(scrollbox) { scrollbox.style.height = (relvalue-bottomspace); }
            
        } else {
            box.style.height = absboxheight + "px";
            boxrand.style.height = (absboxheight-2) + "px";
        }
        
        box.style.width = absboxwidth + "px";
        boxrand.style.width = (absboxwidth-2)+ "px";

        box.style.width = box.offsetWidth+ "px";
        boxrand.style.width = (box.offsetWidth-2)+ "px";
        
        box.style.height = box.offsetHeight+ "px";
        boxrand.style.height = (box.offsetHeight-2)+ "px";

        if(switchCSS) 
        { 
            if(typeof(relvalue) != 'undefined')  
                 { boxposition.style.marginTop = (absboxheight-relvalue) + "px"; }
            else { boxposition.style.marginTop = topmargin + "px"; }
        }
    }
       
    // Braun
    if(box_braun && boxrand_braun) 
    {
        resetBox(box_braun, boxrand_braun);
        
        setBigNews(iWidth);
        setGalerie(iWidth);
        setGeschaeft(iWidth);
        setKartenLogos(iWidth);
        
        var setTopmargin = true;
        var titel = document.getElementById('titel');
        if(titel)
        {
            if(titel.innerHTML.indexOf("Kontaktformular") > -1 ||
               titel.innerHTML.indexOf("Formulaire de contact") > -1 ||
               titel.innerHTML.indexOf("Contatto") > -1 ||
               titel.innerHTML.indexOf("online bestellen!") > -1 ||
               titel.innerHTML.indexOf("Ihre Meinung interessiert uns!") > -1 ||
               titel.innerHTML.indexOf("auf Sie!") > -1) 
            { 
                if(gWidth != iWidth) 
                     { if(iWidth=="1280") {topmargin=267;} else {topmargin=219;} }
                else { if(iWidth=="1280") {topmargin=177;} else {topmargin=190;} }
            }
            else { if(gWidth != iWidth) {topmargin=106;} else {topmargin=19;} }
            
            if(titel.innerHTML.indexOf("Ihre Meinung ist uns wichtig und einiges wert!") > -1)
            {
                if(iWidth == "1680") { topmargin=105; }
            }
            
            if(titel.innerHTML.indexOf("Karte") > -1) { setTopmargin = false; }
            
            /*if(document.getElementById('neuheittext')) 
            {
                var scrollboxnews = document.getElementById('scrollbox');
                
                if(iWidth == "1024") { document.getElementById('neuheittext').style.width = "408px"; scrollboxnews.style.width = "180px"; scrollboxnews.style.height = "385px"; }
                if(iWidth == "1280") { document.getElementById('neuheittext').style.width = "455px"; scrollboxnews.style.width = "200px"; scrollboxnews.style.height = "460px"; }
                if(iWidth == "1680") { document.getElementById('neuheittext').style.width = "516px"; scrollboxnews.style.width = "200px"; scrollboxnews.style.height = "540px"; }
            }*/
        }
        
        if(setTopmargin) { boxposition.style.marginTop = topmargin + "px"; }
    }
    
    // News
    if(box_news && boxrand_news && document.getElementById('neuheiten').style.display == "block") 
    {
        setNews(iWidth);
        setWMTipp(iWidth);
        setShop(iWidth);
        resetBox(box_news, boxrand_news); 
        showNews(); 

        if(hasImages == 0)
        {
            var width;
            
            if(iWidth == "1024") { width = 260; }
            if(iWidth == "1280") { width = 310; }
            if(iWidth == "1680") { width = 360; }
            
            $('box_content').style.width = width;        
        }
        
        if(hasImages == 2)
        {
            setFlash(iWidth);
        }
    }
    
    // Neuheiten
    if(document.getElementById('news'))    
    {
        var imgheight = 307; var imgwidth = 408;
        var textheight = 70; var textwidth = 392;
    
        if(iWidth == "1280") { imgheight = 344; imgwidth = 455; textheight = 100; if(gWidth == "1280") { textwidth = 440; } else { textwidth = 440; } }
        if(iWidth == "1680") { imgheight = 390; imgwidth = 516; textheight = 110; textwidth = 485; }    
        
        $('scrollbox2').style.paddingRight = "15px";
        
        $('scrollbox2').style.width = textwidth + "px";
        $('scrollbox2').style.height = textheight + "px";
        
        $('box_left').style.width =   imgwidth + "px";
        $('box_left').style.height =  imgheight + "px";
        
        if($('scrollbox')) 
        {
            var scrollboxnews = $('scrollbox');
            
            if(iWidth == "1024") { scrollboxnews.style.width = "180px"; scrollboxnews.style.height = "385px"; }
            if(iWidth == "1280") { scrollboxnews.style.width = "200px"; scrollboxnews.style.height = "460px"; }
            if(iWidth == "1680") { scrollboxnews.style.width = "200px"; scrollboxnews.style.height = "540px"; }
        }
        
        if(document.getElementById("flvFrame"))
        {
            /*
            var top = 0;
            var left = 0;

            if(iWidth == "1024") { top = 60; left = 0; }
            if(iWidth == "1280") { top = 34; left = 25; }
            if(iWidth == "1680") { top = 60; left = 20; }

            document.getElementById("flvFrame").style.paddingTop = top + "px";
            document.getElementById("flvFrame").style.paddingLeft = left + "px";
            */
            setSmallFlash(iWidth);
        }        
    }
    
    
    // Suche
    if(box_suche && boxrand_suche) 
    {
//        box_suche.style.width = box_suche.offsetWidth;
//        boxrand_suche.style.width = (box_suche.offsetWidth-2);    
//        
//        box_suche.style.height = box_suche.offsetHeight;
//        boxrand_suche.style.height = (box_suche.offsetHeight-2);
//    
//        //resetBox(box_suche, boxrand_suche);
//        
//        DD_roundies.addRule('.suchbox', 5);
//        DD_roundies.addRule('.suchbox_rand', 5);
    }
    
    
    if(scrollbox) 
    {   
        scrollbox.style.paddingRight=20;
        if(typeof(srollboxheight) != "undefined") 
        { 
            scrollbox.style.height=srollboxheight; 
        }
    }
    
    if($('pullupbutton'))
    {
        if($('pullupbutton').style.display == "block")
        {
            if(typeof(hText) != 'undefined') { document.getElementById('scrollbox').style.height = (scrollbox.style.height.replace("px", "") - 30); }
        }
    }
    
    setPDF(iWidth);
    setLogo(iWidth);
    
    setClose();
    setMinimize();
    
    setCss();
    
    initScroller(loadScroller);
}


function initScroller(loadScroller)
{
    /* SCROLLER: wenn objekt vorhanden dann nimm dieses und führe refresh aus */
    /* scroller existiert noch nicht, also initialisiere scrollBox1 neu (beim ersten mal) */
    if (loadScroller == true || scrollBox1 == null) 
    {
        scrollBox1 = new MooScroll({ selector: '#scrollbox' });
        scrollBox2 = new MooScroll({ selector: '#scrollbox2' });    
    } 
    else 
    {
        // hole element das gemooscrolled wird */
        var sb = $('scrollbox');
        var sb2 = $('scrollbox2');
                
        // hole das erste element, das von mooscroll erzeugt wird ( das mit class=content_el)
        if(sb) { sb.getFirst().style.height = sb.getSize().y + "px"; }
        if(sb2) { sb2.getFirst().style.height = sb2.getSize().y + "px"; }
        
        // aktualisiere scroller
        scrollBox1.refresh(); 
        scrollBox2.refresh(); 
    }
}

function setColor(title)
{
    if(gWidth != "1024")
    {
        var kleinA = document.getElementById('kleinA');
        var grossA = document.getElementById('grossA');
        var sel_color;
        
        if(auswahlid == "23") { sel_color = "#2f2f30"; } else { sel_color = "#d5be70"; }
        
        if(title=="klein") { kleinA.style.color=sel_color; grossA.style.color="#FFFFFF"; }
        if(title=="gross") { grossA.style.color=sel_color; kleinA.style.color="#FFFFFF"; }
    }
}

function resetBox(box, boxrand)
{
    box.style.width = null;
    boxrand.style.width = null;

    box.style.width = box.offsetWidth;
    boxrand.style.width = (box.offsetWidth-2);
    
    box.style.height = null;
    boxrand.style.height = null;
    
    box.style.height = box.offsetHeight;
    boxrand.style.height = (box.offsetHeight-2);
}

function loadStyle() 
{
    var c = getCss();
    if (c && c != Stil) 
    {
        switchStyle(c);
        Stil = c;
    }
}

function setStyle(s) 
{
    if (s != Stil) {
        switchStyle(s);
        Stil = s;
    }
}

//window.onload = loadStyle;


// Cookie-Funktionen

function setCookie(name, value, expdays) 
{    
    // gültig expdays Tage
    var now = new Date();
    var exp = new Date(now.getTime() + (1000*60*60*24*expdays));
    document.cookie = name + "=" + escape(value) + ";" +
                      "expires=" + exp.toGMTString() + ";" +
                      "path=/";
}

function delCookie(name) 
{   
    // expires ist abgelaufen
    var now = new Date();
    var exp = new Date(now.getTime() - 1);
    document.cookie = name + "=;" +
                      "expires=" + exp.toGMTString() + ";" + 
                      "path=/";
}

function getCookie(name)
{
    var cname = name + "=";
    var dc = document.cookie;
    if (dc.length > 0) 
    {
        var start = dc.indexOf(cname);
        if (start != -1) 
        {
          start += cname.length;
          var stop = dc.indexOf(";", start);
          if (stop == -1) stop = dc.length;
          return unescape(dc.substring(start,stop));
        }
    }
    return null;
}

function setCss() 
{
    /*var curr_css
    if(gWidth != iWidth) { curr_css="klein" } else { curr_css="gross"; }
    setCookie("cookie", curr_css, 1); */
    
    setCookie("cookie", iWidth, 1);
}
function getCss() { return getCookie("cookie"); }
function delCss() { delCookie("cookie"); }