var Slideshow = function(){

    var instanz = null;

    function Slideshow(){

        var timer = 4000;
        var bg_dimension = [];
        var background = null;

        var slideshow_count = -1;
        var current_image   = -1;

        var wrapper = null;
        var imgwrapper = null;
        var img     = null;

        var last_width  = 200;
        var last_height = 200;

        var interval = null;

        var stop = false;

        var nexttimeout = null;

        var route = "index.php";

        (function(){

            // bild Count holen
            if( window.location.search != "" )
                route = "index.php"+window.location.search;
            else
                route = window.location.pathname;

            var ajax = {
                path:route,
                method:"POST",
                params:{
                    get:"getSlideshowCount"
                }
            };

            new Load.ajaxContent(ajax).initLoad(null, function(content){
                slideshow_count = content;
            });
        })();

        function setOpacity(node,value){
            if(!navigator.userAgent.match(/msie/i) ) {
                node.style.opacity = value;
            } else {
                node.style.filter = "alpha(opacity="+(Math.round(value*100))+")";
            }
        };

        function getScreenWidth (){
            var width  = 0;
            var height = 0;

            if(window.innerWidth) {
                width  = window.innerWidth;
                height = window.innerHeight;
            } else {
                width  = document.documentElement.clientWidth;
                height = document.documentElement.clientHeight;
            }

            bg_dimension = [width,height];            
        }

        function createGround(){
            
            getScreenWidth();

            if(!background) {
                background = Element.create('div');

                Element.attrib(background,"style",{
                    position:"absolute",
                    top:"0px",
                    left:"0px",
                    zIndex:10
                });

                background.className = "slideshow_background";

                wrapper = Element.create("div");
                wrapper.id = "slideshow";

                imgwrapper = Element.create("div");
                imgwrapper.className = "imgwrapper";

                img     = new Image();

                imgwrapper.appendChild(img);

                // close button
                var btn      = Element.create("a");
                btn.href = "#";

                btn.className   = "closeslide";

                Element.addEvent(btn,"click",stopSlideShow);

                wrapper.appendChild(btn);
                wrapper.appendChild(imgwrapper);

                background.appendChild(wrapper);

                Element.attrib(imgwrapper,"style",{
                    height:"200px",
                    width:"200px"
                });

                wrapper.style.background = "#fff";

                document.getElementsByTagName("body")[0].appendChild(background);
            }
            
            img.style.display = "none";

            Element.attrib(background,"style",{
                display:"block",
                width:bg_dimension[0]+"px",
                height:bg_dimension[1]+"px"
            });

        };

        function grow(display_img,width,height){

            var velocity = 0.135;
            var speed    = 0;

            var new_width  = 0 ;
            var new_height = 0;

            var scale     = [Math.abs(width-last_width),Math.abs(height-last_height)];
            var scale_mod = [(last_width < width)?1:-1,(last_height < height)?1:-1];

            img.style.display = "none";
            img.src = display_img;

            var scaleTo = 0;

            interval = window.setInterval(Scale,40);

            function Scale(){

                speed += velocity;

                if(speed >= Math.PI/2) {

                    scaleTo++;

                    if(scaleTo < 2) {
                        last_width = width;
                        imgwrapper.style.width = width+"px";
                        wrapper.style.width    = width+"px";
                        img.style.width = width+"px";

                        speed = 0;
                    } else {
                        last_height = height;
                        imgwrapper.style.height = height+"px";
                        wrapper.style.height    = height+"px";

                        img.style.height  = height+"px";
                        img.style.display = "block";

                        setOpacity(img,0);

                        fadeImg();
                        
                        window.clearInterval(interval);
                        nexttimeout = window.setTimeout(getNextImage,timer);
                    }

                } else {

                    var nw = scale[scaleTo] * Math.sin(speed) * scale_mod[scaleTo];

                    switch(scaleTo) {
                        case 0:

                            new_width = last_width+nw;

                            imgwrapper.style.width = new_width+"px";
                            wrapper.style.width    = new_width+"px";

                            break;
                        case 1:
                            new_height = last_height+nw;

                            imgwrapper.style.height = new_height+"px";
                            wrapper.style.height    = new_height+"px";

                    }
                }
            };
        };

        function fadeImg(){
          
          var speed = 0;
          var velocity = 0.11;

            function innerFade(){
                speed += velocity;
                var new_opa = Math.sin(speed);

                if(speed >= Math.PI/2) {
                    setOpacity(img,1);
                } else {
                    setOpacity(img,new_opa);
                    window.setTimeout(innerFade,40);
                }
            }            
            innerFade();
        };

        function getNextImage(){            
            if(!stop) {
                if( (current_image+1) == slideshow_count)
                    current_image = 0;
                else
                    current_image++;

                new Load.ajaxContent({
                    method:"POST",
                    path:route,
                    params:{
                        get:"getSlideshowImage",
                        imgid:current_image
                    }
                }).initLoad(null,function(content){
                    var img = content.split("#");

                    grow(img[0],parseInt(img[2]),parseInt(img[3]));
                });
            }
        };

        function startSlideShow(){
            createGround();

            stop   = false;
            // das erste Bild holen
            getNextImage();
        };

        function stopSlideShow(){

            if(interval)    window.clearInterval(interval);
            if(nexttimeout) window.clearTimeout(nexttimeout);

            background.style.display = "none";

            // Hintergrundbild verstecken
            try {
                var bg = document.getElementById("bg");
                    bg.style.display = "";
            } catch (e){}

            stop = true;
        };

        this.init = function(){
            // Hintergrundbild verstecken
            try {
                var bg = document.getElementById("bg");
                    bg.style.display = "none";
            } catch (e) {}

            startSlideShow();
        };
    };

    return (function(){
        if(!instanz)
            instanz = new Slideshow();

        return instanz;
    })();
}();