

function Exclusive(_userAlpha2, _iso1, _navigationSelect){
	var iso1 = _iso1;
	var userAlpha2 = _userAlpha2;

	var selectedAlpha2;
	var selectedCity;
	var navigationSelectText = _navigationSelect;
	var listDivId = 'store_list_320';
	var	stores = new Array();
	this.init = init;
	this.selectCityEvent = selectCityEvent;
	this.countryChangeEvent = countryChangeEvent;
	this.cityChangeEvent = cityChangeEvent;
	
	this.setDivListId = setDivListId;
	
	/* advisor page */
	this.initAdvisor = initAdvisor;
	this.selectAdvisorCountryEvent = selectAdvisorCountryEvent;
	this.advisorCountryChangeEvent = advisorCountryChangeEvent;
	
	function initAdvisor(){
		debug("init exclusive advisors");

		// start loading storedata
		loadStoreData(function(){
				loadCountries(function(){
					if (selectCountry(userAlpha2)){
						showStoresByAlpha2(selectedAlpha2);
					}
				});
			});
	}
	
	function selectAdvisorCountryEvent(_alpha2){
	
	}
	
	function advisorCountryChangeEvent(_event){
		var
			alpha2 = getValueById('store_country');
						
		// if changed or found return true
		if (selectCountry(alpha2)){
			showStoresByAlpha2(selectedAlpha2);
		}
	} 
	
	function setDivListId(_id){
		listDivId = _id;
	}
	
	function init(){
		debug("init exclusive");

		resetCityUI();
		
		// start loading storedata
		loadStoreData(function(){
				loadCountries(function(){					
					
					if (selectCountry(userAlpha2)){
						// load cities
						loadCities(selectedAlpha2, function(){				
							// try to select first, if it is the only one
							var
								citySelect = document.getElementById('store_city');
							
							if (citySelect.options.length == 1){
								selectCityEvent(citySelect.options[0].value);
							}												
						});
					}
				});
							
			});
	}
	function loadCountries(_callback) {
		debug("loadCountries iso1:"+iso1);
		var
			readyFunction = function (_req) {
				if (_req.readyState == 4){
					var select = document.getElementById('store_country');
					var items = _req.responseXML.getElementsByTagName('country');
					var num=0;
					select.options[num++] = new Option(navigationSelectText, 0);
					select.options[0].disabled = true;
					for (var i = 0; i < items.length; i++) {
						var itemAlpha2 = items[i].getAttribute('zn_alpha2');
						var name = items[i].getAttribute('zna_name');
				    select.options[num++]=new Option(name, itemAlpha2);
					}
					
					select.selectedIndex = 0;
					debug("countries loaded");					
					if (_callback)
						_callback();
				}
			};
		ajaxGet('/flash_countries_with_partners_'+iso1+'.xml', readyFunction);
	}
	function resetCityUI(){
			var select = document.getElementById('store_city');
			select.options.length = 0;
			select.options[0] = new Option(navigationSelectText, 0);
			select.options[0].disabled = true;
			select.disabled = true;
	}
	
	function loadCities(_alpha2, _callback){
		//http://localhost/flash_cities_with_partners_de.xml
		debug("loadCities selected alpha2: "+_alpha2+" iso1:"+iso1);
		var
			readyFunction = function (_req) {
				if (_req.readyState == 4){
					var select = document.getElementById('store_city');
					select.disabled = false;
					var items = _req.responseXML.getElementsByTagName('city');
					select.options.length = 0;
					var num=0;
					if (items.length > 1){
						select.options[num++] = new Option(navigationSelectText, 0);
						select.options[0].disabled = true;
					}
						
					for (var i = 0; i < items.length; i++) {
						var name = items[i].getAttribute('st_city');
				    select.options[num++]=new Option(name, name);
					}
				  
				  select.selectedIndex = 0;
				  debug("cities loaded");
				  if (_callback)
						_callback();
 				}
			};
		//http://localhost/flash_countries_with_partners_de.xml
		ajaxGet('/flash_cities_with_partners_'+selectedAlpha2+'.xml', readyFunction);
	}
	
	function cityChangeEvent(_event){
		var
			city = getValueById('store_city');
		if (city){
			selectCityEvent(city);
		}
	}
	
	function selectCityEvent(_city){
		if (!_city || _city == '0'){
			// error message select city
			return;
		}
		if (_city != selectedCity){
			selectedCity = _city;
							
			// do the selection
			showStoresByCity(selectedCity);
		}
	}
	
	function countryChangeEvent(_event){
		var
			alpha2 = getValueById('store_country');
		
		resetCityUI();
		
		// if changed or found return true
		if (selectCountry(alpha2)){
			// load cities
			loadCities(selectedAlpha2, function(){				
				// try to select first, if it is the only one
				var
					citySelect = document.getElementById('store_city');
				
				if (citySelect.options.length == 1){
					selectCityEvent(citySelect.options[0].value);
				}												
			});
		}
	}
	
	/* if found return true*/
	function selectCountry(_alpha2){
		selectedAlpha2 = null;
		var found=false;
		if (_alpha2){
			debug("selecting alpha2: "+_alpha2);
			var
				countrySelect = document.getElementById('store_country');
			for (var i = 0; i < countrySelect.options.length; i++){
				var
					opt = countrySelect.options[i];
				if (opt.value == _alpha2){
					// update selection
					if (countrySelect.selectedIndex != i)
						countrySelect.selectedIndex = i;
						
					// set the new selected alpha2
					selectedAlpha2 = _alpha2;
					
					found = true;
					
					break;
				}
			}			
			
			return found;
		}else{
			return false;
		}
	}
	
	function getStoreDiv(store){
		return '<div class="store">'+
				'<div class="store_headline">'+
				'<h1><img src="'+store.headline_src+'" alt="'+store.exclusive.headline+'"/></h1>'+
				'</div>'+
				'<div class="store_address">'+
				'<b>'+store.st_name+'</b><br/>'+
				store.st_street+'<br/>'+
				'<b>'+store.st_zipcode+' '+store.st_city+'</b><br/>'+
				'Tel '+store.st_phone+'<br/>'+
				'Fax '+store.st_fax+'<br/>'+
				'</div>'+
				'<div class="store_icon"><img src="'+store.icon_src+'"/></div>' +
				'<a href="'+store.exclusive.contact_link+'" class="store_link">E-Mail</a><br/>'+
				'<a href="'+store.exclusive.web_link+'" class="store_link">Internet</a>'+
				'</div>';			
	}
	
	function showStoresByCity(_city){		
		debug('show stores by city:'+_city);
		//store_list
		
		setDisplayBlockOrNoneById('store_welcome_text', false);
			
		var
			text = '';
		
		for (var i=0; i < stores.length; i++){
			var	
				store = stores[i];
			if (store.st_city == _city){
				text += getStoreDiv(store);
			}
		}
		
		setContent(listDivId, text);
		setDisplayBlockOrNoneById(listDivId, true);
	}	
	
	function showStoresByAlpha2(_alpha2){		
		debug('show stores by alpha2:'+_alpha2);
		//store_list
		
		var
			text = '';
		var num=0;
		for (var i=0; i < stores.length; i++){
			var	
				store = stores[i];
			if (store.st_alpha2 == _alpha2){
				text += getStoreDiv(store);
				num++;
			}
		}
		
		if ((num&1) != 0){
			text +='<div class="store"><div class="store_headline"></div></div>';
		}		
		setContent(listDivId, text);
		setDisplayBlockOrNoneById(listDivId, true);
	}	
	
	function loadStoreData(_callback){
		debug('load storedata');
			
		var
			readyFunction = function (_req) {
				if (_req.readyState == 4){					
					var items = _req.responseXML.getElementsByTagName('store');
					stores.length = 0;
					for (var i = 0; i < items.length; i++) {
						var
							item = items[i];
						var
							store = new Object();

						store.st_id  = item.getAttribute('st_id');
						store.st_name  = item.getAttribute('st_name');
						store.st_street  = item.getAttribute('st_street');
						store.st_city  = item.getAttribute('st_city');
						store.st_zipcode  = item.getAttribute('st_zipcode');
						store.st_fax  = item.getAttribute('st_fax');
						store.st_phone  = item.getAttribute('st_phone');
						store.st_alpha2 = item.getAttribute('st_alpha2');
						
						// extra data
						var exclusiveNode = item.firstChild;
						debug(exclusiveNode.firstChild.nodeValue);
						eval("store.exclusive="+exclusiveNode.firstChild.nodeValue+";");
						
						// headline and icon
						store.headline_src = exclusiveNode.getAttribute('headline_src');
						store.icon_src = exclusiveNode.getAttribute('icon_src');
						
						debug("id: "+store.st_id+" name:"+store.st_name+" headline:"+store.exclusive.headline);
						stores.push(store);						
					}
					
					if (_callback)
						_callback();					  
				}
			};
		ajaxGet('/flash_partners.xml', readyFunction);
	}		
}
