﻿if (typeof (iLocalInfoControl) == 'undefined') {
	Type.registerNamespace("iLocalInfoControl");

	iLocalInfoControl.Feed = function() {
		iLocalInfoControl.Feed.initializeBase(this);
		this._address = null;
		this._recenter = false;
		this._ilocalservice = new RoutenetFeedService();
		this._locateservice = xLocateService;
		this._infolabel = null;
		this._query;
		this._subqueries = [];

		this.page = 1;

	};

	iLocalInfoControl.Feed.prototype = {
		initialize: function() {
			iLocalInfoControl.Feed.callBaseMethod(this, "initialize");


			this._ilocalservice.set_path("services/RoutenetFeedService.asmx");
			this._ilocalservice.set_defaultUserContext(this);
			this._ilocalservice.set_defaultSucceededCallback(this.SucceededCallback);
			this._ilocalservice.set_defaultFailedCallback(this.FailedCallback);
			this._ilocalservice.set_timeout(20000);

			this._locateservice.set_path("services/xLocateService.asmx");
			this._locateservice.set_defaultUserContext(this);
			this._locateservice.set_defaultSucceededCallback(this.SucceededCallback);
			this._locateservice.set_defaultFailedCallback(this.FailedCallback);
			this._locateservice.set_timeout(20000);

			date = new Date();
			this._includeCSS("/css/ilocal.css?d=" + date.toUTCString());



		},

		set_infolabel: function(value) {
			this._infolabel = value;

		},

		get_infolabel: function() {
			return this._infolabel;
		},

		reset_subqueries: function() {
			this._subqueries = [];
		},
		get_subqueries: function() {
			return this._subqueries;
		},

		get_query: function() {
			return this._query;
		},

		get_address: function() {
			return this._address;
		},
		set_address: function(value) {
			this._address = value;
		},

		get_recenter: function() {
			return this._recenter;
		},
		set_recenter: function(value) {
			this._recenter = value;
		},

		get_locateservice: function() {
			return this._locateservice;
		},

		Disable: function() {
			if (!xmap) return;
			var mapObj = xmap.getMapObject();
			mapObj.get_layermanager().Remove("ilocal");
		},


		Search: function(query) {

			//    				if (!this._address) {
			//    					OnError(this, "Gebruik eerst de vanuit/naar zoek functie om een adres in te stellen");
			//    					return false;
			//    				}

			if (query) {
				this._query = query;
				this.page = 1;
			}


			if (!this._query || this._query == "") {
				OnError(null, "Er is geen zoekterm opgegeven<br/>Kies of vul een zoekterm in en probeer het opnieuw.");
				return false;
			}

			if (!xmap) return false;
			var mapObj = xmap.getMapObject();

			var obj = mapObj.get_layermanager().Find("ilocal");
			if (!obj) {
				obj = mapObj.get_layermanager().AddLayer("ilocal");
				obj.add_updated(OnILocalUpdated);
				obj.add_unload(OnILocalUnload);
			}
			obj.set_partialloading(false);
			obj.set_searchquery(this._query + "$" + this._subqueries.join("$") + "^" + this.page);
			if (this._recenter) {
				var rect = new TripzoomLib.Element.Rect(this._address.XCoord - 1000, this._address.YCoord + 1000, this._address.XCoord + 1000, this._address.YCoord - 1000);
				this._recenter = false;
				mapObj.set_rect(rect);
			} else {
				obj.Update(mapObj.get_mapview().get_viewrect());
			}
			// reset searchquery for pagenumber = 1, a new map must reset the search 
			obj.set_searchquery(this._query + "$" + this._subqueries.join("$") + "^1");
		},

		PopulateResults: function() {

			if (this._address) {
				if (xmap) {
					var mapObj = xmap.getMapObject();
					this._locateservice.ReverseSearch(this._address.XCoord, this._address.YCoord, 3);
				}
			} else {
				if (xmap) {
					var mapObj = xmap.getMapObject();
					this._locateservice.ReverseSearch(mapObj.get_mapview().get_viewrect().centerx(), mapObj.get_mapview().get_viewrect().centery(), 3);
				}
			}
			this._ilocalservice.GenerateResult("iLocal", "../includes/iLocalFeed.xsl");
		},

		SucceededCallback: function(result, userContext, methodName) {

			if (result) {
				switch (methodName) {
					case "GenerateResult":
						{
							var div = $get("iLocalFeedResult");
							div.style.display = '';
							div.innerHTML = result.HTML;
							companyIds = result.CompanyIds;
							userContext.countLog('http://www.ilocal.nl/service/LogParserFiles', companyIds, '1', companyIds.split('~').length, 'nl', 'NL', '', '', g_iLocal.get_query(), '36');

							break;
						}
					case "ReverseSearch": 
						{
							if (result && result.length > 0) {

								//				  					var text = $get(userContext.get_infolabel());
								//				  					var location = result[0];
								//				  					
								//				  					
								//					    			if (text && location) {
								//		   								text.innerHTML = "Zie beneden voor handige adressen in de buurt van " + ((location.District != "") ? location.District + ", " : "") + location.City;
								//	    							}    								
							}
							break;

						}

				}
			}
		},

		FailedCallback: function(error, userContext, methodName) {
			OnError(null, error.get_message());
		},

		Clear: function() {
			var div = $get("iLocalFeedResult");
			if (div) {
				div.style.display = 'none';
				div.innerHTML = "";
			}
			this.page = 1;
		},

		/* This script and many more are available free online at
		The JavaScript Source!! http://javascript.internet.com
		Created by: Benoit Asselin | http://www.ab-d.fr/ */
		_includeCSS: function(p_file) {
			var v_css = document.createElement('link');
			v_css.rel = 'stylesheet';
			v_css.type = 'text/css';
			v_css.href = p_file;
			document.getElementsByTagName('head')[0].appendChild(v_css);
		},

		/// <summary>
		//// This function handles the logging for iLocal
		///</summary>
		countLog: function(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9) {

			// update google analytics
			TrackPageView("tab", "ilocal");

			ilocal_sitestat("http://nl.sitestat.com/ilocal/ilocal/s?partnersilocal.nl.nl_nl.results&amp;category=routenet.nl");

			// ilocal pageTracker

			// update iLocal log
			var d = document;
			if (document.getElementById('iLocalLogImg') != null && document.getElementById('iLocalLogImg').src != null) {
				if (p2 > 1) {
					d.getElementById('iLocalLogImg').src = p0 + '?companies=' + p1 + '&logtype=' + p2 + '&lang=' + p4 + '&partnerid=' + p9 + '&uid=' + new Date().getTime();
				} else {
					d.getElementById('iLocalLogImg').src = p0 + '?companies=' + p1 + '&logtype=' + p2 + '&nrofresults=' + p3 + '&lang=' + p4 + '&country=' + p5 + '&classid=' + p6 + '&locid=' + p7 + '&searchterm=' + p8 + '&partnerid=' + p9 + '&uid=' + new Date().getTime();
				}
			}
		}


	};

	iLocalInfoControl.Feed.registerClass("iLocalInfoControl.Feed", Sys.Component);

	// ilocal sitestat
	function ilocal_sitestat(ns_l) {
		ns_l += '&amp;ns__t=' + (new Date()).getTime(); ns_pixelUrl = ns_l;
		ns_0 = document.referrer;
		ns_0 = (ns_0.lastIndexOf('/') == ns_0.length - 1) ? ns_0.substring(ns_0.lastIndexOf('/'), 0) : ns_0;
		if (ns_0.length > 0) ns_l += '&amp;ns_referrer=' + escape(ns_0);
		if (document.images) { ns_1 = new Image(); ns_1.src = ns_l; } else document.write('<img src="' + ns_l + '" width="1" height="1" alt="">');
	}

} // typeof (InfoControl) == 'undefined')   
