$(document).ready(function () {
	var ins = $('.upload-images input'),
		index = 1,
		inputActive = $('.upload-images input:eq('+(index - 1)+')'),
		nextImage = $('.upload-images .next-image a');
	
	// hide inputs with index higher than 0
	$(".upload-images input:gt(0)").hide();
	$('.upload-images br:gt(0)').remove();

    // show next image button
    $('.upload-images .next-image').show();

    // set href attribute to the anchor
    nextImage.attr('href', "#img-2");

	// add new item event
	$('.upload-images a.add').live('click', function () {

        // get index
		var last = $('.upload-images input:visible').length + 1;

        // cache active input
		inputActive = $('.upload-images input:hidden:eq(0)');

		if (last == 5) {
		    // hide it, if it's fifth input
		    nextImage.hide();
		}
		else {
			// update next image button
            nextImage.attr('href', '#img-'+(last+1));
		}

		// add remove button
		inputActive.after('<br class="'+last+'">')
	.after('<a class="rm '+last+'" title="Odstranit '+last+'. pole" href="#rm-img-'+last+'"><span>Odstranit</span></a>');

		// show next input
		inputActive.slideDown();
	});

	// rm event
	$('.upload-images a.rm').live('click', function () {

	    // get index
		var href =  $(this).attr('href');
		var rmIndex = Number(href.match(/\d/));

        // hide button, and input
        $(this).hide().prev().slideUp();
        // hide break
        $(this).next().hide();

		// show add button if it is hidden
	    $('a.add:hidden').show();
	});
});

