/*
Background Stretcher 2 jQuery Plugin
© 2010 cyberward.net
This is a heavily modified version of the 1.2v Background Stretcher
jQuery Plugin found at ajaxBlender.
The stop/start funcions and all support for albums was added. 

Version: 2.0
	
Background Stretcher jQuery Plugin
© 2009 ajaxBlender.com
For any questions please visit www.ajaxblender.com 
or email us at support@ajaxblender.com
	
Version: 1.2
*/
; (function($) {
    var container = null;
    var allImgs = '', allLIs = '', containerStr = '';
    var _bgStretcherPause = false;
    var _bgStretcherTm = null;

    $.fn.bgStretcher = function(settings) {
        settings = $.extend({}, $.fn.bgStretcher.defaults, settings);
        $.fn.bgStretcher.settings = settings;

        function _build() {
            _genHtml();

            // slides
            containerStr = '#' + settings.imageContainer;
            container = $(containerStr);
            allImgs = '#' + settings.imageContainer + ' IMG';
            allLIs = '#' + settings.imageContainer + ' LI';
            allTITLEs = '#' + settings.imageContainer + '_titles > div';

            $(allLIs).hide();
            $(allLIs + ':first').show().addClass('bgs-current');
            $(allTITLEs).hide();
            $(allTITLEs + ':first').show().addClass('bgs-current');


            if (!container.length) { return; }
            $(window).resize(_resize);

            if (settings.slideShow && $(allImgs).length > 1) {
                _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', settings.nextSlideDelay);
            }
            _resize();
        };

        function _resize() {
            var winW = $(window).width();
            var winH = $(window).height();
            var imgW = 0, imgH = 0;

            container.width(winW);
            container.height(winH);

            if (!settings.resizeProportionally) {
                imgW = winW;
                imgH = winH;
            } else {
                var initW = settings.imageWidth, initH = settings.imageHeight;
                var ratio = initH / initW;

                imgW = winW;
                imgH = winW * ratio;

                if (imgH < winH) {
                    imgH = winH;
                    imgW = imgH / ratio;
                }
            }
            if (!settings.resizeAnimate) {
                $(allImgs).width(imgW).height(imgH);
            } else {
                $(allImgs).animate({ width: imgW, height: imgH }, 'normal');
            }
        };

        function _genHtml() {
            var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>';
            var titlecode = '<div id="' + settings.imageContainer + '_titles" class="bgtitles">';

            jQuery.each(settings.titles, function(album, titleArray) {
                for (i = 0; i < titleArray.length; i++) {
                    titlecode += '<div class="summary ' + album + '"><div class="summary-inner">' + titleArray[i] + '</div></div>';
                }
            });
            titlecode += '</div>';
            $(titlecode).prependTo((settings.appendTitlesID != '') ? settings.appendTitlesID : 'body');


            jQuery.each(settings.albums, function(album, imageArray) {
                for (i = 0; i < imageArray.length; i++) {
                    code += '<li class="' + album + '"><img src="' + imageArray[i] + '" alt="" /></li>';
                }
            });
            code += '</ul></div>';
            $(code).prependTo('body');

        };
        _build();
    };

    $.fn.bgStretcher.play = function() {
        _bgStretcherPause = false;
        $.fn.bgStretcher._clearTimeout();
        $.fn.bgStretcher.slideShow();
    };

    $.fn.bgStretcher._clearTimeout = function() {
        if (_bgStretcherTm != null) {
            clearTimeout(_bgStretcherTm);
            _bgStretcherTm = null;
        }
    }

    $.fn.bgStretcher.pause = function() {
        _bgStretcherPause = true;
        $.fn.bgStretcher._clearTimeout();
    };

    $.fn.bgStretcher.slideShow = function() {
        // slides
        slideSwitchNext(containerStr, 'li');
        // titles
        slideSwitchNext(containerStr + '_titles', 'div');

        if (!_bgStretcherPause) {
            _bgStretcherTm = setTimeout('$.fn.bgStretcher.slideShow()', $.fn.bgStretcher.settings.nextSlideDelay);
        }
    };

    $.fn.bgStretcher.next = function() {
        // slides
        slideSwitchNext(containerStr, 'li');
        // titles
        slideSwitchNext(containerStr + '_titles', 'div');
    };

    $.fn.bgStretcher.previous = function() {
        // slides
        slideSwitchPrev(containerStr, 'li');
        // titles
        slideSwitchPrev(containerStr + '_titles', 'div');
    };

    $.fn.bgStretcher.changeTo = function(x) {
        // slides
        slideSwitchTo(containerStr, 'li', x);
        // titles
        slideSwitchTo(containerStr + '_titles', 'div', x);
    };

    function slideSwitchPrev(id, o) {
        var current = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album).filter('.bgs-current');
        var previous = current.prev('.' + $.fn.bgStretcher.settings.album);
        if (!previous.length) {
            previous = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album + ':last');
        }
        $(id + ' ' + o).removeClass('bgs-current');
        previous.addClass('bgs-current');
        previous.fadeIn($.fn.bgStretcher.settings.slideShowSpeed);
        current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);
    }

    function slideSwitchNext(id, o) {
        var current = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album).filter('.bgs-current');
        var next = current.next('.' + $.fn.bgStretcher.settings.album);
        if (!next.length) {
            next = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album + ':first');
        }
        $(id + ' ' + o).removeClass('bgs-current');
        next.addClass('bgs-current');
        next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed);
        current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);
    }


    function slideSwitchTo(id, o, iter) {
        var current = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album).filter('.bgs-current');
        var next = $(id + ' ' + o + '.' + $.fn.bgStretcher.settings.album + '::eq(' + iter + ')');
        
        if (next.hasClass('bgs-current')) { return; }

        $(id + ' ' + o).removeClass('bgs-current');
        next.addClass('bgs-current');
        next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed);
        current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);
    }

    $.fn.bgStretcher.changeAlbum = function(album) {
        $.fn.bgStretcher.settings.album = album;

        // slides
        var current = $(containerStr + ' LI.bgs-current');
        var next = $(containerStr + ' LI.' + $.fn.bgStretcher.settings.album + ":first");
        next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed);
        current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);

        // titles
        current = $(containerStr + '_titles div.bgs-current');
        next = $(containerStr + '_titles div.' + $.fn.bgStretcher.settings.album + ":first");
        next.fadeIn($.fn.bgStretcher.settings.slideShowSpeed);
        current.fadeOut($.fn.bgStretcher.settings.slideShowSpeed);

        if (!_bgStretcherPause) {
            $.fn.bgStretcher.play();
        }
    };

    /*  Default Settings  */
    $.fn.bgStretcher.defaults = {
        imageContainer: 'bgstretcher',
        resizeProportionally: true,
        resizeAnimate: false,
        albums: [],
        album: '',
        titles: [],
        appendTitlesID: '',
        imageWidth: 1024,
        imageHeight: 768,
        nextSlideDelay: 3000,
        slideShowSpeed: 'normal',
        slideShow: true
    };
    $.fn.bgStretcher.settings = {};
})(jQuery);
