$(document).ready(function(){	
	bind_hover ();
	bind_search ();
	bind_anchor ();
	bind_cycle ();
	bind_buttons ();
	bind_search ();
	bind_fancybox_inpopup ();
	bind_fancybox ();
	bind_scroll ();
	bind_twitter ();
	bind_position_fixed ();

	$(window).bind('resize', function() {
		bind_buttons ();
	});
});

function bind_twitter()
{
	// Expand Panel
	$("#show").click(function(){
		$("#twitter").animate({width:'160'},350);   
		});
 
	// Collapse Panel
	$("#hide").click(function(){  		
    $("#twitter").animate({width:'0'},350);	      	    
	});	
}

function bind_position_fixed()
{
	var offset = $("body").scrollLeft();
	$("#left").css({'margin-left': '-' + offset + 'px'});
}

/**
 * hover efekt na menu
 **/
function bind_hover ()
{
	$(".menu li, .path li").hover(
		function() {
			$(this).addClass("sfhover");
		},
		function() {
			$(this).removeClass("sfhover");
		}
	);
}

/**
 * leve menu
 **/
function bind_buttons ()
{
	// nastaveni active kdyz kliknu na tlacitko
	$('.buttons ul li a').click(function() {
		$('.buttons ul li a').removeClass("active");
		$(this).addClass("active");
	});
	
	// velikost tlacitek
	
	
	$('#left').removeClass("big");
	$('#left').removeClass("medium");
	$('#left').removeClass("small");
	$('#left').removeClass("mini");
	if( $('#left').height() >= 820 ) 		$('#left').addClass("big");
	else if( $('#left').height() >= 635) 	$('#left').addClass("medium");
	else if( $('#left').height() >= 545) 	$('#left').addClass("small");
	else 									$('#left').addClass("mini");
}

/**
 * cycle banner
 **/
function bind_cycle ()
{
	$('.banner').cycle({ 
	    fx:      'fade', 
	    speed:    1000, 
	    timeout:  7000 
	});
}

function bind_fancybox_inpopup ()
{
	$("#fancybox-inner a[href^=mailto:]").live('click',function(){
        var url = $(this).attr('href');

        $.fancybox({
			'href'				: mail,
		});

		return false;
	});


	// html obsah
	$("#fancybox-inner a:not([href^=/files/])").live('click',function(){
		if ($(this).attr('target') != '_blank') {
    		var url = $(this).attr('href');

            if (!url.match('archiv','i')) {
        		$.fancybox({
        			'href'				: url,
        			'autoScale'			: true,
        			'autoDimensions'	: true,
        			'transitionIn'		: 'none',
        			'transitionOut'		: 'none',
        			'overlayOpacity'	: '0.85',
        			'overlayColor'		: '#000',
        			'margin'			: 64,
        			'padding'			: 32,
        			'centerOnScroll'	: 'true'
        		});

    		    return false;
    		}
        }
	});
	
	// obrazky
	$("#fancybox-inner a[href^=/files/]").live('click',function(){
	
		var url = $(this).attr('href');
			
		$.fancybox({
			'href'				: url,
			'autoScale'			: true,
			'autoDimensions'	: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'overlayOpacity'	: '0.85',
			'overlayColor'		: '#000',
			'margin'			: 64,
			'padding'			: 32,
			'centerOnScroll'	: 'true'
		});
	
		return false;
	});
}

/**
 * fancybox
 * http://fancybox.net/ 
 **/
function bind_fancybox ()
{	
    $('.mmenu ul li a').each(function(){
        $(this).attr('href', $(this).attr('href')+"?layout=articles");
    })
    
	// Nacte odkaz
	$(".mmenu ul li a,.content a:not([href^=/files]):not([href^=http]),.pager a").fancybox({
		'autoScale'			: true,
		'width'				: '75%',
		'height'			: '90%',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: '0.85',
		'overlayColor'		: '#000',
		'margin'			: 64,
		'padding'			: 32,
		'centerOnScroll'	: 'true'		
	});
	
	// Nacte odkaz
	$(".content a[href^='/files']").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'overlayOpacity'	: '0.85',
		'overlayColor'		: '#000',
		'margin'			: 64,
		'padding'			: 32,
		'centerOnScroll'	: 'true'		
	});
	

	// pridani loga
	$('#fancybox-outer').append('<img src="/img/fancybox-logo.png" class="fancybox-logo" alt="Arkadia" />');
	
	// pridani motta
	$('#fancybox-outer').append('<img src="/img/fancybox-motto.png" class="fancybox-motto" alt=""/>');
}

function bind_search()
{
    $(".search-input").Watermark("hledat výraz");
        
	$('.search-input').parent().submit(function(){			
		$.ajax({
			type	: "GET",
			cache	: false,
			url		: "/vyhledavani/?q="+$('.search-input').val()+"&layout=empty",
			data	: $(this).serializeArray(),
			success: function(data) {
			
				$.fancybox({
					'content'			: data,
					'width'				: '75%',
					'height'			: '90%',
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'type'				: 'html',
					'overlayOpacity'	: '0.85',
					'overlayColor'		: '#000',
					'margin'			: 64,
					'padding'			: 32,
					'centerOnScroll'	: 'true'
				});
			}
		});
		return false;
	});
	
	$('#search form').live('submit',function(){
		$.ajax({
			type	: "GET",
			cache	: false,
			url		: "/vyhledavani/?q="+$('#search input').val()+"&layout=empty",
			data	: $(this).serializeArray(),
			success: function(data) {			
				$.fancybox({
					'content'			: data,
					'width'				: '75%',
					'height'			: '90%',
					'autoScale'			: false,
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'type'				: 'html',
					'overlayOpacity'	: '0.85',
					'overlayColor'		: '#000',
					'margin'			: 64,
					'padding'			: 32,
					'centerOnScroll'	: 'true'
				});
			}
		});
		return false;
	});
}

jQuery.fn.anchorAnimate = function (settings) {

    settings = jQuery.extend({
        speed: 800
    }, settings);

    return this.each(function () {
        var caller = this;
        $(caller).click(function (event) {
			$('.buttons li').removeClass('active');
			$(caller).parent().addClass('active');
	
            event.preventDefault()
            var locationHref = window.location.href
            var elementClick = $(caller).attr("href")

            var destination = $(elementClick).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: destination
            }, settings.speed, function () {
                window.location.hash = elementClick
            });

            return false;
        })
    })
}

/**
 * anchor
 **/
function bind_anchor ()
{
    if ($("a.anchorLink").length > 0)
	    $("a.anchorLink").anchorAnimate();
}



function bind_scroll()
{
	$('.content').hover(function(){
		a = $(this).prev().prev().attr('name');
		$('.buttons li').removeClass('active');
		$('.buttons a[href=#'+a+']').parent().addClass('active');
	});
	
	$('#banner').hover(function(){
		$('.buttons li').removeClass('active');
		$('.buttons a[href=#nahoru]').parent().addClass('active');
	});
}


