var predchoziSet = 0;
var web = "http://www.mapaobchodu.cz/";
//var ajaxPrefix = "/job/fashion/mapaobchodu.cz/www/";
var ajaxPrefix = "/";


jQuery(function($){
		
	$('#logoContent').click(function() {
		window.location.href = web;
	});
	
	if($('.tipsy').html() != '' && $('.tipsy').html() != null) {

		$('.tipsy').tipsy({html: true, gravity: $.fn.tipsy.autoNS});
	}
	
	if(document.getElementById('blog') != null) { // homepage a odkazy na články v blogu
	
		$('#blog div.item').hover(function() {			
			
			$(this).find('a').addClass('active');
		},
		function() {
			
			$(this).find('a').removeClass('active');		
		});
		
		$('#blog div.item img').click(function() {
			
			window.open($(this).parent().find('a').attr('href'));
		});
	}
	
	if(document.getElementById('maparea') != null) {
	
		$('#maparea').maphilight({	
			fill: true,
			fillColor: '0fb2e7',
			fillOpacity: 1,
			stroke: true,
			strokeColor: 'ffffff',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: true,
			alwaysOn: false
		});
	
		$.fn.maphilight.defaults = {
			fill: true,
			fillColor: '0fb2e7',
			fillOpacity: 1,
			stroke: true,
			strokeColor: 'ffffff',
			strokeOpacity: 1,
			strokeWidth: 1,
			fade: true,
			alwaysOn: false
		}
	}
	
	// ================================= vygenerujeme si menu v patičce ================================

	$.ajax({method: 		"post", 
  		    url: 				ajaxPrefix + "autocomplete/generateMenu", 
   				dataType: 	"json",
      								  
         	beforeSend: function() { },
  	  	                  
  	  		complete: 	function() { },  
			  								
			  	error: 			function(XMLHttpRequest, textStatus, errorThrown) {

		  				/*data = "";
		  				
		  				for(item in XMLHttpRequest) {		  					
		  					data += item + " -> " + XMLHttpRequest[item] + "\n";
		  				}
		  						
		  				alert("Došlo k chybě:\n\n" + data);*/
				 	},
			  							
					success: 		function(json) {

						html = "";

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

							html += "<li>" + json.left[i] + "</li>"
						}

						for(i = 0; i < json.right.length; i++) {
						
							html += "<li class='right'>" + json.right[i] + "</li>"
						}
		
						$('#foot_left_menu').html(html);
	
	
						// ================================= TOOLTIP pro BLOG ================================
	
						$('.footTooltip').tooltip({
							track: 				true,
							showURL: 			false,
							id:						'footTooltip',
							bodyHandler: 	function() { 
    						return $('<img/>').attr('src', this.tooltipText);
				    	} 
						});
						
						// doplníme ještě privacy
						$('#copyright').append('<p>' + json.center[0] + '</p>');
	    	  }
  });
	
	// ================================= vygenerujeme si abecední filtr ================================
	

	if(document.getElementById('alpha') != null) {
	
		filterData = "type=" + ($('#alpha_type_value').html() != "" ? $('#alpha_type_value').html() : "");
		filterData += "&slug=" + ($('#alpha_slug_value').html() ? $('#alpha_slug_value').html() : "");
		filterData += "&alpha=" + ($('#alpha_value').html() ? $('#alpha_value').html() : "");
		filterData += "&city=" + ($('#alpha_city_value').html() ? $('#alpha_city_value').html() : "");
		filterData += "&suffix=" + ($('#alpha_suffix_value').html() ? $('#alpha_suffix_value').html() : "");
		filterData += "&id=" + ($('#alpha_id_value').html() ? $('#alpha_id_value').html() : "");

		//alert(ajaxPrefix + "autocomplete/generateAlphaFilter?" + filterData);
	
		$.ajax({method: 		"post", 
  			    url: 				ajaxPrefix + "autocomplete/generateAlphaFilter",
  			    data:				filterData, 
   					dataType: 	"html",
      									  
         		beforeSend: function() { },
  	  	                  
	  	  		complete: 	function() { },  
			  								
				  	error: 			function(XMLHttpRequest, textStatus, errorThrown) {
		  						
			  				/*data = "";
		  				
		  					for(item in XMLHttpRequest) {		  					
		  						data += item + " -> " + XMLHttpRequest[item] + "\n";
		  					}
		  						
		  					alert("Došlo k chybě:\n\n" + data);*/
					 	},
			  							
						success: 		function(html) {

							if(html != "") {
							
								$('#alpha').empty();
								$('#alpha').html(html);
								$('#alpha').show();
							}
		    	  }
  	});
  }
	
	if(document.getElementById('maparea') != null || document.getElementById('region') != null) {
	
		// ================================= TOOLTIP ================================
	
		$('.imgTooltip').tooltip({
			track: 				true,
			showURL: 			false,
			id:						'tooltipImg',
			bodyHandler: 	function() { 
    		return $('<img/>').attr('src', this.tooltipText);
  	  } 
		});
	}
	
	// ================================= SELECT v regionech ================================
	
	if(document.getElementById('select') != null) {
	
		$("#quick_div_city").hover(
		  function () {
  	
		   	$(this).css('background-position', '0 -30px');
		   	
		   	$('#quick_city_container').slideDown('fast');
		   
	  	}, 
  		function () { 
  
  			obj = this;
     			 
		   	$('#quick_city_container').slideUp('fast', function() {		   	
  
     			$(obj).css('background-position', '0 0'); 
		   	});
	  	}
	  );
	
		$(".selectLink").hover(
		  function () {
  	
		  	id = $(this).attr('id').replace(/link_/, '');
		  	
		  	$('#count_shop_' + id).show();
		   
	  	}, 
  		function () {
  	
		  	id = $(this).attr('id').replace(/link_/, '');
		  	
		  	$('#count_shop_' + id).hide();
	  	}
	  );
	}
	
	// ================================= generování prokliknutelných obrázků ================================
	
	$('table.malls td.image img').click(function() {
		
		document.location.href = $(this).parent().parent().find('td.link a').attr('href');
	});
	
	$('div.sorter div.item td.image img').click(function() {
		
		document.location.href = $(this).parent().parent().find('td.info h4 a').attr('href');
	});
  
  // ============================= nastavíme fancybox pro Error Reporting =============================	
  if(document.getElementById('error_report') != null) {
  
  	href = $('#error_report_href').html();
	
		$("#error_report").fancybox({
			'transitionIn'	:	'none',
			'transitionOut'	:	'none',
			'overlayShow'		:	true,
			'overlayColor'  : '#000',
			'autoScale'			: false,
			'padding'				: 0,
			'margin'				: 0,
			'titleShow'			: false,
			'href'					: href
		});
	}
	
	// ================================= autocompleter ================================
	
	var cache = {}, lastXhr;
	
	$("#input_search").autocomplete({	minLength: 2,
															source: function(request, response) {
																var term = request.term;
		
																if(term in cache) {
																	response(cache[term]);
																	return;
																}

																lastXhr = $.getJSON(ajaxPrefix + "autocomplete/generate", request, function(data, status, xhr) {
																	cache[term] = data;
																	if(xhr === lastXhr) {
																		response(data);
																	}
																});
															},
															select: function(event, ui) {				
																$('#input_search').val(ui.item.value);
																$('#searchForm').submit();			
																return false;		
															}
														});
	
	
	// ================================= TOOLTIP pro stránku s reklamním textem ================================
	
	if(document.getElementById('advertise') != null) {
				
		//$('.tooltip').tooltip({track: true, showURL: false});
	}
});

// ====================================================================================================================

function unHideBrands(text1, text2) {

	if($('#unhide_brands_links').html() == text1) { // pokud odkrýváme
	
		$('.hidden').show();
		$('#unhide_brands_links').html(text2);
	
	} else {
	
		$('.hidden').hide();
		$('#unhide_brands_links').html(text1);
	}
}

// ====================================================================================================================

function unHideNote(text1, text2) {

	if($('#note_html_link').html() == text1) { // pokud odkrýváme
	
		$('#note_html').show();
		$('#note_html_link').html(text2);
	
	} else {
	
		$('#note_html').hide();
		$('#note_html_link').html(text1);
	}
}

// ====================================================================================================================

function switchChosen(id) {

	// nejdříve odstraníme všem odkazům "active" třídu
	$('#footer h2 a').removeClass('active');
	
	// nastavíme třidu "active" pouze odkaz na který jsme klikli
	$('#a_' + id).addClass('active');
	
	// skryjeme všechny <ul>
	$('#footer ul').hide();
	
	// odkryjeme správný seznam
	$('#ul_' + id).fadeIn('slow');
}

// ====================================================================================================================

function redirectURL(url) {

	document.location.href = url;
}


// ================================================================================================================ 


function checkErrorReport(obj, error) {

	if($('#error_name', obj).val() == '') {
	
		alert(error);
		
		$('#error_name', obj).focus();
		
		return false;
	}

	if($('#error_email', obj).val() == '') {
	
		alert(error);
		
		$('#error_email', obj).focus();
		
		return false;
	}

	filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

	if(!filter.test($('#error_email', obj).val())) {
	
		alert(error);
		
		$('#error_email', obj).focus();
		
		return false;
	}

	if($('#error_message', obj).val() == '') {
	
		alert(error);
		
		$('#error_message', obj).focus();
		
		return false;
	}

	
	//alert("/backend_prod.php/auth/errorReport?text=" + $('#error_report_message', obj).val());
		
	// inicializace ajaxu						 	  	          	  		      	  		 
	$.ajax({	method: 		"post", 
  		    	url: 				ajaxPrefix + "page/errorReport", 
    		  	data: 			"message=" + $('#error_message', obj).val() + "&name=" + $('#error_name', obj).val() + "&email=" + $('#error_email', obj).val(),
   					dataType: 	"html",
      								  
          	beforeSend: function() {
	
  	       		$('#indicator_error_report').show();
		  	    },
  	  	                  
  	  			complete: 	function() {
    	  	  	        	
      	 	  	$('#indicator_error_report').hide();
	          },  
			  								
			  		error: 			function(XMLHttpRequest, textStatus, errorThrown) {
		  						
		  				alert("Došlo k chybě: " + textStatus);
		  				
		  				/*for(property in XMLHttpRequest) {
		  				
		  					alert(XMLHttpRequest[property] + "::" + property);
		  				}*/
				 		},
			  							
				 		success: 		function(data) {

						  $('#error_report_successful').show();
						  	
						 	$('#error_report_heading').hide();
						 	$('#error_report_note').hide();
					  	$('#error_report_table').hide();
						  	
						  // resizujeme okno
						 	$.fancybox.resize();
	    	    }
  				});
	
	return false;
}

// ====================================================================================================================

function removeDefaultText(text, id) {

	if($('#' + id).val() == text) { // pokud je v inputu default text, tak ho smažeme
	
		$('#' + id).val('');
	}
}

// ====================================================================================================================

function addDefaultText(text, id) {

	if($('#' + id).val() == '') { // pokud jsme nic nevyplnili
	
		$('#' + id).val(text);
	}
}

// ====================================================================================================================

function showMore() {

	$('.bankBankomats li.more').hide();
	$('.bankBankomats li.hide').show();
}
