fx.Pictureframe = Class.create();
Object.extend( fx.Pictureframe.prototype, Fx.Style.prototype );
Object.extend( fx.Pictureframe.prototype, {

    fadeIn: function () {
        this.clearTimer();
        this.custom(0, 1);
    },

    fadeOut: function () {
        this.clearTimer();
        this.custom(1, 0);
    }

});

PictureFrame = Class.create();
Object.extend( PictureFrame.prototype, {

    initialize: function( frame ) {

        this.pictures = [];
        this.current = 0;

        this.elements = getAllChildrenContainClassname( frame, "pictureframe-item" );

        for( var i=0; i<this.elements.length; i++ ) {
            this.pictures[i] = new Fx.Pictureframe( this.elements[i], 'opacity', { duration: 1000 } ).set(0);
        }

        this.start();
    },

    start: function() {
        this.pictures[ this.current ].fadeIn();
        this.next();
    },

    next: function() {

        setTimeout( this.pictures[ this.current ].fadeOut.bind( this.pictures[ this.current ] ), 3500 );
        setTimeout( this.next.bind( this ), 5500 );

        if( typeof this.pictures[ this.current+1 ] == 'object' ) {
            setTimeout( this.pictures[ this.current+1 ].fadeIn.bind( this.pictures[ this.current+1 ] ), 3500 );
            this.current += 1;
        } else {
            setTimeout( this.pictures[ 0 ].fadeIn.bind( this.pictures[ 0 ] ), 3500 );
            this.current = 0;
        }
    }

});

window.onload = function() {

    myPictureFrames = [];
    pictureframes = getElementsByClassName( "pictureframe" );

    for( var i=0; i<pictureframes.length; i++ ) {
        myPictureFrames[i] = new PictureFrame( pictureframes[i] );
    }

}

function getElementsByClassName( className ) {
    var elements = document.getElementsByTagName( "*" );
    var matchingElements = [];
    var matchingClass = new RegExp( "(^|\\s)" + className.replace( /\-/g, "\\-" ) + "(\\s|$)" );
    for( var i=0; i<elements.length; i++ ) {
        if( matchingClass.test( elements[i].className ) ) {
            matchingElements.push( elements[i] );
        }
    }
    return matchingElements;
}

function hasClassName( element, className ) {
    var matchingClass = new RegExp( "(^|\\s)" + className.replace( /\-/g, "\\-" ) + "(\\s|$)" );
    if( matchingClass.test( element.className ) ) {
        return true;
    }
    return false;
}

function getAllChildrenContainClassname( parentElement, className ) {
    var children = parentElement.childNodes;
    var result = new Array;

    for ( var i = 0; i < children.length; i++ ) {
        if( children[i].className ) {
            var classNames = children[i].className.split(' ');
        } else {
            var classNames = new Array;
        }
        for ( var j = 0; j < classNames.length; j++ ) {
            if ( classNames[j] == className ) {
                result.push( children[i] );
            }
        }
        if ( children[i].childNodes.length > 0 ) {
                result = result.concat( getAllChildrenContainClassname( children[i], className ) );
        }
    }
    return result;
}
