var SlideShow = function(container, options) {

    // Method definition //

    // Image loading. Accepts Image elements, json definitions and url strings
    this.load = function(image) {
        if( typeof image && typeof image == 'object' ) {
            if( image.nodeName && image.nodeName == 'IMG' ) {
                i = image.cloneNode(true);
            } else {
                var i = new Image();
                for( attr in image ) {
                    i[attr] = image[attr];
                }
            }
            this.images.push(i);

        } else if( typeof image == 'string' ) {
            var i = new Image();
            i.src = image;
            this.images.push(i);

        }

        if( this.firstLoad && this.images.length == 1 ) {
            this.jumpTo(0);
        }

    }

    this.next = function() {
        jump = this.pointer == null? 0 : this.pointer +1;
        this.jumpTo(jump);
    }    

    this.prev = function() {
        this.jumpTo(this.pointer -1);
    }

    this.reset = function() {
        this.jumpTo(this.placeholder ? null : 0);
    }

    this.setOnchange = function(func) {
        this.onchange = func;
    }

    this.jumpTo = function(num) {
        this.lastImage = this.container.getElementsByTagName('IMG')[0];
        if(num < 0) num = this.images.length -1;
        if(num >= this.images.length) num = 0;
        this.pointer = num;
        this._go();
    }

    this._go = function() {
        if( this.onchange ) {
            var nextImage = this.images[this.pointer];
            var lastImage = this.pointer != null? this.lastImage : this.placeholder;
            var pointer = this.pointer;
            this.onchange( nextImage, lastImage, pointer );
        } else {
            this._onchange();
        }
    }

    this._onchange = function() {
        this.container.replaceChild(this.images[this.pointer], this.lastImage);
    }

    // Object initialization //

    var self = this;
    this.container = document.getElementById(container);

    this.images = [];
    this.lastImage = null;
    this.placeholder = null;
    this.pointer = null; // null stands for placeholder or no imagea
    this.onchange = null;
    this.firstLoad = false;

    // Options parsing
    options = options || {};
    switch(  options.embedded ) {
        case 'included':
            this.load(this.container.getElementsByTagName('IMG')[0]);
            this.pointer = 0;
            break;
        case 'placeholder':
            this.placeholder = this.container.getElementsByTagName('IMG')[0];
            break;
        case 'omited':
            if( !this.container.children ) {
                var i = new Image();
                this.container.appendChild(i);
            }
            this.firstLoad = true;
        case 'loading':
    }

    if( options.images ) {
        var n = options.images.length;
        for( i=0; i<n; i++ ) {
                this.load(options.images[i]);
        }
    }

    if( options.controls ) {
        var elem;
        for( key in options.controls ) {
            if( elem = document.getElementById(key) ) {
                switch( options.controls[key] ) {
                    case 'next':
                        $(elem).click( function(e) {
                            e.preventDefault();
                            self.next();
                        } );
                        break;
                    case 'prev':
                        $(elem).click( function(e) {
                            e.preventDefault();
                            self.prev();
                        } );
                        break;
                    case 'reset':
                        $(elem).click( function(e) {
                            e.preventDefault();
                            self.reset();
                        } );
                        break;
                }
            }
        }
    }

    this.onchange = options.onchange;

}




