/**
 * @author Hollin Wilkins
 * @version 1.0
 */

/**
 * @namespace ILR.SlideShow
 */
ILR.namespace("SlideShow");

if( !ILR.SlideShow.Navigation ) {
	/**
	 * Provides an Ajaxified way to navigate between slides.
	 * @namespace ILR.SlideShow
	 * @class Navigation
	 */
	ILR.SlideShow.Navigation = {
		/**
		 * The slide show id.
		 * @property _SLIDE_SHOW_ID
		 * @type String
		 * @private
		 */
		_SLIDE_SHOW_ID: null,
		
		/**
		 * The slide id of the first slide on the page.
		 * @property _SLIDE_ID
		 * @type String
		 * @private
		 */
		_SLIDE_ID: null,
		
		/**
		 * The slide show light box.
		 * @property _LIGHT_BOX
		 * @type ILR.SlideShow.LightBox
		 * @private
		 */
		_LIGHT_BOX: null,
		
		/**
		 * The URL used to get the next or previous slide.
		 * @property _SWITCH_SLIDE_URL
		 * @type String
		 * @private
		 */
		 
		 // removed www.ilr.cornell.edu from URL
		_SWITCH_SLIDE_URL: "/codelib/apps/slideshow/public/ajax/switchSlide.cfm",
		
		/**
		 * The current slide in the show.
		 * @property _CURRENT_SLIDE
		 * @type ILR.Slide
		 * @private
		 */
		_CURRENT_SLIDE: null,
		
		/**
		 * The next slide in the show.
		 * @property _NEXT_SLIDE
		 * @type ILR.Slide
		 * @private
		 */
		_NEXT_SLIDE: null,
		
		/**
		 * The previous slide in the show.
		 * @property _PREVIOUS_SLIDE
		 * @type ILR.Slide
		 * @private
		 */
		_PREVIOUS_SLIDE: null,
		
		/**
		 * Navigation links that go to the next slide.
		 * @property _NEXT_LINKS
		 * @type Array
		 * @private
		 */
		_NEXT_LINKS: null,
		
		/**
		 * Navigation links that go to the previous slide.
		 * @property _PREVIOUS_LINKS
		 * @type Array
		 * @private
		 */
		_PREVIOUS_LINKS: null,
		
		/**
		 * Navigation links that start the light box slide show.
		 * @property _START_LINKS
		 * @type Array
		 * @private
		 */
		_START_LINKS: null,
		
		/**
		 * Browse links that switch to the grid navigation view.
		 * @property _BROWSE_LINKS
		 * @type Array
		 * @private
		 */
		_BROWSE_LINKS: null,
		
		/**
		 * Custom event handler fired when the slide on the page changes
		 * @property onSwitchSlide
		 * @type YAHOO.util.CustomEvent
		 */
		onSwitchSlide: null,
		
		/**
		 * Initializes class for operation based on an event.
		 * @method handleInit
		 * @param {Event} ev the event
		 */
		handleInit:function(ev) {
			this.init();
		},
		
		/**
		 * Initializes class for operation.
		 * @method init
		 */
		init:function() {
			if( document.getElementById("slideShowId") ) {
				this._SLIDE_SHOW_ID = document.getElementById("slideShowId").value;
			}
			
			if( document.getElementById("slideId") ) {
				this._SLIDE_ID = document.getElementById("slideId").value;
			}
			
			this._NEXT_LINKS = ["navigation_next", "navigation_next_image", "slide_image_anchor"];
			this._PREVIOUS_LINKS = ["navigation_previous", "navigation_previous_image"];
			
			this._START_LINKS = ["navigation_start"];
			this._BROWSE_LINKS = ["navigation_browse"];
			
			this._LIGHT_BOX = new ILR.SlideShow.LightBox(
				this._SLIDE_SHOW_ID,
				{
					visible: false,
					close: true,
					modal: true,
					underlay: "none",
					draggable: false
				}
			);
			
			
			this.initCustomEvents();
			this.initEventListeners();
			this.initSlides();
		},
		
		/**
		 * Initializes class event listeners for operation.
		 * @method initCustomEvents
		 * @param none
		 * @return none
		 */
		initCustomEvents:function() {
			this.onSwitchSlide = new YAHOO.util.CustomEvent("onSwitchSlide", this, true);
		},
		
		/**
		 * Initializes class event listeners for operation.
		 * @method initEventListeners
		 * @param none
		 * @return none
		 */
		initEventListeners:function() {
			YAHOO.util.Event.addListener(this._NEXT_LINKS, "click", this.handleNextSlide, this, true);
			YAHOO.util.Event.addListener(this._PREVIOUS_LINKS, "click", this.handlePreviousSlide, this, true);
			YAHOO.util.Event.addListener(this._START_LINKS, "click", this.handleStartLightBox, this, true);
			YAHOO.util.Event.addListener(this._BROWSE_LINKS, "click", this.handleBrowseView, this, true);
			
			if( document.getElementById("grid") ) {
				var elements = document.getElementById("grid").getElementsByTagName("a");
				YAHOO.util.Event.addListener(elements, "click", this.handleLargeView, this, true);
			}
			
			this.onSwitchSlide.subscribe(this.handleOnSwitchSlide, this, true);
		},
		
		/**
		 * Initialize the slides on the page.
		 * @method initSlides
		 */
		initSlides:function() {
			if( this._SLIDE_ID ) {
				var slide = new ILR.SlideShow.Slide({id: this._SLIDE_ID, parentId: this._SLIDE_SHOW_ID});
				
				this.switchSlide(slide);
			}
		},
		
		/**
		 * Custom event handler for switching between slides.
		 * Sets the images and links for new slides.
		 * @method handleOnSwitchSlide
		 * @param {Object} evName name of the event
		 */
		handleOnSwitchSlide:function(evName) {
			// update navigation images
			document.getElementById("next_image").setAttribute("src", this._NEXT_SLIDE.getImageLink("thumbnail"));
			document.getElementById("previous_image").setAttribute("src", this._PREVIOUS_SLIDE.getImageLink("thumbnail"));
			
			if( document.getElementById("slide_content") ) {
				// update current slide information
				document.getElementById("slide_title").innerHTML = this._CURRENT_SLIDE.title;
				document.getElementById("slide_image_anchor").href = "?slideid=" + this._NEXT_SLIDE.id + "#slide";
				document.getElementById("slide_image").src = this._CURRENT_SLIDE.getImageLink("medium");
				document.getElementById("slide_description").innerHTML = this._CURRENT_SLIDE.description;
				document.getElementById("slide_date").innerHTML = this._CURRENT_SLIDE.date;
				document.getElementById("slide_author").innerHTML = this._CURRENT_SLIDE.author;
			}
		},
		
		/**
		 * Handles switching to the large view.
		 * @method handleLargeView
		 * @param {Event} ev The event
		 */
		handleLargeView:function(ev) {
			// Stop event if it is defined
			if( ev ) {
				YAHOO.util.Event.stopEvent(ev);
			}
			
			var target = YAHOO.util.Event.getTarget(ev).parentNode;
			
			if( target.href ) {
				var query = ILR.Util.parseQueryString(ILR.Util.getQueryString(target.href));
				var slide = new ILR.SlideShow.Slide({id: query.slideId, parentId: query.slideShowId});
				
				this.largeView(slide);
			}
		},
		
		/**
		 * Handles switching to the browse view.
		 * @method handleBrowseView
		 * @param {Event} ev The event
		 */
		handleBrowseView:function(ev) {
			// Stop event if it is defined
			if( ev ) {
				YAHOO.util.Event.stopEvent(ev);
			}
			
			this.browseView();
		},
		
		/**
		 * Handles starting the slide show in a light box.
		 * @method handleStartLightBox
		 * @param {Event} ev The event
		 */
		handleStartLightBox:function(ev) {
			// Stop event if it is defined
			if( ev ) {
				YAHOO.util.Event.stopEvent(ev);
			}
			
			this.startLightBox();
		},
		
		/**
		 * Event handler for moving to the next slide.
		 * @method handleNextSlide
		 * @param {Event} ev the event
		 */
		handleNextSlide:function(ev) {
			// Stop event if it is defined
			if( ev ) {
				YAHOO.util.Event.stopEvent(ev);
			}
			
			this.nextSlide();
		},
		
		/**
		 * Event handler for moving to the previous slide.
		 * @method handlePreviousSlide
		 * @param {Event} ev the event
		 */
		handlePreviousSlide:function(ev) {
			// Stop event if it is defined
			if( ev ) {
				YAHOO.util.Event.stopEvent(ev);
			}
			
			this.previousSlide();
		},
		
		/**
		 * Switches navigation into the large view.
		 * @method largeView
		 * @param {ILR.SlideShow.Slide} slide The slide to use for the large view
		 */
		largeView:function(slide) {
			var callback = {
				success:this._SwitchNavigationCallback.success,
				failure:this._SwitchNavigationCallback.failure,
				scope:this,
				argument: {view: "large", slide: slide}
			};
			
			var url = "/ajaxWrapper/include.cfm?ajaxUrl=/codelib/apps/slideshow/public/view/large/view.cfm&slideShowId=" + slide.parentId + "&slideId=" + slide.id;
			YAHOO.util.Connect.asyncRequest("GET", url, callback);
		},
		
		/**
		 * Switches navigation into the browse view.
		 * @method browseView
		 */
		browseView:function(ev) {
			var callback = {
				success:this._SwitchNavigationCallback.success,
				failure:this._SwitchNavigationCallback.failure,
				scope:this,
				argument: {view: "browse"}
			};
			
			var url = "/ajaxWrapper/include.cfm?ajaxUrl=/codelib/apps/slideshow/public/view/grid/view.cfm&slideShowId=" + this._SLIDE_SHOW_ID;
			YAHOO.util.Connect.asyncRequest("GET", url, callback);
		},
		
		/**
		 * Start the slide show in a light box.
		 * @method startLightBox
		 */
		startLightBox:function() {
			if( !this._LIGHT_BOX._READY ) {
				return;
			}
			
			this._LIGHT_BOX.center();
			this._LIGHT_BOX.cfg.setProperty("y", 0);
			this._LIGHT_BOX.show(this._CURRENT_SLIDE);
		},
		
		/**
		 * Move slide show to the slide that is passed in.
		 * @method switchSlide
		 * @param {ILR.SlideShow.Slide} slide The slide to move to
		 */
		switchSlide:function(slide) {
			var callback = {
				success:this._SwitchSlideCallback.success,
				failure:this._SwitchSlideCallback.failure,
				scope: this
			};
			
			var url = "/ajaxWrapper/include.cfm?ajaxUrl=" + this._SWITCH_SLIDE_URL + "&slideShowId=" + slide.parentId + "&slideId=" + slide.id;
			YAHOO.util.Connect.asyncRequest("GET", url, callback);
		},
		
		/**
		 * Move slide show to the next slide.
		 * @method nextSlide
		 */
		nextSlide:function() {
			this.switchSlide(this._NEXT_SLIDE);
		},
		
		/**
		 * Move slide show to the previous slide.
		 * @method previousSlide
		 */
		previousSlide:function() {
			this.switchSlide(this._PREVIOUS_SLIDE);
		},
		
		/**
		 * Ajax callback object for switching between slides in a slideshow.
		 * @property _SwitchSlideCallback
		 * @type Object
		 * @private
		 */
		_SwitchSlideCallback: {
			/**
			 * Handles a successful ajax call to switch slides
			 * @property _SwitchSlideCallback.success
			 * @type Function
			 * @private
			 */
			success:function(o) {
				var response = YAHOO.Tools.JSONParse(o.responseText);
				
				// there was an error
				if( response.error ) {
					switch( response.error ) {
						case("NoSlides"):
							// do nothing, no slides will show up on the page
							break;
						default:
							alert( response.error.message + "\n\n" + response.error.details );
							break;
					}
				}
				// no error, do normal handling
				else {
					this._CURRENT_SLIDE = new ILR.SlideShow.Slide(response.currentSlide);
					this._NEXT_SLIDE = new ILR.SlideShow.Slide(response.nextSlide);
					this._PREVIOUS_SLIDE = new ILR.SlideShow.Slide(response.previousSlide);
					
					if( !(o.argument && o.argument.first) ) {
						this.onSwitchSlide.fire();
					}
				}
			},
			
			/**
			 * Handles a failed ajax call to switch slides
			 * @property _SwitchSlideCallback.failure
			 * @type Function
			 * @private
			 */
			failure:function(o) {
				
			}
		},
		
		/**
		 * Ajax callback object for updating the slide content area.
		 * @property _UpdateSlideCallback
		 * @type Object
		 * @private
		 */
		_UpdateSlideCallback: {
			/**
			 * Handles a successful ajax call to switch slides
			 * @property _UpdateSlideCallback.success
			 * @type Function
			 * @private
			 */
			success:function(o) {
				var eff1 = new YAHOO.widget.Effects.Fade("slideShowContent");
				eff1.onEffectComplete.subscribe(
					function() {
						if( o.responseText ) {
							document.getElementById("slideShowContent").innerHTML = o.responseText;
						}
						
						YAHOO.widget.Effects.Appear("slideShowContent");
					},
					this,
					true
				);
			},
			
			/**
			 * Handles a failed ajax call to switch slides
			 * @property _UpdateSlideCallback.failure
			 * @type Function
			 * @private
			 */
			failure:function(o) {
				alert("Could not display slide.");
			}
		},
		
		/**
		 * Ajax callback for switching between slide show views.
		 * @property _SwitchNavigationCallback
		 * @type Object
		 * @private
		 */
		_SwitchNavigationCallback: {
			/**
			 * Handles successful ajax call to switch navigation view
			 * @property _SwitchNavigationCallback.success
			 * @type Function
			 * @private
			 */
			success:function(o) {
				var eff1;
				var afterEff1;
				
				switch( o.argument.view ) {
					case "browse":
						eff1 = new YAHOO.widget.Effects.Fade("navigation_large", {delay: true});
						afterEff1 = function() {
							YAHOO.util.Dom.addClass("navigation_large", "hidden")
						};
						
						break;
					case "large":
					default:
						eff1 = new YAHOO.widget.Effects.Appear("navigation_large", {delay: true});
						afterEff1 = function() {
							YAHOO.util.Dom.removeClass("navigation_large", "hidden")
						};
						this.switchSlide(o.argument.slide)
				}
				eff1.onEffectComplete.subscribe(afterEff1, this, true);
				
				if( o.argument.view != "large" ) {
					eff1.animate();
				}
				
				var eff2 = new YAHOO.widget.Effects.Fade("slideShowContent");
				eff2.onEffectComplete.subscribe(
					function() {
						if( o.responseText ) {
							document.getElementById("slideShowContent").innerHTML = o.responseText;
							
							if( document.getElementById("grid") ) {
								var elements = document.getElementById("grid").getElementsByTagName("a");
								YAHOO.util.Event.addListener(elements, "click", this.handleLargeView, this, true);
							}
							
							if( o.argument.view == "large" ) {
								eff1.animate();
							}
						}
						
						YAHOO.widget.Effects.Appear("slideShowContent");
					},
					this,
					true
				);
			},
			
			/**
			 * Handles failed ajax call to switch navigation view
			 * @property _SwitchNavigationCallback.failure
			 * @type Function
			 * @private
			 */
			failure:function(o) {
				// do nothing
			}
		}
	}
	
	//ILR.SlideShow.Navigation.init();
	YAHOO.util.Event.addListener(window, "load", ILR.SlideShow.Navigation.init, ILR.SlideShow.Navigation, true);
}