$(function(){


        /**
         * --------------------------------------------------------------------
         * links
         * --------------------------------------------------------------------
         */
        $("a:not([href^='http://www.fonia.cz'])").attr('target','_blank').addClass("external");
        $("a[href^='mailto:']").attr('target','').removeClass("external");
        $("a[href^='javascript:']").attr('target','').removeClass("external");
        $("a[href^='skype:']").attr('target','').removeClass("external");
        $("a[href^='/']").attr('target','').removeClass("external");
        $("a[href^='#']").attr('target','').removeClass("external");
        $("a[href^='..']").attr('target','').removeClass("external");
        $("a[href='']").attr('target','').removeClass("external");
        $(".typotheme a[href$='.mp3']").addClass("audio");
        
        $(".articles .navigation").each(function(){
            if($(this).find(".alignleft *").length == 0
            && $(this).find(".alignright *").length == 0) {
                $(this).hide();                
            }
        });
        
        
        /**
         * --------------------------------------------------------------------
         * lightbox
         * --------------------------------------------------------------------
         */
        $(function(){
            $("a[rel=lightbox], .typotheme a:has(img)[href$='.jpg']").lightBox({
                overlayOpacity: 0.6,
                containerResizeSpeed: 350,
                imageLoading: dirClientside+'/lib/jquery.lightbox/lightbox-ico-loading.gif',
                imageBtnClose: dirClientside+'/lib/jquery.lightbox/lightbox-btn-close.gif',
                imageBtnPrev: dirClientside+'/lib/jquery.lightbox/lightbox-btn-prev.gif',
                imageBtnNext: dirClientside+'/lib/jquery.lightbox/lightbox-btn-next.gif',
                imageBlank: dirClientside+'/lib/jquery.lightbox/lightbox-blank.gif'
            });
        });


        /**
         * --------------------------------------------------------------------
         * navigation
         * --------------------------------------------------------------------
         */
        $('#header .navigation ul li').mouseover(function(){
            $(this).find('ul').show();
        });
        
        $('#header .navigation ul li').mouseout(function(){
            $(this).find('ul').hide();
        });


        /**
         * --------------------------------------------------------------------
         * audio player
         * --------------------------------------------------------------------
         */
        var audioPlayer = $("#jplayer");
        var audioFileLinks = $(".typotheme a[href$='.mp3']");
        
        audioPlayer.jPlayer({
            ready: function() {
                audioFileLinks.click(handleAudioLink);
            },
            swfPath: dirClientside + '/lib/jquery.jplayer'
        });
        
        function handleAudioLink() 
        {
              if($(this).hasClass("playing")) {
                  audioPlayer.setFile('');
                  $(this).removeClass("playing");
              } else {
                  audioFileLinks.removeClass("playing");
                  $(this).addClass("playing");
                  var musicFile = $(this).attr('href');
                  audioPlayer.setFile(musicFile).play();
              }
              return false;
        }
        
        
        /**
         * --------------------------------------------------------------------
         * carusel
         * --------------------------------------------------------------------
         */
        function carusel(carusel)
        {
                var slideshowImagesCount = carusel.find("ul li").length;
                var slideshowObject = carusel.find("ul");
                var width = carusel.find("ul li:first img").width();
                var prevButton = carusel.find(".prev");
                var nextButton = carusel.find(".next");
                var slideshowIndex = 0;
                var direction = "right";

//                if(slideshowImagesCount > 3) {
//                        prevButton.show();
//                        nextButton.show();
//                } else {
//                        prevButton.hide();
//                        nextButton.hide();
//                }

                prevButton.hide();
                nextButton.hide();

                function moveRight()
                {
                        if(slideshowIndex < slideshowImagesCount-4) {
                                slideshowObject.animate({"left": "-="+width+"px"},1500,"swing");
                                slideshowIndex++;
                        }
                        return false;
                }

                function moveLeft()
                {
                        if(slideshowIndex > 0) {
                                slideshowObject.animate({"left": "+="+width+"px"},1500,"swing");
                                slideshowIndex--;
                        }
                        return false;
                }
                
                function slideshow()
                {
                        if(slideshowIndex == slideshowImagesCount-4) {
                                direction = "left";
                        } else if(slideshowIndex == 0) {
                                direction = "right";
                        }
                        if(direction=="right") {
                                moveRight();
                                slideshowObject.animate({"left": "-=0px"},2000,"swing", slideshow);
                        } else {
                                moveLeft();
                                slideshowObject.animate({"left": "-=0px"},2000,"swing", slideshow);
                        }
                }

                function stopSlideshow()
                {
                        slideshowObject.stop(true);

                        nextButton.click(function(){
                                return moveRight()
                        });
                        prevButton.click(function(){
                                return moveLeft()
                        });
                }

                function startSlideshow()
                {
                        slideshowObject.animate({"left": "-=0px"},2000,"swing", slideshow);
                }

                nextButton.mouseover(function(){
                        stopSlideshow()
                });
                nextButton.mouseout(function(){
                        startSlideshow()
                });
                
                prevButton.mouseover(function(){
                        stopSlideshow()
                });
                prevButton.mouseout(function(){
                        startSlideshow()
                });

                startSlideshow();
        }
        carusel($(".carusel"));

        /**
         * --------------------------------------------------------------------
         * ie fixes
         * --------------------------------------------------------------------
         */
         
         


});


