$(function() {
    // odkazy s class="external" otevreme v novem okne
    $('a.external').click(function() {
      if (window.open(this.href)) {
        return false;
      }
    });
    
    // lightbox
    $("a[rel^=lightbox]").lightBox(); 
    
    // zakazani double submitu u formularu
    $('form').preventDoubleSubmit();
    
    // CRAM login form
    $('#cram_form').submit(function() {
        var digest = SHA256($('#cram_token').val() + SHA256($('#cram_salt').val() + $('#password').val()));
        $('#cram_digest').val(digest);
        $('#password').val('');
    });
    $('#cram_form input').focus(function() {
    	$(this).val('');
    });
        
    // ankety
    $('.poll .answer a').each(function () {
    	$(this).click(function () {
    		$.ajax({
				type: 'POST',
				url: this.href,
				data: {
					sent: '1'
				},
				dataType: 'html',
				success: function (data, poll) {
					if (data != null && data != '') {
						$('.poll').html(data);
					}
				}
			});
    		$('.poll').html('<img src="' + BASE_URL + '/images/spinner.gif" alt="" />');
    		return false;
    	});	
    });
    
    // aktuality
    var novinky = $('#aktuality .novinka');
    if (novinky.length > 1) {
	    novinky.each(function() {
	    	$(this).hide();
	    });        
	    if (novinky.length > 0) {
	    	novinkyAnimation(0, "show");
	    }
    }
});

var novinkyAnimation = function(index, action) {
	var novinky = $('#aktuality .novinka');
	if (index >= novinky.length) {
		index = 0;
	}
	
	if (action == "show") {
		$(novinky[index]).show("slow");
		setTimeout('novinkyAnimation('+index+', "hide")', 5000);
	}
	else {
		$(novinky[index]).hide("slow");
		index++;
		setTimeout('novinkyAnimation('+index+', "show")', 500);
	}
};