TiLLT.Browse = Class.create();
TiLLT.Browse.prototype = {
	initialize: function(par, prefix, child, w) {
		this.par = $(par);
		this.pref = prefix + '-';
		this.btn = {'next': this.pref + child[1], 'prev': this.pref + child[0]};
		this.width = w;
		this.clicks = 0;
		this.offset = this.par.offsetLeft;
		
		this.countChildNodes();
	},
	
	countChildNodes: function(){
		this.cLength = 0;
		
		if(this.par.hasChildNodes) {
			for(var i = 0; i < this.par.childNodes.length; i++) {
				if(this.par.childNodes[i].className == 'content dj') {
					this.cLength++;
				}
			}
		}
		
		this.mClicks = Math.round(parseInt(this.cLength) / 5);
		this.tClicks = ((this.mClicks == 1) ? 1 : (parseInt(this.mClicks) - 1));

		this.build();
	},
	
	build: function(type) {
		this.scrollW = this.par.getWidth();
		
		this.moveHandler = this.movePrevNext.bindAsEventListener(this);
		if(type == 'next') {
			Event.observe(this.btn.next, 'click', this.moveHandler);		
			$(this.btn.next).style.cursor = 'pointer';
			
			this.nObserve = true;
		} else if(type == 'prev') {
			Event.observe(this.btn.prev, 'click', this.moveHandler);
			$(this.btn.prev).style.cursor = 'pointer';
			
			this.pObserve = true;
		} else {
			Event.observe(this.btn.next, 'click', this.moveHandler);
			
			$(this.btn.next).style.cursor = 'pointer';
		
			this.nObserve = true;
			this.pObserve = false;
		}
	},
	
	movePrevNext: function(evnt) {
		this.avg = (this.cLength / ((this.mClicks == 1) ? 2 : this.mClicks));
		this.savg = this.avg + '';

		this.elem = Event.element(evnt).id;
		
		if(this.elem.match('next')) {
			if(this.clicks == (this.tClicks - 1)) {
				Event.stopObserving(this.btn.next);
				this.nObserve = false;
				
				$(this.btn.next).style.cursor = '';
				
				if(this.pObserve == false) {
					this.build('prev');
				}
			} else {
				if(this.pObserve == false) {
					this.build('prev');
				}
				
				this.clicks++;
			}
		} else {
			if(this.clicks > 0) {
				this.clicks--;
				
				if(this.nObserve == false) {
					this.build('next');
				}
			} else {
				this.build('next');
				Event.stopObserving(this.btn.prev);
				this.pObserve = false;
				
				$(this.btn.prev).style.cursor = '';
			}
		}

		if(this.savg.match('.1') || this.savg.match('.2') || this.savg.match('.3') || this.savg.match('.4')) {
			this.avg = Math.floor(this.avg + 0.5);
		} else if(this.savg.match('.5')) {
			this.avg = Math.floor(this.avg - 0.1);
		} else {
			this.avg = Math.floor(this.avg);
		}
		
		if(this.avg == 1 && this.avg == 5) {
			this.avg = Math.round(this.avg);
		} else if(this.avg > 1 && this.avg < 5) {
			this.avg = (Math.round(this.avg) - 1);
		}
		
		if(this.elem.match('prev')) {
			if(this.par.offsetLeft == this.offset) {
				this.move = 0;
			} else {
				this.move = (((parseInt(this.width)) * this.avg));
			}
		} else {
			this.move = (((parseInt(this.width)) * this.avg));
		}

		if(this.move != 0 && ((document.all) ? this.clicks < this.tClicks : this.clicks <= this.tClicks)) {
			new Effect.Move(this.par, {y: 0, x: ((this.elem.match('next')) ? -this.move : this.move), mode: 'relative'});
		}
	}
}