var Feedback = function()
{
    this.init();
}

Feedback.prototype = {

    div: false,

    init: function()
    {
        var div = $( '#feedback' );
        if( !div.length )
        {
            div = this.createFeedback();
        }
        this.div = div;
        $( document.body ).append( div );
    },

    createFeedback: function()
    {
        var div = $( '<div id="feedback"><h2>Meddelande</h2><ul></ul>\
                <input type="submit" id="feedbacksubmit" onclick="this.parentNode.parentNode.removeChild( this.parentNode );return false;" value="OK" class="button_small"></div>' );

        return div;
    },

    addFeedback: function( str )
    {
        var found = false;
        this.div.find( 'li' ).each(
            function()
            {
                if( $( this ).html() == str ) found = true;
            }
        );

        if( !found )
        {
            this.div.append( $( '<li>'+ str +'</li>' ) );
        }
    },

    removeFeedback: function()
    {
        this.div.remove();
    }

};

$( function()
{
    $follow = $( '#menu-follower' );
    if( $follow.length )
    {
        $wrapper = $( '#wrapper' );
        var width = $follow.width();
        var max_x = 0;
        $( '#menu li' ).each(
            function()
            {
                max_x += this.offsetWidth;
            }
        );
        max_x = max_x - ( width / 2 );

        $( '#menu' ).mousemove(
            function( e )
            {
                var x = e.pageX - $wrapper.get(0).offsetLeft - ( width / 2 );
                if( x >= 0 && x < max_x )
                    $follow.css( 'left', x );
            }
        );

        var $active = $( '#menu .active' );
        if( $active.length )
        {
            var pos = $active.getPos();
            $follow.css( 'left', pos.left );
        }
    }

    $( '.randoms' ).each(
        function()
        {
            //var images = $( this ).find( 'img' );
            var images = new Array();
            var $this = $( this );
            for( var i = 0, l = window.randomimages.length; i < l; i++ )
            {
                var img = new Image();
                img.src = window.randomimages[i];
                img = $( img );
                images[images.length] = img;
                img.css( 'opacity', 0 );
                $this.append( img );

                //preloadImages( window.randomimages[i] );
            }

            setTimeout(
                function()
                {
                    var x = 0;
                    for( var i = 0, l = window.randomimages.length; i < l; i++ )
                    {
                        var img = new Image();
                        img.src = window.randomimages[i];
                        img = $( img );
                        images[images.length] = img;
                        img.css( 'opacity', 0 );
                        $this.append( img );

                        setTimeout( function() { preloadImages( window.randomimages[x] ); x++; }, i * 3000 );
                    }
                },
                1000
            );

            if( images.length < 2 ) return false;

            var fadetimer = false;
            var switchtimer = false;
            var currentimage = false;
            var nextimage = false;

            currentimage = images[0];
            currentimage.css( 'opacity', 1 );

            var duration = 5000;

            nextimage = currentimage.next( 'img' );

            switchtimer = setTimeout( switchImages, duration );

            function switchImages()
            {
                fade();
            }

            function fade()
            {
                if( parseFloat( currentimage.css( 'opacity' ) ) > 0 && parseFloat( nextimage.css( 'opacity' ) ) < 1 )
                {
                    var op1 = parseFloat( currentimage.css( 'opacity' ) ) - 0.1;
                    currentimage.css( 'opacity', op1 );

                    var op2 = parseFloat( nextimage.css( 'opacity' ) ) + 0.1;
                    nextimage.css( 'opacity', op2 );

                    fadetimer = setTimeout( fade, 50 );
                }
                else
                {
                    currentimage = nextimage;
                    nextimage = nextimage.next( 'img' );
                    if( !nextimage.length ) { nextimage = $( images[0] ); }

                    clearTimeout( fadetimer );
                    switchtimer = setTimeout( switchImages, duration );
                }
            }
        }
    );

    function preloadImages()
    {
        var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
        var i,j=d.MM_p.length,a=arguments; for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

    }

} );
