var BFSlideshow = function(){
	
	this.has_paginated = false;
	
	this.pagination_config = {
		show_previous : true,
		show_next : true,
		left_count : 4,
		right_count : 4,
		middle_count : 15,
		break_delimiter : '...'
	};

	this.controls_id = 'slideshow_header';
	this.pagination_id = 'slideshow_footer';// set this to 'slideshow_footer' to turn on
	this.pagination_ul_id = 'slideshow_pagination';
	this.clicker_id = 'slideshow_clicker';
	this.type = 'lol';
	this.qs_key = 'slideshow';

	this.slideshow_data = {};
	this.buzzes = [];
	this.current_uri = '';
	this.next_uri = '';
	this.username = '';
	
	this.init = function(){
		var params = window.location.toString().toQueryParams();
		if( params.slideshow ){
			this.type = params.slideshow;
			if(this.type == 'contests') {
				this.contest = params.uri;
			}
			this.get_buzzes(this.type);
			if(this.contest) {				
				$('badge_slideshow_info').hide();												
				$('contest_slideshow_info').show();
			}
			this.next_uri = this.find_next_uri( this.current_uri, this.buzzes );

			if( this.next_uri != '' ){
				this.setClickers();
				this.setLaunchers();
				this.setCurrentBadge();
				$(document.body).addClassName('Slideshow');
				$(this.controls_id).show();
				if ($(this.pagination_id) && !Prototype.Browser.IE6) $(this.pagination_id).show();
			}
		}
	};

	this.get_index_by_uri = function( current_uri ) {
		for( var i=0; i < this.buzzes.length; i++ ){
			if( current_uri == this.buzzes[i].uri ){
				break;
			}
		}
		return i;	
	}
	
	this.find_this_uri = function( uri ) {
		var idx = this.get_index_by_uri( uri );
		var buzz = this.buzzes[idx];
		if ( ! buzz ) return '';
		return [BF_STATIC.web_root,buzz.username,buzz.uri].join('/');
	}	
	
	this.find_next_uri = function( current_uri ){
		var next_pos = 0;
		// 0 <= next_pos < buzzes.length
		for( var i=0; i < this.buzzes.length - 1; i++ ){
			// console.log(buzzes[i].uri);
			if( current_uri == this.buzzes[i].uri ){
				next_pos = i + 1;
				break;
			}
		}
		var next_buzz = this.buzzes[next_pos];
		if( ! next_buzz ) return '';
		return BF_STATIC.web_root + '/' + next_buzz.username + '/' + next_buzz.uri;
	};

	this.get_buzzes = function(type) {
		var ajax = new BF_Request();
		var parse_resp = function( resp ){
			this.slideshow_data = resp.responseText.evalJSON();
			this.buzzes = this.slideshow_data.buzzes;
			if ($(this.pagination_id)) this.paginate();
		}.bind(this);
		var err = function(){ 1 == 1 };
		var url = BF_STATIC.web_root + '/slideshow/' + type;
		if(this.contest) {
			url += '/' + this.contest; 
		}
		url += '/json.js';
		ajax.request( url, {method: 'get', onSuccess: parse_resp, onFailure: err, asynchronous: false} );
	};

	this.launch = function( type ){
		this.get_buzzes( type );
		var next_uri = this.find_next_uri( '', this.buzzes );
		var qs = '?' + this.qs_key + '=' + type;
		if(this.contest) qs += '&uri=' + this.contest;
		if( next_uri != '' ) window.location = next_uri + qs; 
	};

	this.setClickers = function(){
		var fn = function( e ){
			this.next();
		}.bind( this );
		$$('.slideshow_clicker').each( function(e){
			e.observe( 'click', fn.bind(e) );
		});
	};

	this.setCurrentBadge = function(){
		var cb = $(this.type);
		if( cb ) cb.up('li').className = 'current';
		var cbimg = $('slideshow_next_badge');
		var badge = this.type;
		if(badge == 'contests') {
			$('slideshow_next_badge').hide();
			$('slideshow_next_contest').show();			
		}
		if( cbimg ) cbimg.src = BF_STATIC.static_root + '/badge_images/' + badge + '_small.png';
	};

	this.setLaunchers = function(){
		var fn = function( e ){
			this.launch( e.element().id );
		}.bind( this );
		$$('.slideshow_launcher').each( function(e){
			e.observe( 'click', fn.bind(e) );
		});
	};

	this.next = function(){
		var qs = '?' + this.qs_key + '=' + this.type;
		if(this.contest) qs += '&uri=' + this.contest;
		window.location = this.next_uri + qs;
	};
	
	this.paginate = function() {
		
		if(this.has_paginated == true) {
			return;
		} else {
			this.has_paginated = true;
		}
		
		var LEFT = 0;
		var MIDDLE = 1;
		var RIGHT = 2;
		var total_slides = this.buzzes.length;
		var current_slide_idx = this.get_index_by_uri( this.current_uri );
		var layout = [
			{ 	start_idx : 0,
				count : this.pagination_config.left_count,
				show: true
			},
			{ 	start_idx : current_slide_idx - (parseInt(this.pagination_config.middle_count / 2)) ,
				count : this.pagination_config.middle_count,
				show: true
			},
			{ 	start_idx : total_slides - this.pagination_config.right_count ,
				count : this.pagination_config.right_count,
				show: true
			}
		];
		if ( total_slides < this.pagination_config.left_count + this.pagination_config.middle_count + this.pagination_config.right_count ) {
			layout[ LEFT ].count = total_slides;
			layout[ MIDDLE ].show = layout[ RIGHT ].show = false;
		}
		else if ( current_slide_idx < this.pagination_config.left_count + (this.pagination_config.middle_count/2) + 1) {
			layout[ LEFT ].count = this.pagination_config.left_count + this.pagination_config.middle_count ;
			layout[ MIDDLE ].show = false;
		}
		else if ( current_slide_idx > total_slides - ( (this.pagination_config.middle_count/2) + this.pagination_config.right_count + 1 ) ) {
			layout[ RIGHT ].count = this.pagination_config.middle_count + this.pagination_config.right_count ;
			layout[ RIGHT ].start_idx = total_slides - ( this.pagination_config.middle_count + this.pagination_config.right_count );
			layout[ MIDDLE ].show = false;
		}
		if (this.pagination_config.show_previous) {
			var div = document.createElement('li');
			$(this.pagination_ul_id).appendChild( div );
			$(div).addClassName('previous');
			if ( current_slide_idx > 0 ) {
				var link = document.createElement('a');
				$(div).appendChild(link);
				$(link).setAttribute('href','#');
				$(link).setAttribute('rel:uri',escape(this.buzzes[current_slide_idx-1].uri));
				$(link).update('&lsaquo;');
				$(link).observe( 'click', function(e) {
					e.stop();
					var qs = '?' + bf_slideshow.qs_key + '=' + bf_slideshow.type;
					if (bf_slideshow.contest) qs += '&uri=' + bf_slideshow.contest;
					var uri = unescape(e.target.getAttribute('rel:uri'));
					window.location.href = bf_slideshow.find_this_uri(uri) + qs;
				});
			}
			else {
				$(div).update('&lsaquo;');
				$(div).addClassName('disabled');
			}
		}
		for( var i = 0; i < layout.length; i++ ) {
			if ( layout[i].show ) {
				if ( i > 0 ) { 
					var ellipses = document.createElement('li');
					$(this.pagination_ul_id).appendChild( ellipses );
					$(ellipses).appendChild( document.createTextNode('...') );
				}
				for( var j = 0; j < layout[i].count; j++ ) {
					var div = document.createElement('li');
					$(this.pagination_ul_id).appendChild( div );
					var link = document.createElement('a');
					$(link).setAttribute('href','#');
					$(link).setAttribute('rel:uri',escape(this.buzzes[layout[i].start_idx + j].uri));
					if ( current_slide_idx == layout[i].start_idx + j )	$(div).addClassName('current');
					$(link).appendChild( document.createTextNode( layout[i].start_idx + j + 1 ) );
					$(div).appendChild(link);
					$(div).observe( 'click', function(e) {
						e.stop();
						var qs = '?' + bf_slideshow.qs_key + '=' + bf_slideshow.type;
						if(bf_slideshow.contest) qs += '&uri=' + bf_slideshow.contest;
						var uri = unescape(e.target.getAttribute('rel:uri'));
						window.location.href = bf_slideshow.find_this_uri(uri) + qs;
					});
				}
			}
		}
		if (this.pagination_config.show_next) {
			var div = document.createElement('li');
			$(this.pagination_ul_id).appendChild( div );
			$(div).addClassName('next');
			if ( current_slide_idx < total_slides - 1 ) {
				var link = document.createElement('a');
				$(div).appendChild(link);
				$(link).setAttribute('href','#');
				$(link).setAttribute('rel:uri',escape(this.buzzes[current_slide_idx+1].uri));
				$(link).update('&rsaquo;');
				$(link).observe( 'click', function(e) {
					e.stop();
					var qs = '?' + bf_slideshow.qs_key + '=' + bf_slideshow.type;
					if(bf_slideshow.contest) qs += '&uri=' + bf_slideshow.contest;
					var uri = unescape(e.target.getAttribute('rel:uri'));
					window.location.href = bf_slideshow.find_this_uri(uri) + qs;
				});
			}
			else {
				$(div).update('&rsaquo;');
				$(div).addClassName('disabled');
			}
		}
	}
};

var bf_slideshow = new BFSlideshow();

if( typeof(BFSlideshow_CurrentUri) != 'undefined' ){
	bf_slideshow.current_uri = BFSlideshow_CurrentUri.split( '/' )[1];
}


