﻿
var ResidentialGallery = function(){
    var config, wrapper, slides, thumbs, currentIdx, autoPlayTimer;
    var firstRun = true;
    
    function showImage(idx){
        
        if (!firstRun) {
            var objFadeOut = new Fx.Tween(slides[currentIdx], {duration: 2000});
            objFadeOut.start('opacity', 1, 0);
        } else {
            firstRun = false;
        }
                
        var objFadeIn = new Fx.Tween(slides[idx], {duration: 2000});
        objFadeIn.start('opacity', 0, 1);
        
        currentIdx = idx;

        if (config.autoPlay === true){
            var newIdx = ((idx + 1) >= slides.length) ? 0 : (idx + 1);
            autoPlayTimer = showImage.delay(config.speed, this, newIdx);
        } else if (autoPlayTimer) {
            $clear(autoPlayTimer);
        }
    }
    
    return {
        init: function(cfg){
            config = cfg;
            
            wrapper = $(config.wrapper);
            slides = $$(config.slides);
            
            if (config.showThumbs === true){
                thumbs = $$('div.gallery ul li a');
            }
            
            currentIdx = Math.floor(Math.random() * (slides.length));
                        
            slides.each(function(el, idx){
                el.setStyles({
                    position: 'absolute',
                    visibility: 'hidden',
                    opacity: 0
                });
            });
            
            wrapper.setStyle('height', config.height);
            
            showImage(currentIdx);
            
            if (thumbs){
            
                $('gallery-thumbs').setStyle('display', 'block');
                
                thumbs.each(function(el, idx){
                    el.addEvent('click', function(e){
                        e.stop();
                        var idx = parseInt(el.rel.replace('slide-', ''), 10) - 1;
                        config.autoPlay = false;
                        showImage(idx);                    
                    })
                });
            }
        }
    }
}();

var CommercialGallery = function(){
    var config, wrapper, slides, thumbs, currentIdx, autoPlayTimer;
    var firstRun = true;
    
    function showImage(idx){
        
        if (!firstRun) {
            var objFadeOut = new Fx.Tween(slides[currentIdx], {duration: 2000});
            objFadeOut.start('opacity', 1, 0);
        } else {
            firstRun = false;
        }
                
        var objFadeIn = new Fx.Tween(slides[idx], {duration: 2000});
        objFadeIn.start('opacity', 0, 1);
        
        currentIdx = idx;

        if (config.autoPlay === true){
            var newIdx = ((idx + 1) >= slides.length) ? 0 : (idx + 1);
            autoPlayTimer = showImage.delay(config.speed, this, newIdx);
        } else if (autoPlayTimer) {
            $clear(autoPlayTimer);
        }
    }
    
    return {
        init: function(cfg){
            config = cfg;
            
            wrapper = $(config.wrapper);
            slides = $$(config.slides);
            
            if (config.showThumbs === true){
                thumbs = $$('div.gallery ul li a');
            }
            
            currentIdx = Math.floor(Math.random() * (slides.length));
                        
            slides.each(function(el, idx){
                el.setStyles({
                    position: 'absolute',
                    visibility: 'hidden',
                    opacity: 0
                });
            });
            
            wrapper.setStyle('height', config.height);
            
            showImage(currentIdx);
            
            if (thumbs){
            
                $('gallery-thumbs').setStyle('display', 'block');
                
                thumbs.each(function(el, idx){
                    el.addEvent('click', function(e){
                        e.stop();
                        var idx = parseInt(el.rel.replace('slide-', ''), 10) - 1;
                        config.autoPlay = false;
                        showImage(idx);                    
                    })
                });
            }
        }
    }
}();


window.addEvent('domready',function(){
	ResidentialGallery.init({
		autoPlay: true,
		speed: 8000,
		wrapper:'residential-wrapper',
		slides:'div.residential',
		height: 390
	});
	
	CommercialGallery.init({
		autoPlay: true,
		speed: 8000,
		wrapper:'commercial-wrapper',
		slides:'div.commercial',
		height: 390
	});
});

