/**

 * URL steuerungstool durch ( mit anchors ) simulierte GET parameter

 */



function med_UrlController()

{

	this.workHistory = false;

	this.getString = false;

	this.urlDivider = '/';

	

	var url = document.location.href.toString();

	var posTarget = url.indexOf('#');

	this.getString = url.substring(posTarget+1);

	

	this.workHistory = (posTarget > -1)?url.substring(0,posTarget):url;

	this.workHistory += '#';

	

	this.getVar = function (variable)

	{

		if(!this.getString) return false;

		if((startPos = this.getString.lastIndexOf(this.urlDivider+variable+this.urlDivider))<0)  return false;

		var tempget = this.getString;

		var initVarString = tempget.substring(startPos+variable.length+this.urlDivider.length*2);

		if(!initVarString.length)return false;

		var varLength = initVarString.indexOf(this.urlDivider);

		var varString = (varLength<0)?initVarString:initVarString.substring(0,varLength);

		return unescape(unescape(varString));

	}

	

	this.setHistory = function ( params )

	{

		var newLocation = this.workHistory;

		for(var i = 0; i< params.length;i++)

		{

			var value = (params[i].value)?escape(this.urlencode(params[i].value)):0;

			newLocation += this.urlDivider+params[i].name+this.urlDivider+value;

		}

		window.location = newLocation;

		

		var posTarget = newLocation.indexOf('#');

		this.getString = newLocation.substring(posTarget+1);

		this.workHistory = (posTarget > -1)?newLocation.substring(0,posTarget):newLocation;

		this.workHistory += '#';

		

	}

	

	

	this.urlencode = function(str){

		return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');

	}

	

}





/**

 * Stationssuch Klasse

 */

function tx_bestationsearch_pi1_jshandle(conf)

{

	//handle f�r Klassenfunktionen

	var _this = this;

	this.containerHeightBuffer = 0;

	

	this.status = 0;

	this.getDefaultVal = function (desiredVal, defaultVal){return !desiredVal?defaultVal:desiredVal;}

	this.transitionSpeed = this.getDefaultVal(conf.transitionSpeed, 1);

	this.requestURL = this.getDefaultVal(conf.requestURL, '/');

	this.containerId = this.getDefaultVal(conf.containerId, 'tx_bestationsearch_pi1_jshandle');

	this.loadCode = this.getDefaultVal(conf.loadCode, '<strong> Loading... </strong>');

	this.listErrors = this.getDefaultVal(conf.listErrors, ['Error while starting searchobject', 'Error while loading searchform','Error while loading searchform','Error processing searchrequest','Error processing searchrequest']);

	this.URLController = new med_UrlController();

	

	if(!$(this.containerId))document.write('<div id="'+this.containerId+'"></div>');

	

	this.init = function()

	{

		if(this.URLController.getVar('street')!=0||this.URLController.getVar('city')!=0||this.URLController.getVar('zip')!=0)

		{

			this.start_search();

		}else{

			this.search_form();

		}

	}

	

	this.search_form = function()

	{

		this.triggerLoader();

		

		this.status = 1;

		

		new Ajax.Request(

			this.requestURL,

			{

				method:'get',

				onSuccess: function(transport)

				{

					_this.transition({id: _this.containerId+'_searchform', content: transport.responseText});

					_this.status = 2;

					_this.URLController.setHistory({name: "city", value: 0},

						{name: "street", value: 0},

						{name: "zip", value: 0},

						{name: "res_id", value: 0});

				},

				onFault: function()

				{

					_this.triggerError();

				}

			}

		);

	}

	

	this.triggerLoader = function()

	{

		this.transition({id: this.containerId+'_loading', content: this.loadCode});

	}

	

	this.transition = function(transTo)

	{

		this.containerHeightBuffer = 0;

		var children = $(this.containerId).childElements();

		if(children.length)

		{

			var appearWasSet = 0;

			var i = 0;

			for(i=0; i<children.length;i++)this.containerHeightBuffer += children[i].getHeight();

			$(this.containerId).setStyle({height: this.containerHeightBuffer});

			for(i=0; i<children.length;i++){

				if(!appearWasSet)

				{

					

					children[i].fade({duration: this.transitionSpeed/2, afterFinish: function(e){

							$(_this.containerId).update('<div id="'+transTo.id+'" style="display:none;">'+transTo.content+'</div>');

							$(_this.containerId).setStyle({height: $(transTo.id).getHeight()+'px'});

							$(transTo.id).appear({duration: _this.transitionSpeed/2});

							$(_this.containerId).setStyle({height: $(transTo.id).getHeight()+'px'});

						}

					});

					appearWasSet = true;

				}else{

					children[i].fade({duration: this.transitionSpeed/2});

				}

				

			}

		}else{

			$(_this.containerId).update('<div id="'+transTo.id+'" style="display:none;">'+transTo.content+'</div>');

			$(_this.containerId).setStyle({height: $(transTo.id).getHeight()});

			$(transTo.id).appear({duration: _this.transitionSpeed/2});

		}

	}

	

	this.checkInput = function (field)

	{

		if($(field))return $(field).value;

		return '';

	}

	

	this.start_search = function (objFields)

	{

		

		var street;

		var zip;

		var city;

		var res_id;

		

		if(objFields && (objFields["street"] || objFields["city"] || objFields["zip"]))

		{

			street = objFields["street"];

			zip = objFields["zip"];

			city = objFields["city"];

		}else{

			street = this.URLController.getVar('street');

			zip = this.URLController.getVar('zip');

			city = this.URLController.getVar('city');

		}

		

		if(objFields && objFields["res_id"])

		{

			res_id = objFields["res_id"];	

		}else{

			res_id = this.URLController.getVar('res_id');

		}

		this.triggerLoader();

		this.status = 3;

		new Ajax.Request(

			this.requestURL+'&action[search][street]='+escape(street)+'&action[search][zip]='+escape(zip)+'&action[search][city]='+escape(city)+'&action[search][res_id]='+escape(res_id),

			{

				method:'get',

				onSuccess: function(transport)

				{

					_this.transition({

						id: _this.containerId+'_results',

						content: transport.responseText

					});

					_this.URLController.setHistory([

						{name: "city", value: city},

						{name: "street", value: street},

						{name: "zip", value: zip},

						{name: "res_id", value: res_id}

					]);

					_this.status = 4;

				},

				onFault: function()

				{

					_this.triggerError();

				}

			}

		);

	}

	

	this.triggerError = function()

	{

		this.trasition({

			id: this.containerId+'_error',

			content: this.listErrors[this.status]

		});

	}

}





/**

 * Googlemaps Klasse f�r Stationssuche

 * (google api wird separat geladen)

 */

function tx_bestationsearch_pi1_gmapsobj(conf)

{

	var _this = this;

	this.getDefaultVal = function (desiredVal, defaultVal){return !desiredVal?defaultVal:desiredVal;}

	this.listMarkers = this.getDefaultVal(conf.listMarkers,[]);

	this.resultPointer = this.getDefaultVal(conf.resultPointer,{lat:52.1,lng:8.6});

	this.resultPointerCaption = this.getDefaultVal(conf.resultPointerCaption, 'Sie sind hier');

	this.mapSize = this.getDefaultVal(conf.mapSize, {width: 400,height: 400});

	this.containerId = this.getDefaultVal( conf.containerId , 'tx_bestationsearch_pi1_gmapsobj');

	this.googleRequestUrl = this.getDefaultVal( conf.googleRequestUrl , false);

	this.stationPointers =  this.getDefaultVal( conf.stationPointers , []);

	this.initZoom =  this.getDefaultVal( conf.initZoom , 8);

	this.map;

		

	this.init = function()

	{

		if(typeof GBrowserIsCompatible != 'function')

		{

			alert('Couldnt load Google API');

			return;

		}

		

		this.map = new GMap2($('gmap_canvas'),{size: new GSize(this.mapSize.width,this.mapSize.height)});

		var center = new GLatLng(this.resultPointer.lat, this.resultPointer.lng);

		this.map.setCenter(center,this.initZoom);

		this.map.addControl(new GSmallMapControl());

		this.map.addControl(new GMapTypeControl());



		var resultMarker = this.addMarker({

			point: center,

			onclick: function(){

				this.openInfoWindowHtml( _this.resultPointerCaption );

			},

			

			autoAdd: true

		}); 

		

		for(var i=0; i<this.stationPointers.length;i++)

		{

			var tmp_icon = new GIcon(G_DEFAULT_ICON);

			tmp_icon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";

			

			this.addMarker({

				id: i,

				point: new GLatLng(this.stationPointers[i].lat,this.stationPointers[i].lng),

				onclick: function(){

					this.openInfoWindowHtml( _this.stationPointers[this.id].html );

				},

				options: {

					icon: tmp_icon

				},

				autoAdd: true

			});



		}

	}

	

	this.addMarker = function (markerConf)

	{

		var marker = new GMarker(markerConf.point, markerConf.options);

		marker.id = markerConf.id;

		if(typeof markerConf.onclick == 'function')GEvent.addListener(marker, "click", markerConf.onclick);

		if(markerConf.autoAdd)this.map.addOverlay(marker);

		return marker;

	}

}

