/**
 * @author femto
 */
var ThumnailInjecter = new Class({
    Implements: [Options, Events],
    options: {
        clickFunc: $empty,
        failedImage: undefined
    },
    
    initialize: function(destEl, id, imagefolder, suffix, altanative, newAdd, options){
        this.setOptions(options);
		
		this.destEl = destEl;
        
        this.id = id;
        this.altanative = altanative;
        this.newAdd = newAdd;
        
        this.thumnailContainer = new Array(this.id.length);
		
		var i = 0;
		
		var callback = function(){
            if (this.id[i]) {
                var tmp = this.id[i].toLowerCase().escapeRegExp();
                var imagePass = imagefolder + tmp + '/' + tmp + suffix;
                
				this.thumnailContainer[i] = new Element('div');
				
                var thumnailAnchor = new Element('a', {
                    'href': '#' + this.id[i]
                });
				
                var faceThum = new Element('img', {
                    'alt': this.altanative[i]
                });
                
                var thumCaption = new Element('a',{'class':'face_caption', 'href': '#' + this.id[i]}).set('text', this.altanative[i]);
                
                if (this.newAdd[i] == 'YES') 
                    var newMark = new Element('img', {
                        'src': 'images/common/new.gif',
                        'alt': 'new',
                        'class': 'new-mark'
                    })
                
				faceThum.set('src', imagePass);
                faceThum.inject(thumnailAnchor);
				thumnailAnchor.inject(this.thumnailContainer[i]);
                thumCaption.inject(this.thumnailContainer[i]);
                if (newMark) newMark.inject(this.thumnailContainer[i]);
							
                
                
                thumnailAnchor.addEvent('click', this.options.clickFunc);
				thumCaption.addEvent('click', this.options.clickFunc);
				
				i++;
				if(this.id[i]) callback.delay(1,this);
				else this.injectThumnails(this.destEl);
            }
        }
		callback.delay(1,this);

        /*this.id.each(function(item, index){
            if (item) {
                var tmp = item.toLowerCase().escapeRegExp();
                var imagePass = imagefolder + tmp + '/' + tmp + suffix;
                
				this.thumnailContainer[index] = new Element('div');
				
                var thumnailAnchor = new Element('a', {
                    'href': '#' + item
                });
				
                var faceThum = new Element('img', {
                    'alt': this.altanative[index]
                });
                
                var thumCaption = new Element('a',{'class':'face_caption', 'href': '#' + item}).set('text', this.altanative[index]);
                
                if (this.newAdd[index] == 'YES') 
                    var newMark = new Element('img', {
                        'src': 'images/common/new.gif',
                        'alt': 'new',
                        'class': 'new-mark'
                    })
                
				faceThum.set('src', imagePass);
                faceThum.inject(thumnailAnchor);
				thumnailAnchor.inject(this.thumnailContainer[index]);
                thumCaption.inject(this.thumnailContainer[index]);
                if (newMark) newMark.inject(this.thumnailContainer[index]);
							
                
                
                thumnailAnchor.addEvent('click', this.options.clickFunc);
				thumCaption.addEvent('click', this.options.clickFunc);
				
				this.injectThumnails(this.destEl);
            }
        }
.bind(this));*/
        
    },
    
    
    injectThumnails: function(injectTarget){
				
        this.thumnailContainer.each(function(item, index){
			if (this.thumnailContainer[index]) {
						this.thumnailContainer[index].inject(injectTarget);
			}
        }
.bind(this))
		var clearEl = new Element('div',{'class':'clear'}).inject(injectTarget);
		$('bottomImage').setStyle('height',injectTarget.getSize().y + 'px');
    }
})
