function TAlist(instanceName){
	
	eval(instanceName + " = this;");

	
	this.daten = new Array();
	
	this.loadList = function(plz,show){
		var obj = this;
		this.plz=plz;
		this.show=show;
			$.getJSON("http://tierarztliste.chinchillahilfe.de/api/list_json.cfm?PLZ="+plz+"&callback=?",
				       function(data){
							obj.daten=data;
							if (obj.show){
								obj.showList();
							}
				       });
	}
	
	this.showList = function(){
		var col;
		var w=800;
		var h=$(window).height()*.7;
		
		var html='<div id="TaDivHeader" style="width:100%; border-bottom: 1px solid #333; font-size:13px; font-weight: bold; height:25px; background-image: URL(http://www.chinchilla-tierarztliste.de/images/bg_header.gif)"><p style="padding:5px;" >Tier&auml;rzte f&uuml;r PLZ- Gebiet '+this.plz;
		html +='</p></div><table style="width:100%; padding:1px;" cellpadding="8px" cellspacing="0px">';
		//html += '<tr><th>Name</th><th>Ort</th></tr>';
		for (var i=0; i < this.daten.rows.length; i++){
			(i>0) ? col="border-top: 1px #666 dotted;" : col="";
			html += '<tr valign="top" taId="'+i+'" onmouseover="this.style.backgroundColor=\'#ccc\';this.style.color=\'white\';" onmouseout="this.style.backgroundColor=\'\';this.style.color=\'\';" onmousedown="'+instanceName+'.showTa(this.attributes.taId.nodeValue)"><td style="'+col+'">'+this.daten.rows[i].name+'</td><td style="'+col+'">'+this.daten.rows[i].plz+'</td><td style="'+col+'">'+this.daten.rows[i].ort+'</td></tr>';
		}
		html += "</table>";
		var style='border:0px solid #666;z-index:1001; font: 12px Arial; cursor:pointer; background-color:white;background-image:url(http://www.chinchilla-tierarztliste.de/images/bg_inner.png); ';
		var divhtml='<div id="TaDiv" style="'+style+' display:none; visibility: hidden; position:absolute; overflow:auto;"></div> ';
		$('body').append(divhtml);
		$('body').append('<div id="TaButtonClose" style="z-index:1002;position:absolute; width:22px; height:22px;background-image:url(http://www.chinchilla-tierarztliste.de/images/button_close.png); "></div>')
		$('body').append('<div id="TaDimmer" style="z-index:100;position:absolute; width:100%; height:'+$('body').height()+'px; left:0px; top:0px; background-color:#000000;filter:Alpha(opacity=50);-moz-opacity:0.5;opacity:0.5;"></div>')
		
		$('#TaDiv').html(html);
		$('#TaDiv').width(w);
		$('#TaDiv').height(h);
		$('#TaDiv').css('left',(($(window).width()-w)/2)+"px");
		$('#TaDiv').css('top',(($(window).height()-h)/2)+"px");
		$('#TaButtonClose').css('left',(($(window).width()-w)/2+w-5)+"px");
		$('#TaButtonClose').css('top',(($(window).height()-h)/2-17)+"px");
		$('#TaDiv').css('display','block');
		($('#TaDiv').height() > $('#TaDiv table').outerHeight(true)+$('#TaDivHeader').outerHeight(true)) ? $('#TaDiv').height($('#TaDiv table').outerHeight(true)+$('#TaDivHeader').outerHeight(true)) : true;
		
		$('#TaDiv').css('-moz-box-shadow','2px 2px 6px rgba(0, 0, 0, 0.25)');  
		$('#TaDiv').css('-webkit-box-shadow','2px 2px 6px rgba(0, 0, 0, 0.25)');

		$('#TaDimmer').bind('click',function() {
			$('#TaDiv').remove();
			$('#TaButtonClose').remove();
			$('#TaDimmer').remove();
		});
		
		$('#TaButtonClose').bind('click',function() {
			$('#TaDiv').remove();
			$('#TaButtonClose').remove();
			$('#TaDimmer').remove();
		});
		
		this.taListeHtml=html;
		
		$('#TaDiv').css('visibility','visible');
	}
	
	this.showTa = function(id){
		window.alert(id);
	}
	
}


function TAsearch(instanceName){
	new TAlist(instanceName);
	eval(instanceName + "Search = this;");
	searchInstanceName=instanceName+"Search";
	this.show = function(e){
		var html='<div id="TaSearch" style="width:300px;cursor:pointer; padding: 10px;background-image:url(http://www.chinchilla-tierarztliste.de/images/bg_inner.png); height: 100px; position:absolute; left:'+(e.pageX-10)+'px; top:'+(e.pageY-10)+'px; background-color:white;">';
		html += '<span style="float:left;">Chinchilla Tierarztliste</span><span style="float:right" onclick="$(\'#TaSearch\').remove();">X</span><p style="width:100%; border-bottom:1px solid #aaa;clear:both; padding-top:5px;"></p><div style="margin-top:5px;clear:both; "><span style="float:left;">PLZ- Gebiet:</span><span style="float:right;">';
		for (var i=0; i < 10; i++){
			html += '<a onclick="'+instanceName+'.loadList('+i+',1);">'+i+'</a>&nbsp;';
			(i<9)? html+='|&nbsp;&nbsp;':true;
		}
		html += '</span></div><div style="padding-top:20px;"><span style="float:left">Tierarzt suchen:</span><span style="float:right"><input type="text" style="width:200px">'
		html += '</span></div><span style="position:absolute; bottom:0px; right:0px; padding:5px; float:right;"><a href="http://www.chinchilla-tierarztliste.de" target="_blank">chinchilla-tierarztliste.de</a></div>';
		$('body').append(html);
		$('#TaSearch').css('-moz-box-shadow','2px 2px 6px rgba(0, 0, 0, 0.25)');  
		$('#TaSearch').css('-webkit-box-shadow','2px 2px 6px rgba(0, 0, 0, 0.25)');

	}
	
	
}


/*
var tierarztliste = {
	daten:new Array(),
	loadList:function(plz){
				var obj = this; 
					$.getJSON("http://tierarztliste.chinchillahilfe.de/api/list_json.cfm?PLZ="+plz+"&callback=?",
				       function(data){
							obj.daten=data;
				       });
			},
	showList:function(){
				var html="";
				window.alert(this.daten.results);
			}
	
}
*/

/* this is the default Object for all Data and Methods - used to prevent namespace conflict */

new TAsearch('tierarztListe');


$(document).ready(function(){
	if ($('#ChinchillaTierarztliste').attr('PLZ') > -1){
		tierarztListe.loadList($('#ChinchillaTierarztliste').attr('PLZ'));
		$('#ChinchillaTierarztliste').bind('click',function() {
			tierarztListe.showList();
		});
	}else{
		$('#ChinchillaTierarztliste').bind('click',function(e) {
			tierarztListeSearch.show(e);
		});
	}
 		
  	});
