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

jQuery(function($){
	
	$('#logo').click(function() {
		window.location.href = web;
	});
	
	// ====================================== hover nad levým menu (skrývání teček) ======================================
	$('#menu li').hover(function() {
		
		$(this).next().addClass('hover');
		
	}, function() {
	
		if(!$(this).find('a').hasClass('active')) { // pokud už není v aktuální položce seznamu aktivní odkaz
		
			$(this).next().removeClass('hover');
		}
	});
	
	// pokud je nějaký prvek menu aktivní tak následnému prvku odebereme tečky
	$('#menu li a.active').parent().next().addClass('hover');
	
	// ====================================== homepage - carousel nad články ======================================
	if(document.getElementById('homepage') != null) {
	
		ul = "<ul>";
	
		$('#homepage h2').each(function(index) {
		
			if(index < 4) {
			
				if(index == 0) { // pro první průchod nastavíme první obrázek			
					$('#carousel_content_image').empty();
					$('#carousel_content_image').append($('#homepage_item_img_0').clone());		
				}
		
				ul += "<li class='" + (index == 0 ? 'active' : '') + "' id='homepage_item_li_" + index + "' onclick='redirectURL(\"" + $(this).find('a').attr('href') + "\")'>" + $(this).html() + "</li>";
			}		
		});
		
		ul += "</ul>";

		$('#carousel_content').css('height', '240px');
		$('#carousel_content').css('width', '770px');
		$('#carousel_content').append(ul);
		
		$('#carousel_content li').hover(function() {
			
			$('#carousel_content li').each(function() {
					
				$(this).css('background-position', '0 0');
			
				if($(this).hasClass('active')) {
					
					$(this).removeClass('active');
				}
								
			});
			
			$(this).css('background-position', '0 -80px');
			
			// zobrazíme správný obrázek
			id = $(this).attr('id').replace(/homepage_item_li_/, '');
			
			$('#carousel_content_image').empty();
			$('#carousel_content_image').append($('#homepage_item_img_' + id).clone());			
		
		}, function() {
		
			
		});
	}
	
	// ======================================= generování odkazů k obrázkům nad odkazem u výpisu článků ============================================
	$('.item .body img').click(function() {		
		document.location.href = $(this).parent().parent().find('h2 a').attr('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;		
															}
														});
});

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

function redirectURL(url) {

	document.location.href = url;
}

// ----------------------------------------------------------------------------------------
	
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;				
}
