
var SearchlightPaginator = Class.create({
	initialize: function(args){
		this.container = $(args.container);
		this.page_container = $(args.pages);
		this.pages = this.page_container.childElements();
		
		this.view_all_url = args.view_all_url;
		this.previous_page = false;
		this.current_page = 0;
		this.animating = 0;
		
		if(this.pages.length > 1)
			this.build_paginator();
	}, 
	build_paginator: function(){
		this.paginator = new Element('ul', {'class':'pagination'});
		this.page_indicators = Array();
		
		for(var page = 0; page < this.pages.length; page++){
			var indicator = new Element('li');
			if(page == this.current_page) indicator.addClassName('active');
			indicator.page = page;
			indicator.paginator = this;
			
			indicator.observe('click', function(){
				this.paginator.show_page(this.page);
			});
			this.page_indicators[page] = indicator;
			this.paginator.insert(this.page_indicators[page]);
		}
		
		// Previous button.
		this.btn_previous = new Element('li', {'class':'previous'});
		this.btn_previous.observe('click', this.show_previous_page.bind(this));
		this.paginator.insert({'top': this.btn_previous });
		
		// Next button.
		this.btn_next = new Element('li', {'class':'next'});
		this.btn_next.observe('click', this.show_next_page.bind(this));
		this.paginator.insert(this.btn_next);
		
		// View all button.
		if(this.view_all_url){
			this.btn_view_all = new Element('li', {'class':'all'});
			this.btn_view_all.observe('click', this.view_all.bind(this));
			this.paginator.insert(this.btn_view_all);
		}
		this.container.insert(this.paginator);
	}, 
	normalize_page_number: function(page){
		if(page < 0) return (this.pages.length - 1);
		if(page > (this.pages.length - 1)) return 0;
		return page;
	}, 
	register_start: function(){
		this.animating++;
	}, 
	register_finish: function(){
		if(this.animating == this.pages.length)
			this.animating = 0;
	}, 
	is_animating: function(){
		return (this.animating > 0)? true : false;
	},
	view_all: function(){
		window.location = this.view_all_url;
	}, 
	show_previous_page: function(){
		return this.show_page(this.current_page - 1);
	}, 
	show_next_page: function(){
		return this.show_page(this.current_page + 1);
	}, 
	show_page: function(page){
		if(this.animating > 0) return false;
		
		this.previous_page = this.current_page;
		this.current_page = this.normalize_page_number(page);
		
		if(this.page_indicators){
			this.page_indicators[this.previous_page].removeClassName('active');
			this.page_indicators[this.current_page].addClassName('active');
		}
		return true;
	}, 
	move_pages: function(options){
		new Effect.multiple(this.pages, Effect.Move, Object.extend({
			mode: 'relative', 
			duration: 0.5, 
			speed: 0, 
			beforeStart: this.register_start.bind(this), 
			afterFinish: this.register_finish.bind(this)
		}, options || { }));
	}
});

var SearchlightHomepageVideoModule = Class.create(SearchlightPaginator, {
	initialize: function($super, args){
		this.module = $(args.id);
		this.content = this.module.select('.content').first();
		$super({
			container: this.module.select('.header').first(), 
			pages: this.content.childElements().first(), 
			view_all_url: args.view_all_url
		});
		
		this.page_height = 0;
		this.module.select('li.entry').each(function(entry, i){
			var entry_height = entry.getHeight();
			if(entry_height > this.page_height)
				this.page_height = entry_height + 10;
		}.bind(this));
		
		this.page_width = (this.pages[0].getWidth() + 24);
		this.module.setStyle({
			height: (this.page_height + 30) + 'px'
		});
		this.content.setStyle({
			position: 'absolute', 
			clip: 'rect(0 ' + (this.page_width - 55) + 'px ' + this.page_height + 'px 0)', 
			width: (this.page_width - 24) + 'px', 
			height: this.page_height + 'px'
		});
		this.page_container.setStyle({
			width: (this.page_width * this.pages.length) + 'px', 
			height: this.page_height + 'px', 
			overflow: 'hidden'
		});
		this.pages.each(function(page, i){
			page.setStyle({
				left: (this.page_width * i) + 'px'
			});
		}.bind(this));
		this.btn_next.setStyle({
			'margin-right': ((this.page_width - this.paginator.getWidth()) / 2) - ((this.view_all_url)? 50 : 10) + 'px'
		});
	}, 
	show_page: function($super, page){
		if(!$super(page)) return false;
		
		var distance = this.page_width * Math.abs(this.current_page - this.previous_page);
		this.move_pages({
			x: (this.current_page > this.previous_page)? -distance : distance
		});
	}
});

var SearchlightHomepageArticleModule = Class.create(SearchlightPaginator , {
	initialize: function($super, args){
		this.module = $(args.id);
		this.content = this.module.select('.content').first().childElements().first();
		$super({
			container: this.module.select('.content').first(), 
			pages: this.module.select('.content div div').first()
		});
		
		this.page_width = this.module.getWidth();
		this.page_height = this.pages[0].getHeight();
		this.module.setStyle({
			height: (this.page_height + 30) + 'px'
		});
		this.content.setStyle({
			position: 'absolute', 
			clip: 'rect(0 ' + this.page_width + 'px ' + this.page_height + 'px 0)', 
			width: this.page_width + 'px', 
			height: this.page_height + 'px'
		});
		this.page_container.setStyle({
			overflow: 'hidden', 
			width: this.page_width + 'px', 
			height: (this.page_height * this.pages.length) + 'px'
		});
		this.container.setStyle({
			position: 'relative'
		})
		this.btn_next.setStyle({
			position: 'absolute', 
			top:  (this.page_height + 19) + 'px'
		});
	}, 
	build_paginator: function(){
		// Up button.
		this.btn_previous = new Element('div', {'class':'pagination up'});
		this.btn_previous.observe('click', this.show_previous_page.bind(this));
		this.container.insert({ top: this.btn_previous });
		
		// Down button.
		this.btn_next = new Element('div', {'class':'pagination down'});
		this.btn_next.observe('click', this.show_next_page.bind(this));
		this.container.insert({ bottom: this.btn_next });
	}, 
	show_page: function($super, page){
		if(!$super(page)) return false;
		
		var distance = this.page_height * Math.abs(this.current_page - this.previous_page);
		this.move_pages({
			y: (this.current_page > this.previous_page)? -distance : distance
		});
	}
});
