var wcl = function(message){ if(window.console) return window.console.log(message); }

var px = function(dimension){ return dimension + 'px'; }

var to_url = function(string){
    return string.toLowerCase().gsub(' ', '-');
}

var flash_content = function(id, movie, width, height, options){
    var options = Object.extend({
        version: "9.0.0"
    }, options || {});
    
    swfobject.embedSWF(movie, id, width, height, options.version, "expressInstall.swf");
}

var pollutantPopup = function(pollutant){
    var width = 500, height = 540;
        left = (screen.width - width) / 2, 
        top = ((screen.height - height) / 2) - 50;
    
    var popup = window.open("/programs/mapping-initiative/pollutants.html#" + pollutant, "_blank", "scrollbars=0,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);
    if(popup){
        popup.moveTo(left, top);
        popup.focus();
    }
}

Element.addMethods({
    insertCollection: function(element, collection){
        // TODO: Support placement: top, bottom, before, after.
        element = $(element);
        collection.each(function(item, i){
            this.insert(item);
        }, element);

        return element;
    }
});


// Even-out the height of modules in a row. 
var normalize_module_heights = function(){
    var offset = 0, max_height = 0;
    var modules = [];
    
    var elements = $$('.landing.module .content, .meeting');
    elements.each(function(module){
        if(module.positionedOffset()[1] > offset && modules.length > 0){
            // New row. Apply height to previous modules.
            modules.each(function(m){
                m.setStyle({ height: px(max_height) });
            }).clear();
            max_height = 0;
        } 
        
        if(module.getHeight() > max_height)
            max_height = module.getHeight();
        
        offset = module.positionedOffset()[1];
        modules.push(module);
    });
    
    // Last row.
    if(modules.length > 0){
        modules.each(function(m){
            m.setStyle({ height: px(max_height) });
        }).clear();
    }
}
Event.observe(window, 'load', normalize_module_heights);


var format_captioned_images = function(){
    var images = $$(['.column.large img.left', '.column.large img.right']);
    images.each(function(image){
        if(![undefined, 'undefined', ''].include(image.alt)){
            var container = new Element('div').addClassName(image.className);
            container.addClassName('image');
            image.removeClassName(image.className);
            
            var caption = new Element('p').setStyle({ width: px(image.width) });
            var href = image.getAttribute('data');
            if(![undefined, 'undefined', ''].include(href)){
                caption.update(new Element('a', { href: href }).update(image.alt));
                image.removeAttribute('data');
            } else {
                caption.update(image.alt);
            }
            
            image.wrap(container);
            container.insert({ bottom: caption });
        }
    });
}
Event.observe(window, 'load', format_captioned_images);


var Slideshow = Class.create({
	initialize: function(options){
		this.options = {
			id: '', 
			onclick: false, 
			
			width: 320, 
			height: 250, 
			
			wait_duration: 5, 
			transition_duration: 1, 
			
			image_path: '', 
			images: [{ id:null, image:null }]
		};
		Object.extend(this.options, options || {});
		
		this.frames = [];
		this.current_frame = 0;
		this.playing = false;
		
		this.build();
	}, 
	
	build: function(){
		this.container = $(this.options.id);
		this.image_container = new Element('ul').setStyle({
			width: px(this.options.width), 
			height: px(this.options.height), 
			position:'relative'
		});
		
		this.options.images.each(function(item, i){
			this.frames[i] = new Element('li').update(
				new Element('img', {
					src: this.options.image_path + this.options.images[i].image, 
					width: this.options.width, 
					height: this.options.height
				})
			).setStyle({
				top: 0, 
				left: 0, 
				width: px(this.options.width), 
				height: px(this.options.height), 
				position:'absolute'
			});
			
			if(this.options.onclick)
				this.frames[i].observe('click', this.options.onclick);
			
			this.image_container.insert(this.frames[i]);
			if(i > 0) this.frames[i].hide();
		}.bind(this));
		
		this.container.insert({ top: this.image_container });
		
		this.play();
	}, 
	
	play: function(){
		if(!this.playing) this.advance_after_delay();
		this.playing = true;
	}, 
	
	pause: function(){
		if(this.playing) this.executor.stop();
		this.playing = false;
	}, 
	
	advance_after_delay: function(){
		this.executor = new PeriodicalExecuter(this.advance_frame.bind(this), 
			this.options.wait_duration + this.options.transition_duration);
	}, 
	
	advance_frame: function(){
		var options = { duration: this.options.transition_duration };
		new Effect.Fade(this.frames[this.current_frame], options);
		
		this.current_frame = this.normalize_frame_number(this.current_frame + 1);
		new Effect.Appear(this.frames[this.current_frame], options);
	}, 
	
	normalize_frame_number: function(frame){
		if(frame < 0) return (this.frames.length - 1);
		if(frame > (this.frames.length - 1)) return 0;
		return frame;
	}
});
