 /*
 * billboard.js 
 * Date Created: March 11, 2009
 * Requires: jquery 1.2.3
 * Author: Brandon Quintana
 * Updated: 4/15 RDown
 */

jQuery(function($) {
	// billboard tooltip
	$.fn.make_bbTooltips = function() {
		return this.each(function() {
			// copy tooltip into its own expando and remove the title
			this.tooltipText = this.title;
			$(this).removeAttr("title");
			// also remove alt attribute to prevent default tooltip in IE
			this.alt = "";
			// overwrite each event
			$(this).mouseover(function() {
				var $tooltip = $("#tooltip");
				$tooltip.find("div.body").html("<p>" + this.tooltipText + "</p>").end()
					.find("h3, div.url").hide().end()
					.show();
				var offset = $(this).offset();
				var offsetLeft = offset.left + ($(this).outerWidth() - $tooltip.outerWidth())/2;
				var offsetTop = offset.top + $(this).outerHeight() - 5;
				$tooltip.css('top', offsetTop).css('left', offsetLeft);
			})
			.mouseout(function() {
				$("#tooltip").hide();
			})
			.click(function() {
				$("#tooltip").hide();
			});
		});
	}
	// billboard
	$.fn.billboard = function(settings) {
		return this.each(function() {
			/* Initialize
			-------------------------------------------*/

			/**
	 		* @method init
	 		**/
			this.init = function(settings) {
				var $this = this;
				this.s = {
					billboardContainer: 'div#hpBillboard',
					billboardContent: 'div.billboard_content_area',
					billboardContentLinks: 'div.hpBillboard_board a',
					sliderContainer: 'div#hpBillboard_thumbs',
					sliderWrapContainer: 'div#sliders',
					sliderContentSelector: 'div.slider-content',
					previousSelector: 'a#bb_left',
					thumbsSelector: 'img.thumb', 
					nextSelector: 'a#bb_right',
					currentSlider: 0,
					currentThumb: 0,
					animate: false
				};
				if (settings) $.extend(this.s, settings);
			
				this.n = {
					sliderContainer: $(this.s.sliderContainer),
					sliderWrapContainer: $(this.s.sliderWrapContainer),
					sliderContent : $(this.s.sliderContentSelector),
					thumbsContent: $(this.s.sliderContainer + ' a'),
					activeContent: $(this.s.billboardContainer + ' ' + this.s.billboardContent + '.active')
				}
			
				$(this.s.sliderContainer).scrollLeft(0); // always scroll to the first pane
				this._setInterval();
			
				this._eventManager();
			
				$(this.s.sliderContainer).find(this.s.thumbsSelector).make_bbTooltips();
			};

			/* Private Methods
			-------------------------------------------*/
			/**
			 * @method _eventManager()
			 **/
			this._eventManager = function() {
				var $this = this;
	
				$(this.s.billboardContentLinks).bind('click', this.s, function(e) { //explicitly capture link event in billboard for freeze
					$this._clearInterval(e.data.interval);
					setTimeout(function() { $this._setInterval();}, 15000);
				});
				
				$(document.body).bind('click', this.s, function(e) {
					var target = $(e.target);
					//$this._clearInterval(e.data.interval);	// causes click on entire page to freeze
									
					if (target.is(e.data.previousSelector)) {
						target.blur();
						if(e.data.animate == false) {
							$this._previousPage();
							$this._setInterval();
						}
						return false;
					} 
				
					if (target.is(e.data.nextSelector)) {
						target.blur();
						if(e.data.animate == false) {
							$this._nextPage();
							$this._setInterval();
						}
						return false;
					}
				
					if (target.is(e.data.thumbsSelector)) {
						//alert(e.target);
						//target.blur();
						$this._loadContent(target);
						$this._clearInterval(e.data.interval);
						setTimeout(function() { $this._setInterval();}, 15000);
						return false;
					}
					
					
					
				});
			}
			
			/**
			 * @method _previousPage()
			 **/
			this._previousPage = function() {
				var $this = this;
				this.n.sliderContent = $(this.s.sliderWrapContainer).find(this.s.sliderContentSelector);
				var firstChild = $(this.n.sliderContent[0]);
				var width = firstChild.width() + parseInt(firstChild.css('margin-left'));
				
				this.s.animate = true;
			
				if (this.n.sliderContent.length <= 1) {
					this.s.animate = false;
					this._hightlightFirstThumb();
				} else if(this.n.sliderContainer.scrollLeft() == 0) {
					var lastChild = $(this.n.sliderContent[this.n.sliderContent.length - 1]);

					this.n.sliderContainer.scrollLeft(width);

					lastChild.insertBefore(firstChild);
				
					this.n.sliderContainer.animate({
							scrollLeft: 0
						}, 
						1000, 
						null, 
						function() { 
							$this.s.animate = false; 
					});
					
					this._hightlightFirstThumb('previous');
				} else {
					this.n.sliderContainer.animate({
							scrollLeft: this.n.sliderContainer.scrollLeft() - width
						}, 
						1000, 
						null, 
						function() { 
							$this.s.animate = false; 
					});
					
					this._hightlightFirstThumb('previous');
				}
			}
		
			/**
			 * @method _nextPage()
			 **/
			this._nextPage = function() {
				var $this = this;
				this.n.sliderContent = $(this.s.sliderWrapContainer).find(this.s.sliderContentSelector);
				var firstChild = $(this.n.sliderContent[0]);
				var width = firstChild.width() + parseInt(firstChild.css('margin-left'));
				
				this.s.animate = true;
			
				if (this.n.sliderContent.length <= 1) {
					this.s.animate = false;
					this._hightlightFirstThumb();
				} else if(this.n.sliderContainer.scrollLeft() == (width * (this.n.sliderContent.length - 1))) {
					var lastChild = $(this.n.sliderContent[this.n.sliderContent.length - 1]);

					this.n.sliderContainer.scrollLeft(this.n.sliderContainer.scrollLeft() - width);

					firstChild.insertAfter(lastChild);
				
					this.n.sliderContainer.animate({
							scrollLeft: this.n.sliderContainer.scrollLeft() + width
						}, 
						1000, 
						null, 
						function() { 
							$this.s.animate = false; 
					});	
					
					this._hightlightFirstThumb('next');
				} else {
					this.n.sliderContainer.animate({
							scrollLeft: this.n.sliderContainer.scrollLeft() + width
						},
						1000,
						null,
						function() { 
							$this.s.animate = false; 
					});
					
					this._hightlightFirstThumb('next');
				}
			}

			/**
			 * @method _loadContent()
			 **/
			this._loadContent = function(target) {
				$(this.s.billboardContainer + ' a.active').removeClass('active');
	
				var contentID = $(target).parent('a').addClass('active').attr('rel');

				this.n.activeContent.fadeOut();
				this.n.activeContent = $('#' + contentID).fadeIn();
			}
		
			/**
			 * @method _loadNextPane()
			 **/
			this._loadNextPane = function() {
				var parent = $(this.s.billboardContainer + ' a.active').parent();
			
				if(parent.is(':last-child')) {
					this._nextPage();
				} else {
					var next = parent.next().find('img');	
					this._loadContent(next);
				}
			}
		
			/**
			 * @method _highlightFirstThumb()
			 * @param direction
			 **/
			this._hightlightFirstThumb = function(direction) {
				if(direction == 'next') {
					var thumb = $(this.s.billboardContainer + ' a.active').
									parents(this.s.sliderContentSelector).
									next().
									find('li:first-child img');
				} else if (direction == 'previous') {
					var thumb = $(this.s.billboardContainer + ' a.active').
									parents(this.s.sliderContentSelector).
									prev().
									find('li:first-child img');
				} else {
					var thumb = $(this.s.billboardContainer + ' a.active').
									parents(this.s.sliderContentSelector).
									find('li:first-child img');
				}
			
				this._loadContent(thumb);
			}
		
			/**
			 * @method _setInterval()
			 **/
			this._setInterval = function() {
				var $this = this;
			
				this._clearInterval(this.s.interval);
			
				this.s.interval = setInterval(function() { $this._loadNextPane() }, 7000);
			}
		
			/**
			 * @method _clearInterval()
			 **/
			this._clearInterval = function(interval) {
				clearInterval(interval);
			}

			/* Public Methods
			-------------------------------------------*/
		
			//Call initialize
			this.init(settings);
		});
	};	
});

jQuery(document).ready(function($) {
	$('div#hpBillboard').billboard({});
});
