var link_new_window = {
	init: function() {
		$$('a').each(function(el) {
			var href = el.getProperty('href');
			if(href.substring(0, 7) == 'http://' || href.substring(0, 8) == 'https://') {
				el.setProperty('target', '_blank');
			}
		});
	}
}

window.addEvent('domready', function() {
	link_new_window.init();
});

var slideshow = {
    init: function(match, idmatch, callback_func) {
        this.matches = $$(match);
        this.idmatches = $$(idmatch);
        this.next_id;
        this.prev_id;
        this.delayed_event = false;
        this.addEvents();
        this.callback = callback_func;

        this.matches.set('opacity', 0);
        this.matches.set('tween', { duration: 1000 });

        if(this.matches.length > 0) {
            this.matches[0].set('opacity', 1);
        }
        if(this.matches.length > 1) this.tween(0);
    },

    addEvents: function() {
        $$(this.idmatches).each(function(el, index) {
            el.addEvent('click', function() {
                this.tween(index);
            }.bind(this));
            
        }.bind(this));
        
        if($('slideshowNext')) {
            $('slideshowNext').addEvent('click', function() {
                this.tween(this.next_id);
                return false;
            }.bind(this));
        }
            
        if($('slideshowPrevious')) {
            $('slideshowPrevious').addEvent('click', function() {
                this.tween(this.prev_id);
                return false;
            }.bind(this));
        }
    },
    
    tween: function(ele_id) {
        this.matches.tween('opacity', 0);
        this.matches[ele_id].tween('opacity', 1);
        
        // calc next
        var next_int = ele_id + 1;
        if(next_int >= this.matches.length) next_int = 0;
        
        // calc previous
        var prev_int = ele_id - 1;
        if(prev_int < 0) prev_int = this.matches.length - 1;

        this.next_id = next_int;
        this.prev_id = prev_int;

        if(this.delayed_event) $clear(this.delayed_event);

        this.delayed_event = (function() { this.tween(next_int) }.bind(this)).delay(5000);

        if(this.callback) {
            eval(this.callback)(this.matches[ele_id]);
        }
    }
}
