function wp_SlideShow( mcfg ) {

    var cfg = {
        id : mcfg.id,
        speed : mcfg.speed || 50,
        pause : mcfg.pause || 5000,
        manual : mcfg.manual || false,
        pause_on_hover : (typeof mcfg.pause_on_hover != "undefined")?mcfg.pause_on_hover:true,
        status_pause_icon_src : mcfg.status_pause_icon_src,
        status_play_icon_src : mcfg.status_play_icon_src,
        status_icon_padding : mcfg.status_icon_padding || 10,
        status_icon_top : mcfg.status_icon_top || "bottom",
        status_icon_left : mcfg.status_icon_left || "right",
        status_icon_opacity : mcfg.status_icon_opacity || 100
    };
    
    var self = this; 
    var count = 0;
    var preloadCount = -1; 
    var item = new Array(); 
    item[0] = new Object();
   
    var timer = null;
    var interval = null;
    var opacity = 100;
    var previous = null; 
   
    var previousIndex = 0; 
   
    var is_paused = false; 
    var img_pause = null; 
    var img_play = null; 
    var status_img = null; 
    
	// -- debug
	try{var debug = new wp_Log();}catch(e){var debug = new Object();debug.log = function(){};};
	this._function = "wp_SlideShow";
	this.debug = debug;
	// --
	
	function preloadError() {
	    debug.log(self,"Error loading image: "+(preloadCount+1));
	    item.splice(preloadCount,1);
	    preloadCount--;
	    preloadImages()
	}
	
	function preloadImages() {
	
	    preloadCount++;
	
	    if( preloadCount != 0 ) {
	        debug.log(self,(preloadCount) + " images loaded");
	    }
	    
	    if( preloadCount < item.length ) {
	        debug.log(self,"Initalizing loading image: "+(preloadCount+1)+" of "+ item.length);
            item[preloadCount].image = new Image();
            item[preloadCount].image.onload = preloadImages;
            item[preloadCount].image.onerror = preloadError;
            item[preloadCount].image.onabort = preloadError;
            item[preloadCount].image.src = item[preloadCount].src; 
        } else {
            debug.log(self,"All images loaded");
        }
	}
    
    this.add = function( data, pos ) {
        item[((typeof pos == "number")?pos:item.length)] = data;
    }  
    
    this.init = function() {
    
        cfg.scene = document.getElementById(cfg.id);
        var first = {
            src : cfg.scene.src,
            alt : cfg.scene.alt,
            href : cfg.scene.parentNode.href
        }
        
        self.add(first,0);
        preloadImages();        
        
        previous = document.createElement("IMG");
        
        if( cfg.pause_on_hover ) {
            previous.onmouseover = self.pause;
            previous.onmouseout = self.start;
        }
        previous.src = first.src;
        previous.style.position = "absolute";
        cfg.scene.parentNode.insertBefore(previous,cfg.scene);
        
        if( cfg.status_pause_icon_src ) {   
            img_pause = new Image();
            img_pause.src = cfg.status_pause_icon_src;
        }
        
        if( cfg.status_play_icon_src ) {   
            img_play = new Image();
            img_play.src = cfg.status_play_icon_src;
        }
        
        if( img_pause || img_play ) {
            status_img = document.createElement("IMG");
            status_img.style.position = "absolute";     
            status_img.style.opacity = cfg.status_icon_opacity/100;
            status_img.style.filter = "alpha(opacity="+cfg.status_icon_opacity+")";
            status_img.style.display = "none";
            cfg.scene.parentNode.insertBefore(status_img,cfg.scene);
        }
        
        if( !cfg.manual )
            timer = window.setTimeout(self.next,cfg.pause);
    }
   
    this.next = function() {
    
        window.clearTimeout(timer); 
        previousIndex = count;
        count++;
        if( count >= item.length ) {
            count = 0;
        }
        
        self.show( count );
    }
   
    this.previous = function() {
    
        window.clearTimeout(timer);
        previousIndex = count; 
        count--;
        if( count < 0 ) {
            count = item.length;
        }
        
        self.show( count );
    }
   
    this.show = function( index ) {
        
        window.clearTimeout(timer);
        debug.log(self,"item: "+index + ", src: "+ item[index].image.src );
        
        if( (typeof item[index].image.naturalWidth != "undefined" && item[index].image.naturalWidth < 1 ) || (typeof item[index].image.fileSize != "undefined" && item[index].image.fileSize < 1 ) ) {
            debug.log(self,"Image does not exist. Deleting...");
            item.splice(index,1);
            index = previousIndex;
            count = previousIndex;
        }   
        
        if( item[index].image.complete ) {
            cfg.scene.src = item[index].image.src;
        } else {
            count = previousIndex;
        }
        self.process();
    }  
   
    this.process = function() {
        
        if( !is_paused || opacity != 100 ) {
            opacity-=5;
            opacity = (opacity>0)?opacity:0;
            
            if( opacity > 0 ) {
                previous.style.opacity = opacity ? (opacity/100) : 0;
                previous.style.filter = "alpha(opacity="+opacity+")";
                timer = setTimeout(self.process,cfg.speed);
            } else {
            
                
                previous.src = cfg.scene.src;
                changeData();
                window.setTimeout( function(){
                    opacity = 100;
                    previous.style.opacity = (opacity/100);
                    previous.style.filter = "alpha(opacity="+opacity+")";
                },100); 
                
                if( !cfg.manual )
                    timer = window.setTimeout(self.next,cfg.pause);
            }        
        } else {
            count = previousIndex;
        }
    }
   
    function changeData() {
    
        if( cfg.scene.parentNode.tagName == "A" ) {
            cfg.scene.parentNode.href = (item[count].href || "JavaScript:void(0);");
            cfg.scene.parentNode.title = (item[count].alt || "");
            cfg.scene.alt  = (item[count].alt || "");
        } else {
            cfg.scene.alt  = (item[count].alt || "");
        }
    }  
   
    this.pause = function() {
        debug.log(self,"pausing..."); 
        is_paused = true;
        showStatusImage(img_pause);
    }
   
    this.start = function() {
        is_paused = false;
        showStatusImage(img_play);
        window.setTimeout(hideStatusImage,2000);
        if( opacity == 100 ) {
            window.clearTimeout(timer);
            debug.log(self,"starting...");
            timer = window.setTimeout(self.next,cfg.pause);
        }
    }
   
    function showStatusImage( img ) {
   
        if( img && status_img ) { 
            var sceneElement = new wp_Element({ element : cfg.scene });
            status_img.src = img.src;
            if( cfg.status_icon_top == "bottom" ) {
                status_img.style.top = (sceneElement.top + sceneElement.height - img_pause.height - cfg.status_icon_padding)+"px";
            } else if( cfg.status_icon_top == "center" ) {
                status_img.style.top = (sceneElement.top + (sceneElement.height/2) - (img_pause.height/2))+"px";
            } else {
                status_img.style.top = (sceneElement.top + cfg.status_icon_padding)+"px";
            }
            if( cfg.status_icon_left == "right" ) {
                status_img.style.left = (sceneElement.left + sceneElement.width - img_pause.width - cfg.status_icon_padding)+"px";
            } else if( cfg.status_icon_left == "center" ) {
                status_img.style.left = (sceneElement.left + (sceneElement.width/2) - (img_pause.width/2))+"px";
            } else {
                status_img.style.left = (sceneElement.left + cfg.status_icon_padding)+"px";
            }
            status_img.style.display = "block";
        }
    }
   
    function hideStatusImage() {
        if( status_img )
            status_img.style.display = "none";
    }  
   
    var e = new wp_Event(); 
    e.add(window,"load",self.init); 
}
