

Event.observe(window, 'load',initKH, false);

function initKH(){
   // appendHrefScript();
   if($('slidewrap_wide')){
    preloadImgs();
    lookForHash();
    buildStatus();
   }
    
}


var img = new Array();
var activeimg = 1;
function preloadImgs(){
    i = 1;
    images.each(function(image){
        img[i] = new Image;
        img[i].src = image;
        i++;
    });
}

var ziel;
function lookForHash(){
    if(ziel = window.location.hash.substr(1))
        showImage(ziel);
}

function buildStatus(id){

     if((!id) && (ziel)) id = ziel;
     if(!id) id = 1;
     if(id < 10) id = '0'+id;
     $('status').innerHTML = '('+id+'/'+i+')';
}

function showImage(id){
    if(img[id]){

       $$(".imagelink").each(
            function(element) {
             element.className = 'imagelink';
            });
       $$('.imagelink')[id-1].className = 'imagelink nav_link_active';
       activeimg = id;
    
       $('singleimage').style.backgroundImage = 'url('+img[id].src+')';

       $('headline').innerHTML = "<h2>"+infos[id].info.title+"</h2>";
       $('info').innerHTML = infos[id].info.content;

       
       $('next_link').href = window.location.pathname+'#'+(id);
       $('prev_link').href = window.location.pathname+'#'+(id);

       buildStatus(id);

       new Effect.Parallel(
            [new Effect.Appear($('singleimage'), { sync: true}),
            new Effect.Fade($('frontimage'), { sync: true})],
            {duration:0.5,transition:Effect.Transitions.linear,

            beforeStart: function(){

               // deactivate browsing to prevent flicker
               $('next_link').onclick = wait;
               $('prev_link').onclick = wait;
            },

            afterFinish: function(){
                $('frontimage').style.backgroundImage = 'url('+img[id].src+')';
                $('frontimage').show();
                Element.setOpacity($('frontimage'), 1);
                $('singleimage').hide();
                Element.setOpacity($('singleimage'), 0);
                
                // reactivate browsing
                $('next_link').onclick = showNextImg;
                $('prev_link').onclick = showPrevImg;

            }}
);
        
    }
}

function wait(){

}

function hallo(){
    alert('hi');
}

function showNextImg(){
    showImage(activeimg+1);
    if(is_int((activeimg-1)/10))
        showMorePics();
}

function showPrevImg(){
    showImage(activeimg-1);
    if(is_int((activeimg)/10))
        showLessPics();
}


function is_int( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Alex
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: is_int(186.31);
    // *     returns 1: false
    // *     example 2: is_int(12);
    // *     returns 2: true

    var y = parseInt(mixed_var * 1);

    if (isNaN(y)) {
        return false;
    }

    return mixed_var == y && mixed_var.toString() == y.toString();
}

fx.ElementScroll = Class.create();
fx.ElementScroll.prototype = Object.extend(new fx.Base(), {
    initialize: function(el, options) {
        this.el = el;
        this.iniMarginLeft = this.el.style.marginLeft;
        this.setOptions(options);
    },

    increase: function() {
        this.el.style.marginLeft = this.now + 'px';
    },

    toggle: function(){
        if (this.el.marginLeft > 0) this.custom(this.el.marginLeft, 0);
        else this.custom(0, this.iniMarginLeft);
    }
});

var visibleblock = 1;

function showMorePics(){
        if($('image_block_'+(visibleblock+1))){
        var clientTween = new fx.ElementScroll($('image_block_'+visibleblock), {duration: 400, transition: fx.sinoidal});
        clientTween.custom(0, -($('image_block_1').getWidth()));
       // $('more_link').hide();
       // $('back_link').show();
        visibleblock++;
        }
}

function showLessPics(){
       if($('image_block_'+(visibleblock-1))){
       var clientTween = new fx.ElementScroll($('image_block_'+(visibleblock-1)), {duration: 400, transition: fx.sinoidal});
        clientTween.custom(-($('image_block_1').getWidth()), 0);
       // $('more_link').show();
       // $('back_link').hide();
            visibleblock--;
       }
}




function appendHrefScript() {

    $$(".has_subnavi").each(
            function(element) {
                Event.observe(element.down(), "click",  function(event){
                    Event.stop(event);
                    var ul = element.down(1);
                    toggleSubnavi(ul);
                    return false;
                }, false);
    });
}

function toggleSubnavi(element){

    $$(".sub_menu").each(function(e) {
       if(e.down(1).href != element.down(1).href)
            new Effect.BlindUp(e, {duration:0.5});

    });


    if(Element.visible(element)){
        new Effect.BlindUp(element, {duration:0.5});
    } else {
        new Effect.BlindDown(element, {duration:0.5});
    }
}


function changeState(event){
    var active = Event.element(event);
      $$('.navi_sub').each(function(element) {
               element.up().className = "";
               if(active == element.href)
                element.up().className = "nav_active";
      });
}

function changeState2(active){
      $$('.exlusive_button').each(function(element) {
               element.className = "exlusive_button";
               if(active == element)
                element.className = "exlusive_button active";
      });
}

function SlideDown(element) {

   if($(element)) {
        new Effect.SlideDown(element, {duration:0.3});
   }
}

function formSubmit(form){
var target = form.parentNode.parentNode;
var params = Form.serialize($(form));
new Ajax.Updater(
    target, form.action,
    {   method:'post',
        asynchronous:true,
        evalScripts:true,
        onComplete: function() {
           // new Effect.Highlight(target, {duration:0.5,startcolor:'#cccccc'})
        },
        parameters:params});
return false;
}




function updateRight(event){

    var element = Event.element(event);

    if ("IMG" == element.tagName) {
        url = element.parentNode.href;
    } else {
        url = element;
    }

    Event.stop(event);
    var url = url+'?ajax=2';
    var target = $('extension');
    killDrags();

        new Ajax.Updater(
            target,
            url,
            {   method:'get',
                asynchronous:true,
                evalScripts:true,
                onComplete: function() {
                    target.hide();
                    new Effect.Appear(target, {duration:0.2});
                    initLightbox();
                }
            }
        );


}

function loadContent(event){

    var element = Event.element(event);

    if ("IMG" == element.tagName) {
        url = element.parentNode.href;
    } else {
        url = element;
    }

    if(url.href.indexOf("?")!==-1){
        var url = url+'&ajax=1';
    }else{
        var url = url+'?ajax=1';
    }


    Event.stop(event);
    
    var target = $('content');
    doAjax(target, url)


}

function doAjax(target, url) {

        new Ajax.Updater(
            target,
            url,
            {   method:'get',
                asynchronous:true,
                evalScripts:true,
                beforeStart: function() {
                    initKH();
                    initLightbox();
                },
                onSuccess: function(){

                }
            }
        );

}

