function updateImageUploadLink(count)
{
	var uploadList = $('.file-upload-list');
	var imagesCount = $('p', uploadList).size() + $('.edit-images li').size();
	var isProduct = $('.edit-images.product-images').size() != 0;
	$('#add_file').remove();	
	if(imagesCount < (isProduct ? 1 : count))
	{ 
		var addText = imagesCount == 0 ? 'Přidat obrázek' : 'Přidat další obrázek';
		uploadList.after('<p><a href="#" id="add_file" class="ico-image-add" >' + addText + '</a></p>');
		$('#add_file').bind('click', function()
		{
			var uploadList = $('.file-upload-list');
			var number = $('p', uploadList).size() + 1;
			uploadList.append('<p><label for="file' + number + '">Obrázek' + (isProduct ? '' : ' č.  <span class="img-no">' + number + '</span>') + ':</label><br /><input type="file" name="file[]" id="file' + number + '" class="sizeFull" /></p> ');
			updateImageUploadLink(count);
			return false;
		});
	}
	var imagesCount = $('.edit-images li').size();
	$('.file-upload-list label').each(function(i){
		$('.img-no',this).html(i + imagesCount + 1);
	});	
};

$(document).ready(function()
{
	// Init pdBox
	$('.thickbox').pdBox({minWidth: 400});
	
	// Create zebra-style tables
	$("tr:nth-child(even)").addClass("evenrow");
	
	// Activate popup links:
	$('a.popup').bind('click', function(){
		return !window.open(this.href);
	});
	
	// SEARCH BOX TEXT
	$('#comments_name, #comments_text').each(function(){
      var btntext = $(this).attr('value');
      $(this).focus(function(){
            text = $(this).attr('value');
            if( text == btntext ){
                $(this).attr('value', '');
            }
      }).blur(function(){
            var text2 = $(this).attr('value');
            if( text2 == '' || text2 == undefined ){
                $(this).attr('value', btntext);
            }
      });
  });
	
	// In user section (review edit or product review page) init image deleting JS helpers
	$('.edit-images input.img-del').each(function(){
		var inpName = $(this).attr('name');
		$(this).before('<a href="#" class="ico-delete">smazat</a>').prev().bind('click', function()
		{
			$(this).parent().parent().remove();
			$('.edit-images').append('<input type="hidden" name="' + inpName + '" value="1" />');
			
			$('.edit-images li').each(function(i){
				$('.img-no',this).html(i + 1);
			});
			updateImageUploadLink('4');
			return false;	
		});		
		$('label[@for=' + $(this).attr('id') + ']').remove();
		$(this).remove();
	});	
	
	// In user section (review edit or product review page) init image uploading JS helpers
	$('.file-upload-list').each(function(){
		var uploadList = $('.file-upload-list').empty();
		updateImageUploadLink('4');	
	});
	
	$('.file-upload-list3').each(function(){
		var uploadList = $('.file-upload-list').empty();
		updateImageUploadLink('3');	
	});	
	// $('#add_file').trigger('click');
	
	// In user section - review edit page - init AJAX textile preview/edit modes 
	$('.edit-preview-links .edit').bind('click', function(){
		$('.edit-preview-links .preview').removeClass('active');
		$(this).addClass('active');		
		$('#reviewContent').show();
		$('#preview').hide();
		return false;
	})
	$('.edit-preview-links .preview').bind('click', function()
	{
		var reviewContent = $('#reviewContent');
		var preview = $('#preview');
		$('.edit-preview-links .edit').removeClass('active');
		$(this).addClass('active');
		preview.css('height', reviewContent[0].offsetHeight - 34 + 'px');
		reviewContent.hide();
		preview.show().addClass('loading').html('Nahrávám náhled…');
		$.post("/inc/rpc_texy.php", { content: reviewContent.val() },
			function(data)
			{
				
				$('#preview').removeClass('loading').empty();
				
				var fn = function(){ $('#preview').html(data).css('height', 'auto'); }
				setTimeout( fn , 10);
				// $('#preview').html(data).css('height', 'auto');
			}
		);
		return false;
	});
	
	
	/*
	if($('.edit-preview-links .edit').size() !== 0) {
		$('a[@href]').not('.thickbox').not('[@href^=#]').bind('click', function(){
			return window.confirm("Odchodem z této stránky se ztratí rozepsaná data z formuláře.\r\nOpravdu chcete pokračovat?");
		});
	}
	*/
	
	/*
	$(window).bind('unload', function(){
		alert('haf');
		return false;
	});
	*/
	
	/*
	$('.cattree a').bind('click', function()
	{
		$('.cattree a.active').removeClass('active');
		$(this).addClass('active');
		if(	$('.user-section .col-h-r').size() == 0) $('.user-section .col-h-l').after('<div class="col-h-r"><h3><label for="select-product">B. Vyberte produkt ze seznamu:</label></h3><p id="ajax-select-hook"></p><p id="ajax-loading-hook"></p></div>');		
		$('#ajax-loading-hook').addClass('loading').html('Nahrávám seznam produktů…');
		$('.order-buttons.r input').attr('disabled', 'disabled');
		$('#ajax-select-hook').load('/tpl/product-finder.html' + $(this).attr('href'), null, function(){
			$('#ajax-loading-hook').removeClass('loading').empty();
			$('#continue').removeAttr('disabled');			
			$('.user-section .col-h-r').animateColor(2000, {backgroundColor:['#F9F900','#FFFFFF']});			
		});		
		return false;		
	});
	*/
	
	$('.navrhnout').click(function()
	{
		$('#new_product').toggle();

	});
	
	$('.close').click(function()
	{
		$('#ok_send_product').css('display','none');
		$('#error_send_product').css('display','none');
		$('#new_product').css('display','none');
	});
	
	$('.enquiry ul .canVote').each(function(){
		$(this).css('cursor', 'pointer');
		$(this).click(function(){	
			window.location = $('.vote a', this).attr('href');
		});	
	});
	
	
	$('.avatars-list').each(function(){
		var id = $('input:checked', this).attr('id');
		$('label[for="'+id+'"] img').addClass('active');
	});
	
	$('.avatars-list img', this).click(function(){
		$('.avatars-list img').removeClass('active');
		$(this).addClass('active');
		var id = $(this).parent().attr('for');
		$('input[id="'+id+'"]').trigger('click');
		return false;
	});
	
	
});


function kontrola()
{
    var text_jmena = self.document.forms.upl_pic.new_suggest_text.value;
    var je_ok = text_jmena != "";
    if (je_ok == false) alert('Zadejte návrh');
    return je_ok; 
}



