dojo.require("dojo.NodeList-traverse");
dojo.require("dojo.fx");
dojo.require("dojo.fx.easing");
dojo.declare('nid.swapContent',null, {
	constructor: function (elem, phrases) {
		this.elem = dojo.byId(elem);
		this.phrases = phrases;
		if(typeof elem == 'undefined' || !elem || typeof phrases == 'undefined' || !phrases) {
			this.moveRight = this.moveLeft = function () {};
			return false;
		}
		this.pos = 0;
	},
	swapTo: function (pos) {
		if(this.phrases.length <= pos) {
			var pos = pos - this.phrases.length;
		} else if(pos < 0) {
			var pos = this.phrases.length + pos;
		}
		dojo.attr(this.elem,'innerHTML',this.phrases[pos]);
		this.pos = pos;
	},
	moveRight: function () {
		this.swapTo(++this.pos);
	},
	moveLeft: function () {
		this.swapTo(--this.pos);
	}
})
dojo.declare('nid.timer',null,{
	constructor: function (activate, interval) {
		console.log('creating timer',arguments);
		this.activate = activate;
		this.value = 0;
		this.interval = interval;
		this.paused = false;
		this.run();
	},
	reset: function () {
		this.value = 0;
	},
	run: function () {
		console.log('timer running',this);
		if(!this.paused) {        
			if(this.value == this.interval) {
				this.activate();
				this.value = 0;
			}
			this.value++;
			var _t = this;
			this.controller = setTimeout(function () {_t.run();}, 1000);
		}
	},
	pause: function () {
		clearTimeout(this.controller);
		this.paused = true;
		this.value = 0;
	},
	unpause: function () {
		this.paused = false;
		this.run();
	},
	toggle: function () {
		this.value=0;
		clearTimeout(this.controller);
		this.paused = !this.paused;
		this.run();
	}
});
dojo.declare('nid.carousel',null,{
	constructor: function (config) {
		//Assign data
		this.posReal = 1;
		this.config = config;
		this.side = (typeof this.config.side != 'undefined')?this.config.side:900;
		
		console.log(config);
		
		//Create ul
		this.wrapper = wrapper = dojo.create('ul',{
			className: 'nid_carousel'
		},config.elem);
		dojo.forEach(this.config.imageList,function (v,k,a) {
			var opacity = 0.0001;
			if(k == 0) {
				opacity = 0.9999;
			}
			var li = dojo.create('li',{
				className: 'nid_carousel_image',
				style: {
					opacity: opacity
				}
			}, wrapper);
			var content = config.genFunction(v);
			dojo.place(content, li);
		});
		
		//Extra hidden fields
		dojo.place(
			config.genFunction(this.config.imageList[this.config.imageList.length-1]), 
			dojo.create('li',{className: 'nid_carousel_image'}, wrapper,'first')
		);
		dojo.place(
			config.genFunction(this.config.imageList[0]), 
			dojo.create('li',{className: 'nid_carousel_image'}, wrapper,'last')
		);
		
		//Position
		//this.calcRealPos(0)
		this.hardMotion(this.posReal);
		dojo.style(dojo.query('li',this.wrapper)[1],"opacity",0.999);
		
		//Assign actions
		if (typeof config.arrowLeft != 'undefined') { 
			dojo.connect(dojo.byId(config.arrowLeft), 'onclick', this, this.moveLeft);
		}
		if (typeof config.arrowRight != 'undefined') { 
			dojo.connect(dojo.byId(config.arrowRight), 'onclick', this, this.moveRight);
		}
		
		//Assign timer 
		var f = (function (t) {
			return function(){t.moveRight()};
		})(this);
		if(typeof this.config.timer != 'undefined' && this.config.timer>2) {
			this.timer = new nid.timer(f, this.config.timer);
		} else {
			console.log('timer cancelled');
		}
		
		//Phrase rotation
		if(typeof config.swapText != 'undefined' && !!config.swapText) {
			if(typeof config.swapText.elem != 'undefined' && !!config.swapText.elem && typeof config.swapText.phrases != 'undefined' && !!config.swapText.phrases) {
				this.swapper = new nid.swapContent(config.swapText.elem, config.swapText.phrases);
			}
		} else {
			this.swapper = new nid.swapContent();
		}
		this.swapper.swapTo(0);
		
		//Stop timer
		if(typeof this.timer!='undefined') {
			dojo.query('#arrow_left').connect('mouseover',[],this.timer.pause).connect('mouseout',[],this.timer.run);
			dojo.query('#arrow_right').connect('mouseover',[],this.timer.pause).connect('mouseout',[],this.timer.run);
		}
		
		//Set end
		dojo.connect("ended",this.calcRealPos);
	},
	pos : function () {
		if (this.posReal > this.config.imageList.length) {
			return 1;
		} else if (this.posReal <= 1) {
			return this.config.imageList.length;
		} else {
			return this.posReal - 1;
		}
	},
	hardMotion: function (pos) {
		dojo.style(this.wrapper, {
			marginLeft: (0 - (pos*this.side)) + 'px'
		});
	},
	calcRealPos: function (signal) {
		if(signal-1) {
			if(this.posReal <= 1) {
				this.posReal = this.config.imageList.length+1;
				this.hardMotion(this.posReal);
				this.moveLeft();
				this.swapper.moveRight();
			} else {
				this.posReal+=signal;
			}
		} else if (signal +1) {
			if(this.posReal >= this.config.imageList.length+1) {
				this.posReal = 1;
				this.hardMotion(this.posReal);
				this.moveRight();
				this.swapper.moveLeft();
			} else {
				this.posReal+=signal;
			}
		}
		console.log("CONTROL:",this.posReal,signal);
	},
	moveLeft : function () {	
		this.calcRealPos(-1);
		this.animate();
		this.swapper.moveLeft();
	},
	moveRight : function () {
		this.calcRealPos(1);
		this.animate();
		this.swapper.moveRight();
	},
	animate: function () {
		var grid = dojo.query('li',this.wrapper)
		var prev = grid[this.posReal-1];
		var actual = grid[this.posReal];
		var simple = (function (t) {
			return dojo.publish("ended",t);
		})(this);
		var moveIt = dojo.animateProperty({
			node: this.wrapper,
			properties: {
				marginLeft: 0-(this.posReal*this.side)
			},
			_t : this,
			onEnd: simple,
			duration: 800,
			easing: dojo.fx.easing.circleOut
		});
		dojo.fx.combine([
			dojo.animateProperty({
				node: prev,
				properties: {
					opacity: 1
				},
				duration: 1000,
				easing: dojo.fx.easing.circleOut
			}),
			moveIt,
			dojo.animateProperty({
				node: actual,
				properties: {
					opacity: 1
				},
				duration: 10,
				easing: dojo.fx.easing.circleIn//dojo.fx.easing.cubicIn
			})
		]).play();
	}
});

