/*

	Supersized - Fullscreen Slideshow jQuery Plugin
	Version : 3.2.4
	Theme 	: AVCI 1.0

*/

var api;
var background;
var videoSlides;
var hidden = false;

(function($){
	
	theme = {
	 	
	 	
	 	/* Initial Placement
		----------------------------*/
	 	_init : function(){
			
            if($('.background-control').length){
                    
                var slides = new Array();
                $('.background-control a img').each(function(){    
                    slides.push($(this).attr('class'));
                });
                
                $('.thumb-list a').click(function(e){
                    e.preventDefault();
                    
                    var show = $(this).find('img').attr('class');
                    
                    /* if thumbnail & current slide are the same do nothing! */
                    if(vars.current_slide != slides.findIndex(show)){
                        api.goTo(slides.findIndex(show) + 1);    
                    }
                });  
            }
            
            /* iterate through slides and add video titles in an array */
            videoSlides = new Array();
            
            $.each(api.options.slides, function(){
                if((this.title).split('_')[0] == 'video') {
                    videoSlides.push(this.title);
                }
            }); 
            
	 	},
	 	
	 	
	 	/* Go To Slide
		----------------------------*/
	 	goTo : function() {
            
            if(api.options.slides[vars.current_slide].title.split('_')[0] == 'video') {

                onVideoEnd();    
                //api.playToggle('play');
            }
		},
	 	
	 	/* Play & Pause Toggle
		----------------------------*/
	 	playToggle : function(){
	 		
	 	},
	 	
	 	
	 	/* Before Slide Transition
		----------------------------*/
	 	beforeAnimation : function(){
            if(api.options.slides[vars.current_slide].title.split('_')[0] == 'video'){
                
                $('#video-controls').fadeIn(250);
                
                if(!vars.is_paused){
                    //api.playToggle();    
                }
                
                $('#supersized').fadeOut(250,function(){
                    background.start();
                    background.playMovieByNum(videoSlides.findIndex(api.options.slides[vars.current_slide].title) + 1);
                    $(this).css({
                        'z-index': -1001
                    }).show();   
                    hidden = true;                 
                }); 
            }
            else {
                
            }
	 	},
	 	
	 	/* After Slide Transition
		----------------------------*/
	 	afterAnimation : function(){
            
            if(api.options.slides[vars.current_slide].title.split('_')[0] == 'video' && background != undefined){                                
                
                if(!vars.is_paused){
                    
                    api.playToggle();    
                }
                
            }
            else {
                if(hidden) {
                    $('#supersized').hide().css({
                    'z-index' : -999
                    }).fadeIn(250,function(){
                        hidden = false;    
                    });    
                }
                
            }
	 	}
        
	 };
})(jQuery);

Array.prototype.findIndex = function(value){
    var ctr = "";
    for (var i=0; i < this.length; i++) {
        // use === to check for Matches. ie., identical (===), ;
        if (this[i] == value) {
            return i;
        }
    }
        return ctr;
};

function onVideoEnd() {

    if(videoSlides.length == 1 && api.options.slides.length == 1) {
         
         background.start(); 
         background.playMovieByNum(); 
         
         $('#video-controls').fadeIn(250);  
    }
    
    else {
        background.stop();
    }
    
    $('#video-controls').fadeOut(250, function(){
        $('#play-button').hide();
        $('#pause-button').show();
        $('#mute-button').removeClass('mute');
    });
    
    api.playToggle();    
}
