/* $ID$ */
var dsHandled;
var Url = {
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = ""; 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 		while ( i < utftext.length ) {
 			c = utftext.charCodeAt(i);
 			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 		} 
		return string;
	}
 
}
function hideHelps(){
	hideHelps(true);
}
function hideHelps(force) {
	if ($('dsNameValue')  != null && (force || !$('dsNameValue').hasFocus))
		$('dsNameHelp').hide();
	if ($('dsNearbyHelp')  != null && (force || !$('dsNearbyValue').hasFocus))
		$('dsNearbyHelp').hide();
}
function performNameSearch(country, value) {
	new Ajax.Updater('dsResults', '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:'getFormattedNameResults', name:value, country:country, highlight:false}, onComplete : function(){ $('dsPreloader').style.display = 'none';  }});
}
function performRegionSearch(country, value) {
	new Ajax.Updater('dsResults', '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds',  action:'getFormattedRegionResults', value:value, country:country}, onComplete : function(){ $('dsPreloader').style.display = 'none';  }});
}
function performPointSearch(country, value, radius) {
	new Ajax.Updater('dsResults', '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds',  action:'findByPoint', value:value, country:country, radius:radius}, onComplete : function(){ $('dsPreloader').style.display = 'none';  }});
}
function updateCountryList(target) {
	//new Ajax.Updater(target, '/index.php', { method:'get', parameters:{eID:'ds', action:"getFormattedCountrySelector"}, onComplete : function(){copySelectOptions("dsNameCountrySelector", "dsRegionCountrySelector");copySelectOptions("dsNameCountrySelector", "dsNearbyCountrySelector");syncCountries($("dsNameCountrySelector").value);}});
}
function updateRegionList(value, target, selected) {
	new Ajax.Updater(target, '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"getFormattedRegionSelector", iso:value}, onComplete:function(){findRegionByName(selected);}});
}
function dsGetWatchlist(target) {
	new Ajax.Updater(target, '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"getWatchlist"}});
}

function dsAddToWatchlist(value, silent) {
	if (silent)
		new Ajax.Updater('wll' + value, '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"addToWatchlist", id:value}});
	else
		new Ajax.Request('/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"addToWatchlist", id:value}, onComplete: function(){SWFAddress.setValue("/Watchlist")}});
}
function dsRemoveFromWatchlist(value, silent) {
	if (silent)
		new Ajax.Updater('wll' + value, '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"removeFromWatchlist", id:value}});
	else
		new Ajax.Request('/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"removeFromWatchlist", id:value}, onComplete: function(){SWFAddress.setValue("/Watchlist/&Removed=" + value)}});
}
function getDetails(target, value) {
	new Ajax.Updater(target, '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"getDetails", value:value}, onComplete : function(){ $('dsPreloader').style.display = 'none';  }});
}
var dsTmpName = "";
var dsNameId = null;
function performUpdateNameHelp(source) {
	var isoV = $("dsNameCountrySelector").value;
	if (dsTmpName.length >0) {
		$('dsNameHelp').show();
		$('dsNameHelp').clonePosition(source, {offsetTop:20, setWidth:false, setHeight:false});
		$('dsNameHelp').absolutize();
		new Ajax.Updater('dsNameHelp', '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:'getFormattedNameResults', name:dsTmpName, country:isoV, highlight:true}});
	}
	else {
		$('dsNameHelp').hide();
	}
}
function updateNameHelp(source) {
	$('dsNameHelp').hide();
	if (dsTmpName == source.value)
		return;
	
 	dsTmpName = source.value;
 	$('dsNameHelp').innerHTML = "";
 	
 	if (dsNameId!=null) {
 		window.clearTimeout(dsNameId);
 	}
 	
	dsNameId = performUpdateNameHelp.delay(0.4, source);
}
function updateZip2Help(source) {
//	target = 'dsNearbyHelp';
//	value = source.value;
//	
//	if (value.length >= 2) {
//		isoV = $("dsNearbyCountrySelector").value;
//		$(target).clonePosition(source, {offsetTop:20, offsetLeft:0, setWidth:false, setHeight:false});
//		$(target).show();
//		new Ajax.Updater(target, '/index.php', { method:'get', parameters:{eID:'ds', action:"getFormattedZipResults2", iso:isoV, value:value}});
//	}	
//	else {
//		$(target).hide();
//	}
}

function updatePoint(name, lon, lat) {
	$("dsNearbyValue").value = name;
	//$("dsNearbyPoint").value = lon + "," + lat;
	$("dsNearbyHelp").hide();
	performPointSearch();
}

function performGoogleLockup(query) {
	new Ajax.Updater('GoogleResults', '/index.php?type='+jstype, { method:'get', parameters:{eID:'ds', action:"getGoogleResults", iso:isoV, value:value}});
}

function switchToForm() {
	var paths = SWFAddress.getPath().split("/");
	if (!paths[1])
		paths[1] = 'Region';
	SWFAddress.setValue(paths[1]);
}


function setCountry(country) {
	var paths = SWFAddress.getPath().split("/");
	syncCountries(country);
	if (!paths[2])
		paths[2] = "Form";
	SWFAddress.setValue(paths[1] + "/" + paths[2] + "/" + country);
}

function handleChange(event) {
	if (event.path.length < 2 && window.dsHandled != null && window.dsHandled) {
		return;
	}
	window.dsHandled = false;
	
	var index, rel, paths = event.path.split("/");
    initCountryLists();
    var divs = document.getElementsByTagName('div');
    if (paths[1] != null && paths[1] != '')
    	prepareChangeDsPage('dsLink' + paths[1]);
    if (paths[2]=="Results") {

  		$('dsForms').style.display = 'none';
    	$('dsResultsPanel').style.display = 'block';
    	
    	$('dsResults').innerHTML = '';
    	$('dsPreloader').style.display = 'block'; 
    	switch(paths[1]) {
    		case 'Name':  
    			if (paths.length > 5)
    				getDetails('dsResults', paths[5]);
    			else
    				performNameSearch(paths[3], paths[4]);
    			break;
    		case 'Region': performRegionSearch(paths[3], paths[4]);break;
    		case 'Nearby': performPointSearch(paths[3], paths[4], paths[5]);break;
    		case 'Watchlist': break;
    	}
    }
    else {
    	if ($('dsForms') != null) $('dsForms').style.display = 'block';
    	if ($('dsResultsPanel') != null) $('dsResultsPanel').style.display = 'none';
    	if ($('dsNameHelp') != null) $('dsNameHelp').hide();
		
		if (event.path.length > 1) {
		    for (var i = 0, div; div = divs[i]; i++) {
		    	if (div.className == "dsPanel") {
		    		if (div.id == "dsPanel" + paths[1]){
		    			var cs = $('ds' + paths[1] + "CountrySelector");
		    			if (paths.length > 3){
		    				syncCountries(paths[3]);
		    			}
		    			switch(paths[1]) {
		    				case 'Name':
		    					break;
		    				case 'Region':
		    					updateRegionList(cs.value, 'dsRegionValue', Url.decode(paths[4]));
		        				break;
		    				case 'Nearby':
		    					break;
		    				case 'Watchlist':
		    					dsGetWatchlist('dsWatchlistResults');break;
		    			}
		    			if (paths.length >= 4){
		    				paths[4] = paths[4] ? paths[4] : '';
		    				$('ds' + paths[1] + 'Value').value = Url.decode(paths[4]);
		    			}
		    			Effect.Appear(div, {duration:0.5});
		    		}
		    		else
		    			div.style.display = "none";
		    	}
		    }
	    }
    }
}

function updateTitle(paths) {
	
}

function findRegionByName(name) {
	for(var i = 0, cur; cur = $('dsRegionValue').options[i]; i++) {
		if (cur.text == name) {
			cur.selected = true;
			break;
		}
	}
}

function syncCountries(value) {
	if (value == "")
		value = ($("dsNameCountrySelector").value);
	
	var i=0;
	while ((i < $("dsNameCountrySelector").options.length) && ($("dsNameCountrySelector").options[i].value != value)){
		i++;
	}
	if (i >= $("dsNameCountrySelector").options.length)
		i = 0;
	$("dsNameCountrySelector").selectedIndex = i;
	$("dsRegionCountrySelector").selectedIndex = i;
	$("dsNearbyCountrySelector").selectedIndex = i;

	/*if (['DE','AT','CH','LU','CZ','SI','IT'].include(value)) {
		$$('.dsMap').each(function(item){item.src = '/fileadmin/ds/map/map_' + value + '.jpg'});
	}
	else {
		$$('.dsMap').each(function(item){item.src = '/fileadmin/ds/map/map.jpg'});
	}
	*/
}

function copySelectOptions(source, target) {
  for (var i=0, m = $(source).options.length; i< m; i++) {
    var o = $(source).options[i];
	$(target).options[$(target).options.length] = new Option( o.text, o.value, o.defaultSelected, o.selected);
  }
}

function initCountryLists() {
	if ($('dsNameCountrySelector') != null && $('dsNameCountrySelector').options.length == 0)
		updateCountryList("dsNameCountrySelector");
}

function active(name,imgObj)	{
	if (version == "n3" && document[name]) {document[name].src = eval(name+"_h.src");}
	else if (document.getElementById && document.getElementById(name)) {document.getElementById(name).src = eval(name+"_h.src");}
	else if (imgObj)	{imgObj.src = eval(name+"_h.src");}
}
function prepareChangeDsPage(id) {
	var cur = $(id);
	var curT = $(id + "T");
	
	if (cur == oldButton)
		return;
	
	if (cur != null) {
		cur.onmouseoutOld = cur.onmouseout;
		cur.onmouseoverOld = cur.onmouseover;
		cur.onmouseout = null;
		cur.onmouseover = null;
		over(cur.childNodes[0].name);
	}
	if (curT != null) {
		curT.style.fontWeight = "bold";
	}
	if (oldButton != null) {
		oldButton.onmouseout = oldButton.onmouseoutOld;
		oldButton.onmouseover = oldButton.onmouseoverOld;
		out(oldButton.childNodes[0].name);
	}
	if (oldTextLink != null) {
		oldTextLink.style.fontWeight = "";
	}
	oldButton = $(id);
	oldTextLink = $(id + "T");
}
var oldButton;
var oldTextLink;
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
//initCountryLists();

function geoHandler(location) {
	
}
