/**
 * Uhlsport dealerfinder.js
 * XML Generator für Flash
 * -------------------------------------------------------------------------
 * @author Weitclick
 * @copyright Copyright(C) 2009, Weitcklick
 * @version $Id: import.php 1 2009-01-21 15:28:03Z ma $
 * @revision $LastChangedRevision:0 $
 **/
var continent = null;
var country = null;
var zip = null;
var offset = 0;
var limit = 20;
var order = 'ranking';
 
function InitSub() {
	try {
		thisMovie('teaser').setMap(continent, country);
	}
	catch (myError){}
}

function setMapList(continent_new, country_new) {
	if(continent_new) {
		continent = continent_new;
	}
	
	if(country_new) {
		country = country_new;
	}
	
	zip = null;
	type = null;
	offset = 0;
	Loader();
}

function Loader(onlyExtendList) {
	var attributes_get = 'continent=' + continent + '&country=' + country + '&zip=' + zip + '&types=' + types + '&type=' + type + '&limit=' + limit + '&offset=' + offset + '&order=' + order + '&lang=' + lang;

	var url1 = '/fileadmin/uhlsport/php/dealers_ajaxLoader.php?' + attributes_get;
	var selected = '';
	
	if (!onlyExtendList) {
		$('#dealerfinder_main').attr('style', 'display:none');
	}

	$.getJSON(url1, function(json){
		if (!onlyExtendList) {
			continent = json.params.continent;
			country = json.params.country
			
			$('#sel_continents').html('<option>' + text_selectContinent + '</option>');
			$.each(json.continents, function(index, item){
				selected = '';
				if (continent == item.code) {
					selected = ' selected="selected"';
				}
				$('#sel_continents').append('<option value="' + item.code + '"' + selected + '>' + item.name + '</option>');
				
				loading = false;
			});
			
			$('#sel_countries').html('<option>' + text_selectCountry + '</option>');
			if (json.countries) {
				$('#country').attr('class', 'sel');
				$('#sel_countries').attr('disabled', false);
				$.each(json.countries, function(index, item){
					selected = '';
					if (country == item.code) {
						selected = ' selected="selected"';
					}
					$('#sel_countries').append('<option value="' + item.code + '"' + selected + '>' + item.name + '</option>');
				});
			}
			else {
				$('#country').attr('class', 'sel disabled');
				$('#sel_countries').attr('disabled', true);
			}
			
			$('#sel_zips').html('<option>' + text_selectZipCode + '</option><option value="null">--</option>');
			if (json.zips) {
				$('#zip').attr('class', 'sel');
				$('#sel_zips').attr('disabled', false);
				$.each(json.zips, function(index, item){
					selected = '';
					if (zip == item) {
						selected = ' selected="selected"';
					}
					$('#sel_zips').append('<option value="' + item + '"' + selected + '>' + item + '</option>');
				});
			}
			else {
				$('#zip').attr('class', 'sel disabled');
				$('#sel_zips').attr('disabled', true);
			}
			
			$('.type').hide();
			$.each(json.types, function(type, status) {
				$('#tab2').attr('href', 'Javascript:SetType(2);');
				
				if(status == 'active') {
					$('#' + type).attr('class', 'selected');
					$('#' + type).show();
				} else if(status == 'selectable') {
					$('#' + type).attr('class', 'selectable');
					$('#' + type).attr('href', 'Javascript:SetType(\''+type+'\');');
					$('#' + type).show();
				} else {
					$('#' + type).hide();
				}
			});		
			
			$('#dealerfinder-list').html('');
		}
		

		$('#more').attr('style', 'display:none');
		if (json.listdata) {
			$('#dealerfinder_main').attr('style', 'display:block');
			
			var count = 0;
			$.each(json.listdata, function(index, item){
				if(index == 'hasMore') {
					$('#more').attr('style', 'display:block');
				} else {
					AddListElement(count, item.name, item.street, item.zip, item.city, item.website, item.phone, item.fax, item.email, item.contact);
				}
				count++;
			});
		}
	});
}

function More(){
	offset = offset + limit;
	Loader(1);
}

function SetContinent(code){
	offset = 0;
	continent = code;
	country = null;
	zip = null;
	type = null;
	
	try {
		thisMovie('teaser').setMap(continent, null);
	}
	catch (myError){}
	
	Loader();
}

function SetCountry(code) {
	offset = 0;
	country = code;
	zip = null;
	type = null;
	
	try {
		thisMovie('teaser').setMap(continent, country);
	}
	catch (myError){}
		
	Loader();
}

function SetZip(code) {
	offset = 0;
	zip = code;
	SetOrder('city');
	Loader();
}
function SetType(code) {
	offset = 0;
	zip = null;
	type = code;
	
	Loader();
}
function SetOrder(field) {
	order = field;
	offset = 0;
	
	$('#sort_name').attr('class', 'name');
	$('#sort_street').attr('class', 'street');
	$('#sort_city').attr('class', 'city');
	$('#sort_website').attr('class', 'website');
	
	if($('#sort_'+field)) {
		$('#sort_'+field).attr('class', field+' active');
	}
	
	Loader();
}

function SH(count) { // ShowHide Details
	var element = '#i'+count;
	var element_details = '#i'+count+'>div[class="details"]';
	
	if($(element_details).is(':hidden')) {
		$(element_details).slideDown('slow');
		$(element).attr('class', 'show');
	} else {
		$(element_details).hide('slow');
		$(element).attr('class', 'hide');
	}
}

function AddListElement(count, name, street, zip, city, website, phone, fax, email, contact) {
	var website_link = '';
	var details = '';
	
	if(website) {
		website_link = '<a name="i'+count+'" href="http://'+website+'" target="_blank">'+website+'</a>'
	}
	if(phone) {
		details += text_phone+': '+phone;
	}
	if(fax) {
		details += '<br />'+text_fax+': '+fax;
	}
	if(email) {
		details += '<br />'+text_email+': <a href="mailto:'+email+'">'+email+'</a>';
	}
	if(contact) {
		details += '<br />'+text_contact+': '+contact;
	}
	
	$('#dealerfinder-list').append('<li id="i'+count+'" onclick="SH('+count+');"><h3><div class="name highlight">'+name+'</div><div class="street">'+street+'&nbsp;</div><div class="city">'+zip+' '+city+'&nbsp;</div><div class="website">'+website_link+'&nbsp;</div><div style="clear:both;"></div></h3><div class="details" style="display:none;">'+details+'</div></li>');
}

