﻿/*!
* jQuery Slideshow Rotator Widget
* Copyright 2011, Edict Incorporated
* http://www.edict.com/
* Date: 04.15.2011
* Packed with: jscompress.com
*/
(function($) {
    $.widget('ui.slideshowRotator', { options: { index: 0, imagesAJAXPage: false, images: false, rotatorInterval: false, speed: 6000 }, _create: function() {
        var self = this; var o = this.options; if (!o.images && o.imagesAJAXPage) { $.ajax({ url: o.imagesAJAXPage, async: false, cache: false, dataType: "json", success: function(data) { o.images = data; } }); }
        self._initSlideshow();
    }, _initSlideshow: function() {
        var self = this; var div = this.element; var o = this.options; if (o.images.length > 0) {
            if (!o.images[o.index]) { o.index = 0; }
            div.html(''); var divBackground = $('<div id="thumbnails-background"></div>'); div.append(divBackground); var divThumbnails = $('<div id="thumbnails-div"></div>'); div.append(divThumbnails); var ulThumbnails = $('<ul></ul>'); divThumbnails.append(ulThumbnails); for (var i = 0; i < o.images.length; i++) {
                if (i == o.index) { div.append('<img id="img' + i + '" src="' + o.images[i]['src'] + '" alt="' + o.images[i]['alt'] + '" style="display: block;" />'); ulThumbnails.append('<li><img id="imgThumb' + i + '" src="/thumbnails' + o.images[i]['src'] + '" alt="' + o.images[i]['alt'] + '" class="on" /></li>'); }
                else {
                    div.append('<img id="img' + i + '" src="' + o.images[i]['src'] + '" alt="' + o.images[i]['alt'] + '" style="display: none;" /></li>'); if (i > 3) { ulThumbnails.append('<li style="display: none;"><img id="imgThumb' + i + '" src="/thumbnails' + o.images[i]['src'] + '" alt="' + o.images[i]['alt'] + '" />'); }
                    else { ulThumbnails.append('<li><img id="imgThumb' + i + '" src="/thumbnails' + o.images[i]['src'] + '" alt="' + o.images[i]['alt'] + '" />'); } 
                } 
            }
            ulThumbnails.find('img').mouseover(function() {
                var divOffset = $('#thumbnails-div').offset().left; var thumbOffset = $('#thumbnails-div ul li #imgThumb' + o.index).offset().left; if ((thumbOffset - divOffset + 28) > $('#thumbnails-div').width()) { $('#thumbnails-div').animate({ scrollLeft: '112px' }); }
                $(this).parent().addClass('hover'); $(this).stop().animate({ marginTop: '-35px', marginLeft: '-50px', top: '28px', left: '31px', width: '112px', height: '64px' }, 250);
            }).mouseout(function() { $(this).parent().removeClass('hover'); $(this).stop().animate({ marginTop: '0px', marginLeft: '0px', top: '9px', left: '9px', width: '56px', height: '32px' }, 250); }).click(function() {
                if (!$(this).hasClass('on')) {
                    var imgIndex = parseInt($(this).attr('id').replace('imgThumb', '')); if (!isNaN(imgIndex)) {
                        if (o.rotatorInterval) { clearInterval(o.rotatorInterval); }
                        self._goToImage(imgIndex); o.rotatorInterval = setInterval(function() { self._nextImage(); }, o.speed);
                    } 
                } 
            }); if (o.images.length > 1) {
                if (o.images.length > 4) {
                    var divPrevious = $('<div id="previous-thumb"></div>'); div.append(divPrevious); var divNext = $('<div id="next-thumb"></div>')
                    div.append(divNext); divNext.click(function() { self._showNextHiddenThumb(); }); divPrevious.click(function() { self._showPreviousHiddenThumb(); });
                }
                self._preLoadImages(); o.rotatorInterval = setInterval(function() { self._nextImage(); }, o.speed);
            } 
        } 
    }, _preLoadImages: function() {
        var self = this; var images = this.element.find('img'); for (var i = 0; i < images.length; i++) {
            var image = images[i]; if (!image.preloaded) {
                var img = $(new Image()); img.attr('src', image.src); if (self._isImageLoaded(img[0])) { image.preloaded = true; image.size = { width: img[0].width, height: img[0].height }; }
                else { img.load(function() { image.preloaded = true; image.size = { width: this.width, height: this.height} }).error(function() { image.error = true; image.preloaded = false; image.size = false; }); } 
            } 
        } 
    }, _isImageLoaded: function(img) { if (typeof img.complete != 'undefined' && !img.complete) { return false; }; if (typeof img.naturalWidth != 'undefined' && img.naturalWidth == 0) { return false; }; return true; }, _nextImage: function() {
        var self = this; var o = this.options; var index = 0; if (o.images[o.index + 1]) { index = o.index + 1; }
        else { index = 0; }
        self._goToImage(index);
    }, _previousImage: function() {
        var self = this; var o = this.options; var index = 0; if (o.images[o.index - 1]) { index = o.index - 1 }
        else { index = o.images.length - 1; }
        self._goToImage(index);
    }, _goToImage: function(index) {
        var div = this.element; var o = this.options; var previousIndex = o.index; if ($('#' + div.attr('id') + ' #img' + index)) {
            o.index = index; $('#thumbnails-div ul li #imgThumb' + o.index).addClass('on'); $('#thumbnails-div ul li #imgThumb' + previousIndex).removeClass('on'); if (!$('#thumbnails-div ul li #imgThumb' + o.index).parent().is(':visible')) { while (!$('#thumbnails-div ul li #imgThumb' + o.index).parent().is(':visible')) { this._showNextHiddenThumb(); } }
            $('#' + div.attr('id') + ' #img' + o.index).fadeIn(2000); $('#' + div.attr('id') + ' #img' + previousIndex).fadeOut(2000);
        } 
    }, _showNextHiddenThumb: function() { if ($('#thumbnails-div ul li:visible:last')) { if ($('#thumbnails-div ul li:visible:last').next()) { $('#thumbnails-div ul li:visible:last').next().show(); $('#thumbnails-div ul li:first').hide(); var clone = $('#thumbnails-div ul li:first').clone(true); $('#thumbnails-div ul li:first').remove(); $('#thumbnails-div ul').append(clone); } } }, _showPreviousHiddenThumb: function() { if ($('#thumbnails-div ul li:last')) { $('#thumbnails-div ul li:last').show(); var clone = $('#thumbnails-div ul li:last').clone(true); $('#thumbnails-div ul li:last').remove(); $('#thumbnails-div ul').prepend(clone); if ($('#thumbnails-div ul li:visible:last')) { $('#thumbnails-div ul li:visible:last').hide(); } } }, destroy: function() { $.Widget.prototype.destroy.apply(this, arguments); } 
    });
})(jQuery);
