/*
  * @dependencies prototype.js 1.5.1+, effects.js, _glider.js
*/

var CountGlide = Class.create();
CountGlide.prototype = {
	initialize: function(options) {
		this.glider						= new Glider(options.glider, {duration:options.dur});
		this.currNum					= 1;
		this.selected 					= false;
		this.contGlide					= (options.contGlide) ? true : false;
		this.adjustContentWidth();
		if(options.over) {
			this.navImgs				= {	il:$(options.left).src,
											ir:$(options.right).src,
											al:options.over.l,
											ar:options.over.r};
			this.navOverImgs			= {left: new Image(), right: new Image()};
			this.navOverImgs.left.src	= this.navImgs.al;
			this.navOverImgs.right.src	= this.navImgs.ar;
			Event.observe(options.left,  'mouseover', this.toggleNav.bindAsEventListener(this, [options.left, options.right]), false);
			Event.observe(options.left,  'mouseout',  this.toggleNav.bindAsEventListener(this, [options.left, options.right]), false);
			Event.observe(options.right, 'mouseover', this.toggleNav.bindAsEventListener(this, [options.left, options.right]), false);
			Event.observe(options.right, 'mouseout',  this.toggleNav.bindAsEventListener(this, [options.left, options.right]), false);
		}
		
		if(options.click) {
			var obj = this;
			Event.observe(this.glider.scroller, 'click',  function(event) {options.click.foo.apply(obj, [event || window.event].concat(options.click.args))}, false);
		};
		
		if(options.mouseroll) {			
			Event.observe(this.glider.scroller, 'mouseover', options.mouseroll.foo.bindAsEventListener(this, options.mouseroll.args), false);
			Event.observe(this.glider.scroller, 'mouseout',  options.mouseroll.foo.bindAsEventListener(this, options.mouseroll.args), false);
		};
		
		if(options.init) {
			options.init.foo.apply(this, [{target:this.glider.scroller.getElementsByTagName('img')[options.init.init[0]-1]}].concat(options.click.args));
			var index = parseInt(options.init.init[0]/options.init.init[1]);
			this.glider.moveTo(this.glider.sections[index], this.glider.scroller, {duration: this.glider.options.duration});
		}
		
		if(options.count) {
			this.counterCurrent				= $(options.count.curr);
			this.counterTotal				= $(options.count.total);
			this.counterCurrent.innerHTML	= this.currNum;
			this.counterTotal.innerHTML		= this.glider.sections.length;
		}
//---------------------------AJAX---------------------------------		
		if(options.ajax) {
			this.sections;
			new Ajax.Request(options.ajax.url, {method:'get', onSuccess:options.ajax.buildfoo.bind(this), onComplete:this.putAjaxContent.bind(this)});
		}
//--------------------------/AJAX/--------------------------------		
		if(options.left && options.right) {
			Event.observe(options.left,  'click', this.glideGo.bindAsEventListener(this, 'previous'), false);
			Event.observe(options.right, 'click', this.glideGo.bindAsEventListener(this, 'next'), false);
		}
	},
	
	glideGo: function(e, direction) {
		if(direction == 'previous') {
			if(!this.contGlide && this.currNum <= 1) return 0;
			this.glider.previous();
			if(this.contGlide && this.currNum <= 1 && this.counterCurrent) this.updateCounter('end');
			else if(this.counterCurrent) this.updateCounter('-');
			else --this.currNum;
			return 1;
		}
		else if(direction == 'next') {
			if(!this.contGlide && this.currNum >= this.glider.sections.length) return 0;		
			this.glider.next();
			if(this.contGlide && this.currNum >= this.glider.sections.length && this.counterCurrent) this.updateCounter('start');
			else if(this.counterCurrent) this.updateCounter('+');
			else ++this.currNum;
			return 1;
		}
	},
	
	toggleNav: function(e, ids) {
		var oTarget = (e.target) ? e.target : e.srcElement;
		
		if(e.type == 'mouseover') {
			if(oTarget.id == ids[0]) oTarget.src = this.navOverImgs.left.src;
			else if(oTarget.id == ids[1]) oTarget.src = this.navOverImgs.right.src;
		}
		else if(e.type == 'mouseout') {
			if(oTarget.id == ids[0]) oTarget.src = this.navImgs.il;
			else if(oTarget.id == ids[1]) oTarget.src = this.navImgs.ir;
		}
	},
	
	adjustContentWidth: function() {		
		this.glider.wrapper.down('div.content').style.width = (this.glider.sections[0].clientWidth * this.glider.sections.length) + 'px';		
	},
	
	updateCounter: function(dir) {
		if(dir == '+') this.counterCurrent.innerHTML = ++this.currNum;
		else if(dir == '-') this.counterCurrent.innerHTML = --this.currNum;
		else if(dir == 'start') this.counterCurrent.innerHTML = (this.currNum = 1);
		else if(dir == 'end') this.counterCurrent.innerHTML = this.currNum = this.glider.sections.length;
		else alert('updateCaunter wrong argument!!!');
	},
	
	putAjaxContent: function() {
		
	},
	
	moveTo: function(index) {
		this.glider.moveTo(this.glider.sections[index], this.glider.scroller, {duration: this.glider.options.duration});
		this.currNum = (index + 1);
	}
}