var HeaderBuzz = function(){
	this.abtest = 0;
	this.header_buzz = $A();
	this.page_quantity = 3;
	this.current_page = 1;
	this.total_items = this.header_buzz.length;

	this.attach_click_handlers = function(){
		$$('.HotHead .abtrack a').each( function(elm){
			elm.observe( 'mousedown', this.click_handler );
		}.bind(this));
	}.bind(this);

	this.click_handler = function( event ){
		// event.stop();

		// abtest is set in header.tt
		Tracker.gClick( '/_ga/click/hot_top/test007/click/' + this.abtest );
	}.bind(this);

	this.draw_page = function( page ){

		if( 1 == page ){
			$('hothead-scripted').hide();
			$('hothead-unscripted').show();
			return;
		}

		var start_index = ((page - 1) * this.page_quantity) - this.page_quantity;
		var buzzes = this.header_buzz.slice( start_index, start_index + this.page_quantity );
		var html = $A('');

		for( var i = 0; i < buzzes.length; i++ ){
			if(!buzzes[i].badge_type) buzzes[i].no_badge = 'no-badge';
			var buzz_str = this.buzz_template.evaluate({ buzz: buzzes[i] });
			html.push( buzz_str );
		}

		$('hothead-unscripted').hide();
		$('hothead-scripted').update( html.join('') );
		$('hothead-scripted').show();

		if (tracker)
			tracker.attach('hothead-scripted');

		if( this.abtest ){
			this.attach_click_handlers();
		}
	};

	this.init = function(){
		var next_link = this.next_link();
		var previous_link = this.previous_link();

		if( next_link ) next_link.observe( 'click', this.next_handler );
		if( previous_link ) previous_link.observe( 'click', this.previous_handler );

		if( this.abtest ){
			this.attach_click_handlers();
		}
	};

	this.is_first_page = function(){
		return 1 >= this.current_page;
	};

	this.is_last_page = function(){
		return ( (this.current_page * this.page_quantity) - this.page_quantity >= this.header_buzz.length );
	};

	this.next_handler = function( event ){
		event.stop();

		var next = this.next_page();

		if( next ){
			this.draw_page( next );
			this.current_page = next;
			this.previous_link().show();

			if( this.is_last_page() ){
				this.next_link().hide();
			}
		}
		
		Tracker.gClick('/_ga/click/hot_top/next');

	}.bind( this );

	this.next_link = function(){
		return $('header_buzz_next');
	};

	this.next_page = function(){
		if( this.is_last_page() ){
			return null;
		}
		return this.current_page + 1;
	};

	this.previous_handler = function( event ){
		event.stop();

		var prev = this.previous_page();

		if( prev ){
			this.draw_page( prev );
			this.current_page = prev;
			this.next_link().show();

			if( this.is_first_page() ){
				this.previous_link().hide();
			}
		}
		
		Tracker.gClick('/_ga/click/hot_top/prev');
		
	}.bind( this );

	this.previous_link = function(){
		return $('header_buzz_previous');
	};

	this.previous_page = function(){
		if( this.is_first_page() ){
			return null;
		}
		return this.current_page - 1;
	};

	this.set_header_buzz = function( json ){
		this.header_buzz = $A( json );
	};
};