/*
 * Shoplocator.js
 *
 * Copyright 2010, exSITEing multimedia concepts, Torben Schwellnus
 * For Tilt Design Studio / Rotring
 *
 */
 function Shoplocator(config) {
	this.config=config;
	this.currentcountry=null;
	this.currentcity=null;
	this.cities=[];
	this.shops=[];
	var me=this;
	
	this.init=function(config) {
		if(config != null) this.config=config;
	};
	
	this.fillcityselect=function() {
		if(this.config.cityselect != null) {
			$(this.config.cityselect).empty();
			if(this.cities.length>0) {
				$(this.config.cityselect).append('<option value="">' + this.config.choosecitylabel + '</option>');
				for(var i=0;i<this.cities.length;i++) {
					$(this.config.cityselect).append('<option value="' + this.cities[i].city + '">' + this.cities[i].city + '</option>');
				}
			}
			$(this.config.cityselect).attr('disabled',false);
		}
	};
	
	this.fillcitylist = function() {
		if(this.config.citylist != null) {
			$(this.config.citylist).empty();
			if(this.cities.length>0) {
				for(var i=0;i<this.cities.length;i++) {
					$(this.config.citylist).append('<a href="javascript:void(0);" id="city_' + escape(this.cities[i].city) + '" class="citylink" onClick="clickcity(\''+ this.addslashes(this.cities[i].city) + '\',this);">' + this.cities[i].city + '</a><br/>');
				}
				$(this.config.citylist).append('<br/>');
			}
		}
	};
	
	this.loadcities=function(country) {
		this.currentcountry=country;
		this.emptyshoptarget();
		this.onShopsUnload();
		this.onCitiesUnload();
		if(this.config.ajaxurl != null) {
			var params={
				action: 'getcities',
				country: country
			};
			$.ajax({
				type: "POST",
				url: this.config.ajaxurl,
				dataType: 'json',
				data: params,
				success: function(result){
					me.citiesloaded(result);
				}
			});			
		}
	};
	
	this.citiesloaded=function(result) {
		if(result.status=='OK') {
			this.cities=new Array();
			if(result.cities.length>0) {
				this.cities=result.cities;
			}
			this.fillcityselect();
			this.fillcitylist();
			this.onCitiesLoaded(this.cities);
		}
	};
	
	this.loadshops=function(city) {
		this.currentcity=city;
		this.emptyshoptarget();
		this.onShopsUnload();
		if(this.config.ajaxurl != null) {
			var params={
				action: 'getshops',
				country: this.currentcountry,
				city: this.currentcity
			};
			$.ajax({
				type: "POST",
				url: this.config.ajaxurl,
				dataType: 'json',
				data: params,
				success: function(result){
					me.shopsloaded(result);
				}
			});			
		}
	};
	
	this.shopsloaded=function(result) {
		if(result.status=='OK') {
			this.shops=new Array();
			if(result.shops.length>0) {
				this.shops=result.shops;
			}
			this.rendershops();
			this.onShopsLoaded(this.shops);
		}
	};
	
	this.emptyshoptarget=function() {
		if(this.config.shoptarget != null) {
			$(this.config.shoptarget).empty();
		}
		return true;
	};
	
	this.rendershops=function() {
		if(this.config.shoptarget != null) {
			this.emptyshoptarget();
			if(this.shops.length>0) {
				for(var i=0;i<this.shops.length;i++) {
					$(this.config.shoptarget).append(this.composeDetailentry(this.shops[i]));
				}
			}
		}
	};
	
	
	this.composeDetailentry=function(dentry) {
		var htmlstring='&nbsp;';
		if(this.config.onComposeDetailentry != null) {
			htmlstring=this.config.onComposeDetailentry(dentry);
		} else {
			htmlstring='<div class="shopdetails">';
			htmlstring = htmlstring + '<strong>' + dentry.company + '</strong><br/>' + dentry.streetaddress;
			
			// FS ZUSATZ START
			if(dentry.streetaddress2 != null) {
				htmlstring = htmlstring +'<br/>' + dentry.streetaddress2;
			}
			
			htmlstring = htmlstring +'<br/>' + dentry.postcode + ' ' + dentry.city;
			
			if(dentry.phone != null) {
				htmlstring = htmlstring + '<br/>tel. ' + dentry.phone;
			}
			
			if(dentry.fax != null) {
				htmlstring = htmlstring +'<br/>' + dentry.fax;
			}
			
			if(dentry.website != null) {
				htmlstring = htmlstring + '<br/><a href="http://' + dentry.website + '">' + dentry.website + '</a>';
			}
			
			if(dentry.email != null) {
				htmlstring = htmlstring + '<br/><a href="mailto:' + dentry.email + '">' + dentry.email + '</a>';
			}
			
			
			// FS ZUSATZ ENDE
			
			
			
			htmlstring= htmlstring + '</div>';
		}
		return htmlstring;
	};
	
	this.onCountriesLoaded=function() {
		if(this.config.onCountriesLoaded != null) {
			this.config.onCountriesLoaded();
		}
	};
	
	this.onCitiesLoaded=function(cities) {
		if(this.config.onCitiesLoaded != null) {
			this.config.onCitiesLoaded(cities);
		}
	};
	
	this.onShopsLoaded=function(shops) {
		if(this.config.onShopsLoaded != null) {
			this.config.onShopsLoaded(shops);
		}
	};
	
	this.onCountriesUnload=function() {
		if(this.config.onCountriesUnload != null) {
			this.config.onCountriesUnload();
		}
	};
	
	this.onCitiesUnload=function() {
		if(this.config.onCitiesUnload != null) {
			this.config.onCitiesUnload();
		}
	};
	
	this.onShopsUnload=function() {
		if(this.config.onShopsUnload != null) {
			this.config.onShopsUnload();
		}
	};
	
	this.addslashes=function(str) {
		str=str.replace(/\\/g,'\\\\');
		str=str.replace(/\'/g,'\\\'');
		str=str.replace(/\"/g,'\\"');
		str=str.replace(/\0/g,'\\0');
		return str;
	};
	this.stripslashes=function(str) {
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\0/g,'\0');
		str=str.replace(/\\\\/g,'\\');
		return str;
	};
	
	
}

