/**
 * @author rossini
 */

		$(document).ready(function() { 

			$("#loading")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
			});

			var options = {
				success:   showResponse,
				url:       'newpoi1/json_obr.php',
				dataType:  'json'
			};
			
			$('#jsonForm').submit(function() {
				$(this).ajaxSubmit(options);
				return false;
			});
			
			$("#ares").click(function(){ 
			
				$.ajax({
			                 type: "GET",
			                 url: "newpoi1/ares_json.php?ico="+ $("#t_ico").val(),
			                 dataType: "xml",
			                 success: function(xml) {
			                    var $firma = ($(xml).find('[nodeName=are:Obchodni_firma]').text());
			                    var $adresa = ($(xml).find('[nodeName=dtt:Nazev_ulice]').text());
			                    var $cisdom = ($(xml).find('[nodeName=dtt:Cislo_domovni]').text());
			                    var $cisor = ($(xml).find('[nodeName=dtt:Cislo_orientacni]').text());
			                    var $mesto = ($(xml).find('[nodeName=dtt:Nazev_obce]').text());
			                    var $psc = ($(xml).find('[nodeName=dtt:PSC]').text());
			                    var $dic = ($(xml).find('[nodeName=dtt:Nazev_obce]').text());					
								var $or = ($(xml).find('[nodeName=are:Priznaky_subjektu]').text().substr(1,1));			
								
								$("#t_odberatel").val($firma);
								$("#t_odb_adresa").val($adresa+' '+$cisdom+'/'+$cisor);
								$("#t_odberatel").val($firma);
								$("#t_odb_mesto").val($mesto);
								$("#t_odb_psc").val($psc);
					
								if ($or=='A') {
									// pokud existuje v OR, rovnou mu dám DIČ ve tvaru CZ+IČ
									$("#t_dic").val('CZ'+$("#t_ico").val());						
								}							
							 }
			             });
			
			});
				
	});
	
	
		function processJson(data) { 
    		// pomocná funkce jen pro debug
    		
			alert(data.message); 
		}
		
		function showResponse(data, statusText)  {
			if (statusText == 'success') {
				if (data.img != '') {
					
					var obrazky= document.getElementById("result");
					var linkobr = document.createElement('a');
					var dalsiobr = document.createElement('img');	
					
					dalsiobr.setAttribute('src','http://www.poi.cz/poi/itemimagetemp/'+data.img );
					dalsiobr.setAttribute('height', '70');
					dalsiobr.setAttribute('width', '100');
					dalsiobr.setAttribute('class', 'det_obrazek_add');
					
					var idobr=data.img;
					linkobr.setAttribute('rel', 'image-set');					
					linkobr.setAttribute('class', 'thickbox');
					linkobr.setAttribute('title', '');
					linkobr.setAttribute('id', idobr.substring(0,String(idobr).length-4));										
					linkobr.setAttribute('href','http://www.poi.cz/poi/itemimagetemp/'+data.img);
					
					obrazky.appendChild(linkobr);
					linkobr.appendChild(dalsiobr);
					
					var i=1;
					while (checkobject('t_obrazek'+i)) {					
						i++;
					}
					
					addInput (data.img, i);

					// Zavírání obrázku
					
					var obrazky= document.getElementById("result");
					var linkobr = document.createElement('a');
					var dalsiobr = document.createElement('img');
					var cont = idobr.substring(0,String(idobr).length-4);
					
					dalsiobr.setAttribute('src','http://www.poi.cz/images/close.jpeg');
					dalsiobr.setAttribute('height', '16');
					dalsiobr.setAttribute('width', '16');
					dalsiobr.setAttribute('class', 'close');
					dalsiobr.setAttribute('id', 'close'+cont);
					
					linkobr.setAttribute('title', '');					
					linkobr.setAttribute('href','#');
					linkobr.setAttribute('id', 'closelink'+cont);
					
					obrazky.appendChild(linkobr);
					linkobr.appendChild(dalsiobr);
					
					
				    $("#close"+cont).click(function () {
				      	$("#"+cont).remove();
				      	$("#closelink"+cont).remove();
						
						if ((i==1) && (checkobject('t_obrazek'))) {
							$("#t_obrazek").remove();
						}
						else {
							$("#t_obrazek"+i).remove();		
						}			
						
						format();

						return false;		
				    });

					
					// Znovu-inicializace ThickBoxu pro zobrazování obrázků -> jsou načteny jen ty nedynamicky vytvořené...
					tb_init('a.thickbox, area.thickbox, input.thickbox');		

					
				} else {
					//chyba, kterou vracím v json
				}
			} else {
				//chyba, kterou jsem neodchytil
			}
		} 

		

		function addInput (img, poradi) {
			
			var x = document.getElementById("nahratefotky");   
    		var input = document.createElement('input');  
			
			if ((poradi==1) && !(checkobject('t_obrazek'))) {
			    input.setAttribute('name', 't_obrazek');
				input.setAttribute('id', 't_obrazek');		
			}
			else {
			    input.setAttribute('name', 't_obrazek'+poradi);
				input.setAttribute('id', 't_obrazek'+poradi);			
			}			
			    
			input.setAttribute('type', 'hidden' );
			input.setAttribute('value', img );	
			
   			x.appendChild(input);
		}

			
		function checkobject(obj) {
			if (document.getElementById(obj)) { return true; } else { return false; }
		}

		function format() {
			
			var i = 0;
			$('#nahratefotky input').each(function() {
			    $(this).attr('name', 't_obrazek'+i);
			    $(this).attr('id', 't_obrazek'+i);
				i++;
			});
			
			if ($('#t_obrazek0').length) {
				$('#t_obrazek0').attr('name', 't_obrazek');
				$('#t_obrazek0').attr('id', 't_obrazek');				
			}

			// Znovu-inicializace ThickBoxu pro zobrazování obrázků -> jsou načteny jen ty nedynamicky vytvořené...
			tb_init('a.thickbox, area.thickbox, input.thickbox');		

		}

