var current={};
current.subNavId=null;current.contentId=1;
current.subContentId=0;
current.over=null;
current.out=null;
current.slideDown=null;
current.slideUp=null;
current.appear=null;
current.fade=null;
current.move=null;
current.timeout=null;
current.image=null;
current.loadedImages=['background_1','background_9'];
current.posY = 12;
function imageLoaded(evt){
    current.loadedImages.push(this.id)
}
function preloadImage(src){
    var img=$(src);
    if(!img){
        img=new Image();
        img.id=src;
        img.className="pageimage";
        img.style.display="none";
        img.src="_i/"+src+".jpg";
        $("page").appendChild(img);
        Event.observe(img,'load',imageLoaded)
    }
}
function preloadImages(id,subId){
    if(document.images&&!subId){
        $("nav_" + id).select("li").each(function(el, ix){
            var src= el.id.replace("nav", "background") + '_1';
            preloadImage(src);
        });
    }
}
function navOut(evt){
    if(current.over){
        current.over.cancel();
    }
    current.out=new Effect.Move("nav_current",
        {duration:0.75,transition:Effect.Transitions.spring,y:current.posY,x:4,mode:"absolute"}
    );
}
function switchContent(id,subId){
    if(current.move){
        current.move.cancel();
    }
    current.move=new Effect.Morph("contentwrapper", {style:"top:504px",afterFinish:function(){
        var ch;
        if(current.subContentId){
            ch=$("content_"+current.contentId+"_"+current.subContentId);
        }
        else{
            ch=$("content_"+current.contentId)
        }
        if(ch){
            ch.hide()
        }
        var cs;
        if(subId){
            cs=$("content_"+id+"_"+subId);
            current.contentId=id;
            current.subContentId=subId;
        }
        else{
            cs=$("content_"+id);
            current.contentId=id;
            current.subContentId=0;
        }
        if(cs){
            cs.show();
            if(cs.hasClassName("full")){
                cs.parentNode.setStyle({height:"504px",width:"780px",left:"180px"});
                cs.setStyle({height:"452px",width:"732px"});
                current.move=new Effect.Morph("contentwrapper",{style:'top:0px;'});
            }
            else{
                cs.parentNode.setStyle({height:"78px",width:"960px",left:"0px"});
                cs.setStyle({height:"54px",width:"912px"});
                current.move=new Effect.Morph("contentwrapper",{style:'top:426px;'})
            }
        }
        navOut();
     }});
}
function switchImage(id,subId,imageId){
    if(current.timeout){
        window.clearTimeout(current.timeout);
    }
    if(current.appear){
        current.appear.cancel();
    }
    if(!imageId){
        imageId=1;
    }
    var src="background_"+id;
    if(subId){
        src+="_"+subId+"_"+imageId
    }
    var loaded=current.loadedImages.indexOf(src);
    if(loaded==-1){
        $("loading").show();
        current.timeout=window.setTimeout("switchImage("+id+", "+subId+", "+imageId+")",500);
        return;
    }
    $("loading").hide();
    if(!current.image){
        current.image=$("background_1");
    }
    current.fade=new Effect.Fade(current.image);
    var img=$(src);
    current.image=img;
    current.appear=new Effect.Appear(img);
    if(subId){
        var images=current.subImages["nav_" + id + "_" + subId];
        if(imageId>=images){
            imageId=0;
        }
        if(images>1){
            var presrc="background_"+id+"_"+subId+"_"+(++imageId);
            preloadImage(presrc);
            current.timeout=window.setTimeout("switchImage("+id+", "+subId+", "+imageId+")",(id==4&&subId==2?8000:4000));
        }
    }
}
function navOver(evt){
    if(current.out){
        current.out.cancel();
    }
    var lis = $("nav").select("li.visible");
    var li = evt.element().parentNode;
    var id = parseInt(li.id.split("_")[1],10);
    var posY = lis.indexOf(li) * 18 + 12;
    current.over=new Effect.Move("nav_current",{duration:0.75,transition:Effect.Transitions.spring,y:posY,x:4,mode:"absolute"});
    return false
}
function navClick(evt){
    if(current.slideDown){
        current.slideDown.cancel();
    }
    if(current.slideUp){
        current.slideUp.cancel();
    }
    var li = evt.element().parentNode;
    var arr = li.id.split("_");
    var id = parseInt(arr[1],10);
    var subId=0;
    if(arr.length==3){
        subId=parseInt(arr[2],10);
    }
    var subnav;
    if(!subId && id!=current.contentId){
        subnav=$("subnav_"+current.contentId);
        if(subnav&&subnav.visible()){
            current.slideUp=new Effect.SlideUp(subnav);
            $(subnav).select("li").each(function(el,ix){
                el.removeClassName("visible");
            });
        }
    }
    if((id!=current.contentId||(!subId&&current.subContentId))||(subId&&subId!=current.subContentId)){
        if(!subId){
            subnav=$("subnav_"+id);
            if(subnav&&!subnav.visible()){
                current.slideDown=new Effect.SlideDown(subnav);
                var items=$(subnav).select("li");
                items.each(function(el,ix){
                    el.addClassName("visible");
                });
            }
        }
        switchImage(id,subId);
        preloadImages(id,subId);
        switchContent(id,subId)
    }
    current.posY = $("nav").select("li.visible").indexOf(li) * 18 + 12;
    return false;
}
function init_menu(){
    current.subImages = {}
    $("nav").immediateDescendants().each(function(el,ix){
        current.subImages[el.id] = 1;
        $(el).select("ul li").each(function(el2, ix2){
            var imageCount = 0;
            if(el2.className){
                imageCount = parseInt(el2.className.split("_")[1], 10);
            }
            current.subImages[el2.id] = imageCount;
        });
    });
    $("nav").select("a").each(function(el,ix){
        Event.observe(el, 'mouseover', navOver);
        Event.observe(el, 'mouseout', navOut);
        Event.observe(el, 'click', navClick);
    });
    window.onload=function(){
        $("nav").immediateDescendants().each(function(el, ix){
            preloadImage(el.id.replace("nav", "background"));
        });
        $("page").select("a.external").each(function(el,ix){
            el.target = "_blank"; 
        });
    }
}
function init_webcam(){
    $("webcam_images").select("a.webcam span").each(function(el, ix){
        var s=webcam_images[ix];
        el.innerHTML=s.title;
        try{
            el.style.opacity=0.65;
            el.style.filter='alpha(opacity=65)';
        }catch(e){}
    });
    $("webcam_stamp").innerHTML=webcam_images[0].title;
    $("webcam_images").select("a.webcam img").each(function(el, ix){
        var s=webcam_images[ix];
        el.src=s.thumb;el.full=s.src;
    });
    $("webcam_images").select("a.webcam").each(function(el,ix){
        var s=webcam_images[ix];
        el.title=s.title;
        Event.observe(el,'mouseover',thumbOver);
        Event.observe(el,'mouseout',thumbOut);
        Event.observe(el,'click',thumbClick);
    });
    var w=webcam_images[0];
    if(!(w.hour<=18&&w.hour>=6)){
        w=webcam_images[1];
    }
    $("webcam").src=w.src;
    $("webcam").title=w.title;
    $("webcam").alt=w.title;
}
function thumbOver(evt){
    $(evt.element()).parentNode.select("span").each(function(el){
        try{
            el.style.opacity=1;
            el.style.filter='alpha(opacity=100)';
        }catch(e){}
    });
}
function thumbOut(evt){
    $(evt.element()).parentNode.select("span").each(function(el){
        try{
            el.style.opacity=0.65;
            el.style.filter='alpha(opacity=65)';
        }catch(e){}
    });
}
function thumbClick(evt){
    $(evt.element()).parentNode.select("img").each(function(el){
        $("webcam").src=el.full;
        $("webcam").title=el.parentNode.title;
        $("webcam").alt=el.parentNode.title;
    });
    return false;
}
function debugWrite(s){
    if(!$("debug")){
        var div = document.createElement("DIV");
        div.id = "debug";
        div.style.position = "absolute";
        div.style.left = "0px";
        div.style.bottom = "0px";
        div.style.textAlign = "left";
        div.style.width = "200px";
        div.style.height= "200px";
        div.style.overflow = "auto";
        div.style.zIndex = 10;
        document.body.appendChild(div);
    }
    $("debug").innerHTML = s + "<br />" + $("debug").innerHTML;
}
function generateEmail(){var coded="QxQlhI@QxQlhI-DjRG.ulR";var key="RFsZE0K1i8wPQYXc7U39hDzabCVO4tIyJdevfuNom2kHAj6M5TqLpSrgxBWGnl";var shift=coded.length;var link=[];for(var i=0;i<coded.length;i++){if(key.indexOf(coded.charAt(i))==-1){ltr=coded.charAt(i);link.push(ltr);}
else{ltr=(key.indexOf(coded.charAt(i))-shift+key.length)%key.length;link.push(key.charAt(ltr));}}
return'<a href="mailto:'+link.join('')+'">'+link.join('')+'</a>';}