swfReady=false;
function setSWFIsReady(){
    swfReady=true;
    thisMovie("background_swf").setOpening(is_gallery);
    if(is_gallery){
        jQuery('#mycarousel a:first').click();
    }else{
        if(is_home){
            thisMovie("background_swf").setImageList(imageListHome);
            thisMovie("background_swf").playAudio();
        }else{
            thisMovie("background_swf").setImageList(imageList0);
        }
    }
}


jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	return this;
};

function thisMovie(movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
 }
/**
 * This is the callback function which receives notification
 * about the state of the next button.
 */
function mycarousel_buttonNextCallback(carousel, button, enabled) {
    display('Next button is now ' + (enabled ? 'enabled' : 'disabled'));
};

/**
 * This is the callback function which receives notification
 * about the state of the prev button.
 */
function mycarousel_buttonPrevCallback(carousel, button, enabled) {
    display('Prev button is now ' + (enabled ? 'enabled' : 'disabled'));
};

/**
 * This is the callback function which receives notification
 * right after initialisation of the carousel
 */
function mycarousel_initCallback(carousel, state) {
    if (state == 'init')
        display('Carousel initialised');
    else if (state == 'reset')
        display('Carousel reseted');
};

/**
 * This is the callback function which receives notification
 * right after reloading of the carousel
 */
function mycarousel_reloadCallback(carousel) {
    display('Carousel reloaded');
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
    display('Item #' + idx + ' is now the first item');
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 */
function mycarousel_itemFirstOutCallback(carousel, item, idx, state) {
    display('Item #' + idx + ' is no longer the first item');
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 */
function mycarousel_itemLastInCallback(carousel, item, idx, state) {
    display('Item #' + idx + ' is now the last item');
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 */
function mycarousel_itemLastOutCallback(carousel, item, idx, state) {
    display('Item #' + idx + ' is no longer the last item');
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered before animation.
 */
function mycarousel_itemVisibleInCallbackBeforeAnimation(carousel, item, idx, state) {
    // No animation on first load of the carousel
    if (state == 'init')
        return;

    jQuery('img', item).fadeIn('slow');
};

/**
 * This is the callback function which receives notification
 * when an item becomes the first one in the visible range.
 * Triggered after animation.
 */
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state) {
    display('Item #' + idx + ' is now visible');
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 * Triggered before animation.
 */
function mycarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state) {
    jQuery('img', item).fadeOut('slow');
};

/**
 * This is the callback function which receives notification
 * when an item is no longer the first one in the visible range.
 * Triggered after animation.
 */
function mycarousel_itemVisibleOutCallbackAfterAnimation(carousel, item, idx, state) {
    display('Item #' + idx + ' is no longer visible');
};
/**
 * Helper function for printing out debug messages.
 * Not needed for jCarousel.
 */
var row = 1;

function display(s) {

};





function singlePage(){
    jQuery('#mycarousel').jcarousel({
        scroll: 3,

        initCallback:   mycarousel_initCallback,
        reloadCallback: mycarousel_reloadCallback,

        buttonNextCallback:   mycarousel_buttonNextCallback,
        buttonPrevCallback:   mycarousel_buttonPrevCallback,

        itemFirstInCallback:  mycarousel_itemFirstInCallback,
        itemFirstOutCallback: mycarousel_itemFirstOutCallback,
        itemLastInCallback:   mycarousel_itemLastInCallback,
        itemLastOutCallback:  mycarousel_itemLastOutCallback,
        itemVisibleInCallback: {
            onBeforeAnimation: mycarousel_itemVisibleInCallbackBeforeAnimation,
            onAfterAnimation:  mycarousel_itemVisibleInCallbackAfterAnimation
        },
        itemVisibleOutCallback: {
            onBeforeAnimation: mycarousel_itemVisibleOutCallbackBeforeAnimation,
            onAfterAnimation:  mycarousel_itemVisibleOutCallbackAfterAnimation
        }


    });
    
    
    
    jQuery(document).keyup(function(event){
        if(event.keyCode == 37){
            jQuery('#images_nav #prev').click();
        }
        else if(event.keyCode == 39){
            jQuery('#images_nav #next').click();
        }
    });
        
    
    
    
    jQuery('#images_nav #prev').click(function(){
        if(jQuery(this).hasClass('active')){
            var newindex = index-1;
            jQuery("#mycarousel .jcarousel-item:eq("+ newindex +")").children("a").click();
        }
    });
    jQuery('#images_nav #next').click(function(){
        if(jQuery(this).hasClass('active')){
            var newindex = index+1;
            jQuery("#mycarousel .jcarousel-item:eq("+ newindex +")").children("a").click();
        }
    });






    jQuery('#mycarousel .jcarousel-item a').click(function(){
        jQuery('#images_nav:hidden').show();
    
    
        var text = jQuery(this).parent().children().filter('dd:first').html();
        var len = jQuery('#mycarousel').children('li').length;
        index = jQuery("#mycarousel li.jcarousel-item").index(jQuery(this).parent());

        if(index>0){
            jQuery('#images_nav #prev').addClass('active');
        }else{
            jQuery('#images_nav #prev').removeClass('active');
        }
        if(index<len-1){
            jQuery('#images_nav #next').addClass('active');
        }else{
            jQuery('#images_nav #next').removeClass('active');
        }
        
        jQuery('#footer #text').html(text);
        
        if(swfReady){
            thisMovie("background_swf").setImage(jQuery(this).attr('href'));
            return false;
        }else{
            return true;
        }
    });


    currentSlide=0;//indice immagine corrente
    index=0;//indice tasto selezionato (galledrymenu)
    slideInterval="";// id del setTimeouu dello slideshow
    transition=false;// variabile che disce se  in corso un atransizione (caricamento immagine e fadeIn)
    slider_playing=false;// dice se c' uno slishow in corso
    
    var py=0;
    jQuery().mousemove(function(e){
        py= e.pageY;
    });


    var over="1";

    
    
    jQuery('#ico_thumb').hide();     
    jQuery('.single').mouseout(function () {
        over='';
        jQuery(this).delay(300,function(){
            if(over==''){
                //jQuery('#ico_thumb').show();
                //jQuery('.entry').fadeOut(300);
                /*jQuery('.post').stop().animate( { opacity:0 } , 300, 'swing');
                jQuery('.jcarousel-container').stop().animate( { opacity:0 } , 300, 'swing');*/
                jQuery('.jcarousel-container').slideUp(300);             
            }
        });
    });
    jQuery('.single').mouseover(function () {
        over='1';
        jQuery('.jcarousel-skin-base').stop(true,true);
        jQuery(this).delay(300,function(){
            if(over=='1'){
                //jQuery('#ico_thumb').hide();
                //jQuery('.entry').fadeIn(300);
                /*jQuery('.post').stop().animate( { opacity:1 } , 300, 'swing');
                jQuery('.jcarousel-container').stop().animate( { opacity:1 } , 300, 'swing');*/
                jQuery('.jcarousel-container').slideDown(300);
            }
        });
    });

    jQuery(this).delay(3000,function(){
        if((py<100)&&(py>270)){over=''; jQuery('.jcarousel-container').mouseout();}
    });
    
    jQuery('#imagemenu a').click(function(){
        if(!transition){
            stop_timer();
            thisMovie("background_swf").setImageList(jQuery(this).attr('list'));
            imageList = jQuery(this).attr('list').split(',');
            jQuery('#incipit').attr('src', 'images/'+jQuery(this).attr('id')+'.png');
            jQuery(this).parent().siblings().removeClass('selected');
            jQuery(this).parent().addClass('selected');
            index = jQuery("#imagemenu li").index(jQuery(this).parent());
            animatemenu();
         }
         return false;
    });
   
   

    function newBackground(){
            if(index!=-1){
                jQuery('#control').show();
            }
            stop_timer();
            currentSlide=-1;
            slider_playing=false;
            jQuery('#control').click();
    }
    
    function newImage(){
              if(currentSlide < imageList.length-1){
                currentSlide++;
                lastSlide = currentSlide-1;
              } else {
                currentSlide=0;
                lastSlide = imageList.length-1;
              }
              jQuery('#load').fadeIn('normal');
              var toLoad = imageList[currentSlide];
              var img = new Image();
              transition=true;
              jQuery(img)
                // once the image has loaded, execute this code
                .load(function () {
                  jQuery(".backgroundimage").clone()
                  .prependTo("body")
                  .removeClass('backgroundimage')
                  .addClass('backgroundimage_old')
                  .css({'z-index': '-12'});

                  
                  jQuery('.backgroundimage').attr("src", toLoad);
                  jQuery('.backgroundimage').maxImage({isBackground: true,verticalAlign: 'bottom',leftSpace: '1'});
                  jQuery('.backgroundimage').fadeIn(700, function(){jQuery('.backgroundimage_old').remove();});
                  
                  if(slider_playing){
                     //slideInterval = setTimeout(newImage, (5000));
                  }
                  jQuery('#load').hide();
                  slider_playing=true;
                  transition=false;
                  
                })
                .error(function () {
                  // notify the user that the image could not be loaded
                })
                .attr('src', toLoad).css({'display':'none'});
            }
    
    function stop_timer() {
        if(slideInterval!=""){
           clearTimeout(slideInterval);
        }
    }

    
    jQuery('#control').click(function(){
        stop_timer();
       if(!slider_playing){
            slider_playing=true;
            //newImage();
            jQuery('#control img').attr("src", 'css/pause.gif');
            if(swfReady){
                thisMovie("background_swf").playSlideSHow();
            }
       }else{
            slider_playing=false;
            jQuery('#control img').attr("src", 'css/play.gif');
            if(swfReady){
                thisMovie("background_swf").stopSlideSHow();
            }
       }
       return false;
    });
}


jQuery(document).ready(function() { 
    if(is_gallery){
        singlePage();
    } 
    if(add_swfbackground){
        var so = new SWFObject(themefolder+"/bg.swf?200912", "background_swf", "100%", "100%", "9", "#000000");
        so.addParam("movie", themefolder+"/bg.swf");
        so.addParam("quality", "high");
        so.addParam("quality", "high");
        so.addParam("wmode", "transparent");
        //so.addVariable("audiointro", themefolder+'/vocals_2.mp3');
        so.addVariable("audiointro", "");
        so.addParam("allowscriptaccess", "always");
        so.useExpressInstall(themefolder+'/expressinstall.swf');
        so.write("backgroundimage");
    }
});