var campfire = (function(){
	var d = document;
	function get(id) {
		return d.getElementById(id);
	};
	function power(value) {
		return secoya.fx.power(value, 5);
	};

	return {
		header: (function(){
			var
				header_scroll,
				back,
				forward,
				current,
				length,
				pos=0,
				fx_forward,
				fx_back,
				fx_rewind,
				fx_fastforward,
				dwell = 5000,   // ms on each slide
				speed = 2,      // secs to change slide
				elemsize = 910, // width of one slide
				initialized = false,
				goBack = false,
				btnPressed = !!core || true,
				moving = false;

			function getLength() {
				length = !!core+0;
				var items = header_scroll.getElementsByTagName('div');
				for (var c = 0; c < items.length; c++) {
					if (items[c].className == "item")
						length++;
				}
			};

			function updatePosition() {
				current.firstChild.nodeValue = 'Side '+(pos+1)+'/'+length;
			};

			function prepareFx() {
				fx_forward = new secoya.fx({
					from: 0,
					to: elemsize,
					type: power,
					duration: speed,
					callback: function(val){
						header_scroll.style.marginLeft = -elemsize * pos - val + "px";
					},
					finish: function(){
						pos = (pos+1)%length;
						updatePosition();
						if (goBack) {
							goBack = false;
							fx_back.start();
							return;
						}
						moving = false;
						setTimeout(function(){
							if(!btnPressed){
								moving = true;
								if(pos==length-1) fx_rewind.start();
								else              fx_forward.start();
							}
						},dwell);
					}
				});
				fx_back = new secoya.fx({
					from: elemsize,
					to: 0,
					type: power,
					duration: speed,
					callback: function(val){
						header_scroll.style.marginLeft = -elemsize * (pos-1) - val + "px";
					},
					finish: function(){
						moving = false;
						goBack = false;
						pos = (pos-1+length)%length;
						updatePosition();
						setTimeout(function(){
							if(!btnPressed){
								moving = true;
								if(pos==length-1) fx_rewind.start();
								else              fx_back.start();
							}
						},dwell);
					}
				});
				fx_rewind = new secoya.fx({
					from: elemsize*(length-1),
					to: 0,
					type: power,
					duration: speed,
					callback: function(val){
						header_scroll.style.marginLeft = - val + "px";
					},
					finish: function(){
						moving = false;
						goBack = false;
						pos=0;
						updatePosition();
						setTimeout(function(){
							if(!btnPressed){
								moving = true;
								fx_forward.start();
							}
						},dwell);
					}
				});
				fx_fastforward = new secoya.fx({
					from: 0,
					to: elemsize*(length-1),
					type: power,
					duration: speed,
					callback: function(val){
						header_scroll.style.marginLeft = - val + "px";
					},
					finish: function(){
						moving = false;
						goBack = false;
						pos=length-1;
						updatePosition();
						setTimeout(function(){
							if(!btnPressed){
								moving = true;
								fx_forward.start();
							}
						},dwell);
					}
				});
			};

			return {
				reset: function() {
					if (initialized) {
						var lastLength = length;
						header_scroll = get("header_scroll");
						back          = get("back");
						forward       = get("forward");
						current       = get("current");
						getLength();

						if (lastLength != length) {
							back.style.display = forward.style.display = current.style.display = (length > 1 || core) ? '' : 'none';

							header_scroll.style.width = elemsize * length + "px";
							header_scroll.style.marginLeft = 0;
							if (fx_forward) {
								fx_forward.kill();
								fx_back.kill();
								fx_rewind.kill();
								fx_fastforward.kill();
							}
							pos = 0;
							prepareFx();
							updatePosition();
						}

						back.onclick = function() {
							if(!btnPressed){
								btnPressed = true;
								if (moving)
									goBack = true;
							}
							if (!moving) {
								moving = true;
								if (pos == 0)
									fx_fastforward.start();
								else
									fx_back.start();
							}
						};

						forward.onclick = function() {
							btnPressed = true;
							if (!moving) {
								moving = true;
								if (pos==length-1)
									fx_rewind.start();
								else
									fx_forward.start();
							}
						};
					}
				},
				initialize: function() {
					initialized = true;
					this.reset();

					if (length > 1) {
						setTimeout(function(){
							if(!btnPressed){
								moving = true;
								fx_forward.start();
							}
						},dwell);
					}
				}
			};
		})(),
		menu: (function() {
			var menuCallback = secoya.menu.registerMenu;
			secoya.menu.registerMenu = function() {
				menuCallback.apply(secoya.menu, arguments);
				campfire.menu.initialize();
			};
			return {
				initialize: function() {
					var
						ul = null,
						lastUl = null,
						duration = 0.7;

					var fx_show = new secoya.fx({
						from: 0,
						to: 0, // must be set when executing
						duration: duration,
						type: power,
						callback: function(value) {
							ul.style.height = value + 'px';
						},
						finish: function() {
							ul.style.height = 'auto';
						}
					});
					var fx_hide = new secoya.fx({
						from: 0, // must be set when executing
						to: 0,
						duration: duration,
						type: power,
						callback: function(value) {
							lastUl.style.height = value + 'px';
						}
					});
					function click() {
						fx_hide.kill();
						fx_show.kill();
						lastUl = ul;
						ul = this.parentNode.getElementsByTagName('ul')[0];
						if (lastUl) {
							lastUl.style.height = 'auto';
							fx_hide.from = lastUl.clientHeight;
							fx_hide.start();
						}
						if (ul != lastUl) {
							ul.style.height = 'auto';
							fx_show.to = ul.clientHeight;
							fx_show.start();
						} else {
							ul = null;
						}
					};
					var menu = get('nav');
					var items = menu.getElementsByTagName('ul');
					var link;
					for (var i = 0; i < items.length; i++) {
						li = items[i].parentNode;
						if (li.tagName == 'LI' && li.className.indexOf('selected') == -1) {
							link = items[i].previousSibling;
							if (link.addEventListener) {
								link.addEventListener('mouseup', click, true);
							} else {
								link.onmouseup = click;
							}
						}
					}
				}
			};
		})(),
		initialize: function() {
			this.header.initialize();
			this.menu.initialize();
		}
	};
})();

lightCore.registerInit(campfire);
