var LocationDataControl = function(parentDivSelector,config)
{
	var self = this;
	
	this.container = $(parentDivSelector);
	this.config = config;
	this.t3_type = 3000;
	
	this.div_select = null;
	this.div_data = null;
	this.div_select_options = null;
	
	this.entrys = null;
	
	this.constants = {
		ris_uid: 14,
		ris_certificate_matrix_href: "http://www.remondis-industrie-service.de/ris/downloads_46454/zertifikate_8259/"
	};
	
	this.current_entry = 0;
	
	this.init = function()
	{
		//create inner structure for container
		this.div_select = $("<form class='niceform' action='' method=''><div></div></form>"); //Div that will contain the selectbox
		this.container.append(this.div_select);
		this.div_data = $("<div></div>"); //Div that will contain the Location-Entry
		this.container.append(this.div_data);
	}
	this.init();
	
	this.load_by_company_city = function(company_id,city_id,ext_config)
	{
		if (typeof ext_config != "undefined")
		{
			$.extend(this.config,ext_config);
		}
		
		$.getJSON('/',{
			type: this.t3_type,
			L: this.config.lang,
			company_id: company_id,
			city_id: city_id,
			locationsPIDs: this.config.locationsPIDs,
			id: (this.config.page_id ? this.config.page_id : 1)
		},this.load_by_company_city_cb);
	}
	
	this.load_by_company_city_cb = function(data)
	{
		self.entrys = data;
		
		if (self.entrys.length > 1) //Render Select
		{
			if (self.div_select_options != null) $(self.div_select_options).remove();
			
			self.render_select(self.entrys[0].city);
			if (self.config.default_translation == 1) self.translate_current();
		}
		else //Render Entrys
		{
			if (self.div_select_options != null) $(self.div_select_options).remove();
			
			self.div_data.empty();
			self.div_select.empty();
			for(var x=0;x<self.entrys.length;x++) self.render_entry(self.entrys[x]);
			
			if (self.config.default_translation == 1) self.translate_current();
		}
	}
	
	this.load_ids = function(ids,ext_config)
	{
		ids = ids+""; //make it a string
		
		if (typeof ext_config != "undefined")
		{
			$.extend(this.config,ext_config);
		}
		
		if (ids.indexOf(",") > -1) //more than one id
		{
			//load select box
			this.load_by_ids(ids);
		}
		else //one id
		{
			//load entry data
			this.load_by_id(ids);
		}
	}
	
	this.load_by_ids = function(ids)
	{
		$.getJSON('/',{
			type: this.t3_type,
			L: this.config.lang,
			location_ids: ids,
			id: (this.config.page_id ? this.config.page_id : 1)
		},this.load_by_ids_cb);
	}
	
	this.load_by_ids_cb = function(data)
	{
		self.entrys = data;
		
		if (self.entrys.length > 1) //Render Select
		{
			if (self.div_select_options != null) $(self.div_select_options).remove();
			self.render_select();
		}
		else //Render Entrys
		{
			if (self.div_select_options != null) $(self.div_select_options).remove();
			self.div_data.empty();
			self.div_select.empty();
			for(var x=0;x<self.entrys.length;x++) self.render_entry(self.entrys[x]);
			
			if (self.config.default_translation == 1) self.translate_current();
		}
	}
	
	this.render_select = function(city_for_title)
	{
		self.div_data.empty();
		self.div_select.empty();
		
		var select_title = "";
		if (city_for_title)
		{
			select_title = this.get_l("business_in")+" "+city_for_title;
		}
		else if (location.href.indexOf("saria") > -1)
		{
			select_title = this.get_l("more_than_three");
		}
		else
		{
			select_title = $.trim(self.entrys[0].titel)+", "+($.trim(self.entrys[0].subtitel).length > 0 ? $.trim(self.entrys[0].subtitel)+" " : "")+$.trim(self.entrys[0].city);
		}
		if (select_title.length > 0)
		{
			this.div_select.append("<div class='select_title'>"+select_title+"</div>");
		}
		
		var select = $("<select></select>");
		select.addClass("width_145"); //Add Class for Niceforms
		select.append("<option value='0'>"+this.get_l("select_company")+"</option>");
		for (var x=0;x<self.entrys.length;x++)
		{
			select.append("<option value='"+self.entrys[x].uid+"'>"+self.entrys[x].titel+" "+self.entrys[x].subtitle_2+"</option>")
		}
		this.div_select.append(select);
		this.div_select.append("<div class=\"hTrenner\"><div><div><hr /></div></div></div>")
		
		select.change(this.select_onChange);
		
		this.div_select_options = replaceSelect(select.get(0));
	}
	
	this.select_onChange = function()
	{
		self.div_data.empty();
		if (this.selectedIndex > 0) self.render_entry(self.entrys[this.selectedIndex-1]);
		
		if (self.config.default_translation == 1) self.translate_current();
	}
	
	this.load_by_id = function(id,sys_language_uid,ext_config)
	{
		if (typeof ext_config != "undefined")
		{
			$.extend(this.config,ext_config);
		}
		
		$.getJSON('/',{
			type: this.t3_type,
			L: this.config.lang,
			sys_language_uid: (sys_language_uid ? sys_language_uid : 0),
			location_id: id,
			id: (this.config.page_id ? this.config.page_id : 1)
		},this.load_by_id_cb);
	}
	
	this.load_by_id_cb = function(data)
	{
		if (self.div_select_options != null) $(self.div_select_options).remove();
		
		self.data = data;
		
		self.div_data.empty();
		self.div_select.empty();
		self.render_entry(data);
		
		if (self.config.default_translation == 1) self.translate_current();
	}
	
	this.translate_current = function()
	{
		if (!self.current_entry.translations || !self.current_entry.translations[0]) return; 
		self.div_data.empty();
		self.render_entry(self.current_entry.translations[0],true);
	}
	this.untranslate_current = function()
	{
		self.div_data.empty();
		self.render_entry(self.current_entry);
	}
	
	this.render_entry = function(data,is_translation)
	{
		if (!data.uid) return;
		
		if (!is_translation) this.current_entry = data;
		
		var entry_div = $("<div class='entry entry_uid_"+data.uid+"'></div>");
		
		if (is_translation || data.translations.length > 0)
		{
			var flag_img = $("<img src='/typo3conf/ext/3s_locations/res/img/translation_flag_"+data.country_id+".gif' style='margin-bottom:5px; cursor:pointer;'/>");
			if (is_translation)
			{
				flag_img.click(this.untranslate_current);
			}
			else
			{
				flag_img.click(this.translate_current);
			}	
			var flag_div = $("<div class='translation'></div>");
			flag_div.append(flag_img);
			//entry_div.append(flag_div);
		}
		
		if (data.freetext_above.length > 0) entry_div.append("<div class='freetext_above'>"+data.freetext_above+"</div>");
		
		entry_div.append("<div class='titel fett'>"+data.titel+"</div>");
		
		if (data.fk_contact_over > 0)
		{
			var contact_over_link = $("<a href='#'>"+this.get_l("contact_via")+"</a>");
			contact_over_link.bind("click",{location_id:data.fk_contact_over},function(event){
				self.load_by_id(event.data.location_id);
				return(false);
			});
			
			var contact_over_link_div = $("<div class='contact_over'></div>");
			contact_over_link_div.append(contact_over_link);
			
			entry_div.append(contact_over_link_div);
			
			this.div_data.append(entry_div);
			
			return;
		}
		
		if (data.subtitel.length > 0) entry_div.append("<div class='subtitel'>"+data.subtitel+"</div>");
		if (data.subtitle_2.length > 0) entry_div.append("<div class='subtitle_2'>"+data.subtitle_2+"</div>");
		if (data.street.length > 0)
		{
			switch (parseInt(data.country_id))
			{
				case 20: //Australien
					entry_div.append("<div class='street_housenumber'>"+data.housenumber+" "+data.street+"</div>");
					break;
				default:
					entry_div.append("<div class='street_housenumber'>"+data.street+" "+data.housenumber+"</div>");
					break;
			}
		}
		if (data.zip.length > 0)
		{
			if (data.city == null) data.city = "";
			switch (parseInt(data.country_id))
			{
				case 20: //Australien
					entry_div.append("<div class='zip_city'>"+data.city+" "+data.zip+"</div>");
					break;
				default:
					entry_div.append("<div class='zip_city'>"+data.zip+" "+data.city+"</div>");
					break;
			}
		}
		if (data.country_name.length > 0) entry_div.append("<div class='country_name'>"+data.country_name+"</div>");
		if (data.telehpone.length > 0) entry_div.append("<div class='telehpone'>"+this.get_l("tel")+" "+data.telehpone+"</div>");
		if (data.telefax.length > 0) entry_div.append("<div class='telefax'>"+this.get_l("fax")+" "+data.telefax+"</div>");
		if (data.email.length > 0) entry_div.append("<div class='email'><a href='javascript:linkTo_UnCryptMailto(\""+data.email+"\");'>"+this.get_l("email")+"</a></div>");
		if (data.url.length > 0 && data.url.indexOf(location.hostname) < 0) entry_div.append("<div class='url'><a href='"+(data.url.indexOf("http://") < 0 && data.url.indexOf("https://") < 0 ? "http://"+data.url : data.url)+"' target='_blank'>"+this.get_l("internet")+"</a></div>");
		if (data.route_pdf.length > 0) entry_div.append("<div class='route_pdf'><a href='/uploads/tx_3slocations/"+data.route_pdf+"' target='_blank'>"+this.get_l("directions")+"</a></div>");
		if (data.times.length > 0) entry_div.append("<div class='times'><div class='title'>"+this.get_l("opening_hours")+"</div>"+data.times.replace(/\n/g,"<br />")+"</div>");
		
		detailLink = "";
		if (this.config.regionOutputPID && (data.showRegionLink == 1 || this.config.forceRegionLink == 1)) detailLink = "/index.php?id="+this.config.regionOutputPID+"&L="+this.config.lang+"&locationID="+data.uid;
		if (data.detailpid != null && data.detailpid.length > 0 && this.config.hidedetailpid != 1) detailLink = data.detailpid+"&L="+this.config.lang;
		if (detailLink.length > 0) entry_div.append("<div class='regionOutput '><div class='title'>"+this.get_l("further_information")+"</div><p class='grau'>"+this.get_l("learn_more")+" <a href='"+detailLink+"'>"+this.get_l("here")+"</a></p></div>");
		
		if (this.config.directOpenDetailLink && this.config.directOpenDetailLink == 1)
		{
			location.href = detailLink;
		}
		
		if (data.freetext.length > 0) entry_div.append("<div class='freetext'>"+data.freetext+"</div>");
		
		//Wenn Zertifikate vorhanden sind & Firma RIS ist
		if (parseInt(data.num_certificates) > 0 && (this.constants.ris_uid == data.company || $.inArray(this.constants.ris_uid,data.company) >= 0))
		{
			entry_div.append("<div class='certificate_matrix'><p>"+this.get_l("certifications")+"</p><p class='trenner_gestrichelt'>&nbsp;</p><p class='grau'>"+this.get_l("site_is_certified")+" <a target='_blank' href='"+this.constants.ris_certificate_matrix_href+"'>"+this.get_l("more")+"</a></p></div>");
		}
		
		this.div_data.append(entry_div);
		this.div_data.append("<div class=\"hTrenner\"><div><div><hr /></div></div></div>");
		
		if(layoutscript) layoutscript();
	}
	this.get_l = function(val)
	{
		l = 0;
		if (self.config.lang) l = self.config.lang;
		
		if (!self.LANG[val]) return("");
		
		if (!self.LANG[val][l]) return(self.LANG[val][0]);
		
		return(self.LANG[val][l]);
	}
	/*
	 *	0:Deutsch
		1:Englisch
		2:Niederländisch
		3:Französisch
		4:Ungarisch
		7:Polnisch
		8:Russisch
	 */
	this.LANG = {
		more: {
			0: "Mehr",
			1: "More",
			2: "Meer",
			3: "En savoir plus…",
			4: "Bővebben",
			7: "Więcej",
			8: "More"
		},
		site_is_certified: {
			0: "Dieser Standort ist zertifiziert.",
			1: "This site is certified.",
			2: "Deze locatie is gecertificeerd.",
			3: "Ce site est certifié.",
			4: "Ez a telephely tanúsított.",
			7: "ten zakład jest certyfikowany.",
			8: "This site is certified."
		},
		certifications: {
			0: "Zertifikate",
			1: "Certifications",
			2: "Certificaten",
			3: "Certificats",
			4: "Tanúsítvány",
			7: "Certyfikaty",
			8: "Certifications"
		},
		business_in: {
			0: "Unternehmen in",
			1: "Business in",
			2: "Onderneming in",
			3: "Entreprise à",
			4: "Vállalat x városban",
			7: "Przedsiębiorstwo w",
			8: "Business in"
		},
		more_than_three: {		
			0: "An dem von Ihnen gewählten Standort gibt es mehr als drei Unternehmen",
			1: "In the location chosen by you there are more than three businesses",
			2: "Op de door u gekozen locatie bevinden zich meer dan drie bedrijven ",
			3: "Il existe plus de trois entreprises dans la localité que vous avez choisie",
			4: "Az ön által választott helyszínen háromnál több vállalat van",
			7: "W wybranej lokalizacji znajdują się ponad trzy przedsiębiorstwa",
			8: "In the location chosen by you there are more than three businesses"
		},
		select_company: {
			0: "Unternehmen wählen",
			1: "Select business",
			2: "Bedrijven kiezen",
			3: "Choisir une entreprise",
			4: "Válasszon vállalatot",
			7: "Wybrać przedsiębiorstwo",
			8: "Select business"
		},
		contact_via: {
			0: "Kontakt über...",
			1: "Contact via...",
			2: "Contact via ...",
			3: "Contact par ...",
			4: "Kapcsolattartó:",
			7: "Kontakt przez",
			8: "Contact via..."
		},
		tel: {
			0: "Tel.:",
		    1: "Tel.:",
			2: "Tel.:",
			3: "Tél. :",
			4: "Tel.:",
			7: "Tel.:",
			8: "Tel.:"
		},
		fax: {
		    0: "Fax:",
		    1: "Fax:",
			2: "Fax:",
			3: "Fax :",
			4: "Fax:",
			7: "Fax:",
			8: "Fax:"
		},
		email: {
		    0: "E-Mail",
		    1: "E-Mail",
			2: "E-Mail",
			3: "E-Mail",
			4: "E-Mail",
			7: "E-Mail",
			8: "E-Mail"
		},
		internet: {
		    0: "Internet",
		    1: "Internet",
			2: "Internet",
			3: "Internet",
			4: "Internet",
			7: "Internet",
			8: "Internet"
		},
		directions: {
		    0: "Anfahrtsskizze",
		    1: "Directions",
			2: "Routebeschrijving",
			3: "Plan d’accès",
			4: "Megközelítési vázlatrajz",
			7: "Dojazd",
			8: "Directions"
		},
		opening_hours: {
		    0: "Öffnungszeiten:",
		    1: "Opening hours:",
			2: "Openingstijden:",
			3: "Horaires d’ouverture :",
			4: "Nyitvatartási idők:",
			7: "Godziny otwarcia:",
			8: "Opening hours:"
		},
		learn_more: {
		    0: "Mehr über diesen Standort erfahren Sie",
		    1: "You can learn more about this location",
			2: "Meer over deze locatie leest u",
			3: "Pour plus d’informations sur la localité, cliquez",
			4: "Erről a helyszínről többet tud meg",
			7: "Dalsze informacje na temat danej",
			8: "You can learn more about this location"
		},
		here: {
		    0: "hier",
		    1: "here",
			2: "hier",
			3: "ici.",
			4: "itt",
			7: "lokalizacji",
			8: "here"
		},
		further_information: {
		    0: "Weitere Informationen",
		    1: "Further information",
			2: "Meer informatie",
			3: "Plus d'informations",
			4: "További információk",
			7: "Więcej informacji",
			8: "Further information"
		}
	};
}
