$(document).ready(function() {
	
    // Open links in new window
    $("a[rel='external']").click(function(){
	window.open(this.href); return false;
    });

    // Focus text for searchbox
    $("#searchstring").focus(function(){
	if ($(this).val() == $(this)[0].title){
	    $(this).removeClass("TextActive");
	    $(this).val("");
	}
    });
    $("#searchstring").blur(function(){
	if ($(this).val() == ""){
	    $(this).addClass("TextActive");
	    $(this).val($(this)[0].title);
	}
    });
    $(".Text").blur();

    // Navigation
    $('#nav li').hover(function() {
	$(this)
	.find('ul:first')
	.stop(true, true)
	.slideDown(100);
    }, function() {
	$(this)
	.find('ul:first')
	.stop(true,true)
	.slideUp(20);
    });

    $('#nav li').mouseover(function() {
	$(this).addClass('hover');
    });

    $('#nav li').mouseout(function() {
	$(this).removeClass('hover');
    });

    // Search-box hidding :( ??
    $('#search-box').hide();
    $("#site-nav li.find a").click(function(){
	if ($("#search-box").is(':hidden'))
	    $("#search-box").show();
	else{
	    $("#search-box").hide();
	}
	return false;
    });

    $('#search-box').click(function(e) {
	e.stopPropagation();
    });
    $(document).click(function() {
	$('#search-box').hide();
    });

    // Calendar
    
    $(".previous-m").live('click',function(){
	var year = $('#year').val();
	var month = $('#month').val();
	if(month == 1){
	    month = 12;
	    year -= 1;
	}else
	    month -= 1;
	$.post('/modules/calendar/calendar.php', {
	    print: true,
	    month: month,
	    year: year
	},
	function(data){
	    //$('#calendar').remove();
	    //$('#calendar').append(data);
	    $('#calendar').html(data);
	});
    });
    $(".next-m").live('click',function(){
	var year = $('#year').val();
	var month = $('#month').val();
	if(month == 12){
	    month = 1;
	    year = parseInt(year,10) + 1;
	}else
	    month = parseInt(month,10) + 1;
	$.post('/modules/calendar/calendar.php', {
	    print: true,
	    month: month,
	    year: year
	},
	function(data){
	    //$('#calendar').remove();
	    //$('#calendar').append(data);
	    $('#calendar').html(data);
	});
    });

    // Antispam
    if($('#antispamblock').length){
	$('#antispam').val('777');
	$('#antispamblock').hide();
    }

    // Send to friend
    $("#friend").fancybox({
	'width'			: 500,
	'height'		: 325,
	'autoScale'		: false,
	'transitionIn'	: 'none',
	'transitionOut'	: 'none',
	'type'			: 'iframe',
	'titleShow'		: false,
	'scrolling'		: 'no'
    });

    // Activate Selectbox
    if($('.selectbox').length){
	$('.selectbox').sSelect();
    }

    // Fancybox
    if ($('a[rel*=group]').length) {
	$("a[rel*=group]").fancybox({
	    'titlePosition': 'over',
	    'titleFormat': function(title, currentArray, currentIndex, currentOpts){
		return '<span id="fancybox-title-over">Obrázek ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
	    }
	});
    }

    // Fancybox IE
    if ($.browser.msie) {
	if ($('a[rel*=group]').length) {
	    $("a[rel*=group]").fancybox({
		'titlePosition': 'over',
		'transitionIn': 'none',
		'transitionOut': 'none',
		'titleFormat': function(title, currentArray, currentIndex, currentOpts){
		    return '<span id="fancybox-title-over">Obrázek ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	    });
	}
    }

});//End document ready

// Slideshow
function slideSwitch() {
    var $active = $('#slideshow div.active');

    if ( $active.length == 0 ) $active = $('#slideshow div:last');


    var $next =  $active.next().length ? $active.next()
    : $('#slideshow div:first');

    $active.addClass('last-active');

    $next.css({
	opacity: 0.0
    })
    .addClass('active')
    .animate({
	opacity: 1.0
    }, 1000, function() {
	$active.removeClass('active last-active');
    });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});
