﻿var url = 'index.php';
var queryString = 'eID=therapist';



window.addEvent('load', function() {
 new Request.JSON({ url: url,
					method: 'get',
					onSuccess: function(countries) {
						$('country').empty();
						therapists.loadCountries(countries);
					}
				}).send(queryString+'&L='+util.getLang('L'));
 // preload zones
 therapists.getZones(220);
 $('country').addEvent('change', function() {
	therapists.getZones($('country').value);
 });  
 

 /* Sprache bei Suche einstellen */
 if (util.getLang('L') == '1') {
	$('practitionerText').innerHTML = 'Finde einen ONDAMED<sup>®</sup> Anwender';
	therapists.firstElementString = '-- bitte auswählen --';
	if (util.gup('id') != '51') {
		$('zip').value = 'PLZ';
		$('city').value = 'Stadt';
	}
	$('button').value = 'finden';
	$('L').value = util.getLang('L');
 }
 
  if (util.getLang('L') == '2') {
	$('practitionerText').innerHTML = 'Encuentre un médico';
	therapists.firstElementString = '-- por favor, elija --';
	if (util.gup('id') != '51') {
		$('zip').value = 'Código postal';
		$('city').value = 'Ciudad';
	}
	$('button').value = 'Enviar';
	$('L').value = util.getLang('L');
 }
});


var therapists = {
    firstElementString: '-- please choose --',
	
	preLoadSelectBoxes: function(countryId, regionId) {
		therapists.setCountrySelected(countryId);
		therapists.getZones(countryId);
		therapists.setRegionSelected.delay(2000,$('states'), regionId);
	},	
	
	setCountrySelected: function(countryId) {
		$$('option[class=country][value=220]').each(function(s) {
			s.selected = false;
		});
		$$('option[class=country][value='+countryId+']').each(function(s) {
			s.selected = true;
		});
	},
	
	setRegionSelected: function(regionId) {
		$$('option[class=region][value=0]').each(function(s) {
			s.selected = false;
		});
		$$('option[class=region][value='+regionId+']').each(function(s) {
			s.selected = true;
		});		
	},
	
	loadCountries: function(countries) {
		var text = '';
		countries.each( function(s,index) {
			var item = new Element('option', {value:s.id, 'class':'country'}).appendText(s.country).inject($('country'));
		});
		if (util.getLang('L') =='1') {
			therapists.preLoadSelectBoxes(54,0);
		} else if (util.getLang('L') =='2') {
			therapists.preLoadSelectBoxes(65,0);
		} else {
			therapists.preLoadSelectBoxes(220,0);
		}
	},
	
	getZones: function(country) {
		new Request.JSON({ url: url, 
						   method: 'get',
						   onSuccess: function(states) {
								$('states').empty();
								therapists.loadZones(states);
						   }
						}).send(queryString+'&country='+country);	
	},
	
	loadZones: function(states) {
		new Element('option', {value:'0', 'class':'region'}).appendText(therapists.firstElementString).inject($('states'));
		states.each(function(s, index) {
			new Element('option', {value:s.id, 'class':'region'}).appendText(s.region).inject($('states'));
		});
	}
};
	/*  Start Teaser-Wechsel HOME */
var teaser = {
    currentId: 2,
		next: function() {
		// hide all teaser pictures, but show the current
		$$('.articleTeaserPicture').each(function(item) {
			item.setStyle('display', 'none');
		});
		$('atp'+teaser.currentId).setStyle('display', 'inline');
		// switch all arrows to 'off', but the current to 'on'
		$$('img[name^=arrow]').each(function(item) {
			item.setProperty('src', 'fileadmin/templates/images/home_teaser_article_pfeil_off.gif'); 
		});
		$$('img[name=arrow'+teaser.currentId+']').each(function(item) {
			item.setProperty('src', 'fileadmin/templates/images/home_teaser_article_pfeil_on.gif');
		});
		// set all backgrounds to light blue, but the current to white
		$$('div[id^=att]').each(function(item) {
			item.removeClass('teaserHover');
		});
		$('att'+teaser.currentId).addClass('teaserHover');
		// increment the counter
		teaser.currentId = (teaser.currentId==4) ? 1 : ++teaser.currentId;
	}
};

/* allgemeine Utilities */
var util = {
    getLang: function(name) {
		var url = window.location.href;
		if (url.match(/\.html$/)) {
			var lang = url.substr(util.getBaseHref().length, 2);
			switch (lang) {
				case "de":
					L = "1";
					break;
				case "es":
					L = "2";
					break;
				default:
					L = "";
					break;
			}
			return L;
		} else {
			return util.gup(name);
		}
	},
	
	gup: function (name) {
		
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec( window.location.href );
		if( results == null )
			return "";
		else
			return results[1];
	},
	
	getBaseHref: function() {
		var base = (document.getElementsByTagName ('BASE')[0] && document.getElementsByTagName( 'BASE')[0].href) || location.href;
		return base;
	}
}

var eBlast = {
	openWindow: function(url) {
		newWindow = window.open(url, "window1", "width=600,height=800,status=yes,scrollbars=yes,resizable=yes");
		newWindow.focus();
	}
};

/*  Ende Teaser-Wechsel HOME */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
MM_preloadImages('../images/home_teaser_article_pfeil_on.gif'); 