/**
 * Base namespace setup
 */
var SLIDESHOW = {
    globals: {
		labels: {}
	},
    pages: {
        all: {},
        home: {}
    },
    widgets: {},
    util: {}
}


/**
 * Initialize SLIDESHOW home page
 */
SLIDESHOW.pages.home.init = function () {

	// Initialize small slideshow
	SLIDESHOW.globals.slideShow = new SLIDESHOW.widgets.Slideshow("slides", "slide-controls", {timer: 3000});
};



/**
 * Creates an image gallery widget based on markup
 * 
 * @constructor
 * @param {String|HTMLElement} id The element representing the gallery
 * @param {Object} config An object literal with override config values
 * @requires YAHOO.util.Dom Required for extended/normalized DOM manipulation
 */
SLIDESHOW.widgets.Gallery = function (id, config) {
    if (typeof id == "undefined") return false;
    if (typeof config == "undefined") config = {};
    
    this.galleryEl = YAHOO.util.Dom.get(id);
    
    if (this.galleryEl){
    
		// Set config defaults
		this.config = {
			scroll: "vertical",
			thumbsScrollerClassName: "scroller",
			thumbsViewportClassName: "viewport",
			thumbsClassName: "thumbs",
			thumbsTagName: "ul",
			prevClassName: "prev",
			prevText: "Previous",
			nextClassName: "next",
			nextText: "Next",
			dataset: [],
			skipBy: 5,
			rating: false,
			comments: false,
			commentsClassName: "comments",
			postCommentHref: null,
			postNoCommentHref: SLIDESHOW.globals.NoCommentUrl,
			commentLabels: {},
			ratingsElId: this.galleryEl.id + "-rating",
			viewportHeight: null,
			viewportWidth: null,
			scrollBy: null
		};
	}
	else {
		return false;
		}

}

/**
 * Creates a slideshow with previous/next buttons and pips in between representing
 * the current slide among the total number of slides in the show
 */
SLIDESHOW.widgets.Slideshow = function (slidesId, controlbarId, userConfig) {
    this.slidesEl = YAHOO.util.Dom.get(slidesId);
    this.controlBarEl = YAHOO.util.Dom.get(controlbarId);
    
    if (!this.slidesEl || !this.controlBarEl) return;
    
    //this.refreshSlidesElDims();
    this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };
    
    
    this.uc = userConfig || {};
    this.slides = YAHOO.util.Dom.getElementsByClassName("slide", this.uc.slidesElType || "li", this.slidesEl);
    this.numSlides = this.slides.length;
    
    this.slidesPtr = 0;
    this.currentSlide = function () {return this.slidesPtr+1}
    
    // Only create previous and next buttons if there is more than one slide
    if (this.numSlides > 1) {
		// Previous button
        this.prevBtn = document.createElement(this.uc.buttonNodeName || "span");
        this.prevBtn.innerHTML = "&larr;";
        this.prevBtn.className = this.uc.prevBtnClass || "prev";
        this.prevBtn.onclick = function (that) {
            return function () {
                that.goTo(-1);
                that.resetTimer();
                return false;
            }
        }(this);
        this.controlBarEl.appendChild(this.prevBtn);
    
		// Indicator
        this.indicator = document.createElement(this.uc.buttonNodeName || "span");
        this.indicator.className = this.uc.indicatorClass || "indicator";
        this.pips = [];
        for (var i=0; i<this.numSlides; i++) {
			var pip = document.createElement("span");
			if ((i+1) === this.currentSlide()) {
				pip.className = "current";
			}
			pip.onclick = function (that, num) {
				return function () {
					var dir = (that.slidesPtr > num) ? 1 : -1;
					that.goTo(dir, num);
	                that.resetTimer();
				}
			}(this, i);
			this.indicator.appendChild(pip);
			this.pips.push(pip);
        }
        this.controlBarEl.appendChild(this.indicator);

		// Next button
        this.nextBtn = document.createElement(this.uc.buttonNodeName || "span");
        this.nextBtn.innerHTML = "&rarr;";
        this.nextBtn.className = this.uc.nextBtnClass || "next";
        this.nextBtn.onclick = function (that) {
            return function () {
                that.goTo(1);
                that.resetTimer();
                return false;
            }
        }(this);
        this.controlBarEl.appendChild(this.nextBtn);
        
        // Get slides out of the way for Safari 2.0.x because they block page links
        for (var i=1; this.slides[i]; i++) {
            this.slides[i].style.top = "-3000px";
            this.slides[i].style.left = "-3000px";
        }
    }
    this.goTo(0); // to initialize the first slide if it has a popup
    this.setTimer(); // set automated forward timer
}

SLIDESHOW.widgets.Slideshow.prototype.setTimer = function () {
    if (this.uc.timer) {
		this.slideShowInterval = window.setInterval(
			function (that) {
				return function () {
					that.goTo(1);
				}
			}(this),
			this.uc.timer
		);
	}
}

SLIDESHOW.widgets.Slideshow.prototype.resetTimer = function () {
	if (this.slideShowInterval) {
		window.clearInterval(this.slideShowInterval);
		this.setTimer();
	}
}

SLIDESHOW.widgets.Slideshow.prototype.refreshSlidesElDims = function () {
    this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };
}

SLIDESHOW.widgets.Slideshow.prototype.goTo = function (dir, skipTo) {
   this.slidesElDims = {
        xy: YAHOO.util.Dom.getXY(this.slidesEl),
        width: this.slidesEl.offsetWidth,
        height: this.slidesEl.offsetHeight
    };
    
    var outSlide = this.slides[this.slidesPtr];
    var outSlideXY = YAHOO.util.Dom.getXY(outSlide);
    var outSlideWidth = outSlide.offsetWidth;
    
    if (dir == -1) {
        var outSlideAnim = new YAHOO.util.Motion(outSlide, {
                opacity: {
                    from: 1,
                    to: 0
                },
                points: {
                    from: [
                        this.slidesElDims.xy[0], 
                        this.slidesElDims.xy[1]
                    ],
                    to: [
                        this.slidesElDims.xy[0] + this.slidesElDims.width, 
                        this.slidesElDims.xy[1]
                    ]
                }
            }
        );
        outSlideAnim.onComplete.subscribe(
           function (el) {
               return function () {
                   el.style.top = "-3000px";
                   el.style.left = "-3000px";
               }
           }(outSlide)
        );
        outSlideAnim.duration = 0.4;
        outSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        this.slidesPtr = (this.slides[skipTo]) ? skipTo : this.slides[this.slidesPtr-1] ? this.slidesPtr-1 : this.numSlides-1;
        var inSlide = this.slides[this.slidesPtr];
        var inSlideXY = YAHOO.util.Dom.getXY(inSlide);
        var inSlideAnim = new YAHOO.util.Motion(inSlide, {
                opacity: {
                    from: 0,
                    to: 1
                },
                points: {
                    from: [
                        this.slidesElDims.xy[0] - this.slidesElDims.width, 
                        this.slidesElDims.xy[1]
                    ],
                    to: [
                        this.slidesElDims.xy[0], 
                        this.slidesElDims.xy[1]
                    ]
                }
            }
        );
        inSlideAnim.duration = 0.2;
        inSlideAnim.method = YAHOO.util.Easing.easeOutStrong;
        
        outSlideAnim.animate();
        inSlideAnim.animate();
        
        
    } else if (dir == 1) {
        var outSlideAnim = new YAHOO.util.Motion(outSlide, {
                opacity: {
                    from: 1,
                    to: 0
                },
                points: {
                    from: [
                        this.slidesElDims.xy[0], 
                        this.slidesElDims.xy[1]
                    ],
                    to: [
                        this.slidesElDims.xy[0] - this.slidesElDims.width, 
                        this.slidesElDims.xy[1]
                    ]
                }
            }
        );
        outSlideAnim.onComplete.subscribe(
           function (el) {
               return function () {
                   el.style.top = "-3000px";
                   el.style.left = "-3000px";
               }
           }(outSlide)
        );
        outSlideAnim.duration = 0.4;
        outSlideAnim.method = YAHOO.util.Easing.easeOutStrong;

        this.slidesPtr = (this.slides[skipTo]) ? skipTo : this.slides[this.slidesPtr+1] ? this.slidesPtr+1 : 0;
        var inSlide = this.slides[this.slidesPtr];
        var inSlideXY = YAHOO.util.Dom.getXY(inSlide);
        var inSlideAnim = new YAHOO.util.Motion(inSlide, {
                opacity: {
                    from: 0,
                    to: 1
                },
                points: {
                    from: [
                        this.slidesElDims.xy[0] + this.slidesElDims.width, 
                        this.slidesElDims.xy[1]
                    ],
                    to: [
                        this.slidesElDims.xy[0], 
                        this.slidesElDims.xy[1]
                    ]
                }
            }
        );
        inSlideAnim.duration = 0.5;
        inSlideAnim.method = YAHOO.util.Easing.easeOutStrong;
        
        outSlideAnim.animate();
        inSlideAnim.animate();
    }

    if (this.indicator) {
		for (var i=0; this.pips[i]; i++) {
			if ((i+1) === this.currentSlide()) {
				this.pips[i].className = "current";
			} else {
				
				this.pips[i].className = "";
			}
		}
    }
}
