/*************************************************************

	Class:    	ImageFader 
	Author:   	RossCo-Designs
	Website:    http://www.rossco-designs.com
	Version:  	1.0.1
	Date:     	28/09/2010 21:10
	Built For:  Mootools 1.3 and MODx Rev Gallery Integration
	License : 	The use of this code is under license and under conditions defined by Rossco-Designs.com.  
				The purchaser, or licensee, is given the right to use the software under certain conditions,
				but restricted from other uses, such as modification, further distribution, or reverse engineering.
				This code is Proprietary to Rossco-Designs.com.  
				Under no circumstances is this script allowed to be used without prior agreement with Rossco-Designs.com.
				
*************************************************************/
/*
var data = {"fader":[{"resource":"1","title":"1","src":"http://www.safworld.net/photos/user_images/phil/3017franklin_trees_01.jpg"},{"resource":"2","title":"1","src":"http://www.strattontreesurgeons.com/communities/1/004/008/154/151/images/4535943002.jpg"},{"resource":"3","title":"1","src":"http://www.droyt.com/blog/wp-content/uploads/2010/04/maple_trees-572.jpg"},{"resource":"4","title":"1","src":"http://www.amcotswoldtrees.co.uk/custom/tree%20pic2.jpg"}]};
*/   
    
var ImageFader = new Class({

  Implements: [Options, Events],
    options: {
		feedUrl : '',
        slideSelector: 'li',
        counterIdPrefix: 'counter-',
        slideDelay: 4000,
        slideDuration: 800,
		longtitle : '',
		introtext : ''
    },
    initialize: function (b, c, d) {
        this.setOptions(d);
        this.slideEl = $(b);
        this.counterEl = $(c);
        this.currentSlide = 0;
        this.totalSlides = 0;
        this.slides = {};
        this.timer = false;
        this.triggers = $$('.trig');
        this.getFeed();
    },
    buildWorld: function() {
        var f = this.items = this.slideEl.getElements(this.options.slideSelector);

        if (f) {
            this.totalSlides = f.length;
            this.start()
        }
    },
    autoSlide: function () {
        var a = this.currentSlide + 1;
        if (a >= this.totalSlides) {
            a = 0
        }
        
        this.items[this.currentSlide].fade('out');
        this.slideTo(a);
        var b = $$('.trig').morph({
            'opacity': '0.5'
        });
        var c = $$('#counter-' + a).morph({
           'opacity': '1'
        });
    },
    slideTo: function (i) {
        this.items[this.currentSlide].fade('out');
        this.currentSlide = i;
        this.items[i].setStyle('display', null);
        this.items[i].setStyle("opacity", 0).setStyle("display", "block").fade(1);
        var a = $$('.trig').morph({
            'opacity': '0.5'
        });
        var b = $$('#counter-' + i).morph({
            'opacity': '1'
        })
    },
    
    getFeed: function(){
        var self = this;
        
        var gallery = $('gallery');
		var thumbGallery = $('thumbs');
		var imagePath = '/assets/components/gallery/files/';
        var addImages = function(images){
            images.each(function(image){
				
                var el = new Element('li'),
                addimage = new Element('img', {'src':  imagePath+image.filename, 'alt':image.description,'height':'360px', 'width':'560px'}).inject(el);
				if(!document.id('offer') && image.name != ''){

					imageText = new Element('div', {'html' : '<div class="left50">'+image.name+'</div>' , 'class': 'imageTitle png' }).inject(el);
				}
                el.inject(gallery);
									$$('.imageTitle').setStyle('display','block');
            });
        };
		
		if(document.id('subProductHolder')){
			var prods = $$('#subProductHolder li .productOptA');
				var addProducts = function(products){
				products.each(function(product, key){
					var el = new Element('div', {'class':'productOptImg'});
					addImg = new Element('img', {'src': imagePath+product.filename, 'height':'110px', 'width':'110px'}).inject(el);
					if (prods[key])
					el.inject(prods[key], 'top');

				});
			};
		}

        var request = new Request.JSON({
			noCache: true,
            url: this.options.feedUrl,
            onRequest: function(){
               
            },
            onComplete: function(jsonObj) {
             
                addImages(jsonObj.fader);
				if(jsonObj.tagged){
					addProducts(jsonObj.tagged);
				}
                self.buildWorld();
            }
           
        }).send();
    },
    
    
    start: function () {
        this.timer = this.autoSlide.periodical(this.options.slideDelay, this)
    },
    stop: function () {
        if (this.timer) {
            $clear(this.timer)
        }
    }
});
