/**
 * Slide Show Component
 * Example:
 * new slideShow('element', {
 *	sourceUrl: 'source.xml', // Source XML
 *  customXmlParser: function(){}, //Custom XML Parser, return array of files.
 *  timeDelay: [int] //count second for changepicture
 * });
 */
slideShow = Class.create();
slideShow.prototype = {
    target: null,
    params: null,
    files: null,
    xml: null,
    curentIndex: null,
    initialize: function(targetElement, params){
        this.curentIndex = 0;
        this.files = null;
        this.xml = null;
        this.target = $(targetElement);
        if (!this.target) 
            throw ('Target Element is undefined');
        this.params = $H({
            timeDelay: 10
        });
        this.params.merge($H(params));
        if (this.params.sourceURL) 
            this.getFilesFromSource();
        this.slideShowIteration();
    },
    getFilesFromSource: function(){
        
        new Ajax.Request(this.params.sourceURL, {
            method: 'get',
            onSuccess: function(originalRequest){
                if (originalRequest.readyState == 4) {
                    var xml = originalRequest.responseXML;
                    
                    if (typeof(DOMParser) != "undefined") {
                        // Mozilla, Firefox, and related browsers   
                        this.xml = (new DOMParser()).parseFromString(originalRequest.responseText, "application/xml");
                    }
                    else 
                        if (typeof(ActiveXObject) != "undefined") {
                            // Internet Explorer.
                            
                            var doc = new ActiveXObject("Microsoft.XMLDOM"); // Create an empty document
                            doc.loadXML(originalRequest.responseText); // Parse text into it   
                            this.xml = doc; // Return it 
                        }
                    this.parseXml();
                }
            }.bind(this)
        });
        
    },
    parseXml: function(){
        if (this.params.customXmlParser) 
            this.files = this.params.customXmlParser.bind(this)();
        else {
            //some parse code
        }
        //preload first image
        this.preloadImage(0);
    },
    preloadImage: function(i){
        if (typeof(this.files[i]) == 'string') {
            var img = document.createElement('img');
            img.src = this.files[i];
            this.files[i] = img;
        }
    },
    slideShowIteration: function(){
        if (this.target) {        
            if (this.files && this.files.length && this.files[(this.curentIndex) % this.files.length].complete) {
                //				this.target.src = this.files[this.curentIndex];
                this.target.src = this.files[this.curentIndex].src;
                this.curentIndex = (++this.curentIndex) % this.files.length;				
                setTimeout(this.slideShowIteration.bind(this), this.params.timeDelay * 1000);
                if (this.params.sourceURL && !(this.curentIndex)) 
                    this.getFilesFromSource();
                else 
                    this.preloadImage(this.curentIndex);
            }
            else 
                setTimeout(this.slideShowIteration.bind(this), 1000);
        }
    }
};
