function nostalgiePage(o, n, max) {
    if(o == 1 && n > 1) {
        $("#nostalgiePrevDis").hide();
        $("#nostalgiePrev").show();
    } else if(n == 1 && o > 1) {
        $("#nostalgiePrev").hide();
        $("#nostalgiePrevDis").show();
    }
    $("#nostalgie-page-" + o).hide();
    $("#nostalgie-page-" + n).show();
    $("#nostalgiePage" + o).addClass('page');
    $("#nostalgiePage" + o).removeClass('active');
    $("#nostalgiePage" + n).addClass('active');
    $("#nostalgiePage" + n).removeClass('page');
    if(o == max && n < max) {
        $("#nostalgieNextDis").hide();
        $("#nostalgieNext").show();
    } else if(n == max && o < max) {
        $("#nostalgieNext").hide();
        $("#nostalgieNextDis").show();
    }
    return n;
}

function setupDatepicker() {
	try {
		var now = new Date();
		$("#datepicker").attr('value', zeroFill(now.getDate(), 2) + '.' + zeroFill(now.getMonth() + 1, 2) + '.' + now.getFullYear());
		$("#datepicker").datepicker({ showOn: 'button', buttonImage: '/images/cdos/cal.gif', buttonImageOnly: true });
	}
	catch (e) { }
}

function changeLinks(kraj,oblast,misto,gps)
{
	$("#detailKraj").html(kraj);
	$("#detailOblast").html(oblast);
	$("#detailMisto").html(misto);
	$("#detailGPS").html(gps);
}


$(document).ready(function() {
	$('#tabcontent').html($('#tab-1').html());

	var selected = $('#frmfilterForm-oblast').val();
	var selectedKategorie = $('#frmfilterForm-kategorie').val();
	var mapValue = $('#oblast_nazev').html();

        $(".galleryitem a").colorbox({
            'photo': true
        });

	$('.contentboxgreygrad .boxextra .tabmenu a').click(function() {
		$('.contentboxgreygrad .boxextra .tabmenu a').removeClass("over");
		var idtab = $(this).attr('href');
		$('#tabcontent').html($(idtab).html());
                    if(idtab == "#tab-4") {

                        $("#nostalgie").css('height', $("#nostalgie").height() + "px");
                        
                        $('.fancy').click(function() {
                          var hr = $(this).attr('href');
                          $.colorbox({
                            href : '' + hr + ' table',
                            width: 500,
                            initialWidth: 200,
                            initialHeight: 100,
                            title: $(this).attr('title')
                          });
                          return false;
                        });

                        var page = 1;
                        var pages = $("#nostalgiePageMax").text();

                        $("#nostalgieNext").click(function() {
                            page = nostalgiePage(page, page+1, pages)
                            return false;
                        });

                        $("#nostalgiePrev").click(function() {
                            page = nostalgiePage(page, page-1, pages)
                            return false;
                        });

                        $(".nostalgiePage").click(function() {
                            var newPage = $(this).attr('rel');
                            page = nostalgiePage(page, newPage, pages);
                            return false;
                        });

                    }
		$(this).addClass("over");
		return false;
	});

	$("#frmstanice-list").change(function(){
		$(".staniceDetail").hide();
		var id = $(this).val();
		$("#stanice" + id).fadeIn();
	});

	$("#showAllTerms").click(function(){
		$("#divAllTerms li.display-none").toggle();
		return false;
	});

	$("#tipRating").click(function() {
		if ($("#tipRatingVote").is(':hidden')) {
			$("#tipRatingDiv").attr('class', 'greenboxextra');
		}
		$("#tipRatingVote").slideToggle('slow', function() {
			if ($("#tipRatingVote").is(':hidden')) {
				$("#tipRatingDiv").attr('class', 'greenbox');
			}
		});
		return false;
	});

	$("#frmfilterForm-datum_od").datepicker( {
		showOn : 'button',
		buttonImage : '/images/cdos/cal.gif',
		buttonImageOnly : true,
                onSelect : function() {$('#frm-filterForm').submit()}
	});
	$("#frmfilterForm-datum_do").datepicker( {
		showOn : 'button',
		buttonImage : '/images/cdos/cal.gif',
		buttonImageOnly : true,
                onSelect : function() {$('#frm-filterForm').submit()}
	});
	
	// prihlasovani
    $("a.login").click(function() {
        if (typeof fillAZAKSourceApp != "undefined") {
            $('#loginbox').load(smFragmentPath + 'loginbox.htm div.subbox', fillAZAKSourceApp);
        } else {
            $('#loginbox').load(smFragmentPath + 'loginbox.htm div.subbox');
        }
        $('#loginbox').show();
    });


    // smart menu
    $("#smartmenuclose div.box a").click(function() {
        return smartMenuItemOnClick(this);
    });

    $("a.najitSpojeni").click(function(){
    	var title = $(this).attr('rel');
        $('#sm1').load(smFragmentPath + 'f1.htm #smForm', {}, function() {
            //setupIcons2($('#icf-1')[0]);
            setupDatepicker();
            setupTimeControl();
            $('#sm1 input[name=t]').val(title);
        });
        $('#sm1').show();
        //return false;
    });
    
    $('.gallerystart').click(function() {
        var $lefty = $('.galleryimagesall');
        var $l = parseInt($lefty.css('left'),10);
        var $w = $lefty.outerWidth();
        if($l < 0) {
         $lefty.animate({
          left:  $l + 144
         });
        }
        return false;
      });
    
    $('.galleryend').click(function() {
        var $lefty = $('.galleryimagesall');
        var $l = parseInt($lefty.css('left'),10);
        var $w = $lefty.outerWidth();
        if($l > -$w + 572) {
         $lefty.animate({
          left:  $l - 144
         });
        }
        return false;
      });
	$("#oblasti_cz area.clickable").hover(function(){
		var title = $(this).attr('title');
		var id = $(this).attr('alt');
		$('#oblast_nazev').html(title);
		$('#frmfilterForm-oblast').val(id);
	}, function(){
		$('#oblast_nazev').html(mapValue);
		$('#frmfilterForm-oblast').val(selected);
	});
	
	$("#typy a.katlink").hover(function(){
		var id = $(this).attr('rel');
		$('#frmfilterForm-kategorie').val(id);
	}, function(){
		$('#frmfilterForm-kategorie').val(selectedKategorie);
	});

	$("#oblasti_cz area.clickable").click(function(){
		$('#frm-filterForm').submit();
		return false;
	});
	
	$("#typy a.katlink").click(function(){
		$('#frm-filterForm').submit();
		return false;
	});
	
   	$("#showall").click(function () {
   	  $("#showall").hide();
      $("#typy a.katlink").show();
    });


});
