
if(typeof jQuery.topZIndex == "undefined"){
    /*
            TopZIndex 1.2 (October 21, 2010) plugin for jQuery
            http://topzindex.googlecode.com/
            Copyright (c) 2009-2011 Todd Northrop
            http://www.speednet.biz/
            Licensed under GPL 3, see  <http://www.gnu.org/licenses/>
    */
    (function(a){a.topZIndex=function(b){return Math.max(0,Math.max.apply(null,a.map((b||"*")==="*"?a.makeArray(document.getElementsByTagName("*")):a(b),function(b){return parseFloat(a(b).css("z-index"))||null})))};a.fn.topZIndex=function(b){if(this.length===0)return this;b=a.extend({increment:1},b);var c=a.topZIndex(b.selector),d=b.increment;return this.each(function(){this.style.zIndex=c+=d})}})(jQuery);
}

DRHorton.slideshows = DRHorton.slideshows || {};


DRHorton.slideshows.SimpleSlideShow = function(params){
    
    var Button = DRHorton.components.Button;
    var Event = DRHorton.events.Event;
    var EventDispatcher = DRHorton.events.EventDispatcher;
    var SimpleSlideShow = DRHorton.slideshows.SimpleSlideShow;
    var SimpleSlideShowImage = DRHorton.slideshows.SimpleSlideShowImage;
    
    var _this = this,
        _timeStart = new Date().getTime(),
        _selector = params.selector,
        _wrapper = $(params.selector),
        _centerType = params.centerType || "fill",
        _imgContainer = $(document.createElement("div")),
        _imgContainerId = "DRHorton_SimpleSlideShow_" + ((new Date()).getTime().toString()),
        _images = [],
        _targetDims = {
            w: $(params.selector).width(),
            h: $(params.selector).height()
        },
        _currentImageIndex,
        _interval,
        _intervalId = 0
    ;

    if(typeof params.interval == "undefined")
        _interval = 5000;
    else _interval = params.interval;
    
        
    function _beginSlideShow(){
        _intervalId = -1;
        var remainingInterval = _interval - ((new Date().getTime()) - _timeStart);
        if(remainingInterval <= 0){            
            _this.next();
            _beginInterval();
        }
        else setTimeout(function() { _this.next(); _beginInterval()}, remainingInterval)
    }
    
    function _beginInterval(){
        _intervalId = setInterval(function(){
            _this.next();
        }, _interval);
    }
    
    this.pause_button = new Button({imageUrl:"/App_Themes/DRHortonSite/templateWizard/images/slideshow_pause.png"});

    this.play_button = new Button({imageUrl:"/App_Themes/DRHortonSite/templateWizard/images/slideshow_play.png"});
    
    this._addImage = function(src){
        
        var img = new Image();
        
        $(img).bind("load", function(){            
            $(this).unbind("load");
            _images.push(new SimpleSlideShowImage({
                imageObj: this,
                targetWidth: _targetDims.w,
                targetHeight: _targetDims.h
            }));
            
            
            if(_images.length == params.images.length){
                _this.dispatchEvent(new Event(SimpleSlideShow.IMAGES_LOADED));
            }
            if(_images.length == 1){
               _this.dispatchEvent(new Event(SimpleSlideShow.FIRST_IMAGE_LOADED));
            }
        });
        
        img.src = src;
        
    };
    
    this._centerImage = function(){
        var _parent = _imgContainer,
            _img = _imgContainer.find("img")
        ;
        if(_parent.css("position") == "static")
            _parent.css("position", "relative");
        
        _imgContainer.width(_wrapper.width());
        _imgContainer.height(_wrapper.height());
        
        _imgContainer.css("overflow", "hidden");
        
        if(_img.width() != _parent.width()){
            _img.css({
                position: "relative",
                left: Math.round(_parent.width() / 2 - _img.width() / 2 + parseInt(_parent.css("padding-left"))) + "px" 
            });
        }
        else {
            _img.css({
                position: "relative",
                top: Math.round(_parent.height() / 2 - _img.height() / 2 + parseInt(_parent.css("padding-top"))) + "px" 
            });
        }
    };
    
    this._showImage = function(img){        
        var html = "<img src=\""+img.src+"\" width=\"" + img.displayWidth + "\" height=\"" + img.displayHeight + "\" style=\"width:" + img.displayWidth + "px;height:" + img.displayHeight + "px\" />";
        _imgContainer.html(html);
        this._centerImage()
    };   
    
    this._showImageByIndex = function(index){
        this._showImage(_images[index]);
        
    };
    
    this._pause = function(){
        clearInterval(_intervalId);
        _intervalId = 0;
    };
    
    this._play = function(){
        _beginInterval();
    };
    
    this._next = function(){
        _currentImageIndex = (_currentImageIndex + 1) % _images.length
        this._showImageByIndex(_currentImageIndex);
    };
    
    this._showPauseButton = function(){
        _this.pause_button.addEventListener(Button.CLICK, _onPauseClicked);
        _onClick = _onPauseClicked;
        $(_this.pause_button.element()).show();
    };
    
    this._showPlayButton = function(){
        _this.play_button.addEventListener(Button.CLICK, _onPlayClicked);
        _onClick = _onPlayClicked;
        $(_this.play_button.element()).show();
    };
    
    this._hidePauseButton = function(){
        _this.pause_button.removeEventListener(Button.CLICK, _onPauseClicked);
        $(_this.pause_button.element()).hide();
    };
    
    this._hidePlayButton = function(){
        _this.play_button.removeEventListener(Button.CLICK, _onPlayClicked);
        $(_this.play_button.element()).hide();
    };
    
    this._hideButtons = function(){
        this._hidePlayButton();
        this._hidePauseButton();
    };
    
    this._onFirstImageLoaded = function(e){
        _currentImageIndex = 0;
        _timeStart = new Date().getTime();
        _this._showImage(_images[0]);
    };
    
    this._onImagesLoaded = function(e){
        if(_wrapper.css("position") == "static")
            _wrapper.css("position", "relative");
            
        _imgContainer
            .attr("id", _imgContainerId)
            .css({
                cursor: "hand",
                cursor: "pointer"
            })
            .prependTo(_wrapper)
            .bind("click", function(){_onClick();});
        
            
        _wrapper
            .bind("mouseover", function(){
                if(_intervalId == 0){
                    _this._showPlayButton();
                }
                else {
                    _this._showPauseButton();
                }
            })
            .bind("mouseout", function(){
                _this._hideButtons();
            });
        
        ;
            
        _beginSlideShow();
        
        $(_this.play_button.element())
            .prependTo(_wrapper)
            .css({
                display: "none",
                position: "absolute",
                left: (_wrapper.innerWidth() / 2 - $(_this.play_button.element()).width() / 2) + "px",
                top: (_wrapper.innerHeight() / 2 - $(_this.play_button.element()).height() / 2) + "px",
                cursor: "hand",
                cursor: "pointer"
            })
            .topZIndex();
        
        $(_this.pause_button.element())
            .prependTo(_wrapper)
            .css({
                display: "none",
                position: "absolute",
                left: (_wrapper.innerWidth() / 2 - $(_this.play_button.element()).width() / 2) + "px",
                top: (_wrapper.innerHeight() / 2 - $(_this.play_button.element()).height() / 2) + "px",
                cursor: "hand",
                cursor: "pointer"
            })
            .topZIndex();
    };
    
    function _onPauseButtonLoaded(){
        if(_this.pause_button.loaded && _this.play_button.loaded){
            _this.play_button.removeEventListener(Button.CREATION_COMPLETE, _onPlayButtonLoaded);
            _this.pause_button.removeEventListener(Button.CREATION_COMPLETE, _onPlayButtonLoaded);
            _this.addImages(params.images);
            _onButtonsLoaded();
        }
    }
    
    function _onPlayButtonLoaded(){
        if(_this.pause_button.loaded && _this.play_button.loaded){
            _this.play_button.removeEventListener(Button.CREATION_COMPLETE, _onPlayButtonLoaded);
            _this.pause_button.removeEventListener(Button.CREATION_COMPLETE, _onPlayButtonLoaded);
            _this.addImages(params.images);
            _onButtonsLoaded();
        }
    }
    
    function _onButtonsLoaded(){
        _this.dispatchEvent(new Event(DRHorton.slideshows.SimpleSlideShow.ON_BUTTONS_LOADED));
        _this.dispatchEvent(new Event(DRHorton.slideshows.SimpleSlideShow.CREATION_COMPLETE));
    }
    
    function _onClick(){}
    
    function _onPauseClicked(){
        _this._hidePauseButton();  
        _this._showPlayButton();      
        _this.pause();
    }
    
    function _onPlayClicked(){
        _this._hidePlayButton();
        _this._showPauseButton(); 
        _this.play();
    }    


    this.addEventListener(SimpleSlideShow.IMAGES_LOADED, this._onImagesLoaded);
    this.addEventListener(SimpleSlideShow.FIRST_IMAGE_LOADED, this._onFirstImageLoaded);
    
    this.pause_button.addEventListener(Button.CREATION_COMPLETE, _onPauseButtonLoaded);
    this.play_button.addEventListener(Button.CREATION_COMPLETE, _onPlayButtonLoaded);    
    
    this.pause_button.load();
    this.play_button.load();
    
};

DRHorton.slideshows.SimpleSlideShow.inherits(DRHorton.events.EventDispatcher);

DRHorton.slideshows.SimpleSlideShow.prototype.next = function(){
    this._next();
};
    
DRHorton.slideshows.SimpleSlideShow.prototype.addImages = function(images){
    if(typeof images == "string"){
        this.addImage(images);
        return;
    }
    for(var i = 0; i < images.length; i++){
        this.addImage(images[i]);
    }
};

DRHorton.slideshows.SimpleSlideShow.prototype.addImage = function(src){
    if(typeof src == "object"){
        this.addImages(src);
        return;
    }
    this._addImage(src);
};

DRHorton.slideshows.SimpleSlideShow.prototype.pause = function(){
    this._pause();
};

DRHorton.slideshows.SimpleSlideShow.prototype.play = function(){
    this._play();
};

DRHorton.slideshows.SimpleSlideShow.FIRST_IMAGE_LOADED = "DRHorton.slideshows.SimpleSlideShow.FIRST_IMAGE_LOADED";
DRHorton.slideshows.SimpleSlideShow.ON_BUTTONS_LOADED = "DRHorton.slideshows.SimpleSlideShow.ON_BUTTONS_LOADED";
DRHorton.slideshows.SimpleSlideShow.IMAGES_LOADED = "DRHorton.slideshows.SimpleSlideShow.IMAGES_LOADED";
DRHorton.slideshows.SimpleSlideShow.CREATION_COMPLETE = "DRHorton.slideshows.SimpleSlideShow.CREATION_COMPLETE";

DRHorton.slideshows.SimpleSlideShowImage = function(params){
    var DimUtils = DRHorton.utils.DimensionUtils;
    
    this.imageObj = params.imageObj;
    this.src = params.imageObj.src;
    this.sourceWidth = this.imageObj.width;
    this.sourceHeight = this.imageObj.height;
    
    var fillDims = DimUtils.getFillDimensions({
                        sourceWidth: this.sourceWidth,
                        sourceHeight: this.sourceHeight,
                        targetWidth: params.targetWidth,
                        targetHeight: params.targetHeight
                    });
    
    
    this.displayWidth = fillDims.w;
    this.displayHeight = fillDims.h;
};

