document.observe('dom:loaded', function(){
	fire_module_load_event();
});

document.recalc = function(){
	if(IE7.CSS.screen) IE7.recalc();
}

var popup = function(location, name, width, height, scroll) {
	new SearchlightUtils.Popup({
		location: location, 
		name: name, 
		width: width, 
		height: height, 
		scroll: (scroll == 1 || scroll == true)? true : false
	});
}

// Someone added these, now we have to support them. Please don't do the same.
var NewWindow = popup; 
var new_popup = function(location, width, height, scroll){
	return popup(location, '', width, height, scroll);
}

var fire_module_load_event = function(){
	if(Prototype.Browser.IE){
		Event.observe(window, 'load', function(){
			$(document).fire('searchlight:modules_loaded');
		});
	} else {
		$(document).fire('searchlight:modules_loaded');
	}
}

var tag_module_load_tag = function(element, tag_id){
	new Ajax.Request('?tag='+tag_id, {
		method: 'get',
		onSuccess: function(transport){
			$('modules').update(transport.responseText);
			$('tag-module').select('.active').first().removeClassName('active');
			$(element).addClassName('active');
			
			fire_module_load_event();
		}
	});
	return false;
}

var SearchlightPoll = Class.create({
	initialize: function(options){
		this.options = {
			id: '', 
			poll_id: 0, 
			question: '', 
			answers: []
		};
		Object.extend(this.options, options || {});
		
		this.answers = Array();
		this.container = $(this.options.id);
		
		this.build();
	}, 
	
	build: function(){
		this.answer_container = new Element('ul');
		
		this.options.answers.each(function(item, i){
			this.answers[i] = new Element('li').update('<span>&raquo;</span>'+this.options.answers[i].answer);
			this.answers[i].observe('click', function(){ this.vote(i); }.bind(this))
			this.answers[i].observe('mouseover', function(){ this.addClassName('selected'); })
			this.answers[i].observe('mouseout', function(){ this.removeClassName('selected'); });
			this.answers[i].id = this.options.answers[i].id;
			
			this.answer_container.insert(this.answers[i]);
		}.bind(this));
		
		this.container.insert(new Element('h2').update(this.options.question));
		this.container.insert(this.answer_container);
		document.recalc();
	}, 
	
	vote: function(i){
		this.answers[i].addClassName('selected');
		new Ajax.Request('/api/poll/vote.php', {
			method: 'post',
			parameters: { poll_id: this.options.poll_id, answer_id: this.answers[i].id }, 
			onSuccess: function(transport){
				var percentages = transport.responseText.evalJSON(true);
				
				this.answers.each(function(item, i){
					Event.stopObserving(item);
					item.insert(new Element('span', {'class':'percentage'}).update(percentages[i]+'%'));
				});
				document.recalc();
			}.bind(this)
		});
	}
});

var SearchlightSlideshow = 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, title:null, src:null }]
		};
		Object.extend(this.options, options || {});
		
		this.frames = Array();
		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].src, 
					title: this.options.images[i].title, 
					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.update(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;
	}
});

