﻿




window.addEvent('domready', function() {

	var setLocationRegion = new Class ({
		initialize:function(opt){
			this.id = opt.id;
			this.coordinates = opt.coordinates;
			this.name = opt.name;
			this.cid = opt.cid;
		}
		,getCoordinates:function(index){
			var xCoordinates = [];
			for (var i = 0; i < this.coordinates.length; i++) {
				xCoordinates.push(this.coordinates[i][index]);
			}
			return xCoordinates;
		}
		,getXCoordinates : function () {
			return this.getCoordinates(0);
		}
		,getYCoordinates : function () {
			return this.getCoordinates(1);
		}
		,getVerticeCount : function () {
			return this.coordinates.length;
		}

	});

	document.DRGeoLocation = {
		Regions:[]
		,getdata: function(){
			return document.dr.parameters;
		}		
		, evalueate: function(obj){
			if (document.dr && document.dr.parameters) {
				var merged = document.dr.parameters;
				if (obj.data) merged['data'] = obj.data;
				obj.fnc(merged);
			} else {			
				document.getElement('body').addEvent('geoLoaded',function(){
					var merged = document.dr.parameters;
					if (obj.data) merged['data'] = obj.data;
					this.fnc(merged);
				}.bind(obj));
			}
		}
		
		, addRegions: function(regions){
			$each(regions,function(e){
				this.Regions.push(new setLocationRegion({id:e[0], coordinates:e[1],name:e[2],cid:e[3]}));
			}.bind(this))			
		}
		
		, determineRegionForPosition: function (position) {

			var found = false;
			var theRegion;
			$each(this.Regions,function(e){
				if (this.pnpoly(e.getVerticeCount(), e.getXCoordinates(), e.getYCoordinates(), position.lat, position.lng)) {
					theRegion = e;
					found = true;
				}								
			}.bind(this));
			
			if (!found) theRegion = GeoDims.DefaultRegion;
			return {cid:theRegion.cid,name:theRegion.name};
		}
		
		, pnpoly: function (nvert, vertx, verty, testx, testy) {
			var i, j, c = false;
			for (i = 0, j = nvert - 1; i < nvert; j = i++) {
				if (((verty[i] > testy) != (verty[j] > testy)) &&
					(testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i])) {
					c = !c;
				}
			}
			return c;
		}
	}
	document.DRGeoLocation.addRegions(
    [
        ["97ebad32-fec0-4ad4-bbca-51c6838f9fdb", [[55.57019, 11.79519], [55.677189, 11.74026], [55.730591, 11.80343], [55.957272, 11.80137], [56.226551, 11.61117], [56.284149, 12.01149], [56.161991, 12.84164], [55.57019, 12.85331], [55.468262, 12.72033], [55.49091, 12.36511], [55.37677, 12.09869], [55.427841, 11.95655], [55.54689, 11.89476], [55.57019, 11.79519]], "København","KH4"],
        ["ec33bcfa-e263-405f-bd70-512554b6689c", [[55.428909, 11.06589], [55.008339, 11.067352], [54.751888, 10.8197], [54.4911, 11.70959], [54.65667, 12.53356], [55.274731, 12.68737], [55.490898, 12.36512], [55.376759, 12.09868], [55.42783, 11.95653], [55.546879, 11.89474], [55.570179, 11.79517], [55.524349, 11.59606], [55.569752, 11.60053], [55.617519, 11.45874], [55.549412, 11.30115], [55.51701, 11.37239], [55.459728, 11.25412], [55.428909, 11.06589]], "Sjælland","NV4"],
        ["5576d188-6d2b-41aa-8786-1ca47d5113b1", [[56.592758, 9.99618], [56.69017, 10.20046], [56.708271, 10.26457], [56.701859, 10.35873], [56.773891, 10.6248], [56.840328, 10.98495], [56.790649, 11.85241], [55.716591, 10.70983], [55.700508, 10.06656], [55.793751, 10.15926], [55.823078, 9.94348], [55.80135, 9.91241], [55.80246, 9.79512], [55.908161, 9.58359], [55.9109, 9.50939], [56.10957, 9.27658], [56.10498, 9.36584], [56.229801, 9.31709], [56.255486, 9.192166], [56.301338, 9.203], [56.370266, 9.402838], [56.313091, 9.491479], [56.305664, 9.671418], [56.567829, 9.747019], [56.592758, 9.99618]], "Østjylland","AR4"],
        ["b8e952be-7501-4c70-994c-aeb06ed0ca17", [[54.848152, 14.359131], [55.50375, 14.36462], [55.502193, 15.545654], [54.841827, 15.556641], [54.848152, 14.359131]], "Bornholm","RO4"],
        ["c826b1d9-a4c4-4cdd-801f-22a64b2f5ad9", [[55.881081, 7.82707], [55.791237, 8.476631], [55.846409, 8.50547], [55.849781, 8.63109], [55.802429, 8.74031], [55.77309, 8.71284], [55.71666, 8.79867], [55.700359, 8.75986], [55.63652, 8.85154], [55.584942, 9.03968], [55.614761, 9.1598], [55.578339, 9.24156], [55.49519, 9.24293], [55.422771, 9.15607], [55.36945, 9.2171], [55.295528, 9.09256], [55.333241, 9.05754], [55.301399, 8.89755], [55.245461, 8.90167], [55.264832, 8.62323], [55.165531, 7.95547], [55.881081, 7.82707]], "Esbjerg","ES4"],
        ["6cdfca06-be51-4a94-a20a-e5dd63c9c176", [[55.715889, 10.70926], [55.429111, 11.06529], [55.009121, 11.06597], [54.751572, 10.81741], [54.556931, 10.44799], [55.412987, 9.630461], [55.453159, 9.70916], [55.495579, 9.66041], [55.524738, 9.67346], [55.512489, 9.74109], [55.548248, 9.74796], [55.63361, 10.00099], [55.699928, 10.06605], [55.715889, 10.70926]], "Fyn","OD4"],
        ["185e4a01-b57f-41ed-b3a8-4e5be0068836", [[55.88147, 7.82775], [55.778889, 8.48419], [55.849869, 8.63113], [55.802704, 8.739839], [55.899181, 8.8282], [55.895721, 8.95179], [55.844479, 8.96209], [55.845249, 9.15573], [55.95266, 9.2134], [55.915352, 9.39399], [55.910912, 9.50935], [56.1096, 9.2765], [56.105099, 9.36567], [56.22979, 9.31698], [56.255459, 9.19212], [56.301392, 9.20293], [56.370281, 9.40283], [56.313099, 9.4915], [56.305679, 9.6714], [56.567799, 9.74693], [56.632771, 9.63488], [56.659809, 9.16603], [56.685841, 9.14337], [56.72485, 9.20826], [56.902248, 9.0232], [57.02055, 9.09256], [57.30867, 8.9645], [56.8979, 7.75543], [55.88147, 7.82775]], "Midt & Vest","HO4"],
        ["c3e3a280-72cc-4766-b61b-a9b655056267", [[57.30872, 8.96484], [57.020451, 9.09281], [56.902302, 9.02338], [56.72485, 9.20929], [56.685459, 9.14371], [56.66, 9.16637], [56.632809, 9.63501], [56.567848, 9.74689], [56.592861, 9.99592], [56.69046, 10.20046], [56.708271, 10.26432], [56.702, 10.35873], [56.773979, 10.62429], [56.840561, 10.98266], [57.54295, 11.53198], [57.955669, 10.64575], [57.727612, 9.40429], [57.30872, 8.96484]], "Nordjylland","AL4"],
        ["b8487fe4-7ff2-46d4-8a25-d7e3c7427b94", [[56.084297, 11.101685], [55.716469, 10.70995], [55.429008, 11.06597], [55.459782, 11.25412], [55.516972, 11.37222], [55.54932, 11.30089], [55.617519, 11.45874], [55.569752, 11.60061], [55.524349, 11.59606], [55.57019, 11.79513], [55.677391, 11.73992], [55.730492, 11.80292], [55.785839, 11.8024], [55.957272, 11.80103], [56.226368, 11.610146], [56.084297, 11.101685]], "Nordvestsjælland","NS4"],
        ["02d02cc4-db2d-4200-b68f-22e1e84faf42", [[55.164749, 7.9541], [55.264252, 8.62426], [55.245361, 8.9018], [55.301319, 8.89763], [55.333141, 9.05745], [55.295441, 9.09247], [55.36916, 9.21718], [55.32885, 9.26233], [55.358028, 9.36103], [55.30843, 9.42764], [55.412792, 9.630289], [55.336369, 9.70436], [54.556732, 10.447483], [54.541801, 8.11615], [55.164749, 7.9541]], "Syd","AB4"],
        ["48ae6b81-3e90-440b-9c8a-820faea2d2d4", [[55.908031, 9.58351], [55.802429, 9.79499], [55.801281, 9.91241], [55.82288, 9.94331], [55.793751, 10.15857], [55.633808, 10.0003], [55.548828, 9.74762], [55.512489, 9.74075], [55.525131, 9.67346], [55.49519, 9.65972], [55.45335, 9.70848], [55.308491, 9.42762], [55.358028, 9.36172], [55.328911, 9.26233], [55.422771, 9.1571], [55.49519, 9.24362], [55.578339, 9.24224], [55.614811, 9.15985], [55.585041, 9.03973], [55.636589, 8.85167], [55.70034, 8.75996], [55.716709, 8.79889], [55.77317, 8.71297], [55.899132, 8.8282], [55.89571, 8.951733], [55.848526, 8.962768], [55.848511, 9.06345], [55.952461, 9.21358], [55.915329, 9.39399], [55.908031, 9.58351]], "Trekanten","TR4"]
    ]);
	
	var DRGeoLocationAssets = new Asset.javascript('http://j.maxmind.com/app/geoip.js', {
		id: 'DRGeoLocation', 
		onload: function(){
			if (!document.dr) document.dr = {}
			if (!document.dr.parameters) document.dr.parameters = {}
			
			if ( (typeof geoip_latitude =='function') && (typeof geoip_longitude =='function') ) {
				document.dr.parameters['lat'] = geoip_latitude();
				document.dr.parameters['lng'] = geoip_longitude();
				document.dr.parameters['contry'] = geoip_country_name();
				document.dr.parameters['region']  = (geoip_country_name() == "Denmark") ? document.DRGeoLocation.determineRegionForPosition({lat:geoip_latitude(),lng:geoip_longitude() }) : false; // geoip_region_name()
				document.dr.parameters['geoInfo'] = true;
			} else {
				document.dr.parameters['geoInfo'] = false;		 
			}
			document.getElement('body').fireEvent('geoLoaded');
		}
	})
});




