var predchoziSet = 0;
var web = "http://www.fashionlevel.net/";
var ajaxPrefix = "/";

jQuery(function($){
	
	$('#logo').click(function() {
		window.location.href = web;
	});
	
	// přidáme hovery na položkami článků (articleItem)
	$('.articleItem').hover(function() {
		$(this).addClass('active');		
		$(this).find('h2 a').addClass('hover');			
	},function() {
		$(this).removeClass('active');
		$(this).find('h2 a').removeClass('hover');	
	});
	
	// přidáme listener pro klik
	$('.articleItem').click(function() {
		document.location.href = $(this).find('h2 a').attr('href');
	});
	
	// přidáme hovery na položkami článků (sales .item)
	$('.sales .item').hover(function() {
		$(this).addClass('active');		
		$(this).find('h2 a').addClass('hover');			
	},function() {
		$(this).removeClass('active');
		$(this).find('h2 a').removeClass('hover');	
	});
	
	// přidáme listener pro klik
	$('.sales .item').click(function() {
		document.location.href = $(this).find('h2 a').attr('href');
	});
	
	// přidáme listener pro klik
	$('.mainArticle .link img').click(function() {
		document.location.href = $(this).parent().parent().parent().find('h2 a').attr('href');
	});
	
	// přidáme listener pro klik
	$('.mainArticle .img').hover(function() {
		$(this).parent().parent().find('h2 a').addClass('hover');
	}, function() {
		$(this).parent().parent().find('h2 a').removeClass('hover');	
	});
	
	// ================================= 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.info.length; i++) {			
							html += "<li>" + json.info[i] + "</li>"
						}
						
						$('#ul_information').prepend(html);						

						html = "";
						
						for(i = 0; i < json.advert.length; i++) {							
							html += "<li>" + json.advert[i] + "</li>"
						}
						
						$('#ul_advert').append(html);				

						html = "";
						
						for(i = 0; i < json.support.length; i++) {						
							html += "<li>" + json.support[i] + "</li>"
						}
						
						$('#ul_support').append(html);
	    	  }
  			});
	
	// ================================= 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;		
															}
														});
});


// ----------------------------------------------------------------------------------------
	
function showCategories(text1, text2) {

	if($('#viewAll').html() == text1) {
	
		$('#hideMenu').slideDown("normal");		
		$('#viewAll').html(text2);
		
	} else {
	
		$('#hideMenu').slideUp("normal");
		$('#viewAll').html(text1);
	}
		
	return false;	
}

// ----------------------------------------------------------------------------------------
	
function showArticles(type) {

	if(type == 1) { // checeme zobrazit související
	
		$('#related').show();
		$('#new').hide();
		$('#relatedArticles').attr('class', 'deactive');
		$('#newsArticles').attr('class', 'active');
		
	} else { // zobrazit nejnovější články
	
		$('#new').show();
		$('#related').hide();
		$('#newsArticles').attr('class', 'deactive');
		$('#relatedArticles').attr('class', 'active');
	}
		
	return false;				
}

// ----------------------------------------------------------------------------------------
	
function submitForm(type, url) {

	if($('#' + type + ' option:selected').val() != 0) {
	
		document.location.href = url + $('#' + type + ' option:selected').val() + '/';
	}
	
	return false;
}
