var imgbar_state=0,imgbar_savedheight=0,imgbar_savedwidth=0,imgbar_savedlogopos=0,imgbar_hlogo=52,imgbar_ylogo=4,imgbar_ytext=12,imgbar_slidesteps=15;wrapper_savedwidth=0;photo_savedwidth=0;photocaption_savedwidth=0;caption_calc=0;

function nextRandomImageBar(){
    document.getElementById("shg_imagebar").style.backgroundImage="url("+shg_rootpath+"virtual_tour/"+shg_imagebars[Math.floor(Math.random()*(shg_imagebars.length-1+0.5))]+"imagebar.jpg)"
    InitializeTimer()
}

function getWindowHeight(){
    var a=0;
    if(typeof window.innerHeight=="number")a=window.innerHeight;
    else if(document.documentElement&&document.documentElement.clientHeight)a=document.documentElement.clientHeight;
    else if(document.body&&document.body.clientHeight)a=document.body.clientHeight;
    return a
}

function oc(a){
    for(var b={},c=0;c<a.length;c++)b[a[c]]="";
    return b
}

function showFlash(){
    StopTheClock()
    
    var a=String(document.getElementById("shg_imagebar").style.backgroundImage),b=a.indexOf("virtual_tour/")+13,c=a.indexOf("imagebar.jpg");
    
    a=a.slice(b,c);
    a='<iframe id="iframeview" src="'+(shg_rootpath+"index.php?target=virtual_tour/"+(a+"demo.html")+"&shglogo=false")+'" name="Bildframe'+a+'" width="100%" height="100%" scrolling="no" marginheight="0" marginwidth="0" frameborder="0" style="z-Index:101"><p style="padding:10%;"></iframe>';
    document.getElementById("shg_imagebar").innerHTML=a
}

function hideFlash(){
    InitializeTimer()
    document.getElementById("shg_imagebar").innerHTML=""
}

function imgbar_getspace(){
    var a=getWindowHeight(),b=document.getElementById("shg_imagebar").offsetTop,c=imgbar_hlogo+imgbar_ylogo;
    if(c<16)c=16;
    a=a*0.8;
//    a=a-b-c;
//    a=a-b;
    if(a<imgbar_savedheight)a=imgbar_savedheight;
    return a
}

function incHeightImageBar(){
    if(imgbar_state==0){
        imgbar_state=1;
        wrapper_savedwidth=document.getElementById("wrapper").offsetWidth;
        photo_savedwidth=document.getElementById("photo_on_header_go").offsetWidth - 40; // 40 is the defined padding in the css
        photocaption_savedwidth=document.getElementById("photo_on_header_caption").offsetWidth;
        imgbar_savedheight=document.getElementById("shg_imagebar").offsetHeight;
        imgbar_savedwidth=document.getElementById("shg_imagebar").offsetWidth;
        document.getElementById("top").style.display="none"; 
        document.getElementById("head_menu_subline").style.display="none"; 
        document.getElementById("shg_showpano").style.display="none";
        document.getElementById("middle_content").style.display="none";
        document.getElementById("left").style.display="none";
        document.getElementById("footer").style.display="none";
        document.getElementById("wrapper").style.width=90+"%";    
        document.getElementById("photo_on_header_go").style.width=95+"%";
        caption_calc=document.getElementById("photo_on_header_go").offsetWidth;
        document.getElementById("photo_on_header_go").style.width=(caption_calc-40)+"px";
        document.getElementById("photo_on_header_caption").style.width=caption_calc+"px";
        imgbar_savedlogopos=document.getElementById("shg_imagebar").offsetTop+imgbar_savedheight-document.getElementById("shg_logo").offsetTop
    }
    var a=document.getElementById("shg_imagebar").offsetTop,b=imgbar_getspace(),c=parseInt(document.getElementById("shg_imagebar").offsetHeight,10),d=b/imgbar_slidesteps;
    if(c<b){
        c+=d;
        if(c>b)c=b;         
        document.getElementById("photo_on_header_go").style.height=c+"px";
        document.getElementById("shg_imagebar").style.height=c+"px";
        document.getElementById("shg_imagebar").style.width=100+"%";
//        document.getElementById("shg_imagebar").style.position="relative";
//        document.getElementById("shg_imagebar").style.width=90+"%";
//        document.getElementById("shg_imagebar").style.clear="both";
//        document.getElementById("shg_imagebar").style.left="-"+50+"%";
//        document.getElementById("shg_logo").style.top=a+c+imgbar_ylogo+"px";
        if(c>=b){
//            document.getElementById("shg_hidepano").style.top=a+c+imgbar_ytext+"px";
            document.getElementById("shg_hidepano").style.display="inline";  
            imgbar_state=2;setTimeout("showFlash()",1)
        } else setTimeout("incHeightImageBar()",1)
    } else {
        c+=d;         
        if(c>b)c=b;         
        document.getElementById("shg_imagebar").style.height=c+"px";
        document.getElementById("shg_imagebar").style.width=100+"%";
        document.getElementById("photo_on_header_go").style.height=c+"px";
//        document.getElementById("shg_logo").style.top=a+c+imgbar_ylogo+"px";
        if(c>=b){
//            document.getElementById("shg_hidepano").style.top=a+c+imgbar_ytext+"px";
            document.getElementById("shg_hidepano").style.display="inline";  
            imgbar_state=2;setTimeout("showFlash()",1)
        } else setTimeout("incHeightImageBar()",1)
    }
}

function decHeightImageBar(){
    if(imgbar_state==2){
        imgbar_state=1;
        hideFlash();
        document.getElementById("shg_hidepano").style.display="none"
    }
    var a=document.getElementById("shg_imagebar").offsetTop,b=imgbar_getspace(),c=parseInt(document.getElementById("shg_imagebar").offsetHeight,10);
    b=b/imgbar_slidesteps;
    if(c>imgbar_savedheight){
        c-=b;
        if(c<imgbar_savedheight+1)c=imgbar_savedheight;
        document.getElementById("wrapper").style.width=wrapper_savedwidth+"px"; 
        document.getElementById("shg_imagebar").style.height=c+"px";
        document.getElementById("photo_on_header_go").style.width=photo_savedwidth+"px";
        document.getElementById("photo_on_header_caption").style.width=photocaption_savedwidth+"px";
        document.getElementById("photo_on_header_go").style.height=c+"px";
        document.getElementById("shg_imagebar").style.position="static";
        document.getElementById("shg_imagebar").style.width=imgbar_savedwidth+"px";
        document.getElementById("shg_logo").style.top=a+c-imgbar_savedlogopos+"px";
        if(c-imgbar_savedheight<1){
            document.getElementById("shg_showpano").style.display="inline"; 
//            document.getElementById("shg_showpano").style.display="inline";
            document.getElementById("top").style.display="inline";
            document.getElementById("head_menu_subline").style.display="inline";
            document.getElementById("middle_content").style.display="inline";
            document.getElementById("left").style.display="inline";
            document.getElementById("footer").style.display="inline";
            imgbar_state=0
        } else setTimeout("decHeightImageBar()",1)
    }
}

function showImagebarPano(){
    imgbar_state==0&&incHeightImageBar()
}

function hideImagebarPano(){
    imgbar_state==2&&decHeightImageBar()
}

function resizeImagebarpano(){
    if(imgbar_state==2){
        var a=document.getElementById("shg_imagebar").offsetTop,b=imgbar_getspace();
        document.getElementById("shg_imagebar").style.height=b+"px";
        document.getElementById("shg_logo").style.top=a+b+imgbar_ylogo+"px";
        document.getElementById("shg_hidepano").style.top=a+b+imgbar_ytext+"px";
        if(document.getElementById("iframeview"))document.getElementById("iframeview").style.height=b+"px"
    }
    return true
}

var imagebar_touch_x=0,imagebar_touch_y=0;

function imagebar_onTouchStart(a){
    a.preventDefault();
    imagebar_touch_x=a.changedTouches[0].pageX;
    imagebar_touch_y=a.changedTouches[0].pageY
}

function imagebar_onTouchMove(a){
    a.preventDefault()
}

function imagebar_onTouchEnd(a){
    var b=a.changedTouches[0].pageX-imagebar_touch_x;
    if(a.changedTouches[0].pageY-imagebar_touch_y>100){
        a.preventDefault();
        showImagebarPano()
    } else if(b<-100||b>100){
        a.preventDefault();
        nextRandomImageBar()
    }
}

function touch_inits(){
    var a=navigator.platform.toLowerCase();
    if(a.indexOf("iphone")>=0||a.indexOf("ipod")>=0||a.indexOf("ipad")>=0)try{
        var b=document.getElementById("shg_imagebar");
        if(b){
            b.addEventListener("touchstart",imagebar_onTouchStart,true);
            b.addEventListener("touchmove",imagebar_onTouchMove,true);
            b.addEventListener("touchend",imagebar_onTouchEnd,true)
        }
    }
    catch(c){}
}
window.onload=touch_inits;
window.onresize=resizeImagebarpano;
