var Slideshow = new Class({
	
    initialize: function(slides, container, delay) {
        
		this.delayTime = 6500;
		this.delayTimer;
        this.currentIdx = 0;
        this.nextIdx = 0;
        this.fxIdx = 1;
        this.nextFxIdx = 0;
		this.navButtons = [];

        container = $(container);
        
        if ($type(delay) == false) delay = 0; // Default start delay
        
        this.loader = container.getElement('.loading'); // Loader
        
        // Slide list and info/fx arrays
        
        this.slides = slides;
        this.fx = [];
        
        var preload = [];
        
        this.slides.each(function(item) {
            preload.push(item.img);
        });
        
        // Preload images
        
        new Asset.images(preload, {
            onComplete: function() {
                
                // Create two image elements
                
                var obj0 = {};
                
                var img0 = new Element('div', {'class': 'img-slide'});
                obj0.img = img0.injectInside(container);
                //obj0.fx = new Fx.Tween(obj0.img, 'opacity', {duration: 750}).set(0);
				obj0.fx = new Fx.Tween(obj0.img, {duration: 750}).set('opacity', '0');
                
                this.fx[0] = obj0;
                
                if (this.slides.length > 1) {
                
                    var obj1 = {};
                
                    var img1 = new Element('div', {'class': 'img-slide'});
                    obj1.img = img1.injectInside(container);
                    //obj1.fx = new Fx.Tween(obj1.img, 'opacity', {duration: 750}).set(0);
					obj1.fx = new Fx.Tween(obj1.img, {duration: 750}).set('opacity', '0');
                	
                    this.fx[1] = obj1;
                }
                this.begin.delay(delay, this); // Start slideshow
                
            }.bind(this)
        });
    },
    
    begin: function() {
        this.loader.setStyle('display', 'none');
        this.setImg();
        if (this.slides.length > 1) {
            this.fx[0].fx.start('opacity', '1').chain(function(){
                //this.nextImg.delay(this.delayTime, this);
				this.cycle();
            }.bind(this))
			
			//add navigation
			for(x=1; x<=this.slides.length; x++){
				var myId = "mainslider-nav-"+x;
				var myLinkHref = "javascript: mySlideshow.nextImg("+(x-1)+")";
				var myLink = new Element('a', {href: myLinkHref, id: myId}); //, class: myClass
				if (x == 1){ myLink.addClass("selstate"); }
				
				this.navButtons[x] = myLink;
				myLink.inject( $("mainslider-nav") );
			}
			//some Customized goodness for the ui
			var myNavWidth = this.slides.length * 21;// this is the width+margin of the links.
			$("mainslider-nav").setStyle("width", myNavWidth);
			var myNavOffset = (927/2) - (myNavWidth/2);
			$("mainslider-nav").setStyle("margin-left", myNavOffset);
			//listen to nav
			$("mainslider-nav").getElements('a').addEvent('click', function(evt) {
                //alert( this.get("rel") );
				//alert( evt )
            }.bind(this));
			
			
        } else { // Single image, just fade out loader and fade up first image
            this.fx[0].fx.start('opacity', '1');
        }
    },
    
    nextImg: function(whatImage) {
        this.currentIdx = this.nextIdx;
		if (whatImage == undefined) {
			this.nextIdx = (this.currentIdx == (this.slides.length - 1)) ? 0 : (this.currentIdx + 1);
		} else {
			this.nextIdx = whatImage;
		}
		
        this.fxIdx = this.nextFxIdx;
        this.nextFxIdx = (this.fxIdx == 0) ? 1 : 0;
        this.setImg();
        this.cycle();
    },
    
    setImg: function() {
		
		//set navigation
		for(x=1; x<=this.slides.length; x++){
			if( this.nextIdx == (x-1) ){
				$("mainslider-nav-" +x ) != null ? $("mainslider-nav-" +x ).addClass("selstate") : "";
			}else{
				$("mainslider-nav-" +x ) != null ? $("mainslider-nav-" +x ).removeClass("selstate") : "";
			}
		}
		
        // Clear link settings
        
        this.fx[this.nextFxIdx].img.removeClass('linked');
        this.fx[this.nextFxIdx].img.removeEvents('click');
        this.fx[this.nextFxIdx].img.removeEvents('over');
        this.fx[this.nextFxIdx].img.removeEvents('out');
        
        // Update properties
        //
		this.fx[this.nextFxIdx].img.style.backgroundImage = "url('"+this.slides[this.nextIdx].img +"')";
        //this.fx[this.nextFxIdx].img.src = this.slides[this.nextIdx].img;
        //this.fx[this.nextFxIdx].img.alt = this.slides[this.nextIdx].imgName;
        
        // Set linking if necessary
        
        if (this.slides[this.nextIdx].imgUrl != '') {
            this.fx[this.nextFxIdx].img.addClass('linked');
            this.fx[this.nextFxIdx].img.addEvent('click', function(evt) {
                document.location.href = this.slides[this.nextIdx].imgUrl;
            }.bind(this));
            this.fx[this.nextFxIdx].img.addEvent('over', function(evt) {
                this.fx[this.nextFxIdx].img.setStyle('cursor', 'pointer');
            }.bind(this));
            this.fx[this.nextFxIdx].img.addEvent('out', function(evt) {
                this.fx[this.nextFxIdx].img.setStyle('cursor', 'normal');
            }.bind(this));
        }
    },
    
    cycle: function() {
		
        // Set z index
        this.fx[this.nextFxIdx].img.setStyle('z-index', 0);
        this.fx[this.fxIdx].img.setStyle('z-index', 1);
        
        this.fx[this.nextFxIdx].fx.start('opacity', '1');
        this.fx[this.fxIdx].fx.start('opacity', '0').chain(function() {
			this.delayTimer = $clear(this.delayTimer);
            this.delayTimer = this.nextImg.delay(this.delayTime, this);
        }.bind(this));
    }
    


});
