$(document).ready(function(){
    
    /* --------------------------------------- */
    /* --( Fancybox )-- */
    /* --------------------------------------- */

    $('a.fancybox').fancybox({
        overlayOpacity: 0.5,
        overlayColor: '#000',
        scrolling: 'no',
        onStart: openHandler,
        onComplete: openHandler,
        onCleanup: closeHandler
    });
    
    function openHandler(elem){
        var id = $(elem).attr('href');
        var jw = $jw($(id + ' object').attr('id'));
        
        jw.play(true);
    }
    
    function closeHandler(elem){
        var id = $(elem).attr('href');
        var jw = $jw($(id + ' object').attr('id'));
        
        jw.pause(true);
    }
    
    /* --------------------------------------- */
    /* --( Homepage Header Code)-- */
    /* --------------------------------------- */
    
    if ($('body').hasClass('page-template-homepage-page-php'))
    {
        var headerSlideshow = {
            
            delay           : 5000,
            timer           : null,
            rootImgDir      : '/wp-content/themes/majid/images/',
            images          : ['header_photo_1.jpg', 'header_photo_2.jpg', 'header_photo_3.jpg', 'header_photo_4.jpg'],
            currentImgIndex : 0,
            
            init : function() {
                this.startTimer();
            },

            startTimer : function () {
                var parentObj = this;
                this.timer = setTimeout(function(){parentObj.loadNextImage()}, this.delay);
            },

            loadNextImage : function () {
                var parentObj = this;
                
                this.currentImgIndex = (this.currentImgIndex + 1 < this.images.length) ? this.currentImgIndex + 1 : 0;
                
                var img = new Image();
                
                $(img)
                    .load(function() {
                        
                        var container = $('#header div.photo_bg');
                        
                        $(this).hide();
                        
                        container.append($(this));
                        
                        $(this).fadeIn(1000, function(){
                            
                            container.children('img.current').remove();
                            
                            $(this).addClass('current');
                            
                            parentObj.startTimer();
                            
                        })
                        
                    })
                    .attr('src', this.rootImgDir + this.images[this.currentImgIndex]);
            }
            
        }
        
        headerSlideshow.init();
    }
    
})
