$.noConflict();
jQuery(document).ready(function($){
	$('.socialDropButton').find('li a').each(function(){
		$(this).attr('href', $.encoder.encodeForHTML($(this).attr('href')));
	});
	
	$.RBS.init();
});

(function($){
	
	$.RBS = {
		$carousel: null,
		init: function(){
			this.$carousel = $('.homepageCarousel');
			$.RBS.Carousel.init();
			$.RBS.DropDowns.init();
		}
	}
	
	$.RBS.DropDowns = {
		init: function(){
			$('.customSelect').each(function(){
				$(this).customSelect({
					startValue: $(this).find('h4').remove().text(),
					maxHeight: 500
				})
				.bind('onopencustomselect', function(){
					$.RBS.$carousel.trigger('stoprevolver');
				})
				.bind('onclosecustomselect', function(){
					$.RBS.$carousel.trigger('startrevolver');
				})
				.height(33);
			});
		}
	}
	
	$.RBS.Carousel = {
		init: function(){ 
			this.setRevolver();
		},
		setRevolver: function(){
			$.RBS.$carousel.find('.carouselPod').height(288);
			$.RBS.$carousel.find('.carouselContent').height(60);
			$.RBS.$carousel.revolver({
				infinite: true,
				interval: 10000,
				selectors: {
					window:	'.carouselPods',
					belt: 	'.carouselBelt',
					item: 	'.carouselPod',
					pager: 	'.dots'
				}
			})
			.find('.dots').css({
				marginLeft: $.RBS.$carousel.width()/2 - $.RBS.$carousel.find('.dots').width()/2
			});
			if($.browser.msie && $.browser.version.substr(0,1)<7){
				$.RBS.$carousel.find('.dots').css({
					marginLeft: 200
				});
			}
			
			$.RBS.$carousel.find('.carouselPods').height(290);
		},
		pagerOverlays: function(){
			var obj = this;
			$.RBS.$carousel.find('.dots .pagerItem').each(function(i){
				var $pod = $.RBS.$carousel.find('.carouselPod').eq(i),
					$overlay = $(
					'<div class="pagerOverlay">' +
						'<div class="pagerOverlayInner1">' +
							'<div class="pagerOverlayInner2">' +
								'<img src="' + $pod.find('img').attr('src') + '" width="101px" height="76px" />' +
								'<div class="pagerOverlayContent">' +
									'<p>' + $pod.find('.carouselHeading p, .carouselHeading h2').html() + '</p>' +
								'</div>' +
							'</div>' +
						'</div>' +
						'<div class="pagerOverlayArrow" />' +
					'</div>'
				);
				$(this).append($overlay);
				$overlay.show();
				$overlay.find('p').maxLines(3);
				$overlay.hide();
				
				$(this).hover(function(){
					$(this).addClass('hover');
					$overlay.show();
				}, function(){
					$(this).removeClass('hover');
					$overlay.hide();
				});
			});
		},
		showHide: function(){
			$.RBS.$carousel.find('.carouselPod').unbind();
			$.RBS.$carousel.find('.carouselPod').each(function(i){
				var timer = null,
					$carouselContent = $(this).find('.carouselContent'),
					$carouselHidden = $(this).find('.carouselHidden'),
					smallHeight = $carouselContent.height(),
					largeHeight = 175,
					destination = $(this).find('.destination a').attr('href');
				
				$(this).find('.destination').hide();
				$(this).css('cursor', 'pointer');
				$(this).click(function(){
					window.location = destination;
				});
				
				$carouselHidden.remove(); //ie6 bug. Can't just show and hide
				smallHeight = $carouselContent.height();
				$carouselContent.find('.carouselContentInner').append($carouselHidden);
				$carouselContent.height(smallHeight);
				$carouselHidden.hide();
				
				if(largeHeight > smallHeight){
					$(this)
						.hover(function(){
							clearTimeout(timer);
							var $this = $carouselContent;
							if($this.height() <= smallHeight){
								$this.animate({
									top:(largeHeight-smallHeight)*-1
								},
								{
									duration:250,
									complete:function(){
										$carouselHidden.fadeIn();
									}, 
									step: function(heightVal){
										$this.height(smallHeight - heightVal);
									}
								});
							}
						}, function(){
							var $this = $carouselContent,
								time = 0;
							if(!$carouselHidden.is(':visible')){
								time = 500;
							}
							timer = setTimeout(function(){
								var top = parseInt($this.css('top'));
								$carouselHidden.fadeOut(function(){
									$this.animate({
										top:0
									},
									{
										duration:250,
										complete:function(){
		
										}, 
										step: function(heightVal){
											$this.height(smallHeight + (heightVal*-1));
										}
									});
								});
							}, time);
						});
					}
			});
		}
	}
	
})(jQuery);

