﻿/***************************
*  MillersShow (Slideshow For Miller's Pro)
*  requires: jQuery
*  author: abe[at]lanit[dot]com     
****************************/
$.fn.loadImage = function(src,navigateUrl,altText, f){
    return this.each(function(){                        
        $("<img>").appendTo(this).load( f ).attr("src", src).attr("alt",altText);
        if (navigateUrl!=null && navigateUrl.length>0){
            $(this).find("img").wrap("<a href='" + navigateUrl + "'></a>");
        }
    });
};
var MillersShow = {
    settings : {        
        slideTimeout : 5000,
        slideFadeOutTime : 2000,
        clickFadeOutTime : 500,
        slideIntervalId : null,
        pcontIntervalId : null,
        pcontOpacity : 0.85,
        pcontFadeInTime : 500,
        pcontFadeOutTime : 750,
        pcontFadeOutWait : 1500
    },
    isPaused : false,
    inTransition : false,        
    imageLoading : false,
    previousIndex : 0,
    numberOfImages : 0,      
    init : function(){       
        ms$.numberOfImages = $("#slide-container li").length;
        ms$.loadImagesComplete();        
    },
    hidePhotoControl : function(){
        var pcont = $("#slideshow-control");
        if (pcont.css('display')=='block') {
            pcont.fadeOut(ms$.settings.pcontFadeOutTime);
        }
    },
    showPhotoControl : function(){        
        window.clearTimeout(ms$.settings.pcontIntervalId);            
        var pcont = $("#slideshow-control");
        if (pcont.css('display')=='none'){                
            if (pcont[0].style.filter!=null){ // ie                    
                pcont[0].style.filter = 'alpha(opacity=0)';                    
            }
            pcont.css('opacity',0);
            pcont.css('display','block');
            pcont.fadeTo(ms$.settings.pcontFadeInTime, ms$.settings.pcontOpacity);
        }
    },    
    findCurrentIndex : function() {
        var li = $("#lb .photo li");
        // get previous index
        for (var i=0;i<li.length;i++){
            var img = $(li[i]).find("img");
            if (img.length>0){
                img.attr('imgLoaded', 'true');
                return i;
            }
        }        
        return 0;
    },
    getNextIndex : function(currentIndex, direction){
        if (direction=='back'){
            return (currentIndex==0) ? ms$.numberOfImages-1 : currentIndex-1;
        } else {
            return (currentIndex==ms$.numberOfImages-1) ? 0 : currentIndex+1;
        }
    },
    loadNextImage : function(nextIndex,count){
        
        if (!count){
            count=0;
        }
        
        if (ms$.imageLoading){            
            //setTimeout("ms$.loadNextImage(" + nextIndex + "," + count + ")", 3000);
            return;
        }
        
        var spanInfo = $("#slide" + nextIndex + " span");
        if (spanInfo.find("img").length == 0 && spanInfo.attr('imgLoaded')!='true'){
            
            ms$.imageLoading = true;
            $("#slideshow-control a").hide();
            $("#slideshow-control .loading").show();    
            
            spanInfo.loadImage(spanInfo.attr('imageUrl'), spanInfo.attr('navigateUrl'), spanInfo.attr('alternateText'), function(){
                
                spanInfo.attr('imgLoaded', 'true');
                ms$.imageLoading = false;
                $("#slideshow-control a").show();
                $("#slideshow-control .loading").hide();
            });
        }
        
    },
    loadImagesComplete : function(){
        
        ms$.setNextWait();
        
        /* Bind events */
        $("#slideshow").bind("mouseover",function(e){
            ms$.showPhotoControl();
        });
        $("#slideshow").bind("mouseout",function(e){
            ms$.settings.pcontIntervalId = window.setTimeout(ms$.hidePhotoControl, ms$.settings.pcontFadeOutWait);
        });
        $("#slideshow-control").bind("mouseover", function(e){
            ms$.showPhotoControl();
        });               
        $("#slideshow a.pause").click(function(){
            ms$.togglePlay(); 
            this.blur(); 
            return false;
        });
        $("#slideshow a.forward").click(function(){
            ms$.next('forward', true);
            this.blur();
            return false;
        });
        $("#slideshow a.back").click(function(){
            ms$.next('back', true);
            this.blur();
            return false;
        });
    },
    setNextWait : function(timeout, direction, isClick){
        if (timeout==null) {
            timeout = ms$.settings.slideTimeout;
        }
        if (direction==null){
            direction = 'forward';
        }
        window.clearTimeout(ms$.settings.slideIntervalId);
        if (ms$.isPaused && !isClick){            
            return;
        }
        ms$.settings.slideIntervalId = window.setTimeout(function(){
            ms$.next(direction,isClick);
        }, timeout);
    },
    next : function(direction, isClick) {            
        if (ms$.inTransition) return;
        
        // determine current index
        var nextIndex = ms$.getNextIndex(ms$.previousIndex,direction);                
        var spanInfo = $("#slide" + nextIndex + " span");
        
        if (spanInfo.attr('imgLoaded')=='true'){
            ms$.doTransition(ms$.previousIndex, nextIndex, isClick);            
        } else {
            ms$.loadNextImage(nextIndex);            
            ms$.setNextWait(500, direction, isClick);
        }        
    },  
    doTransition : function (previousIndex,nextIndex, isClick) {                
        ms$.inTransition = true;
        var prev = $("#slide" + previousIndex);
        var next = $("#slide" + nextIndex);  
        next.attr('style', '');
        prev[0].style.zIndex = 55; // setting values using DOM for ie
        next[0].style.zIndex = 50;
        next[0].style.display = 'block';        
        var fadeOutTime = (isClick) ? ms$.settings.clickFadeOutTime : ms$.settings.slideFadeOutTime;
        prev.fadeOut(fadeOutTime, function(){
            ms$.setNextWait();
            ms$.inTransition = false;         
        });
        ms$.previousIndex = nextIndex;
    },
    togglePlay : function(){                
        ms$.isPaused = !ms$.isPaused;
        if (!ms$.isPaused){
            $("#slideshow a.play")[0].className='pause';            
        } else {            
            $("#slideshow a.pause")[0].className='play';
        }
        ms$.setNextWait();
    }
};
var ms$ = MillersShow;
$(window).load(function(){
    ms$.init();
});