﻿/*

Title:		jShowOff: a jQuery Content Rotator Plugin
Author:		Erik Kallevig
Version:	0.1.1
Website:	http://ekallevig.com/jshowoff
License: 	Dual licensed under the MIT and GPL licenses.

*/

(function($) {
    $.fn.jshowoff = function(settings) {
        var config = { speed: 3000, changeSpeed: 600, controls: true, links: true, autoPlay: true }; if (settings) $.extend(config, settings); if (config.speed < (config.changeSpeed + 20)) { alert('jShowOff: Make speed at least 20ms longer than changeSpeed; the fades aren\'t always right on time.'); return this; }; this.each(function(i) {
            var cont = this; var gallery = $(this).children('div').remove(); var timer = ''; var counter = 0; var preloaded = []; var howManyInstances = $('.jshowoff').length + 1; var cssClass = config.cssClass !== undefined ? 'jshowoff-custom-' + howManyInstances + ' ' + config.cssClass : 'jshowoff-' + howManyInstances; var uniqueClass = cssClass.split(' ')[0]; function start() {
                $(cont).css('position', 'relative').append('<div class="jshowoff ' + cssClass + '"></div>'); $(gallery[0]).clone().appendTo('.' + uniqueClass); preload(); if (config.controls) { addControls(); if (config.autoPlay == false) { $('.' + uniqueClass + '-play').addClass(uniqueClass + '-paused jshowoff-paused').text('Play'); } }
                if (config.links) { addLinks(); $('.' + uniqueClass + '-slidelinks a').eq(0).addClass(uniqueClass + '-active jshowoff-active'); }
                $('<div class="' + uniqueClass + '-cache jshowoff-cache"></div>').appendTo(cont); $(gallery).each(function() { $(this).appendTo($('.' + uniqueClass + '-cache')).hide(); }); if (config.autoPlay && gallery.length > 1) { timer = setTimeout(function() { play(); }, config.speed); }; if (gallery.length < 1) { $('.' + uniqueClass).append('<p>For jShowOff to work, the container element must have child divs.</p>'); } 
            }; function transitionTo(gallery, index) {
                if ((counter >= gallery.length) || (index >= gallery.length)) { counter = 0; }
                else if ((counter < 0) || (index < 0)) { counter = gallery.length - 1; }
                else { counter = index; }
                $(gallery[counter]).clone().appendTo('.' + uniqueClass).hide().fadeIn(config.changeSpeed); if ($('.' + uniqueClass + ' div').length > 1) { $('.' + uniqueClass + ' div:first').css('position', 'absolute').fadeOut(config.changeSpeed, function() { $(this).remove(); }); }; if (config.links) { $('.' + uniqueClass + '-active').removeClass(uniqueClass + '-active jshowoff-active'); $('.' + uniqueClass + '-slidelinks a').eq(counter).addClass(uniqueClass + '-active jshowoff-active'); } 
            }; function addControls() { $(cont).append('<p class="jshowoff-controls ' + uniqueClass + '-controls"><a class="jshowoff-play ' + uniqueClass + '-play" href="#null">Pause</a> <a class="jshowoff-prev ' + uniqueClass + '-prev" href="#null">Previous</a> <a class="jshowoff-next ' + uniqueClass + '-next" href="#null">Next</a></p>'); $('.' + uniqueClass + '-controls a').each(function() { if ($(this).hasClass('jshowoff-play')) $(this).click(function() { updatePlayPause(); return false; }); if ($(this).hasClass('jshowoff-prev')) $(this).click(function() { previous(); return false; }); if ($(this).hasClass('jshowoff-next')) $(this).click(function() { next(); return false; }); }); }; function updatePlayPause() {
                if (isPlaying()) { pause(); $('.' + uniqueClass + '-play').text('Play').toggleClass('jshowoff-paused ' + uniqueClass + '-paused'); }
                else { play(); $('.' + uniqueClass + '-play').text('Pause').toggleClass('jshowoff-paused ' + uniqueClass + '-paused'); };
            }; function isPlaying() {
                if ($('.' + uniqueClass + '-play').hasClass('jshowoff-paused')) { return false; }
                else { return true; };
            }; function play() { if (!isBusy()) { counter++; transitionTo(gallery, counter); clearTimeout(timer); timer = setTimeout(function() { play(gallery); }, config.speed); } }; function pause() { clearTimeout(timer); $('<p class="' + uniqueClass + '-pausetext">Pause</p>').css({ fontSize: '62%', color: '#fff', textAlign: 'center', position: 'absolute', top: '40%', lineHeight: '100%', width: '100%' }).appendTo('.' + uniqueClass).animate({ fontSize: '600%', top: '30%', opacity: 0 }, { duration: 400, complete: function() { $(this).remove(); } }); }; function stopit() { clearTimeout(timer); }; function next() { goToAndPause(counter + 1); }; function previous() { goToAndPause(counter - 1); }; function isBusy() { return $('.' + uniqueClass + ' div').length > 1 ? true : false; }; function goToAndPause(index) { $('.' + uniqueClass + ' div').stop(true); stopit(); $('.' + uniqueClass + '-play').text('Play').addClass('jshowoff-paused ' + uniqueClass + '-paused'); if ((counter != index) || ((counter == index) && isBusy())) { if (isBusy()) $('.' + uniqueClass + ' div:first').remove(); transitionTo(gallery, index); } }; function addLinks() { $(cont).append('<p class="jshowoff-slidelinks ' + uniqueClass + '-slidelinks"></p>'); $.each(gallery, function(i, val) { $('<a class="jshowoff-slidelink-' + i + ' ' + uniqueClass + '-slidelink-' + i + '" href="#null">' + (i + 1) + '</a>').bind('click', { index: i }, function(e) { goToAndPause(e.data.index); return false; }).appendTo('.' + uniqueClass + '-slidelinks'); }); }; function preload() { $(gallery).each(function(i) { $(this).find('img').each(function(i) { preloaded[i] = $('<img>').attr('src', $(this).attr('src')); }); }); }; start();
        }); return this;
    };
})(jQuery);

