/**
 * MooSwap - Automatic swap images
 *
 * Dependencies: MooTools 1.2
 *
 * @version            0.1.1
 *
 * @license          MIT-style license
 * @author           Davide Caffaratti <davcaffa [at] gmail.com>
 * @copyright        Author
 * @site             http://davidecaffaratti.com
 */ 
var teller = 0;  
var image_1 = 1;  
var image_2 = 2;  
var array_element 	= new Array();  
var array_src 		= new Array();   
var array_old_src 	= new Array();  

var MooSwap = new Class({
			
    Implements: [Options],

    options: {
        imgHoverPrefix: '_hover',
        useTransition: false,
        transition: Fx.Transitions.Sine.easeOut,
        transitionDuration: 400,
        opacity: 1    
    },
    
    initialize: function(elements, options) {
        //set options
        this.setOptions(options);
        // Set elements
        this.setSwap(elements);
    },
    
    setSwap: function(elements) { 
        $$(elements).each(function(el) { 
          	teller++;          
            var holdSrc 	= el.getProperty('src');  
			//alert(holdSrc);
            var extension 	= holdSrc.substring(holdSrc.lastIndexOf('.'),holdSrc.length);       
            //var newSrc = holdSrc.replace(extension, this.options.imgHoverPrefix + '' + extension);
            var newSrc 		= holdSrc.replace('black-white', 'thumbs');
            var this_nr 	= teller;
            
            // set new image for preloading
            new Asset.image(newSrc, { alt: el.getProperty('alt') });
      
            // default link on current img element
            var link = el;
            
            // check if there is a link a href parent
            var parent_a = el.getParent('a');
            if (parent_a) {
                var link = parent_a;        
            }
            
			array_element[this_nr]	= el;
			array_src[this_nr]		= newSrc;
			array_old_src[this_nr]	= holdSrc;
			
			if (this.options.useTransition && this.options.opacity < 1) {
                el.setStyles({'opacity': this.options.opacity});  
                var effect = new Fx.Morph(el, {duration: this.options.transitionDuration, transition: this.options.transition});
            } 
            
            link.addEvents({
                mouseover: function() {
                    if (this.options.useTransition && this.options.opacity < 1) {
                        effect.cancel();
                        effect.start({opacity:1});
                    }
					el.setProperty('src', newSrc);
                }.bind(this),
                mouseout: function() {
                    if (this.options.useTransition && this.options.opacity < 1) {
                        effect.cancel();
                        effect.start({opacity: this.options.opacity});
                    }
                    el.setProperty('src', holdSrc);
                }.bind(this)
				/*click: function(){
					if(busyBig == false){
						if(array_element[image_1]){
							array_element[image_1].setProperty('src', array_old_src[image_1]);
						}
						if(array_element[image_2]){
							array_element[image_2].setProperty('src', array_old_src[image_2]);
						}
						if(this_nr%2 == 0){	
							image_1				= this_nr - 1;
							image_2				= this_nr;	
						}else{
							//de eerste..
							image_1				= this_nr;
							image_2				= this_nr + 1;	
						}
						
						if(array_element[image_1]){
							array_element[image_1].setProperty('src', array_src[image_1]);
						}
						if(array_element[image_2]){
							array_element[image_2].setProperty('src', array_src[image_2]);
						}
					}
					
				}.bind(this)*/
           });
        }, this);    
    }
});
