var aircombat = {
	scroller: (function(){
		function updatePositionText(){};
		var
			scroller,
			pageIndex,
			back,
			forward,
			index = 0,
			width = 591,
			waitInterval,
			fx = new secoya.fx({
				from: 0,
				to: 0,
				type: function(x){return secoya.fx.power(x, 5)},
				duration: 1.0,
				callback: function(value) {
					scroller.style.marginLeft = -value + 'px';
				},
				finish: function() {
					updatePositionText();
				}
			});

		function id(s) {
			return document.getElementById(s);
		};
		function count() {
			var m = /(?:^|\s)item(?:\s|$)/;
			var result = !!core+0;
			var e = scroller.getElementsByTagName('div');
			for (var i = 0; i < e.length; i++) {
				if (e[i].className.match(m)) {
					result++;
				}
			}
			return result;
		};
		function updateWidth() {
			scroller.style.width = count()*width+'px'
		};
		function updateElements() {
			scroller = id('scrollSpots');
			pageIndex = id('scrollPage');
			back = id('scrollBack');
			forward = id('scrollForward');
		};
		function updatePositionText() {
			var child = pageIndex.firstChild;
			if (child.nodeType == 1) {
				secoya.sifr.rollback(pageIndex);
				child = pageIndex.firstChild;
			}
			var value = (index + 1)+'/'+count();
			var text = child.nodeValue;
			text = text.replace(/[0-9]+\s*\/\s*[0-9]+/, value)
			child.nodeValue = text;
			if (child.nodeType == 3 && secoya.sifr.fonts) {
					secoya.sifr.turnOn();
			}
		};
		function stopAuto() {
			clearInterval(waitInterval);
		};
		function clickForward(event) {
			fx.resetToEnd();
			fx.finish();
			if (index < count()-1) {
				fx.from = index*width;
				fx.to = (index+1)*width;
				fx.start();
				index++;
			}
			if (event || window.event)
				stopAuto();
		};
		function clickBack(event) {
			fx.resetToEnd();
			fx.finish();
			if (index > 0) {
				fx.from = index*width;
				fx.to = (index-1)*width;
				fx.start();
				index--;
			}
			if (event || window.event)
				stopAuto();
		};
		function goToStart() {
			fx.kill();
			fx.from = Math.max((count()-1)*width, 0);
			fx.to = 0;
			fx.start();
			index = 0;
		};
		function updateEvents() {
			if (forward.addEventListener) {
				forward.addEventListener('mouseup', clickForward, true);
				back.addEventListener('mouseup', clickBack, true);
			} else {
				forward.onmouseup = clickForward;
				back.onmouseup = clickBack;
			}
		};
		function startAuto() {
			stopAuto();
			waitInterval = setInterval(function() {
				if (index < count()-1)
					clickForward();
				else
					goToStart();
			}, 5000);
		};
		return {
			refresh: function(reset) {
				setTimeout(function(){
					updateElements();
					updateWidth();
					updatePositionText();
					if (reset) {
						updateEvents();
						index = 0;
						if (!core)
							startAuto();
					}
				}, 100);
			}
		};
	})(),
	imagemap: (function(){
		var map = {
			randers:{x: 189, y: 139},
			vamdrup:{x: 124, y: 259},
			//odense:{x: 191, y: 272},
			roskilde:{x: 309, y: 235}
		},
		checkbox = {
			vamdrup: 1,
			roskilde: 2,
			odense: 3,
			randers: 4
		}
		width = 39,
		height = 46,
		current = null;
		function hide() {
			if (!this.hidden) {
				this.style.top = this.style.left = '-9999em';
				this.hidden = true;
			}
		};
		function show() {
			if (this.hidden) {
				this.style.top = this.style.left = '';
				this.hidden = false;
			}
		};
		function d(id) {
			return document.getElementById(id);
		};
		return {
			initialize: function(uid) {
				if (core)
					return;
				var img = d('item_'+uid).getElementsByTagName('img')[0];
				if (img) {
					var elements = {};
					for (var i in map) {
						hide.call(elements[i] = d(uid+'_'+i));
					}
					img.onclick = function() {
						if (current) {
							var form = document.forms[0];
							if (form)
								form['afi'][checkbox[current]].checked = true;
						}
					};
					img.onmousemove = function(event) {
						event = event || window.event;
						var x = typeof event.layerX == 'undefined' ? event.offsetX : event.layerX;
						var y = typeof event.layerY == 'undefined' ? event.offsetY : event.layerY;
						var pos;
						var cursor = '';
						var func;
						current = null;
						for (var i in map) {
							pos = map[i];
							func = hide;
							if (x >= pos.x && x <= pos.x+width &&
									y >= pos.y && y <= pos.y+height) {
								cursor = 'pointer';
								current = i;
								func = show;
							}
							func.call(elements[i]);
						}
						if (this.style.cursor != cursor)
							this.style.cursor = cursor;
					};
				}
			}
		};
	})(),
	form: (function() {
		var
			parts,
			type,
			currentProgress,
			progress, // nodeType = 3
			form; // FORM

		function d(id) {
			return document.getElementById(id);
		};

		function show(element) {
		if(element){
			element.style.position = '';
			element.style.top = '';
			element.style.left = '';
			}
		};

		function hide(element) {
		if(element){
			element.style.position = 'absolute';
			element.style.top = '-99999em';
			element.style.left = '-99999em';
			}
		};

		function showProgress() {
			for (var i = 0; i < parts.length; i++) {
				if (i != currentProgress) {
					for (var j = 0; j < parts[i].length; j++) {
						hide(parts[i][j]);
					}
				}
			}
			for (i = 0; i < parts[currentProgress].length; i++) {
				show(parts[currentProgress][i]);
			}
			if(type!='gift' && currentProgress==2){
				var elm = document.getElementById('afi');
				elm.parentNode.style.display = 'none';
			}
			var progressLength = 4 - (type=='gift');
			progress.nodeValue = currentProgress+(type=='booking')+'/'+progressLength;
			document.title = document.title.replace(/[0-9]\/[34]/, progress.nodeValue);

			document.body.className = document.body.className.replace(/ ?progress[1-4] ?/, '');
			document.body.className += ' progress'+(currentProgress+1);
			var a = form['afl'];
			//if (a && a[1] && a[2]) {
				//a[1].checked = true;
				//a[2].parentNode.style.display = 'none';
			//}
		};

		function createReview() {
			function insert(parent, elements, delimiter) {
				parent = d(parent);
				var e;
				if (typeof elements == 'string')
					elements = [elements];
				for (var i = 0; i < elements.length; i++) {
					e = form[elements[i]];
					switch (typeof e) {
						case 'object':
							switch (e.tagName) {
								case 'SELECT':
									e = e.options[e.selectedIndex];
									e = e.firstChild.nodeValue;
									break;
								case 'INPUT':
									e = e.value;
									break;
								default: // list of radio-buttons
									for (var j = 0; j < e.length; j++) {
										if (e[j].checked) {
											e = e[j];
											do {
												e = e.nextSibling;
											} while (e && e.nodeType != 1);
											if (e)
												e = e.firstChild;
											if (e)
												e = e.nodeValue;
											else
												e = '';
											break;
										}
									}
									if (typeof e != 'string')
										e = '';
									break;
							}
							break;
						case 'string':
						case 'number':
							break;
					}
					elements[i] = e;
				}
				while (parent.firstChild)
					parent.removeChild(parent.lastChild);

				switch (typeof delimiter) {
					case 'undefined':
						delimiter = '';
					case 'string':
						parent.appendChild(document.createTextNode(elements.join(delimiter)));
						break;
					case 'object':
						if (delimiter.nodeType) {
							for (i = 0; i < elements.length; i++) {
								if (i)
									parent.appendChild(delimiter.cloneNode(true));
								parent.appendChild(document.createTextNode(elements[i]));
							}
						}
						break;
				}
			};
			if (type == 'booking') {
				//insert('ahe_dato', ['afu', 'afw'], '.');
				//insert('ahe_tid', 'axo');
				//insert('ahe_lufthavn', 'afi');
			}
			insert('ahe_produkt', 'afl');
			insert('ahe_deltagere', 'aga');
			insert('ahe_personAdresse', ['agd', 'age', 'agf', 'agg', 'agh'], document.createElement('br'));
			insert('ahe_personTelefon', 'agi');
			insert('ahe_personEmail', 'agj');
			insert('ahe_firmaAdresse', ['agm', 'agn', 'ago', 'agp', 'agq'], document.createElement('br'));
			insert('ahe_firmaTelefon', 'agr');
			insert('ahe_merch1', 'merchpack1');
			insert('ahe_merch2', 'merchpack2');
			insert('ahe_merch3', 'merchpack3');
			insert('ahe_dvd', 'merchdvd1');
			//insert('ahe_firmaEmail', null);
			insert('ahe_firmaCvr' ,'ags');
			var v = form['agu'].value;
			if (!v)
				v = '';
			v = v.split(/\r\n|\r|\n/);
			var parent = d('ahe_kommentar');
			while (parent.firstChild)
				parent.removeChild(parent.lastChild);
			for (var i = 0; i < v.length; i++) {
				if (i != 0)
					parent.appendChild(document.createElement('br'));
				parent.appendChild(document.createTextNode(v[i]));
			}
//			insert('ahe_kommentar', 'agu');
			var removeBr = {'ahe_personAdresse': 5, 'ahe_firmaAdresse': 5};
			parent;
			for (i in removeBr) {
				parent = d(i);
				parent.replaceChild(document.createTextNode(' '), parent.childNodes[removeBr[i]]);
			}
		};
		function autoFill() {
			var f = {
				//afi: 2,
				//afl: 1,
				//aga: '3',
				//afu: '10',
				//afw: '11',
				agd: 'Julemand Mads',
				age: 'X-mas vej',
				agf: '3900',
				agg: 'Nuuk',
				agh: 'Greenland',
				agi: '+299 24 12 00 00',
				agj: 'santa@christmas-workshop.gl',
				agm: 'Secoya ApS',
				agn: '\xC5bogade 15',
				ago: '8200',
				agp: '\xC5rhus N',
				agq: 'Denmark',
				agr: '+45 2396 1000',
				ags: '30 80 09 82',
				agu: "We're looking forward to the arrangement.\r\n\r\nSee you there!"
			};
			var elm;
			for (var i in f) {
				elm = form[i];
				if (typeof f[i] == 'number') {
					elm[f[i]].checked = true;
				} else {
					elm.value = f[i];
				}
			}
		};
		function populateSelect(select, from, to, callback, totalReset) {
			select = form[select];
			var option, value;
			if (select.tagName == 'SELECT') {
				if (select.firstChild) {
					if (!totalReset && (select.firstChild.value == from && select.lastChild.value == to))
						return;
				}
				while (select.firstChild)
					select.removeChild(select.lastChild);
				for (; from <= to; from++) {
					option = document.createElement('option');
					value = callback ? callback(from) : from;
					option.setAttribute('value', from);
					option.appendChild(document.createTextNode(value));
					select.appendChild(option);
				}
			}
		};
		var
			map, // IMG
			spot1, // DIV
			spot2, // DIV
			titleContact, // H1
			titleReview, // H1
			selectAirport, // DIV
			selectProduct, // DIV
			formPart1, // DIV
			formPart2, // DIV
			formPart3, // DIV
			formPart4, // DIV
			formPart5, // DIV
			buttonsNext, // DIV
			buttonsSend, // DIV
			submitButton, // BUTTON
			review, // DIV
			backHref; // string
		function specials() {
			switch(currentProgress) {
				case 0:
				case 1:
				case 2:
					break;
				case 3:
					createReview();
					break;
			}
		};
		function validate() {
			function checkboxes(name) {
				var items = form[name];
				var result = false;
				for (var i = 1-!!core; i < items.length && !result; i++) { // first is hidden
					result = items[i].checked;
				}
				return result;
			};
			function checkinput(name, reg) {
				var input = form[name];
				var result = true;
				if (!reg.test(input.value)) {
					result = false;
					input.style.border = '3px solid maroon';
					input.style.margin = '-3px';
				} else {
					input.style.margin = input.style.border = '';
				}
				return result;
			};
			var validators = [
				/*
				 * Progress 1/4:
				 * Time and place. Skipped if buying a giftcard
				 */
				function() {
					var result = true;
				/*
					//var result = checkboxes('afi');
					var boxes = form['afi'];
					var item;
					var widths = ['5px 5px 0', '0 5px', '0 5px', '0 5px 5px'];
					for (var i = 1-!!core; i < boxes.length; i++) {
						item = boxes[i].parentNode.style;
						item.border = result ? '' : 'solid maroon';
						item.borderWidth = result ? '' : widths[i-!core];
						item.margin = result ? '' : widths[i-!core].replace(/[0-9]+px/g, function(str){return '-'+str;});
					}*/

/*
					var then = new Date(form['afy'].value, form['afw'].value-1, form['afu'].value);
					var now = new Date();
					if (then.getTime() - now.getTime() < 2*24*60*60*1000) { // two days
						result = false;
						form['afy'].parentNode.style.border =
						form['afw'].parentNode.style.border =
						form['afu'].parentNode.style.border = '5px solid maroon';
						form['afy'].parentNode.style.margin =
						form['afw'].parentNode.style.margin =
						form['afu'].parentNode.style.margin = '-5px';
						form['afy'].parentNode.style.marginLeft =
						form['afw'].parentNode.style.marginLeft =
						form['afw'].parentNode.style.marginRight =
						form['afu'].parentNode.style.marginRight =
						form['afy'].parentNode.style.borderLeft =
						form['afw'].parentNode.style.borderLeft =
						form['afw'].parentNode.style.borderRight =
						form['afu'].parentNode.style.borderRight = '0';
					} else {
						form['afy'].parentNode.style.margin =
						form['afw'].parentNode.style.margin =
						form['afu'].parentNode.style.margin =
						form['afy'].parentNode.style.border =
						form['afw'].parentNode.style.border =
						form['afu'].parentNode.style.border = '';
					}*/
					return result;
				},

				/*
				 * Progress 2/4:
				 * Product and number of participants
				 */
				function() {
					var result = checkboxes('afl');
					form['afl'][1-!!core].parentNode.style.border = result ? '' : '5px solid maroon';
					form['afl'][1-!!core].parentNode.style.margin = result ? '' : '-5px';
					form['afl'][2-!!core].parentNode.style.border = result ? '' : '5px solid maroon';
					form['afl'][2-!!core].parentNode.style.margin = result ? '' : '-5px';
					if (!result) {
						form['afl'][1-!!core].parentNode.style.marginBottom =
						form['afl'][1-!!core].parentNode.style.borderBottom =
						form['afl'][2-!!core].parentNode.style.marginTop =
						form['afl'][2-!!core].parentNode.style.borderTop = '0';
					}
					return result;
				},

				/*
				 * Progress 3/4
				 * Contact information, Compagny information and comment
				 * 
				 */
				function() {
					var
						nonEmpty = /./,
						phone = /^\+?[\- 0-9]+$/,
						zipcode = /^[A-Z0-9\-]+$/,
						cvr = /^[0-9 ]{8,}$/,
						empty = /^$/,
						email = /^[a-z0-9\.\-_]+@[a-z0-9\.\-_]+\.[a-z]{2,6}$/,
						result = true;

					result = checkinput('agd', nonEmpty) && result; // Navn
					result = checkinput('age', nonEmpty) && result; // Adresse
					result = checkinput('agf', zipcode)  && result; // Postnr
					result = checkinput('agg', nonEmpty) && result; // By
					result = checkinput('agh', nonEmpty) && result; // Land
					result = checkinput('agi', phone)    && result; // Tlf/mobil
					result = checkinput('agj', email)    && result; // E-mail

					if (form['agm'].value != '') { // firmanavn
						result = checkinput('agn', nonEmpty) && result; // Adresse
						result = checkinput('ago', zipcode)  && result; // Postnr
						result = checkinput('agp', nonEmpty) && result; // By
						result = checkinput('agq', nonEmpty) && result; // Land
						result = checkinput('agr', phone)    && result; // Tlf
						result = checkinput('ags', cvr)      && result; // CVR
					} else {
						result = checkinput('agn', empty) && result; // Adresse
						result = checkinput('ago', empty) && result; // Postnr
						result = checkinput('agp', empty) && result; // By
						result = checkinput('agq', empty) && result; // Land
						result = checkinput('agr', empty) && result; // Tlf
						result = checkinput('ags', empty) && result; // CVR
					}
					return result;
				},

				/*
				 * Progress 4/4
				 * No validation, just reviewing information
				 */
				function() {
					return false;
				}
			];
			var result = false;
			if (typeof validators[currentProgress] == 'function')
				result = validators[currentProgress]();
			return result;
		};
		return {
			initialize: function() {
				if (core)
					return;
				type = (type = window.location.search.match(/type=(gift|booking)/)) ? type[1] : 'booking';
				map = d('item_aeb');
				spot1 = d('item_aet');
				spot2 = d('item_aev');
				titleContact = d('item_aex');
				titleReview = d('item_afb');
				progress = d('item_aek');
				selectAirport = d('item_aem');
				selectProduct = d('item_aer');
				formPart1 = d('c0ppp');
				formPart2 = d('c1ppp');
				formPart3 = d('c2ppp');
				formPart4 = d('c3ppp');
				formPart5 = d('c4ppp');
				buttonsNext = d('item_agv');
				buttonsSend = d('item_agy');
				review = d('ahe_review');
				currentProgress = 0;
				if (!document.forms.length) {
					var h = [spot1, spot2 ,titleContact ,titleReview, progress, selectAirport, selectProduct, buttonsNext, buttonsSend, review];
					for (var i = 0; i < h.length; i++) {
						if (h[i])
							hide(h[i]);
					}
					return;
				}
				progress = progress.firstChild;
				form = document.forms['afg'];
	
				form.onsubmit = function() {
					return false;
				};
				submitButton = form.getElementsByTagName('button')[0];
				hide(submitButton);
				var products = form['afl'];
				// first is hidden
				products[1-!!core].onchange = products[1-!!core].onclick = function() {
					populateSelect('aga', 1, 8);
				};
				products[2-!!core].onchange = products[2-!!core].onclick = function() {
					populateSelect('aga', 1, 12);
				};
				parts = [
					[buttonsNext, formPart1, map, selectAirport],
					[buttonsNext, formPart2, spot1, spot2, selectProduct],
					[buttonsNext, formPart3, formPart4, formPart5, titleContact],
					[buttonsSend, titleReview, review]
				];
				backHref = d('abk_abw').getElementsByTagName('a')[0];
				if (backHref)
					backHref = backHref.getAttribute('href');
				else
					backHref = null;

				function formatZero(value) {
					return value < 10 ? '0' + value : value;
				};
				function formatHour(value) {
					return formatZero(value)+':00';
				};
				populateSelect('aga', 1, 8, null, true); // deltagere, 1-3 for dogfight, 4-12 for gruppe-arrangement
				//populateSelect('afu', 1, 31, formatZero, true); // dag
				//populateSelect('afw', 1, 12, formatZero, true); // m�ned
				var year = (new Date()).getFullYear();
				//populateSelect('afy', year, year + 2, null, true); // �r
				//populateSelect('axo', 8, 18, formatHour, true);

				if (type == 'gift')
					currentProgress++;

				showProgress();
				//autoFill();
			},
			next: function() {
				if (validate()) {
					currentProgress++;
					showProgress();
					specials();
				}
			},
			previous: function() {
				currentProgress--;
				if (currentProgress-(type=='gift') >= 0) {
					showProgress();
					specials();
				}
				else if (secoya.menu && secoya.menu.navigate)
					secoya.menu.navigate(backHref);
				else
					window.location.href = backHref;
			},
			submit: function() {
				form.submit();
			},
			autoFill: function() {
				autoFill();
			}
		}
	})()
};

//lightCore.registerInit({initialize:function(){aircombat.form.autoFill();}});