/***********************************
* LOOP BANNER
************************************/

var LoopBanner = new Class({
	
	Implements: [Options, Events],
	
	option: {
		
		contentArea: {},
		
		requestUrl: "",
		
		bUp: {},
		bDown: {}
	},
	
	contentContainer: {},
	contentHeight: 0,
	numBans: 0,
	currentBan: 0,
	
	timer: 0,
	
	initialize: function(options){
		
		this.setOptions(options);
		
		//active buttons
		this.options.bUp.addEvent('click', function(event){
			event.stop();
			this.goUp();
		}.bind(this));
		
		this.options.bDown.addEvent('click', function(event){
			event.stop();
			this.goDown();
		}.bind(this));
		
		//create content container
		this.contentContainer = new Element('div', {
			styles: {
				'position': 'absolute',
				'top' : '0px'
			}
		});
		this.options.contentArea.adopt(this.contentContainer);
		
		//retreive content area height
		this.contentHeight = this.options.contentArea.getSize().y;
		
		//set tween
		this.contentContainer.set('tween', {
			duration: 500,
			transition: Fx.Transitions.Quart.easeInOut
			});
		
		//load html content
		this.loadContent();
	},
	
	loadContent: function(){
		
		//start request
		var request = new Request.HTML({
			url: this.options.requestUrl,
			update: this.contentContainer,
			evalScripts: false,
			onComplete: this.loadComplete.bind(this)


		});
		
		request.send();
	},
	
	loadComplete: function(responseTree, responseElements, responseHTML, responseJavaScript){
		
		//this.contentContainer.set('html', responseHTML);
		
		//count ban nums
		this.numBans = $$('.loopbanner_element').length;
	
		
		//start loop
		this.timer = this.loop.periodical(5000, this);
	},
	
	goUp: function(){
		
		if (this.currentBan > 0){
			this.goTo(this.currentBan - 1)
		}
		
		$clear(this.timer)
		
	},
	
	goDown: function(){
		
		if (this.currentBan < this.numBans - 1){
			this.goTo(this.currentBan + 1)
		}
		
		$clear(this.timer)
	},
	
	goTo: function(index){

		var newTop = - (index*this.contentHeight);
		this.contentContainer.tween('top', newTop);
		
		this.currentBan = index;
	},
	
	loop: function(){
		
		if (this.currentBan < this.numBans - 1){
			this.goTo(this.currentBan + 1);
		} else {
			this.goTo(0);
		}
	}
	
	
});

