if (window['PB'] == undefined) {
    PB = {};
}
PB.Slideshow = {
    currentIndex: 0,
    windowWidth: 0,
    list: null,
    currentSlide: null,
    nextSlide: null,
    prevSlide: null,
    data: null,
    thumbWidth: 60,
    thumbnails: [],
    listContent: null,
    interval: null,
    speed: 5000,
    isPlaying: true,
    controlFade: 0,
    allowFadeControls: true,
    initialize: function() {
        this.setDimensions();
        jQuery.fx.interval = Math.floor(jQuery.fx.interval * 2);
        jQuery('.play').click(jQuery.proxy(this.onPlay, this));
        jQuery('.pause').click(jQuery.proxy(this.onPause, this));
        jQuery('.nextButton').click(jQuery.proxy(this.onNext, this));
        jQuery('.prevButton').click(jQuery.proxy(this.onPrev, this));
        jQuery('.nextScrollButton').click(jQuery.proxy(this.onScrollNext, this));
        jQuery('.prevScrollButton').click(jQuery.proxy(this.onScrollPrev, this));
        jQuery('#optionButton').click(jQuery.proxy(this.toggleOptions, this));
        jQuery('#shareButton').click(jQuery.proxy(this.toggleShare, this));
        jQuery('.speedRadio').change(jQuery.proxy(this.onSpeedChange, this));
        jQuery('#chkHide').click(jQuery.proxy(this.onChkHide, this));
        var self = this;
        jQuery(document).keydown(function(event) {
            self.onKeyPress(event);
        });
        var slides = jQuery('.slide');
        this.currentSlide = new PB.Slideshow.Slide(jQuery(slides[0]));
        this.nextSlide = new PB.Slideshow.Slide(jQuery(slides[1]));
        this.prevSlide = new PB.Slideshow.Slide(jQuery(slides[2]));
        jQuery(window).resize(function() {
            self.setDimensions();
        });
        this.list = new PB.HorizontalList();
        this.list.initialize();
        this.list.itemRenderer = PB.Slideshow.Thumbnail;
        this.list.setWidth(this.windowWidth - 40 - 12 - 16 - 12 - 16 - 12);
        this.list.setData(this.data);
        this.list.addEventListener(PB.Event.SELECT, this.onSelect, this);
        this.list.selectIndex(0);
        this.startSlideshow();
        this.isMobile = this.getIsMobile();
        if (!this.isMobile) {
            this.startControlFade();
        } else {
            jQuery('.behavior').hide();
            var self = this;
            jq('.playArea').touchwipe({
                wipeLeft: function() { self.onNext(); },
                wipeRight: function() { self.onPrev(); },
                wipeUp: function() { return;},
                wipeDown: function() { return;},
                min_move_x: 20,
                min_move_y: 20,
                preventDefaultEvents: true
            });
            jq('.footer').touchwipe({
                wipeLeft: function() { self.onScrollNext(); },
                wipeRight: function() { self.onScrollPrev(); },
                wipeUp: function() { return;},
                wipeDown: function() { return;},
                min_move_x: 20,
                min_move_y: 20,
                preventDefaultEvents: true 
            });
        }
    },
    startSlideshow: function() {
        this.resetInterval();
    },
    onInterval: function() {
        this.gotoNext();
    },
    resetInterval: function() {
        if (this.interval != null) {
            clearInterval(this.interval);
        }
        if (this.isPlaying == false) {
            return;
        }
        var self = this;
        this.interval = setInterval(function(){self.onInterval()}, this.speed);
    },
    stop: function() {
        this.isPlaying = false;
        if (this.interval != null) {
            clearInterval(this.interval);
        }
        jQuery('.play').show();
        jQuery('.pause').hide();
    },
    play: function() {
        this.isPlaying = true;
        this.resetInterval();
        jQuery('.play').hide();
        jQuery('.pause').show();

    },
    setSpeed: function(speed) {
        if (speed == 'medium') {
            this.speed = 5000;
        } else if (speed == 'slow') {
            this.speed = 8000;
        } else if (speed == 'fast') {
            this.speed = 3000;
        } else {
            this.speed = speed;
        }

    },
    setDimensions: function() {
        var newWidth = jQuery(window).width();
        if (newWidth == this.windowWidth) {
            return;
        }
        this.windowWidth = newWidth;
        var maxHeight = jQuery(window).height() - 50 - 100;
        var maxWidth =  this.windowWidth * .88;
        var slides =jQuery('.slide img');
        slides.css("max-height", maxHeight+"px");
        slides.css("max-width", maxWidth+"px");
        if (this.list) {
            this.list.setWidth(this.windowWidth * .88);
        }
    },
    onNext: function(event) {
        this.resetFadeTimeout();
        this.resetInterval();
        this.gotoNext();
    },
    onPrev: function(event) {
        this.resetFadeTimeout();
        this.resetInterval();
        this.gotoPrev();
    },
    onPlay: function(event) {
        this.resetFadeTimeout();
        this.resetInterval();
        this.play();
    },
    onPause: function(event) {
        this.resetFadeTimeout();
        this.resetInterval();
        this.stop();
    },
    onScrollNext: function(event) {
        this.resetFadeTimeout();
        this.userHasScrolled = true;
        if (this.list) {
            this.list.scrollNextPage();
        }
    },
    onScrollPrev: function(event) {
        this.resetFadeTimeout();
        this.userHasScrolled = true;
        if (this.list) {
            this.list.scrollPrevPage();
        }
    },
    onSelect: function(event) {
        this.resetFadeTimeout();
        this.resetInterval();
        this.gotoIndex(event.data);
    },
    onKeyPress: function(event) {
        this.resetInterval();
        switch (event.keyCode) {
            case 39: // Left
                this.gotoNext();
                break;
            case 37: // Right
                this.gotoPrev();
                break;
            case 33: // Page Down
                this.userHasScrolled = true;
                if (this.list) {
                    this.list.scrollNextPage();
                }
                break;
            case 34: // Page Up
                this.userHasScrolled = true;
                if (this.list) {
                    this.list.scrollPrevPage();
                }
                break;
            case 36: // Home
                this.userHasScrolled = true;
                if (this.list) {
                    this.gotoIndex(0);
                    this.list.scrollToIndex(0);
                }
                break;
            case 35: // End
                this.userHasScrolled = true;
                if (this.list) {
                    this.gotoIndex(this.data.length - 1);
                    this.list.scrollToIndex(this.data.length - 1);
                    this.stop();
                }
                break;
             case 84: // S
                this.stop();
                break;
             case 80: // P
                 this.play();
                 break;
             case 32: // Spacebar
                 if (this.isPlaying) {
                     this.stop();
                 } else {
                     this.play();
                 }
                 break;
             default:

        }
    },
    onSpeedChange: function(event) {
        this.resetFadeTimeout();
        this.setSpeed(event.target.value);
        this.resetInterval();
    },
    gotoNext: function() {
        if (this.currentIndex == this.data.length - 1) {
            return;
        }
        this.currentSlide.hide();
        this.nextSlide.show();
        var tmpSlide = this.prevSlide;
        this.prevSlide = this.currentSlide;
        this.currentSlide = this.nextSlide;
        this.nextSlide = tmpSlide;
        this.currentIndex++;
        this.list.selectIndex(this.currentIndex);
        if (!this.userHasScrolled) {
            this.list.scrollIfNeeded(this.currentIndex);
        }
        var nextData = this.data[this.currentIndex + 1]
        if (nextData) {
            this.nextSlide.setData(nextData);
        } else {
            this.stop();
        }
    },
    gotoPrev: function() {
        if (this.currentIndex == 0) {
            return;
        }
        this.currentSlide.hide();
        this.prevSlide.show();
        var tmpSlide = this.nextSlide;
        this.nextSlide = this.currentSlide;
        this.currentSlide = this.prevSlide;
        this.prevSlide = tmpSlide;
        this.currentIndex--;
        this.list.selectIndex(this.currentIndex);
        if (!this.userHasScrolled) {
            this.list.scrollIfNeeded(this.currentIndex);
        }
        var prevData = this.data[this.currentIndex - 1];
        if (prevData) {
            this.prevSlide.setData(prevData);
        }
    },
    gotoIndex: function(index) {
        if (index == this.currentIndex) {
            return;
        }
        if (index == this.currntIndex + 1) {
            this.gotoNext();
        }
        if (index == this.currentIndex - 1) {
            this.gotoPrev();
        }
        this.nextSlide.setData(this.data[index]);
        this.currentSlide.hideFast();
        this.prevSlide.hideFast();
        this.nextSlide.show();
        var tmpSlide = this.prevSlide;
        this.prevSlide = this.currentSlide;
        this.currentSlide = this.nextSlide;
        this.nextSlide = tmpSlide;
        this.currentIndex = index;
        var nextData = this.data[this.currentIndex + 1];
        if (nextData) {
            this.nextSlide.setData(nextData);
        }
        var prevData = this.data[this.currentIndex - 1];
        if (prevData) {
            this.prevSlide.setData(prevData);
        }
    },
    onChkHide: function(event) {
      if (event.target.checked) {
         this.allowFadeControls = true;
      } else {
         this.allowFadeControls = false;
      }
      this.resetFadeTimeout();
    },
    controlsFaded: false,
    lastX: 0,
    lastY: 0,
    startControlFade: function() {
        jQuery(document).mousemove(jQuery.proxy(this.onMouseMove, this));
        this.resetFadeTimeout();
    },
    resetFadeTimeout: function() {
        if (this.isMobile) { return; }
        if (this.controlFade != 0) {
            clearTimeout(this.controlFade);
        }
        if (this.allowFadeControls == true) {
            var self = this;
            this.controlFade = setTimeout(function() {self.fadeControls()}, 5000);
        }
    },
    onMouseMove: function(event) {
        //odd ie behavior on fade
        if ((this.lastX == event.pageX)&&(this.lastY == event.pageY)) {
            return;
        }
        this.lastX = event.pageX;
        this.lastY = event.pageY;
        this.resetFadeTimeout();
        if (this.controlsFaded) {
            this.showControls();
        }
    },
    fadeControls: function() {
        this.controlsFaded = true;
        jQuery('.header').animate({opacity: 0}, 900);
        jQuery('.footer').animate({opacity: 0}, 900);
        jQuery('.prev').animate({opacity: 0}, 900);
        jQuery('.next').animate({opacity: 0}, 900);
    },
    showControls: function(){
        this.controlsFaded = false;
        jQuery('.header').animate({opacity: 1}, 900, 'linear', function() { if (photobucket.browser.isIE8) {this.style.removeAttribute('filter');}});
        jQuery('.footer').animate({opacity: 1}, 900);
        jQuery('.prev').animate({opacity: 1}, 900);
        jQuery('.next').animate({opacity: 1}, 900);;

    },
    toggleOptions: function() {
        jQuery('.optionsMenu').toggle();
        jQuery('.shareMenu').hide();
    },
    toggleShare: function() {
        jQuery('.shareMenu').toggle();
       	jQuery('.optionsMenu').hide();
    },
    getIsMobile: function() {
        var ua = navigator.userAgent;
        if ((ua.match(/iPad/i) != null)||(ua.match(/iPhone/i) != null)||(ua.match(/Android/i) != null)||(ua.match(/IEMobile/i) != null)) {
           return true;
        }
        return false;
    }


};

PB.Slideshow.Slide = function(element) {
    var title = element.find('.title');
    var description = element.find('.description');
    var image = element.find('.fullimage');
    var data;
    var obj = {};
    obj.setData = function(value) {
        data = value;
        if (data) {
            title.html(data.title);
            description.html(data.description);
            image.attr('src', data.url);
        } else {
            title.html('');
            description.html('');
            image.attr('src', '');
        }
    };
    obj.getElement = function() {
        return element;
    };
    obj.hide = function(callback) {
        element.css("z-index", 1);
        element.stop();
        element.animate({opacity: 0}, 900);
        //element.fadeOut(900, callback);
    };
    obj.show = function(callback) {
        element.css("z-index", 2);
        element.stop();
        element.animate({opacity: 1}, 1500);
        //element.fadeIn(1500, callback);
    };
    obj.hideFast = function() {
        element.stop();
        element.css('opacity', 0);
    }
    return obj;
};

PB.Slideshow.Thumbnail = function(data, index, container, list) {
    var html = '<div class="slidethumbnail"><div class="mask"><div class="thumb outer"><div class="middle"><div class="inner">';
    html += '<img class="thumbimage" src="' + data.thumb + '" alt="image"/>';
    html += '</div></div></div></div></div>';
    var elm = jQuery(html).appendTo(container);
    elm.click(function() {
        list.onItemClick(index);
    });
    var selected = false;
    this.select = function() {
        if (!selected) {
            selected = true;
            elm.css("background-color", "#FFFFFF");
        }
    };
    this.unselect = function() {
        if (selected) {
            selected = false;
            elm.css("background-color", "#000000");
        }
    };
    return this;
}


