window.dkigroup = (function(w, d){
	function getId(id) {
		return d.getElementById(id);
	};
	var
		googlemap = null,
		infoWindow,
		colors = {
			international: true,
			interior: true,
			care: true,
			premedia: true,
			falengreen: true,
			grambogaard: true,
			logumkloster: true,
			logistics: true,
			zeuner: true,
			prezious: true
		};
	return {
		setupColorLink: function() {
			//event = event || w.event;
			var e = getId('colorLinks');
			if (e && (e = e.getElementsByTagName('ul')[0])) {
				e.onclick = function(event) {
					event = event || window.event;
					var elm = event.target || event.srcElement;
					while (elm && elm.tagName != 'LI') {
						elm = elm.parentNode;
					}
					if (elm && (elm = elm.getElementsByTagName('a')[0])) {
						if (elm.target == '_blank') {
							window.open(elm.href);
						} else {
							window.location.href = elm.href;
						}
					}
				};
			}
		},
		scroll: function() {
			var
				left = getId('arrowLeft'),
				right = getId('arrowRight'),
				scroll = getId('spot1picture'),
				parent = scroll.parentNode,
				width = parent.offsetWidth,
				autorun = !w.core;
			function getItems() {
				var divs = scroll.getElementsByTagName('div');
				var count = 0;
				for (var i = 0; i < divs.length; i++) {
					if (divs[i].className == 'item')
						count++;
				}
				count += !!w.core;
				return count;
			};
			var f = function(){
				width = parent.offsetWidth;
				scroll.style.width = getItems()*width+'px';
			};
			if (w.lightCore.initialized)
				f();
			else
				lightCore.registerInit({initialize:f});

			var index = 0;
			var fx = new secoya.fx({
				from: 0,
				to: 0,
				duration: 0.5,
				type: secoya.fx.power,
				callback: function(x) {
					parent.scrollLeft = x+width*index;
				}
			});
			left.onclick = function() {
				if (w.core)
					f();
				if (index > 0) {
					if (fx.running)
						fx.resetToEnd();
					index--;
					fx.from = width;
					fx.to = 0;
					fx.start();
				}
			};
			right.onclick = function() {
				if (w.core)
					f();
				var count = getItems();
				if (index < count-1) {
					if (fx.running)
						fx.resetToEnd();
					index++;
					fx.from = -width;
					fx.to = 0;
					fx.start();
				}
			};
			if (autorun && getItems() > 1) {
				var intervalId = setInterval(function(){
					if (w.lightCore.initialized) {
						if (autorun) {
							if (index < getItems()-1) {
								right.onclick();
							} else {
								(new secoya.fx({
									from: scroll.offsetWidth-width,
									to: 0,
									duration: 0.5,
									type: secoya.fx.power,
									callback: function(x) {
										parent.scrollLeft = x;
									}
								})).start();
								index = 0;
							}
						} else {
							clearInterval(intervalId);
						}
					}
				}, 7000);
			}
		},
		googlemap: function(id) {
			var API = w.google.maps;
			googlemap = new API.Map(getId(id), {
				//backgroundColor: 'white',
				center: new API.LatLng(56.114936, 10.360107),
				disableDefaultUI: true,
				mapTypeControl: false,
				mapTypeId: API.MapTypeId.ROADMAP,
				navigationControl: true,
				navigationControlOptions: {
					position: API.ControlPosition.TOP_LEFT,
					style: API.NavigationControlStyle.ZOOM_PAN
				},
				noClear: true,
				streetViewControl: true,
				zoom: 6
			});
		},
		addMapMarker: function(id) {
			var API = w.google.maps;
			if (!infoWindow)
				infoWindow = new API.InfoWindow();
			var div = getId(id);
			var link = div.getElementsByTagName('a')[0];
			if (link) {
				var url = link.href;
				var fragment = d.createDocumentFragment();
				while (link.firstChild) {
					fragment.appendChild(link.firstChild);
				}
				link.parentNode.replaceChild(fragment, link);
				var match = url.match(/&ll=([0-9\-\.]+),([0-9\-\.]+)/);
				if (match) {
					var lat = parseFloat(match[1]), lng = parseFloat(match[2]);
					var color = url.match(/&_dki_color=([a-z]+)/);
					if (color && color[1] in colors)
						color = color[1];
					else
						color = 'unknown';
					if (!isNaN(lat) && !isNaN(lng)) {
						var marker = new API.Marker({
							flat: true,
							icon: '/Templates/default/Css/css/img/dki/'+color+'.png',
							map: googlemap,
							position: new API.LatLng(lat, lng),
							visible: true
						});
						var ll = match[1]+','+match[2];
						var q = url.match(/[\?&]q=([^&]+)/);
						if (q)
							q = q[1];
						else
							q = ll;
						var click = function(){
							var content = div.cloneNode(true);
							content.id = "";
							infoWindow.close();
							infoWindow.setPosition(marker.getPosition());
							content.appendChild(secoya.dom.createElement('div', {className: 'externalmap'}, null,
								secoya.dom.createElement('a', {target: '_blank'}, {href: 'http://maps.google.dk/?ie=UTF8&ll='+ll+'&daddr='+q+'&z='+googlemap.getZoom()}, 'Start rutevejledning')
							));
							infoWindow.setContent(content);
							infoWindow.open(googlemap, marker);
							getId('googlemap_canvas').className = color;
						};
						API.event.addListener(marker, 'click', click);
						div.onclick = click;
						div.className += ' clickable '+color;
					}
				}
			}
		}
	};
})(window, document);
